| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1 | /* | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2 |  | 
|  | 3 | Unicode implementation based on original code by Fredrik Lundh, | 
| Benjamin Peterson | 31616ea | 2011-10-01 00:11:09 -0400 | [diff] [blame] | 4 | modified by Marc-Andre Lemburg <mal@lemburg.com>. | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5 |  | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6 | Major speed upgrades to the method implementations at the Reykjavik | 
|  | 7 | NeedForSpeed sprint, by Fredrik Lundh and Andrew Dalke. | 
|  | 8 |  | 
| Guido van Rossum | 16b1ad9 | 2000-08-03 16:24:25 +0000 | [diff] [blame] | 9 | Copyright (c) Corporation for National Research Initiatives. | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10 |  | 
| Fredrik Lundh | 0fdb90c | 2001-01-19 09:45:02 +0000 | [diff] [blame] | 11 | -------------------------------------------------------------------- | 
|  | 12 | The original string type implementation is: | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14 | Copyright (c) 1999 by Secret Labs AB | 
|  | 15 | Copyright (c) 1999 by Fredrik Lundh | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 16 |  | 
| Fredrik Lundh | 0fdb90c | 2001-01-19 09:45:02 +0000 | [diff] [blame] | 17 | By obtaining, using, and/or copying this software and/or its | 
|  | 18 | associated documentation, you agree that you have read, understood, | 
|  | 19 | and will comply with the following terms and conditions: | 
|  | 20 |  | 
|  | 21 | Permission to use, copy, modify, and distribute this software and its | 
|  | 22 | associated documentation for any purpose and without fee is hereby | 
|  | 23 | granted, provided that the above copyright notice appears in all | 
|  | 24 | copies, and that both that copyright notice and this permission notice | 
|  | 25 | appear in supporting documentation, and that the name of Secret Labs | 
|  | 26 | AB or the author not be used in advertising or publicity pertaining to | 
|  | 27 | distribution of the software without specific, written prior | 
|  | 28 | permission. | 
|  | 29 |  | 
|  | 30 | SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO | 
|  | 31 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND | 
|  | 32 | FITNESS.  IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR BE LIABLE FOR | 
|  | 33 | ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | 
|  | 34 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | 
|  | 35 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT | 
|  | 36 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | 
|  | 37 | -------------------------------------------------------------------- | 
|  | 38 |  | 
|  | 39 | */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 40 |  | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 41 | #define PY_SSIZE_T_CLEAN | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 42 | #include "Python.h" | 
| Marc-André Lemburg | d49e5b4 | 2000-06-30 14:58:20 +0000 | [diff] [blame] | 43 | #include "ucnhash.h" | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 44 |  | 
| Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 45 | #ifdef MS_WINDOWS | 
| Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 46 | #include <windows.h> | 
|  | 47 | #endif | 
| Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 48 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 49 | /* Limit for the Unicode object free list */ | 
|  | 50 |  | 
| Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 51 | #define PyUnicode_MAXFREELIST       1024 | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 52 |  | 
|  | 53 | /* Limit for the Unicode object free list stay alive optimization. | 
|  | 54 |  | 
|  | 55 | The implementation will keep allocated Unicode memory intact for | 
|  | 56 | all objects on the free list having a size less than this | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 57 | limit. This reduces malloc() overhead for small Unicode objects. | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 58 |  | 
| Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 59 | At worst this will result in PyUnicode_MAXFREELIST * | 
| Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 60 | (sizeof(PyUnicodeObject) + KEEPALIVE_SIZE_LIMIT + | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 61 | malloc()-overhead) bytes of unused garbage. | 
|  | 62 |  | 
|  | 63 | Setting the limit to 0 effectively turns the feature off. | 
|  | 64 |  | 
| Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 65 | Note: This is an experimental feature ! If you get core dumps when | 
|  | 66 | using Unicode objects, turn this feature off. | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 67 |  | 
|  | 68 | */ | 
|  | 69 |  | 
| Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 70 | #define KEEPALIVE_SIZE_LIMIT       9 | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 71 |  | 
|  | 72 | /* Endianness switches; defaults to little endian */ | 
|  | 73 |  | 
|  | 74 | #ifdef WORDS_BIGENDIAN | 
|  | 75 | # define BYTEORDER_IS_BIG_ENDIAN | 
|  | 76 | #else | 
|  | 77 | # define BYTEORDER_IS_LITTLE_ENDIAN | 
|  | 78 | #endif | 
|  | 79 |  | 
| Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 80 | /* --- Globals ------------------------------------------------------------ | 
|  | 81 |  | 
|  | 82 | The globals are initialized by the _PyUnicode_Init() API and should | 
|  | 83 | not be used before calling that API. | 
|  | 84 |  | 
|  | 85 | */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 86 |  | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 87 |  | 
|  | 88 | #ifdef __cplusplus | 
|  | 89 | extern "C" { | 
|  | 90 | #endif | 
|  | 91 |  | 
| Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 92 | /* Generic helper macro to convert characters of different types. | 
|  | 93 | from_type and to_type have to be valid type names, begin and end | 
|  | 94 | are pointers to the source characters which should be of type | 
|  | 95 | "from_type *".  to is a pointer of type "to_type *" and points to the | 
|  | 96 | buffer where the result characters are written to. */ | 
|  | 97 | #define _PyUnicode_CONVERT_BYTES(from_type, to_type, begin, end, to) \ | 
|  | 98 | do { \ | 
|  | 99 | const from_type *iter_; to_type *to_; \ | 
|  | 100 | for (iter_ = (begin), to_ = (to_type *)(to); \ | 
|  | 101 | iter_ < (end); \ | 
|  | 102 | ++iter_, ++to_) { \ | 
|  | 103 | *to_ = (to_type)*iter_; \ | 
|  | 104 | } \ | 
|  | 105 | } while (0) | 
|  | 106 |  | 
| Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 107 | #define _PyUnicode_UTF8(op)                             \ | 
|  | 108 | (((PyCompactUnicodeObject*)(op))->utf8) | 
|  | 109 | #define PyUnicode_UTF8(op)                              \ | 
|  | 110 | (assert(PyUnicode_Check(op)),                       \ | 
|  | 111 | assert(PyUnicode_IS_READY(op)),                    \ | 
|  | 112 | PyUnicode_IS_COMPACT_ASCII(op) ?                   \ | 
|  | 113 | ((char*)((PyASCIIObject*)(op) + 1)) :          \ | 
|  | 114 | _PyUnicode_UTF8(op)) | 
| Victor Stinner | bc8b81b | 2011-09-29 19:31:34 +0200 | [diff] [blame] | 115 | #define _PyUnicode_UTF8_LENGTH(op)                      \ | 
| Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 116 | (((PyCompactUnicodeObject*)(op))->utf8_length) | 
|  | 117 | #define PyUnicode_UTF8_LENGTH(op)                       \ | 
|  | 118 | (assert(PyUnicode_Check(op)),                       \ | 
|  | 119 | assert(PyUnicode_IS_READY(op)),                    \ | 
|  | 120 | PyUnicode_IS_COMPACT_ASCII(op) ?                   \ | 
|  | 121 | ((PyASCIIObject*)(op))->length :               \ | 
|  | 122 | _PyUnicode_UTF8_LENGTH(op)) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 123 | #define _PyUnicode_WSTR(op) (((PyASCIIObject*)(op))->wstr) | 
|  | 124 | #define _PyUnicode_WSTR_LENGTH(op) (((PyCompactUnicodeObject*)(op))->wstr_length) | 
|  | 125 | #define _PyUnicode_LENGTH(op) (((PyASCIIObject *)(op))->length) | 
|  | 126 | #define _PyUnicode_STATE(op) (((PyASCIIObject *)(op))->state) | 
|  | 127 | #define _PyUnicode_HASH(op) (((PyASCIIObject *)(op))->hash) | 
|  | 128 | #define _PyUnicode_KIND(op) \ | 
|  | 129 | (assert(PyUnicode_Check(op)), \ | 
|  | 130 | ((PyASCIIObject *)(op))->state.kind) | 
|  | 131 | #define _PyUnicode_GET_LENGTH(op)                \ | 
|  | 132 | (assert(PyUnicode_Check(op)),               \ | 
|  | 133 | ((PyASCIIObject *)(op))->length) | 
| Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 134 | #define _PyUnicode_DATA_ANY(op) (((PyUnicodeObject*)(op))->data.any) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 135 |  | 
| Victor Stinner | b15d4d8 | 2011-09-28 23:59:20 +0200 | [diff] [blame] | 136 | /* The Unicode string has been modified: reset the hash */ | 
|  | 137 | #define _PyUnicode_DIRTY(op) do { _PyUnicode_HASH(op) = -1; } while (0) | 
|  | 138 |  | 
| Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 139 | /* This dictionary holds all interned unicode strings.  Note that references | 
|  | 140 | to strings in this dictionary are *not* counted in the string's ob_refcnt. | 
|  | 141 | When the interned string reaches a refcnt of 0 the string deallocation | 
|  | 142 | function will delete the reference from this dictionary. | 
|  | 143 |  | 
|  | 144 | 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] | 145 | 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] | 146 | */ | 
|  | 147 | static PyObject *interned; | 
|  | 148 |  | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 149 | /* The empty Unicode object is shared to improve performance. */ | 
| Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 150 | static PyObject *unicode_empty; | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 151 |  | 
|  | 152 | /* Single character Unicode strings in the Latin-1 range are being | 
|  | 153 | shared as well. */ | 
| Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 154 | static PyObject *unicode_latin1[256]; | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 155 |  | 
| Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 156 | /* Fast detection of the most frequent whitespace characters */ | 
|  | 157 | const unsigned char _Py_ascii_whitespace[] = { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 158 | 0, 0, 0, 0, 0, 0, 0, 0, | 
| Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 159 | /*     case 0x0009: * CHARACTER TABULATION */ | 
| Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 160 | /*     case 0x000A: * LINE FEED */ | 
| Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 161 | /*     case 0x000B: * LINE TABULATION */ | 
| Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 162 | /*     case 0x000C: * FORM FEED */ | 
|  | 163 | /*     case 0x000D: * CARRIAGE RETURN */ | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 164 | 0, 1, 1, 1, 1, 1, 0, 0, | 
|  | 165 | 0, 0, 0, 0, 0, 0, 0, 0, | 
| Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 166 | /*     case 0x001C: * FILE SEPARATOR */ | 
|  | 167 | /*     case 0x001D: * GROUP SEPARATOR */ | 
|  | 168 | /*     case 0x001E: * RECORD SEPARATOR */ | 
|  | 169 | /*     case 0x001F: * UNIT SEPARATOR */ | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 170 | 0, 0, 0, 0, 1, 1, 1, 1, | 
| Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 171 | /*     case 0x0020: * SPACE */ | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 172 | 1, 0, 0, 0, 0, 0, 0, 0, | 
|  | 173 | 0, 0, 0, 0, 0, 0, 0, 0, | 
|  | 174 | 0, 0, 0, 0, 0, 0, 0, 0, | 
|  | 175 | 0, 0, 0, 0, 0, 0, 0, 0, | 
| Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 176 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 177 | 0, 0, 0, 0, 0, 0, 0, 0, | 
|  | 178 | 0, 0, 0, 0, 0, 0, 0, 0, | 
|  | 179 | 0, 0, 0, 0, 0, 0, 0, 0, | 
|  | 180 | 0, 0, 0, 0, 0, 0, 0, 0, | 
|  | 181 | 0, 0, 0, 0, 0, 0, 0, 0, | 
|  | 182 | 0, 0, 0, 0, 0, 0, 0, 0, | 
|  | 183 | 0, 0, 0, 0, 0, 0, 0, 0, | 
|  | 184 | 0, 0, 0, 0, 0, 0, 0, 0 | 
| Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 185 | }; | 
|  | 186 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 187 | static PyObject * | 
|  | 188 | unicode_encode_call_errorhandler(const char *errors, | 
| Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 189 | PyObject **errorHandler,const char *encoding, const char *reason, | 
|  | 190 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, | 
|  | 191 | Py_ssize_t startpos, Py_ssize_t endpos, Py_ssize_t *newpos); | 
|  | 192 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 193 | static void | 
|  | 194 | raise_encode_exception(PyObject **exceptionObject, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 195 | const char *encoding, | 
|  | 196 | const Py_UNICODE *unicode, Py_ssize_t size, | 
|  | 197 | Py_ssize_t startpos, Py_ssize_t endpos, | 
|  | 198 | const char *reason); | 
| Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 199 |  | 
| Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 200 | /* Same for linebreaks */ | 
|  | 201 | static unsigned char ascii_linebreak[] = { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 202 | 0, 0, 0, 0, 0, 0, 0, 0, | 
| Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 203 | /*         0x000A, * LINE FEED */ | 
| Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 204 | /*         0x000B, * LINE TABULATION */ | 
|  | 205 | /*         0x000C, * FORM FEED */ | 
| Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 206 | /*         0x000D, * CARRIAGE RETURN */ | 
| Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 207 | 0, 0, 1, 1, 1, 1, 0, 0, | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 208 | 0, 0, 0, 0, 0, 0, 0, 0, | 
| Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 209 | /*         0x001C, * FILE SEPARATOR */ | 
|  | 210 | /*         0x001D, * GROUP SEPARATOR */ | 
|  | 211 | /*         0x001E, * RECORD SEPARATOR */ | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 212 | 0, 0, 0, 0, 1, 1, 1, 0, | 
|  | 213 | 0, 0, 0, 0, 0, 0, 0, 0, | 
|  | 214 | 0, 0, 0, 0, 0, 0, 0, 0, | 
|  | 215 | 0, 0, 0, 0, 0, 0, 0, 0, | 
|  | 216 | 0, 0, 0, 0, 0, 0, 0, 0, | 
| Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 217 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 218 | 0, 0, 0, 0, 0, 0, 0, 0, | 
|  | 219 | 0, 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, | 
|  | 223 | 0, 0, 0, 0, 0, 0, 0, 0, | 
|  | 224 | 0, 0, 0, 0, 0, 0, 0, 0, | 
|  | 225 | 0, 0, 0, 0, 0, 0, 0, 0 | 
| Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 226 | }; | 
|  | 227 |  | 
| Ezio Melotti | 48a2f8f | 2011-09-29 00:18:19 +0300 | [diff] [blame] | 228 | /* The max unicode value is always 0x10FFFF while using the PEP-393 API. | 
|  | 229 | 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] | 230 | Py_UNICODE | 
| Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 231 | PyUnicode_GetMax(void) | 
| Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 232 | { | 
| Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 233 | #ifdef Py_UNICODE_WIDE | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 234 | return 0x10FFFF; | 
| Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 235 | #else | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 236 | /* This is actually an illegal character, so it should | 
|  | 237 | not be passed to unichr. */ | 
|  | 238 | return 0xFFFF; | 
| Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 239 | #endif | 
|  | 240 | } | 
|  | 241 |  | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 242 | /* --- Bloom Filters ----------------------------------------------------- */ | 
|  | 243 |  | 
|  | 244 | /* stuff to implement simple "bloom filters" for Unicode characters. | 
|  | 245 | to keep things simple, we use a single bitmask, using the least 5 | 
|  | 246 | bits from each unicode characters as the bit index. */ | 
|  | 247 |  | 
|  | 248 | /* the linebreak mask is set up by Unicode_Init below */ | 
|  | 249 |  | 
| Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 250 | #if LONG_BIT >= 128 | 
|  | 251 | #define BLOOM_WIDTH 128 | 
|  | 252 | #elif LONG_BIT >= 64 | 
|  | 253 | #define BLOOM_WIDTH 64 | 
|  | 254 | #elif LONG_BIT >= 32 | 
|  | 255 | #define BLOOM_WIDTH 32 | 
|  | 256 | #else | 
|  | 257 | #error "LONG_BIT is smaller than 32" | 
|  | 258 | #endif | 
|  | 259 |  | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 260 | #define BLOOM_MASK unsigned long | 
|  | 261 |  | 
|  | 262 | static BLOOM_MASK bloom_linebreak; | 
|  | 263 |  | 
| Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 264 | #define BLOOM_ADD(mask, ch) ((mask |= (1UL << ((ch) & (BLOOM_WIDTH - 1))))) | 
|  | 265 | #define BLOOM(mask, ch)     ((mask &  (1UL << ((ch) & (BLOOM_WIDTH - 1))))) | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 266 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 267 | #define BLOOM_LINEBREAK(ch)                                             \ | 
|  | 268 | ((ch) < 128U ? ascii_linebreak[(ch)] :                              \ | 
|  | 269 | (BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK(ch))) | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 270 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 271 | Py_LOCAL_INLINE(BLOOM_MASK) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 272 | make_bloom_mask(int kind, void* ptr, Py_ssize_t len) | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 273 | { | 
|  | 274 | /* calculate simple bloom-style bitmask for a given unicode string */ | 
|  | 275 |  | 
| Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 276 | BLOOM_MASK mask; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 277 | Py_ssize_t i; | 
|  | 278 |  | 
|  | 279 | mask = 0; | 
|  | 280 | for (i = 0; i < len; i++) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 281 | BLOOM_ADD(mask, PyUnicode_READ(kind, ptr, i)); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 282 |  | 
|  | 283 | return mask; | 
|  | 284 | } | 
|  | 285 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 286 | #define BLOOM_MEMBER(mask, chr, str) \ | 
|  | 287 | (BLOOM(mask, chr) \ | 
|  | 288 | && (PyUnicode_FindChar(str, chr, 0, PyUnicode_GET_LENGTH(str), 1) >= 0)) | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 289 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 290 | /* --- Unicode Object ----------------------------------------------------- */ | 
|  | 291 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 292 | static PyObject * | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 293 | fixup(PyUnicodeObject *self, Py_UCS4 (*fixfct)(PyUnicodeObject *s)); | 
|  | 294 |  | 
|  | 295 | Py_LOCAL_INLINE(char *) findchar(void *s, int kind, | 
|  | 296 | Py_ssize_t size, Py_UCS4 ch, | 
|  | 297 | int direction) | 
|  | 298 | { | 
|  | 299 | /* like wcschr, but doesn't stop at NULL characters */ | 
|  | 300 | Py_ssize_t i; | 
|  | 301 | if (direction == 1) { | 
|  | 302 | for(i = 0; i < size; i++) | 
|  | 303 | if (PyUnicode_READ(kind, s, i) == ch) | 
|  | 304 | return (char*)s + PyUnicode_KIND_SIZE(kind, i); | 
|  | 305 | } | 
|  | 306 | else { | 
|  | 307 | for(i = size-1; i >= 0; i--) | 
|  | 308 | if (PyUnicode_READ(kind, s, i) == ch) | 
|  | 309 | return (char*)s + PyUnicode_KIND_SIZE(kind, i); | 
|  | 310 | } | 
|  | 311 | return NULL; | 
|  | 312 | } | 
|  | 313 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 314 | static int | 
|  | 315 | unicode_resize(register PyUnicodeObject *unicode, | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 316 | Py_ssize_t length) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 317 | { | 
|  | 318 | void *oldstr; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 319 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 320 | /* Resizing is only supported for old unicode objects. */ | 
|  | 321 | assert(!PyUnicode_IS_COMPACT(unicode)); | 
|  | 322 | assert(_PyUnicode_WSTR(unicode) != NULL); | 
|  | 323 |  | 
|  | 324 | /* ... and only if they have not been readied yet, because | 
|  | 325 | callees usually rely on the wstr representation when resizing. */ | 
|  | 326 | assert(unicode->data.any == NULL); | 
|  | 327 |  | 
| Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 328 | /* Shortcut if there's nothing much to do. */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 329 | if (_PyUnicode_WSTR_LENGTH(unicode) == length) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 330 | goto reset; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 331 |  | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 332 | /* Resizing shared object (unicode_empty or single character | 
|  | 333 | objects) in-place is not allowed. Use PyUnicode_Resize() | 
|  | 334 | instead ! */ | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 335 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 336 | if (unicode == unicode_empty || | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 337 | (_PyUnicode_WSTR_LENGTH(unicode) == 1 && | 
|  | 338 | _PyUnicode_WSTR(unicode)[0] < 256U && | 
|  | 339 | unicode_latin1[_PyUnicode_WSTR(unicode)[0]] == unicode)) { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 340 | PyErr_SetString(PyExc_SystemError, | 
| Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 341 | "can't resize shared str objects"); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 342 | return -1; | 
|  | 343 | } | 
|  | 344 |  | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 345 | /* We allocate one more byte to make sure the string is Ux0000 terminated. | 
|  | 346 | The overallocation is also used by fastsearch, which assumes that it's | 
|  | 347 | safe to look at str[length] (without making any assumptions about what | 
|  | 348 | it contains). */ | 
|  | 349 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 350 | oldstr = _PyUnicode_WSTR(unicode); | 
|  | 351 | _PyUnicode_WSTR(unicode) = PyObject_REALLOC(_PyUnicode_WSTR(unicode), | 
|  | 352 | sizeof(Py_UNICODE) * (length + 1)); | 
|  | 353 | if (!_PyUnicode_WSTR(unicode)) { | 
|  | 354 | _PyUnicode_WSTR(unicode) = (Py_UNICODE *)oldstr; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 355 | PyErr_NoMemory(); | 
|  | 356 | return -1; | 
|  | 357 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 358 | _PyUnicode_WSTR(unicode)[length] = 0; | 
|  | 359 | _PyUnicode_WSTR_LENGTH(unicode) = length; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 360 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 361 | reset: | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 362 | if (unicode->data.any != NULL) { | 
|  | 363 | PyObject_FREE(unicode->data.any); | 
| Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 364 | if (_PyUnicode_UTF8(unicode) && _PyUnicode_UTF8(unicode) != unicode->data.any) { | 
|  | 365 | PyObject_FREE(_PyUnicode_UTF8(unicode)); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 366 | } | 
| Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 367 | _PyUnicode_UTF8(unicode) = NULL; | 
|  | 368 | _PyUnicode_UTF8_LENGTH(unicode) = 0; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 369 | unicode->data.any = NULL; | 
|  | 370 | _PyUnicode_LENGTH(unicode) = 0; | 
|  | 371 | _PyUnicode_STATE(unicode).interned = _PyUnicode_STATE(unicode).interned; | 
|  | 372 | _PyUnicode_STATE(unicode).kind = PyUnicode_WCHAR_KIND; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 373 | } | 
| Victor Stinner | b15d4d8 | 2011-09-28 23:59:20 +0200 | [diff] [blame] | 374 | _PyUnicode_DIRTY(unicode); | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 375 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 376 | return 0; | 
|  | 377 | } | 
|  | 378 |  | 
|  | 379 | /* 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] | 380 | Ux0000 terminated; some code (e.g. new_identifier) | 
|  | 381 | relies on that. | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 382 |  | 
|  | 383 | XXX This allocator could further be enhanced by assuring that the | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 384 | free list never reduces its size below 1. | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 385 |  | 
|  | 386 | */ | 
|  | 387 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 388 | #ifdef Py_DEBUG | 
|  | 389 | int unicode_old_new_calls = 0; | 
|  | 390 | #endif | 
|  | 391 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 392 | static PyUnicodeObject * | 
|  | 393 | _PyUnicode_New(Py_ssize_t length) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 394 | { | 
|  | 395 | register PyUnicodeObject *unicode; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 396 | size_t new_size; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 397 |  | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 398 | /* Optimization for empty strings */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 399 | if (length == 0 && unicode_empty != NULL) { | 
|  | 400 | Py_INCREF(unicode_empty); | 
| Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 401 | return (PyUnicodeObject*)unicode_empty; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 402 | } | 
|  | 403 |  | 
| Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 404 | /* Ensure we won't overflow the size. */ | 
|  | 405 | if (length > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { | 
|  | 406 | return (PyUnicodeObject *)PyErr_NoMemory(); | 
|  | 407 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 408 | if (length < 0) { | 
|  | 409 | PyErr_SetString(PyExc_SystemError, | 
|  | 410 | "Negative size passed to _PyUnicode_New"); | 
|  | 411 | return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 412 | } | 
|  | 413 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 414 | #ifdef Py_DEBUG | 
|  | 415 | ++unicode_old_new_calls; | 
|  | 416 | #endif | 
|  | 417 |  | 
|  | 418 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); | 
|  | 419 | if (unicode == NULL) | 
|  | 420 | return NULL; | 
|  | 421 | new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); | 
|  | 422 | _PyUnicode_WSTR(unicode) = (Py_UNICODE*) PyObject_MALLOC(new_size); | 
|  | 423 | if (!_PyUnicode_WSTR(unicode)) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 424 | PyErr_NoMemory(); | 
|  | 425 | goto onError; | 
| Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 426 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 427 |  | 
| Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 428 | /* Initialize the first element to guard against cases where | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 429 | * the caller fails before initializing str -- unicode_resize() | 
|  | 430 | * reads str[0], and the Keep-Alive optimization can keep memory | 
|  | 431 | * allocated for str alive across a call to unicode_dealloc(unicode). | 
|  | 432 | * We don't want unicode_resize to read uninitialized memory in | 
|  | 433 | * that case. | 
|  | 434 | */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 435 | _PyUnicode_WSTR(unicode)[0] = 0; | 
|  | 436 | _PyUnicode_WSTR(unicode)[length] = 0; | 
|  | 437 | _PyUnicode_WSTR_LENGTH(unicode) = length; | 
|  | 438 | _PyUnicode_HASH(unicode) = -1; | 
|  | 439 | _PyUnicode_STATE(unicode).interned = 0; | 
|  | 440 | _PyUnicode_STATE(unicode).kind = 0; | 
|  | 441 | _PyUnicode_STATE(unicode).compact = 0; | 
|  | 442 | _PyUnicode_STATE(unicode).ready = 0; | 
|  | 443 | _PyUnicode_STATE(unicode).ascii = 0; | 
| Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 444 | _PyUnicode_DATA_ANY(unicode) = NULL; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 445 | _PyUnicode_LENGTH(unicode) = 0; | 
| Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 446 | _PyUnicode_UTF8(unicode) = NULL; | 
|  | 447 | _PyUnicode_UTF8_LENGTH(unicode) = 0; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 448 | return unicode; | 
| Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 449 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 450 | onError: | 
| Amaury Forgeot d'Arc | 7888d08 | 2008-08-01 01:06:32 +0000 | [diff] [blame] | 451 | /* XXX UNREF/NEWREF interface should be more symmetrical */ | 
|  | 452 | _Py_DEC_REFTOTAL; | 
| Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 453 | _Py_ForgetReference((PyObject *)unicode); | 
| Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 454 | PyObject_Del(unicode); | 
| Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 455 | return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 456 | } | 
|  | 457 |  | 
| Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 458 | static const char* | 
|  | 459 | unicode_kind_name(PyObject *unicode) | 
|  | 460 | { | 
|  | 461 | assert(PyUnicode_Check(unicode)); | 
|  | 462 | if (!PyUnicode_IS_COMPACT(unicode)) | 
|  | 463 | { | 
|  | 464 | if (!PyUnicode_IS_READY(unicode)) | 
|  | 465 | return "wstr"; | 
|  | 466 | switch(PyUnicode_KIND(unicode)) | 
|  | 467 | { | 
|  | 468 | case PyUnicode_1BYTE_KIND: | 
|  | 469 | if (PyUnicode_IS_COMPACT_ASCII(unicode)) | 
|  | 470 | return "legacy ascii"; | 
|  | 471 | else | 
|  | 472 | return "legacy latin1"; | 
|  | 473 | case PyUnicode_2BYTE_KIND: | 
|  | 474 | return "legacy UCS2"; | 
|  | 475 | case PyUnicode_4BYTE_KIND: | 
|  | 476 | return "legacy UCS4"; | 
|  | 477 | default: | 
|  | 478 | return "<legacy invalid kind>"; | 
|  | 479 | } | 
|  | 480 | } | 
|  | 481 | assert(PyUnicode_IS_READY(unicode)); | 
|  | 482 | switch(PyUnicode_KIND(unicode)) | 
|  | 483 | { | 
|  | 484 | case PyUnicode_1BYTE_KIND: | 
|  | 485 | if (PyUnicode_IS_COMPACT_ASCII(unicode)) | 
|  | 486 | return "ascii"; | 
|  | 487 | else | 
|  | 488 | return "compact latin1"; | 
|  | 489 | case PyUnicode_2BYTE_KIND: | 
|  | 490 | return "compact UCS2"; | 
|  | 491 | case PyUnicode_4BYTE_KIND: | 
|  | 492 | return "compact UCS4"; | 
|  | 493 | default: | 
|  | 494 | return "<invalid compact kind>"; | 
|  | 495 | } | 
|  | 496 | } | 
|  | 497 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 498 | #ifdef Py_DEBUG | 
|  | 499 | int unicode_new_new_calls = 0; | 
|  | 500 |  | 
|  | 501 | /* Functions wrapping macros for use in debugger */ | 
|  | 502 | char *_PyUnicode_utf8(void *unicode){ | 
| Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 503 | return PyUnicode_UTF8(unicode); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 504 | } | 
|  | 505 |  | 
|  | 506 | void *_PyUnicode_compact_data(void *unicode) { | 
|  | 507 | return _PyUnicode_COMPACT_DATA(unicode); | 
|  | 508 | } | 
|  | 509 | void *_PyUnicode_data(void *unicode){ | 
|  | 510 | printf("obj %p\n", unicode); | 
|  | 511 | printf("compact %d\n", PyUnicode_IS_COMPACT(unicode)); | 
|  | 512 | printf("compact ascii %d\n", PyUnicode_IS_COMPACT_ASCII(unicode)); | 
|  | 513 | printf("ascii op %p\n", ((void*)((PyASCIIObject*)(unicode) + 1))); | 
|  | 514 | printf("compact op %p\n", ((void*)((PyCompactUnicodeObject*)(unicode) + 1))); | 
|  | 515 | printf("compact data %p\n", _PyUnicode_COMPACT_DATA(unicode)); | 
|  | 516 | return PyUnicode_DATA(unicode); | 
|  | 517 | } | 
| Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame^] | 518 |  | 
|  | 519 | void | 
|  | 520 | _PyUnicode_Dump(PyObject *op) | 
|  | 521 | { | 
|  | 522 | PyASCIIObject *ascii = (PyASCIIObject *)op; | 
|  | 523 | printf("%s: len=%zu, wstr=%p", | 
|  | 524 | unicode_kind_name(op), | 
|  | 525 | ascii->length, | 
|  | 526 | ascii->wstr); | 
|  | 527 | if (!ascii->state.ascii) { | 
|  | 528 | PyCompactUnicodeObject *compact = (PyCompactUnicodeObject *)op; | 
|  | 529 | printf(" (%zu), utf8=%p (%zu)", | 
|  | 530 | compact->wstr_length, | 
|  | 531 | compact->utf8, | 
|  | 532 | compact->utf8_length); | 
|  | 533 | } | 
|  | 534 | if (!ascii->state.compact) { | 
|  | 535 | PyUnicodeObject *unicode = (PyUnicodeObject *)op; | 
|  | 536 | printf(", data=%p", | 
|  | 537 | unicode->data.any); | 
|  | 538 | } | 
|  | 539 | printf("\n"); | 
|  | 540 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 541 | #endif | 
|  | 542 |  | 
|  | 543 | PyObject * | 
|  | 544 | PyUnicode_New(Py_ssize_t size, Py_UCS4 maxchar) | 
|  | 545 | { | 
|  | 546 | PyObject *obj; | 
|  | 547 | PyCompactUnicodeObject *unicode; | 
|  | 548 | void *data; | 
|  | 549 | int kind_state; | 
|  | 550 | int is_sharing = 0, is_ascii = 0; | 
|  | 551 | Py_ssize_t char_size; | 
|  | 552 | Py_ssize_t struct_size; | 
|  | 553 |  | 
|  | 554 | /* Optimization for empty strings */ | 
|  | 555 | if (size == 0 && unicode_empty != NULL) { | 
|  | 556 | Py_INCREF(unicode_empty); | 
| Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 557 | return unicode_empty; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 558 | } | 
|  | 559 |  | 
|  | 560 | #ifdef Py_DEBUG | 
|  | 561 | ++unicode_new_new_calls; | 
|  | 562 | #endif | 
|  | 563 |  | 
|  | 564 | struct_size = sizeof(PyCompactUnicodeObject); | 
|  | 565 | if (maxchar < 128) { | 
|  | 566 | kind_state = PyUnicode_1BYTE_KIND; | 
|  | 567 | char_size = 1; | 
|  | 568 | is_ascii = 1; | 
|  | 569 | struct_size = sizeof(PyASCIIObject); | 
|  | 570 | } | 
|  | 571 | else if (maxchar < 256) { | 
|  | 572 | kind_state = PyUnicode_1BYTE_KIND; | 
|  | 573 | char_size = 1; | 
|  | 574 | } | 
|  | 575 | else if (maxchar < 65536) { | 
|  | 576 | kind_state = PyUnicode_2BYTE_KIND; | 
|  | 577 | char_size = 2; | 
|  | 578 | if (sizeof(wchar_t) == 2) | 
|  | 579 | is_sharing = 1; | 
|  | 580 | } | 
|  | 581 | else { | 
|  | 582 | kind_state = PyUnicode_4BYTE_KIND; | 
|  | 583 | char_size = 4; | 
|  | 584 | if (sizeof(wchar_t) == 4) | 
|  | 585 | is_sharing = 1; | 
|  | 586 | } | 
|  | 587 |  | 
|  | 588 | /* Ensure we won't overflow the size. */ | 
|  | 589 | if (size < 0) { | 
|  | 590 | PyErr_SetString(PyExc_SystemError, | 
|  | 591 | "Negative size passed to PyUnicode_New"); | 
|  | 592 | return NULL; | 
|  | 593 | } | 
|  | 594 | if (size > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) | 
|  | 595 | return PyErr_NoMemory(); | 
|  | 596 |  | 
|  | 597 | /* Duplicated allocation code from _PyObject_New() instead of a call to | 
|  | 598 | * PyObject_New() so we are able to allocate space for the object and | 
|  | 599 | * it's data buffer. | 
|  | 600 | */ | 
|  | 601 | obj = (PyObject *) PyObject_MALLOC(struct_size + (size + 1) * char_size); | 
|  | 602 | if (obj == NULL) | 
|  | 603 | return PyErr_NoMemory(); | 
|  | 604 | obj = PyObject_INIT(obj, &PyUnicode_Type); | 
|  | 605 | if (obj == NULL) | 
|  | 606 | return NULL; | 
|  | 607 |  | 
|  | 608 | unicode = (PyCompactUnicodeObject *)obj; | 
|  | 609 | if (is_ascii) | 
|  | 610 | data = ((PyASCIIObject*)obj) + 1; | 
|  | 611 | else | 
|  | 612 | data = unicode + 1; | 
|  | 613 | _PyUnicode_LENGTH(unicode) = size; | 
|  | 614 | _PyUnicode_HASH(unicode) = -1; | 
|  | 615 | _PyUnicode_STATE(unicode).interned = 0; | 
|  | 616 | _PyUnicode_STATE(unicode).kind = kind_state; | 
|  | 617 | _PyUnicode_STATE(unicode).compact = 1; | 
|  | 618 | _PyUnicode_STATE(unicode).ready = 1; | 
|  | 619 | _PyUnicode_STATE(unicode).ascii = is_ascii; | 
|  | 620 | if (is_ascii) { | 
|  | 621 | ((char*)data)[size] = 0; | 
|  | 622 | _PyUnicode_WSTR(unicode) = NULL; | 
|  | 623 | } | 
|  | 624 | else if (kind_state == PyUnicode_1BYTE_KIND) { | 
|  | 625 | ((char*)data)[size] = 0; | 
|  | 626 | _PyUnicode_WSTR(unicode) = NULL; | 
|  | 627 | _PyUnicode_WSTR_LENGTH(unicode) = 0; | 
|  | 628 | unicode->utf8_length = 0; | 
|  | 629 | unicode->utf8 = NULL; | 
|  | 630 | } | 
|  | 631 | else { | 
|  | 632 | unicode->utf8 = NULL; | 
|  | 633 | if (kind_state == PyUnicode_2BYTE_KIND) | 
|  | 634 | ((Py_UCS2*)data)[size] = 0; | 
|  | 635 | else /* kind_state == PyUnicode_4BYTE_KIND */ | 
|  | 636 | ((Py_UCS4*)data)[size] = 0; | 
|  | 637 | if (is_sharing) { | 
|  | 638 | _PyUnicode_WSTR_LENGTH(unicode) = size; | 
|  | 639 | _PyUnicode_WSTR(unicode) = (wchar_t *)data; | 
|  | 640 | } | 
|  | 641 | else { | 
|  | 642 | _PyUnicode_WSTR_LENGTH(unicode) = 0; | 
|  | 643 | _PyUnicode_WSTR(unicode) = NULL; | 
|  | 644 | } | 
|  | 645 | } | 
|  | 646 | return obj; | 
|  | 647 | } | 
|  | 648 |  | 
|  | 649 | #if SIZEOF_WCHAR_T == 2 | 
|  | 650 | /* Helper function to convert a 16-bits wchar_t representation to UCS4, this | 
|  | 651 | will decode surrogate pairs, the other conversions are implemented as macros | 
|  | 652 | for efficency. | 
|  | 653 |  | 
|  | 654 | This function assumes that unicode can hold one more code point than wstr | 
|  | 655 | characters for a terminating null character. */ | 
| Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 656 | static void | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 657 | unicode_convert_wchar_to_ucs4(const wchar_t *begin, const wchar_t *end, | 
|  | 658 | PyUnicodeObject *unicode) | 
|  | 659 | { | 
|  | 660 | const wchar_t *iter; | 
|  | 661 | Py_UCS4 *ucs4_out; | 
|  | 662 |  | 
|  | 663 | assert(unicode && PyUnicode_Check(unicode)); | 
|  | 664 | assert(_PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND); | 
|  | 665 | ucs4_out = PyUnicode_4BYTE_DATA(unicode); | 
|  | 666 |  | 
|  | 667 | for (iter = begin; iter < end; ) { | 
|  | 668 | assert(ucs4_out < (PyUnicode_4BYTE_DATA(unicode) + | 
|  | 669 | _PyUnicode_GET_LENGTH(unicode))); | 
|  | 670 | if (*iter >= 0xD800 && *iter <= 0xDBFF | 
|  | 671 | && (iter+1) < end && iter[1] >= 0xDC00 && iter[1] <= 0xDFFF) | 
|  | 672 | { | 
|  | 673 | *ucs4_out++ = (((iter[0] & 0x3FF)<<10) | (iter[1] & 0x3FF)) + 0x10000; | 
|  | 674 | iter += 2; | 
|  | 675 | } | 
|  | 676 | else { | 
|  | 677 | *ucs4_out++ = *iter; | 
|  | 678 | iter++; | 
|  | 679 | } | 
|  | 680 | } | 
|  | 681 | assert(ucs4_out == (PyUnicode_4BYTE_DATA(unicode) + | 
|  | 682 | _PyUnicode_GET_LENGTH(unicode))); | 
|  | 683 |  | 
|  | 684 | return 0; | 
|  | 685 | } | 
|  | 686 | #endif | 
|  | 687 |  | 
| Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 688 | static int | 
|  | 689 | _PyUnicode_Dirty(PyObject *unicode) | 
|  | 690 | { | 
|  | 691 | assert(PyUnicode_Check(unicode)); | 
|  | 692 | if (Py_REFCNT(unicode) != 1) { | 
|  | 693 | PyErr_SetString(PyExc_ValueError, | 
|  | 694 | "Cannot modify a string having more than 1 reference"); | 
|  | 695 | return -1; | 
|  | 696 | } | 
|  | 697 | _PyUnicode_DIRTY(unicode); | 
|  | 698 | return 0; | 
|  | 699 | } | 
|  | 700 |  | 
| Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 701 | Py_ssize_t | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 702 | PyUnicode_CopyCharacters(PyObject *to, Py_ssize_t to_start, | 
|  | 703 | PyObject *from, Py_ssize_t from_start, | 
|  | 704 | Py_ssize_t how_many) | 
|  | 705 | { | 
| Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 706 | unsigned int from_kind, to_kind; | 
|  | 707 | void *from_data, *to_data; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 708 |  | 
| Victor Stinner | b153615 | 2011-09-30 02:26:10 +0200 | [diff] [blame] | 709 | if (!PyUnicode_Check(from) || !PyUnicode_Check(to)) { | 
|  | 710 | PyErr_BadInternalCall(); | 
|  | 711 | return -1; | 
|  | 712 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 713 |  | 
|  | 714 | if (PyUnicode_READY(from)) | 
|  | 715 | return -1; | 
|  | 716 | if (PyUnicode_READY(to)) | 
|  | 717 | return -1; | 
|  | 718 |  | 
| Victor Stinner | ff9e50f | 2011-09-28 22:17:19 +0200 | [diff] [blame] | 719 | how_many = Py_MIN(PyUnicode_GET_LENGTH(from), how_many); | 
| Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 720 | if (to_start + how_many > PyUnicode_GET_LENGTH(to)) { | 
|  | 721 | PyErr_Format(PyExc_ValueError, | 
|  | 722 | "Cannot write %zi characters at %zi " | 
|  | 723 | "in a string of %zi characters", | 
|  | 724 | how_many, to_start, PyUnicode_GET_LENGTH(to)); | 
|  | 725 | return -1; | 
|  | 726 | } | 
| Victor Stinner | f5ca1a2 | 2011-09-28 23:54:59 +0200 | [diff] [blame] | 727 | if (how_many == 0) | 
|  | 728 | return 0; | 
|  | 729 |  | 
| Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 730 | if (_PyUnicode_Dirty(to)) | 
| Victor Stinner | f5ca1a2 | 2011-09-28 23:54:59 +0200 | [diff] [blame] | 731 | return -1; | 
| Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 732 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 733 | from_kind = PyUnicode_KIND(from); | 
| Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 734 | from_data = PyUnicode_DATA(from); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 735 | to_kind = PyUnicode_KIND(to); | 
| Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 736 | to_data = PyUnicode_DATA(to); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 737 |  | 
| Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 738 | if (from_kind == to_kind | 
|  | 739 | /* deny latin1 => ascii */ | 
|  | 740 | && PyUnicode_MAX_CHAR_VALUE(to) >= PyUnicode_MAX_CHAR_VALUE(from)) | 
|  | 741 | { | 
| Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 742 | Py_MEMCPY((char*)to_data | 
| Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 743 | + PyUnicode_KIND_SIZE(to_kind, to_start), | 
| Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 744 | (char*)from_data | 
| Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 745 | + PyUnicode_KIND_SIZE(from_kind, from_start), | 
|  | 746 | PyUnicode_KIND_SIZE(to_kind, how_many)); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 747 | } | 
| Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 748 | else if (from_kind == PyUnicode_1BYTE_KIND | 
|  | 749 | && to_kind == PyUnicode_2BYTE_KIND) | 
| Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 750 | { | 
|  | 751 | _PyUnicode_CONVERT_BYTES( | 
|  | 752 | Py_UCS1, Py_UCS2, | 
|  | 753 | PyUnicode_1BYTE_DATA(from) + from_start, | 
|  | 754 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, | 
|  | 755 | PyUnicode_2BYTE_DATA(to) + to_start | 
|  | 756 | ); | 
| Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 757 | } | 
| Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 758 | else if (from_kind == PyUnicode_1BYTE_KIND | 
| Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 759 | && to_kind == PyUnicode_4BYTE_KIND) | 
|  | 760 | { | 
|  | 761 | _PyUnicode_CONVERT_BYTES( | 
|  | 762 | Py_UCS1, Py_UCS4, | 
|  | 763 | PyUnicode_1BYTE_DATA(from) + from_start, | 
|  | 764 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, | 
|  | 765 | PyUnicode_4BYTE_DATA(to) + to_start | 
|  | 766 | ); | 
| Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 767 | } | 
|  | 768 | else if (from_kind == PyUnicode_2BYTE_KIND | 
|  | 769 | && to_kind == PyUnicode_4BYTE_KIND) | 
|  | 770 | { | 
|  | 771 | _PyUnicode_CONVERT_BYTES( | 
|  | 772 | Py_UCS2, Py_UCS4, | 
|  | 773 | PyUnicode_2BYTE_DATA(from) + from_start, | 
|  | 774 | PyUnicode_2BYTE_DATA(from) + from_start + how_many, | 
|  | 775 | PyUnicode_4BYTE_DATA(to) + to_start | 
|  | 776 | ); | 
| Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 777 | } | 
| Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 778 | else { | 
|  | 779 | int invalid_kinds; | 
| Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 780 |  | 
|  | 781 | /* check if max_char(from substring) <= max_char(to) */ | 
|  | 782 | if (from_kind > to_kind | 
|  | 783 | /* latin1 => ascii */ | 
|  | 784 | || (PyUnicode_IS_COMPACT_ASCII(to) | 
|  | 785 | && to_kind == PyUnicode_1BYTE_KIND | 
|  | 786 | && !PyUnicode_IS_COMPACT_ASCII(from))) | 
|  | 787 | { | 
| Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 788 | /* slow path to check for character overflow */ | 
|  | 789 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); | 
|  | 790 | Py_UCS4 ch, maxchar; | 
|  | 791 | Py_ssize_t i; | 
|  | 792 |  | 
|  | 793 | maxchar = 0; | 
|  | 794 | invalid_kinds = 0; | 
|  | 795 | for (i=0; i < how_many; i++) { | 
|  | 796 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); | 
|  | 797 | if (ch > maxchar) { | 
|  | 798 | maxchar = ch; | 
|  | 799 | if (maxchar > to_maxchar) { | 
|  | 800 | invalid_kinds = 1; | 
|  | 801 | break; | 
|  | 802 | } | 
|  | 803 | } | 
|  | 804 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); | 
|  | 805 | } | 
|  | 806 | } | 
|  | 807 | else | 
|  | 808 | invalid_kinds = 1; | 
|  | 809 | if (invalid_kinds) { | 
|  | 810 | PyErr_Format(PyExc_ValueError, | 
| Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 811 | "Cannot copy %s characters " | 
|  | 812 | "into a string of %s characters", | 
|  | 813 | unicode_kind_name(from), | 
|  | 814 | unicode_kind_name(to)); | 
| Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 815 | return -1; | 
|  | 816 | } | 
|  | 817 | } | 
|  | 818 | return how_many; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 819 | } | 
|  | 820 |  | 
| Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 821 | /* Find the maximum code point and count the number of surrogate pairs so a | 
|  | 822 | correct string length can be computed before converting a string to UCS4. | 
|  | 823 | This function counts single surrogates as a character and not as a pair. | 
|  | 824 |  | 
|  | 825 | Return 0 on success, or -1 on error. */ | 
|  | 826 | static int | 
|  | 827 | find_maxchar_surrogates(const wchar_t *begin, const wchar_t *end, | 
|  | 828 | Py_UCS4 *maxchar, Py_ssize_t *num_surrogates) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 829 | { | 
|  | 830 | const wchar_t *iter; | 
|  | 831 |  | 
| Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 832 | assert(num_surrogates != NULL && maxchar != NULL); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 833 | if (num_surrogates == NULL || maxchar == NULL) { | 
|  | 834 | PyErr_SetString(PyExc_SystemError, | 
|  | 835 | "unexpected NULL arguments to " | 
|  | 836 | "PyUnicode_FindMaxCharAndNumSurrogatePairs"); | 
|  | 837 | return -1; | 
|  | 838 | } | 
|  | 839 |  | 
|  | 840 | *num_surrogates = 0; | 
|  | 841 | *maxchar = 0; | 
|  | 842 |  | 
|  | 843 | for (iter = begin; iter < end; ) { | 
|  | 844 | if (*iter > *maxchar) | 
|  | 845 | *maxchar = *iter; | 
|  | 846 | #if SIZEOF_WCHAR_T == 2 | 
|  | 847 | if (*iter >= 0xD800 && *iter <= 0xDBFF | 
|  | 848 | && (iter+1) < end && iter[1] >= 0xDC00 && iter[1] <= 0xDFFF) | 
|  | 849 | { | 
|  | 850 | Py_UCS4 surrogate_val; | 
|  | 851 | surrogate_val = (((iter[0] & 0x3FF)<<10) | 
|  | 852 | | (iter[1] & 0x3FF)) + 0x10000; | 
|  | 853 | ++(*num_surrogates); | 
|  | 854 | if (surrogate_val > *maxchar) | 
|  | 855 | *maxchar = surrogate_val; | 
|  | 856 | iter += 2; | 
|  | 857 | } | 
|  | 858 | else | 
|  | 859 | iter++; | 
|  | 860 | #else | 
|  | 861 | iter++; | 
|  | 862 | #endif | 
|  | 863 | } | 
|  | 864 | return 0; | 
|  | 865 | } | 
|  | 866 |  | 
|  | 867 | #ifdef Py_DEBUG | 
|  | 868 | int unicode_ready_calls = 0; | 
|  | 869 | #endif | 
|  | 870 |  | 
|  | 871 | int | 
| Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 872 | _PyUnicode_Ready(PyObject *obj) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 873 | { | 
| Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 874 | PyUnicodeObject *unicode = (PyUnicodeObject *)obj; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 875 | wchar_t *end; | 
|  | 876 | Py_UCS4 maxchar = 0; | 
|  | 877 | Py_ssize_t num_surrogates; | 
|  | 878 | #if SIZEOF_WCHAR_T == 2 | 
|  | 879 | Py_ssize_t length_wo_surrogates; | 
|  | 880 | #endif | 
|  | 881 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 882 | /* _PyUnicode_Ready() is only intented for old-style API usage where | 
| Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 883 | strings were created using _PyObject_New() and where no canonical | 
|  | 884 | representation (the str field) has been set yet aka strings | 
|  | 885 | which are not yet ready. */ | 
|  | 886 | assert(PyUnicode_Check(obj)); | 
|  | 887 | assert(!PyUnicode_IS_READY(obj)); | 
|  | 888 | assert(!PyUnicode_IS_COMPACT(obj)); | 
|  | 889 | assert(_PyUnicode_KIND(obj) == PyUnicode_WCHAR_KIND); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 890 | assert(_PyUnicode_WSTR(unicode) != NULL); | 
| Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 891 | assert(_PyUnicode_DATA_ANY(unicode) == NULL); | 
| Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 892 | assert(_PyUnicode_UTF8(unicode) == NULL); | 
| Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 893 | /* Actually, it should neither be interned nor be anything else: */ | 
|  | 894 | assert(_PyUnicode_STATE(unicode).interned == SSTATE_NOT_INTERNED); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 895 |  | 
|  | 896 | #ifdef Py_DEBUG | 
|  | 897 | ++unicode_ready_calls; | 
|  | 898 | #endif | 
|  | 899 |  | 
|  | 900 | end = _PyUnicode_WSTR(unicode) + _PyUnicode_WSTR_LENGTH(unicode); | 
| Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 901 | if (find_maxchar_surrogates(_PyUnicode_WSTR(unicode), end, | 
| Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 902 | &maxchar, &num_surrogates) == -1) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 903 | return -1; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 904 |  | 
|  | 905 | if (maxchar < 256) { | 
| Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 906 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(_PyUnicode_WSTR_LENGTH(unicode) + 1); | 
|  | 907 | if (!_PyUnicode_DATA_ANY(unicode)) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 908 | PyErr_NoMemory(); | 
|  | 909 | return -1; | 
|  | 910 | } | 
| Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 911 | _PyUnicode_CONVERT_BYTES(wchar_t, unsigned char, | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 912 | _PyUnicode_WSTR(unicode), end, | 
|  | 913 | PyUnicode_1BYTE_DATA(unicode)); | 
|  | 914 | PyUnicode_1BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; | 
|  | 915 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); | 
|  | 916 | _PyUnicode_STATE(unicode).kind = PyUnicode_1BYTE_KIND; | 
|  | 917 | if (maxchar < 128) { | 
| Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 918 | _PyUnicode_UTF8(unicode) = _PyUnicode_DATA_ANY(unicode); | 
| Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 919 | _PyUnicode_UTF8_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 920 | } | 
|  | 921 | else { | 
| Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 922 | _PyUnicode_UTF8(unicode) = NULL; | 
|  | 923 | _PyUnicode_UTF8_LENGTH(unicode) = 0; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 924 | } | 
|  | 925 | PyObject_FREE(_PyUnicode_WSTR(unicode)); | 
|  | 926 | _PyUnicode_WSTR(unicode) = NULL; | 
|  | 927 | _PyUnicode_WSTR_LENGTH(unicode) = 0; | 
|  | 928 | } | 
|  | 929 | /* In this case we might have to convert down from 4-byte native | 
|  | 930 | wchar_t to 2-byte unicode. */ | 
|  | 931 | else if (maxchar < 65536) { | 
|  | 932 | assert(num_surrogates == 0 && | 
|  | 933 | "FindMaxCharAndNumSurrogatePairs() messed up"); | 
|  | 934 |  | 
| Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 935 | #if SIZEOF_WCHAR_T == 2 | 
|  | 936 | /* We can share representations and are done. */ | 
| Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 937 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); | 
| Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 938 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; | 
|  | 939 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); | 
|  | 940 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; | 
| Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 941 | _PyUnicode_UTF8(unicode) = NULL; | 
|  | 942 | _PyUnicode_UTF8_LENGTH(unicode) = 0; | 
| Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 943 | #else | 
|  | 944 | /* sizeof(wchar_t) == 4 */ | 
| Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 945 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC( | 
| Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 946 | 2 * (_PyUnicode_WSTR_LENGTH(unicode) + 1)); | 
| Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 947 | if (!_PyUnicode_DATA_ANY(unicode)) { | 
| Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 948 | PyErr_NoMemory(); | 
|  | 949 | return -1; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 950 | } | 
| Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 951 | _PyUnicode_CONVERT_BYTES(wchar_t, Py_UCS2, | 
|  | 952 | _PyUnicode_WSTR(unicode), end, | 
|  | 953 | PyUnicode_2BYTE_DATA(unicode)); | 
|  | 954 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; | 
|  | 955 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); | 
|  | 956 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; | 
| Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 957 | _PyUnicode_UTF8(unicode) = NULL; | 
|  | 958 | _PyUnicode_UTF8_LENGTH(unicode) = 0; | 
| Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 959 | PyObject_FREE(_PyUnicode_WSTR(unicode)); | 
|  | 960 | _PyUnicode_WSTR(unicode) = NULL; | 
|  | 961 | _PyUnicode_WSTR_LENGTH(unicode) = 0; | 
|  | 962 | #endif | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 963 | } | 
|  | 964 | /* maxchar exeeds 16 bit, wee need 4 bytes for unicode characters */ | 
|  | 965 | else { | 
|  | 966 | #if SIZEOF_WCHAR_T == 2 | 
|  | 967 | /* in case the native representation is 2-bytes, we need to allocate a | 
|  | 968 | new normalized 4-byte version. */ | 
|  | 969 | length_wo_surrogates = _PyUnicode_WSTR_LENGTH(unicode) - num_surrogates; | 
| Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 970 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(4 * (length_wo_surrogates + 1)); | 
|  | 971 | if (!_PyUnicode_DATA_ANY(unicode)) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 972 | PyErr_NoMemory(); | 
|  | 973 | return -1; | 
|  | 974 | } | 
|  | 975 | _PyUnicode_LENGTH(unicode) = length_wo_surrogates; | 
|  | 976 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; | 
| Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 977 | _PyUnicode_UTF8(unicode) = NULL; | 
|  | 978 | _PyUnicode_UTF8_LENGTH(unicode) = 0; | 
| Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 979 | unicode_convert_wchar_to_ucs4(_PyUnicode_WSTR(unicode), end, unicode); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 980 | PyObject_FREE(_PyUnicode_WSTR(unicode)); | 
|  | 981 | _PyUnicode_WSTR(unicode) = NULL; | 
|  | 982 | _PyUnicode_WSTR_LENGTH(unicode) = 0; | 
|  | 983 | #else | 
|  | 984 | assert(num_surrogates == 0); | 
|  | 985 |  | 
| Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 986 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 987 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); | 
| Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 988 | _PyUnicode_UTF8(unicode) = NULL; | 
|  | 989 | _PyUnicode_UTF8_LENGTH(unicode) = 0; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 990 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; | 
|  | 991 | #endif | 
|  | 992 | PyUnicode_4BYTE_DATA(unicode)[_PyUnicode_LENGTH(unicode)] = '\0'; | 
|  | 993 | } | 
|  | 994 | _PyUnicode_STATE(unicode).ready = 1; | 
|  | 995 | return 0; | 
|  | 996 | } | 
|  | 997 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 998 | static void | 
|  | 999 | unicode_dealloc(register PyUnicodeObject *unicode) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1000 | { | 
| Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1001 | switch (PyUnicode_CHECK_INTERNED(unicode)) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1002 | case SSTATE_NOT_INTERNED: | 
|  | 1003 | break; | 
| Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1004 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1005 | case SSTATE_INTERNED_MORTAL: | 
|  | 1006 | /* revive dead object temporarily for DelItem */ | 
|  | 1007 | Py_REFCNT(unicode) = 3; | 
|  | 1008 | if (PyDict_DelItem(interned, (PyObject *)unicode) != 0) | 
|  | 1009 | Py_FatalError( | 
|  | 1010 | "deletion of interned string failed"); | 
|  | 1011 | break; | 
| Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1012 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1013 | case SSTATE_INTERNED_IMMORTAL: | 
|  | 1014 | Py_FatalError("Immortal interned string died."); | 
| Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1015 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1016 | default: | 
|  | 1017 | Py_FatalError("Inconsistent interned string state."); | 
| Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1018 | } | 
|  | 1019 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1020 | if (_PyUnicode_WSTR(unicode) && | 
|  | 1021 | (!PyUnicode_IS_READY(unicode) || | 
|  | 1022 | _PyUnicode_WSTR(unicode) != PyUnicode_DATA(unicode))) | 
|  | 1023 | PyObject_DEL(_PyUnicode_WSTR(unicode)); | 
| Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1024 | if (!PyUnicode_IS_COMPACT_ASCII(unicode) | 
|  | 1025 | && _PyUnicode_UTF8(unicode) | 
|  | 1026 | && _PyUnicode_UTF8(unicode) != PyUnicode_DATA(unicode)) | 
|  | 1027 | PyObject_DEL(_PyUnicode_UTF8(unicode)); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1028 |  | 
|  | 1029 | if (PyUnicode_IS_COMPACT(unicode)) { | 
|  | 1030 | Py_TYPE(unicode)->tp_free((PyObject *)unicode); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1031 | } | 
|  | 1032 | else { | 
| Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1033 | if (_PyUnicode_DATA_ANY(unicode)) | 
|  | 1034 | PyObject_DEL(_PyUnicode_DATA_ANY(unicode)); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1035 | Py_TYPE(unicode)->tp_free((PyObject *)unicode); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1036 | } | 
|  | 1037 | } | 
|  | 1038 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1039 | static int | 
|  | 1040 | _PyUnicode_Resize(PyUnicodeObject **unicode, Py_ssize_t length) | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1041 | { | 
|  | 1042 | register PyUnicodeObject *v; | 
|  | 1043 |  | 
|  | 1044 | /* Argument checks */ | 
|  | 1045 | if (unicode == NULL) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1046 | PyErr_BadInternalCall(); | 
|  | 1047 | return -1; | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1048 | } | 
| Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1049 | v = *unicode; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1050 | if (v == NULL || !PyUnicode_Check(v) || Py_REFCNT(v) != 1 || length < 0 || | 
|  | 1051 | PyUnicode_IS_COMPACT(v) || _PyUnicode_WSTR(v) == NULL) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1052 | PyErr_BadInternalCall(); | 
|  | 1053 | return -1; | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1054 | } | 
|  | 1055 |  | 
|  | 1056 | /* Resizing unicode_empty and single character objects is not | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1057 | possible since these are being shared. | 
|  | 1058 | The same goes for new-representation unicode objects or objects which | 
|  | 1059 | have already been readied. | 
|  | 1060 | For these, we simply return a fresh copy with the same Unicode content. | 
|  | 1061 | */ | 
|  | 1062 | if ((_PyUnicode_WSTR_LENGTH(v) != length && | 
|  | 1063 | (v == unicode_empty || _PyUnicode_WSTR_LENGTH(v) == 1)) || | 
|  | 1064 | PyUnicode_IS_COMPACT(v) || v->data.any) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1065 | PyUnicodeObject *w = _PyUnicode_New(length); | 
|  | 1066 | if (w == NULL) | 
|  | 1067 | return -1; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1068 | Py_UNICODE_COPY(_PyUnicode_WSTR(w), _PyUnicode_WSTR(v), | 
|  | 1069 | length < _PyUnicode_WSTR_LENGTH(v) ? length : _PyUnicode_WSTR_LENGTH(v)); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1070 | Py_DECREF(*unicode); | 
|  | 1071 | *unicode = w; | 
|  | 1072 | return 0; | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1073 | } | 
|  | 1074 |  | 
|  | 1075 | /* Note that we don't have to modify *unicode for unshared Unicode | 
|  | 1076 | objects, since we can modify them in-place. */ | 
|  | 1077 | return unicode_resize(v, length); | 
|  | 1078 | } | 
|  | 1079 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1080 | int | 
|  | 1081 | PyUnicode_Resize(PyObject **unicode, Py_ssize_t length) | 
| Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1082 | { | 
|  | 1083 | return _PyUnicode_Resize((PyUnicodeObject **)unicode, length); | 
|  | 1084 | } | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1085 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1086 | static PyObject* | 
|  | 1087 | get_latin1_char(unsigned char ch) | 
|  | 1088 | { | 
| Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1089 | PyObject *unicode = unicode_latin1[ch]; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1090 | if (!unicode) { | 
| Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1091 | unicode = PyUnicode_New(1, ch); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1092 | if (!unicode) | 
|  | 1093 | return NULL; | 
|  | 1094 | PyUnicode_1BYTE_DATA(unicode)[0] = ch; | 
|  | 1095 | unicode_latin1[ch] = unicode; | 
|  | 1096 | } | 
|  | 1097 | Py_INCREF(unicode); | 
| Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1098 | return unicode; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1099 | } | 
|  | 1100 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1101 | PyObject * | 
|  | 1102 | PyUnicode_FromUnicode(const Py_UNICODE *u, Py_ssize_t size) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1103 | { | 
|  | 1104 | PyUnicodeObject *unicode; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1105 | Py_UCS4 maxchar = 0; | 
|  | 1106 | Py_ssize_t num_surrogates; | 
|  | 1107 |  | 
|  | 1108 | if (u == NULL) | 
|  | 1109 | return (PyObject*)_PyUnicode_New(size); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1110 |  | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1111 | /* If the Unicode data is known at construction time, we can apply | 
|  | 1112 | some optimizations which share commonly used objects. */ | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1113 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1114 | /* Optimization for empty strings */ | 
|  | 1115 | if (size == 0 && unicode_empty != NULL) { | 
|  | 1116 | Py_INCREF(unicode_empty); | 
| Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1117 | return unicode_empty; | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1118 | } | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1119 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1120 | /* Single character Unicode objects in the Latin-1 range are | 
|  | 1121 | shared when using this constructor */ | 
|  | 1122 | if (size == 1 && *u < 256) | 
|  | 1123 | return get_latin1_char((unsigned char)*u); | 
|  | 1124 |  | 
|  | 1125 | /* If not empty and not single character, copy the Unicode data | 
|  | 1126 | into the new object */ | 
| Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1127 | if (find_maxchar_surrogates(u, u + size, | 
|  | 1128 | &maxchar, &num_surrogates) == -1) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1129 | return NULL; | 
|  | 1130 |  | 
|  | 1131 | unicode = (PyUnicodeObject *) PyUnicode_New(size - num_surrogates, | 
|  | 1132 | maxchar); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1133 | if (!unicode) | 
|  | 1134 | return NULL; | 
|  | 1135 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1136 | switch (PyUnicode_KIND(unicode)) { | 
|  | 1137 | case PyUnicode_1BYTE_KIND: | 
| Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1138 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, unsigned char, | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1139 | u, u + size, PyUnicode_1BYTE_DATA(unicode)); | 
|  | 1140 | break; | 
|  | 1141 | case PyUnicode_2BYTE_KIND: | 
|  | 1142 | #if Py_UNICODE_SIZE == 2 | 
|  | 1143 | Py_MEMCPY(PyUnicode_2BYTE_DATA(unicode), u, size * 2); | 
|  | 1144 | #else | 
| Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1145 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, Py_UCS2, | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1146 | u, u + size, PyUnicode_2BYTE_DATA(unicode)); | 
|  | 1147 | #endif | 
|  | 1148 | break; | 
|  | 1149 | case PyUnicode_4BYTE_KIND: | 
|  | 1150 | #if SIZEOF_WCHAR_T == 2 | 
|  | 1151 | /* This is the only case which has to process surrogates, thus | 
|  | 1152 | a simple copy loop is not enough and we need a function. */ | 
| Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1153 | unicode_convert_wchar_to_ucs4(u, u + size, unicode); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1154 | #else | 
|  | 1155 | assert(num_surrogates == 0); | 
|  | 1156 | Py_MEMCPY(PyUnicode_4BYTE_DATA(unicode), u, size * 4); | 
|  | 1157 | #endif | 
|  | 1158 | break; | 
|  | 1159 | default: | 
|  | 1160 | assert(0 && "Impossible state"); | 
|  | 1161 | } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1162 |  | 
|  | 1163 | return (PyObject *)unicode; | 
|  | 1164 | } | 
|  | 1165 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1166 | PyObject * | 
|  | 1167 | PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) | 
| Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1168 | { | 
|  | 1169 | PyUnicodeObject *unicode; | 
| Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1170 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1171 | if (size < 0) { | 
|  | 1172 | PyErr_SetString(PyExc_SystemError, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1173 | "Negative size passed to PyUnicode_FromStringAndSize"); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1174 | return NULL; | 
|  | 1175 | } | 
| Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1176 |  | 
| Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1177 | /* If the Unicode data is known at construction time, we can apply | 
| Martin v. Löwis | 9c12106 | 2007-08-05 20:26:11 +0000 | [diff] [blame] | 1178 | some optimizations which share commonly used objects. | 
|  | 1179 | Also, this means the input must be UTF-8, so fall back to the | 
|  | 1180 | UTF-8 decoder at the end. */ | 
| Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1181 | if (u != NULL) { | 
|  | 1182 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1183 | /* Optimization for empty strings */ | 
|  | 1184 | if (size == 0 && unicode_empty != NULL) { | 
|  | 1185 | Py_INCREF(unicode_empty); | 
| Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1186 | return unicode_empty; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1187 | } | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1188 |  | 
|  | 1189 | /* Single characters are shared when using this constructor. | 
|  | 1190 | Restrict to ASCII, since the input must be UTF-8. */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1191 | if (size == 1 && Py_CHARMASK(*u) < 128) | 
|  | 1192 | return get_latin1_char(Py_CHARMASK(*u)); | 
| Martin v. Löwis | 9c12106 | 2007-08-05 20:26:11 +0000 | [diff] [blame] | 1193 |  | 
|  | 1194 | return PyUnicode_DecodeUTF8(u, size, NULL); | 
| Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1195 | } | 
|  | 1196 |  | 
| Walter Dörwald | 5550731 | 2007-05-18 13:12:10 +0000 | [diff] [blame] | 1197 | unicode = _PyUnicode_New(size); | 
| Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1198 | if (!unicode) | 
|  | 1199 | return NULL; | 
|  | 1200 |  | 
| Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1201 | return (PyObject *)unicode; | 
|  | 1202 | } | 
|  | 1203 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1204 | PyObject * | 
|  | 1205 | PyUnicode_FromString(const char *u) | 
| Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1206 | { | 
|  | 1207 | size_t size = strlen(u); | 
|  | 1208 | if (size > PY_SSIZE_T_MAX) { | 
|  | 1209 | PyErr_SetString(PyExc_OverflowError, "input too long"); | 
|  | 1210 | return NULL; | 
|  | 1211 | } | 
|  | 1212 |  | 
|  | 1213 | return PyUnicode_FromStringAndSize(u, size); | 
|  | 1214 | } | 
|  | 1215 |  | 
| Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1216 | static PyObject* | 
|  | 1217 | _PyUnicode_FromUCS1(const unsigned char* u, Py_ssize_t size) | 
| Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1218 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1219 | PyObject *res; | 
|  | 1220 | unsigned char max = 127; | 
|  | 1221 | Py_ssize_t i; | 
|  | 1222 | for (i = 0; i < size; i++) { | 
|  | 1223 | if (u[i] & 0x80) { | 
|  | 1224 | max = 255; | 
|  | 1225 | break; | 
| Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1226 | } | 
|  | 1227 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1228 | res = PyUnicode_New(size, max); | 
|  | 1229 | if (!res) | 
|  | 1230 | return NULL; | 
|  | 1231 | memcpy(PyUnicode_1BYTE_DATA(res), u, size); | 
|  | 1232 | return res; | 
| Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1233 | } | 
|  | 1234 |  | 
| Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1235 | static PyObject* | 
|  | 1236 | _PyUnicode_FromUCS2(const Py_UCS2 *u, Py_ssize_t size) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1237 | { | 
|  | 1238 | PyObject *res; | 
|  | 1239 | Py_UCS2 max = 0; | 
|  | 1240 | Py_ssize_t i; | 
|  | 1241 | for (i = 0; i < size; i++) | 
|  | 1242 | if (u[i] > max) | 
|  | 1243 | max = u[i]; | 
|  | 1244 | res = PyUnicode_New(size, max); | 
|  | 1245 | if (!res) | 
|  | 1246 | return NULL; | 
|  | 1247 | if (max >= 256) | 
|  | 1248 | memcpy(PyUnicode_2BYTE_DATA(res), u, sizeof(Py_UCS2)*size); | 
|  | 1249 | else | 
|  | 1250 | for (i = 0; i < size; i++) | 
|  | 1251 | PyUnicode_1BYTE_DATA(res)[i] = (Py_UCS1)u[i]; | 
|  | 1252 | return res; | 
|  | 1253 | } | 
|  | 1254 |  | 
| Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1255 | static PyObject* | 
|  | 1256 | _PyUnicode_FromUCS4(const Py_UCS4 *u, Py_ssize_t size) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1257 | { | 
|  | 1258 | PyObject *res; | 
|  | 1259 | Py_UCS4 max = 0; | 
|  | 1260 | Py_ssize_t i; | 
|  | 1261 | for (i = 0; i < size; i++) | 
|  | 1262 | if (u[i] > max) | 
|  | 1263 | max = u[i]; | 
|  | 1264 | res = PyUnicode_New(size, max); | 
|  | 1265 | if (!res) | 
|  | 1266 | return NULL; | 
|  | 1267 | if (max >= 0x10000) | 
|  | 1268 | memcpy(PyUnicode_4BYTE_DATA(res), u, sizeof(Py_UCS4)*size); | 
|  | 1269 | else { | 
|  | 1270 | int kind = PyUnicode_KIND(res); | 
|  | 1271 | void *data = PyUnicode_DATA(res); | 
|  | 1272 | for (i = 0; i < size; i++) | 
|  | 1273 | PyUnicode_WRITE(kind, data, i, u[i]); | 
|  | 1274 | } | 
|  | 1275 | return res; | 
|  | 1276 | } | 
|  | 1277 |  | 
|  | 1278 | PyObject* | 
|  | 1279 | PyUnicode_FromKindAndData(int kind, const void *buffer, Py_ssize_t size) | 
|  | 1280 | { | 
|  | 1281 | switch(kind) { | 
|  | 1282 | case PyUnicode_1BYTE_KIND: | 
| Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1283 | return _PyUnicode_FromUCS1(buffer, size); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1284 | case PyUnicode_2BYTE_KIND: | 
| Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1285 | return _PyUnicode_FromUCS2(buffer, size); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1286 | case PyUnicode_4BYTE_KIND: | 
| Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1287 | return _PyUnicode_FromUCS4(buffer, size); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1288 | } | 
| Victor Stinner | 202b62b | 2011-10-01 23:48:37 +0200 | [diff] [blame] | 1289 | PyErr_SetString(PyExc_ValueError, "invalid kind"); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1290 | return NULL; | 
|  | 1291 | } | 
|  | 1292 |  | 
| Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1293 | PyObject* | 
|  | 1294 | PyUnicode_Copy(PyObject *unicode) | 
|  | 1295 | { | 
| Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1296 | Py_ssize_t size; | 
|  | 1297 | PyObject *copy; | 
|  | 1298 | void *data; | 
|  | 1299 |  | 
| Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1300 | if (!PyUnicode_Check(unicode)) { | 
|  | 1301 | PyErr_BadInternalCall(); | 
|  | 1302 | return NULL; | 
|  | 1303 | } | 
|  | 1304 | if (PyUnicode_READY(unicode)) | 
|  | 1305 | return NULL; | 
| Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1306 |  | 
|  | 1307 | size = PyUnicode_GET_LENGTH(unicode); | 
|  | 1308 | copy = PyUnicode_New(size, PyUnicode_MAX_CHAR_VALUE(unicode)); | 
|  | 1309 | if (!copy) | 
|  | 1310 | return NULL; | 
|  | 1311 | assert(PyUnicode_KIND(copy) == PyUnicode_KIND(unicode)); | 
|  | 1312 |  | 
|  | 1313 | data = PyUnicode_DATA(unicode); | 
|  | 1314 | switch (PyUnicode_KIND(unicode)) | 
|  | 1315 | { | 
|  | 1316 | case PyUnicode_1BYTE_KIND: | 
|  | 1317 | memcpy(PyUnicode_1BYTE_DATA(copy), data, size); | 
|  | 1318 | break; | 
|  | 1319 | case PyUnicode_2BYTE_KIND: | 
|  | 1320 | memcpy(PyUnicode_2BYTE_DATA(copy), data, sizeof(Py_UCS2) * size); | 
|  | 1321 | break; | 
|  | 1322 | case PyUnicode_4BYTE_KIND: | 
|  | 1323 | memcpy(PyUnicode_4BYTE_DATA(copy), data, sizeof(Py_UCS4) * size); | 
|  | 1324 | break; | 
|  | 1325 | default: | 
|  | 1326 | assert(0); | 
|  | 1327 | break; | 
|  | 1328 | } | 
|  | 1329 | return copy; | 
| Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1330 | } | 
|  | 1331 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1332 |  | 
| Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1333 | /* Widen Unicode objects to larger buffers. Don't write terminating null | 
|  | 1334 | character. Return NULL on error. */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1335 |  | 
|  | 1336 | void* | 
|  | 1337 | _PyUnicode_AsKind(PyObject *s, unsigned int kind) | 
|  | 1338 | { | 
| Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1339 | Py_ssize_t len; | 
|  | 1340 | void *result; | 
|  | 1341 | unsigned int skind; | 
|  | 1342 |  | 
|  | 1343 | if (PyUnicode_READY(s)) | 
|  | 1344 | return NULL; | 
|  | 1345 |  | 
|  | 1346 | len = PyUnicode_GET_LENGTH(s); | 
|  | 1347 | skind = PyUnicode_KIND(s); | 
|  | 1348 | if (skind >= kind) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1349 | PyErr_SetString(PyExc_RuntimeError, "invalid widening attempt"); | 
|  | 1350 | return NULL; | 
|  | 1351 | } | 
|  | 1352 | switch(kind) { | 
| Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1353 | case PyUnicode_2BYTE_KIND: | 
|  | 1354 | result = PyMem_Malloc(len * sizeof(Py_UCS2)); | 
|  | 1355 | if (!result) | 
|  | 1356 | return PyErr_NoMemory(); | 
|  | 1357 | assert(skind == PyUnicode_1BYTE_KIND); | 
|  | 1358 | _PyUnicode_CONVERT_BYTES( | 
|  | 1359 | Py_UCS1, Py_UCS2, | 
|  | 1360 | PyUnicode_1BYTE_DATA(s), | 
|  | 1361 | PyUnicode_1BYTE_DATA(s) + len, | 
|  | 1362 | result); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1363 | return result; | 
| Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1364 | case PyUnicode_4BYTE_KIND: | 
|  | 1365 | result = PyMem_Malloc(len * sizeof(Py_UCS4)); | 
|  | 1366 | if (!result) | 
|  | 1367 | return PyErr_NoMemory(); | 
|  | 1368 | if (skind == PyUnicode_2BYTE_KIND) { | 
|  | 1369 | _PyUnicode_CONVERT_BYTES( | 
|  | 1370 | Py_UCS2, Py_UCS4, | 
|  | 1371 | PyUnicode_2BYTE_DATA(s), | 
|  | 1372 | PyUnicode_2BYTE_DATA(s) + len, | 
|  | 1373 | result); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1374 | } | 
| Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1375 | else { | 
|  | 1376 | assert(skind == PyUnicode_1BYTE_KIND); | 
|  | 1377 | _PyUnicode_CONVERT_BYTES( | 
|  | 1378 | Py_UCS1, Py_UCS4, | 
|  | 1379 | PyUnicode_1BYTE_DATA(s), | 
|  | 1380 | PyUnicode_1BYTE_DATA(s) + len, | 
|  | 1381 | result); | 
|  | 1382 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1383 | return result; | 
| Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1384 | default: | 
|  | 1385 | break; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1386 | } | 
| Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1387 | PyErr_SetString(PyExc_ValueError, "invalid kind"); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1388 | return NULL; | 
|  | 1389 | } | 
|  | 1390 |  | 
|  | 1391 | static Py_UCS4* | 
|  | 1392 | as_ucs4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, | 
|  | 1393 | int copy_null) | 
|  | 1394 | { | 
|  | 1395 | int kind; | 
|  | 1396 | void *data; | 
|  | 1397 | Py_ssize_t len, targetlen; | 
|  | 1398 | if (PyUnicode_READY(string) == -1) | 
|  | 1399 | return NULL; | 
|  | 1400 | kind = PyUnicode_KIND(string); | 
|  | 1401 | data = PyUnicode_DATA(string); | 
|  | 1402 | len = PyUnicode_GET_LENGTH(string); | 
|  | 1403 | targetlen = len; | 
|  | 1404 | if (copy_null) | 
|  | 1405 | targetlen++; | 
|  | 1406 | if (!target) { | 
|  | 1407 | if (PY_SSIZE_T_MAX / sizeof(Py_UCS4) < targetlen) { | 
|  | 1408 | PyErr_NoMemory(); | 
|  | 1409 | return NULL; | 
|  | 1410 | } | 
|  | 1411 | target = PyMem_Malloc(targetlen * sizeof(Py_UCS4)); | 
|  | 1412 | if (!target) { | 
|  | 1413 | PyErr_NoMemory(); | 
|  | 1414 | return NULL; | 
|  | 1415 | } | 
|  | 1416 | } | 
|  | 1417 | else { | 
|  | 1418 | if (targetsize < targetlen) { | 
|  | 1419 | PyErr_Format(PyExc_SystemError, | 
|  | 1420 | "string is longer than the buffer"); | 
|  | 1421 | if (copy_null && 0 < targetsize) | 
|  | 1422 | target[0] = 0; | 
|  | 1423 | return NULL; | 
|  | 1424 | } | 
|  | 1425 | } | 
|  | 1426 | if (kind != PyUnicode_4BYTE_KIND) { | 
|  | 1427 | Py_ssize_t i; | 
|  | 1428 | for (i = 0; i < len; i++) | 
|  | 1429 | target[i] = PyUnicode_READ(kind, data, i); | 
|  | 1430 | } | 
|  | 1431 | else | 
|  | 1432 | Py_MEMCPY(target, data, len * sizeof(Py_UCS4)); | 
|  | 1433 | if (copy_null) | 
|  | 1434 | target[len] = 0; | 
|  | 1435 | return target; | 
|  | 1436 | } | 
|  | 1437 |  | 
|  | 1438 | Py_UCS4* | 
|  | 1439 | PyUnicode_AsUCS4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, | 
|  | 1440 | int copy_null) | 
|  | 1441 | { | 
|  | 1442 | if (target == NULL || targetsize < 1) { | 
|  | 1443 | PyErr_BadInternalCall(); | 
|  | 1444 | return NULL; | 
|  | 1445 | } | 
|  | 1446 | return as_ucs4(string, target, targetsize, copy_null); | 
|  | 1447 | } | 
|  | 1448 |  | 
|  | 1449 | Py_UCS4* | 
|  | 1450 | PyUnicode_AsUCS4Copy(PyObject *string) | 
|  | 1451 | { | 
|  | 1452 | return as_ucs4(string, NULL, 0, 1); | 
|  | 1453 | } | 
|  | 1454 |  | 
|  | 1455 | #ifdef HAVE_WCHAR_H | 
| Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1456 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1457 | PyObject * | 
|  | 1458 | PyUnicode_FromWideChar(register const wchar_t *w, Py_ssize_t size) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1459 | { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1460 | if (w == NULL) { | 
| Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 1461 | if (size == 0) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1462 | return PyUnicode_New(0, 0); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1463 | PyErr_BadInternalCall(); | 
|  | 1464 | return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1465 | } | 
|  | 1466 |  | 
| Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 1467 | if (size == -1) { | 
|  | 1468 | size = wcslen(w); | 
|  | 1469 | } | 
|  | 1470 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1471 | return PyUnicode_FromUnicode(w, size); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1472 | } | 
|  | 1473 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1474 | #endif /* HAVE_WCHAR_H */ | 
| Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1475 |  | 
| Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 1476 | static void | 
| Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1477 | makefmt(char *fmt, int longflag, int longlongflag, int size_tflag, | 
|  | 1478 | int zeropad, int width, int precision, char c) | 
| Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 1479 | { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1480 | *fmt++ = '%'; | 
|  | 1481 | if (width) { | 
|  | 1482 | if (zeropad) | 
|  | 1483 | *fmt++ = '0'; | 
|  | 1484 | fmt += sprintf(fmt, "%d", width); | 
|  | 1485 | } | 
|  | 1486 | if (precision) | 
|  | 1487 | fmt += sprintf(fmt, ".%d", precision); | 
|  | 1488 | if (longflag) | 
|  | 1489 | *fmt++ = 'l'; | 
| Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1490 | else if (longlongflag) { | 
|  | 1491 | /* longlongflag should only ever be nonzero on machines with | 
|  | 1492 | HAVE_LONG_LONG defined */ | 
|  | 1493 | #ifdef HAVE_LONG_LONG | 
|  | 1494 | char *f = PY_FORMAT_LONG_LONG; | 
|  | 1495 | while (*f) | 
|  | 1496 | *fmt++ = *f++; | 
|  | 1497 | #else | 
|  | 1498 | /* we shouldn't ever get here */ | 
|  | 1499 | assert(0); | 
|  | 1500 | *fmt++ = 'l'; | 
|  | 1501 | #endif | 
|  | 1502 | } | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1503 | else if (size_tflag) { | 
|  | 1504 | char *f = PY_FORMAT_SIZE_T; | 
|  | 1505 | while (*f) | 
|  | 1506 | *fmt++ = *f++; | 
|  | 1507 | } | 
|  | 1508 | *fmt++ = c; | 
|  | 1509 | *fmt = '\0'; | 
| Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 1510 | } | 
|  | 1511 |  | 
| Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 1512 | /* helper for PyUnicode_FromFormatV() */ | 
|  | 1513 |  | 
|  | 1514 | static const char* | 
|  | 1515 | parse_format_flags(const char *f, | 
|  | 1516 | int *p_width, int *p_precision, | 
|  | 1517 | int *p_longflag, int *p_longlongflag, int *p_size_tflag) | 
|  | 1518 | { | 
|  | 1519 | int width, precision, longflag, longlongflag, size_tflag; | 
|  | 1520 |  | 
|  | 1521 | /* parse the width.precision part, e.g. "%2.5s" => width=2, precision=5 */ | 
|  | 1522 | f++; | 
|  | 1523 | width = 0; | 
|  | 1524 | while (Py_ISDIGIT((unsigned)*f)) | 
|  | 1525 | width = (width*10) + *f++ - '0'; | 
|  | 1526 | precision = 0; | 
|  | 1527 | if (*f == '.') { | 
|  | 1528 | f++; | 
|  | 1529 | while (Py_ISDIGIT((unsigned)*f)) | 
|  | 1530 | precision = (precision*10) + *f++ - '0'; | 
|  | 1531 | if (*f == '%') { | 
|  | 1532 | /* "%.3%s" => f points to "3" */ | 
|  | 1533 | f--; | 
|  | 1534 | } | 
|  | 1535 | } | 
|  | 1536 | if (*f == '\0') { | 
|  | 1537 | /* bogus format "%.1" => go backward, f points to "1" */ | 
|  | 1538 | f--; | 
|  | 1539 | } | 
|  | 1540 | if (p_width != NULL) | 
|  | 1541 | *p_width = width; | 
|  | 1542 | if (p_precision != NULL) | 
|  | 1543 | *p_precision = precision; | 
|  | 1544 |  | 
|  | 1545 | /* Handle %ld, %lu, %lld and %llu. */ | 
|  | 1546 | longflag = 0; | 
|  | 1547 | longlongflag = 0; | 
| Victor Stinner | e7faec1 | 2011-03-02 00:01:53 +0000 | [diff] [blame] | 1548 | size_tflag = 0; | 
| Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 1549 |  | 
|  | 1550 | if (*f == 'l') { | 
| Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 1551 | if (f[1] == 'd' || f[1] == 'u' || f[1] == 'i') { | 
| Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 1552 | longflag = 1; | 
|  | 1553 | ++f; | 
|  | 1554 | } | 
|  | 1555 | #ifdef HAVE_LONG_LONG | 
|  | 1556 | else if (f[1] == 'l' && | 
| Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 1557 | (f[2] == 'd' || f[2] == 'u' || f[2] == 'i')) { | 
| Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 1558 | longlongflag = 1; | 
|  | 1559 | f += 2; | 
|  | 1560 | } | 
|  | 1561 | #endif | 
|  | 1562 | } | 
|  | 1563 | /* handle the size_t flag. */ | 
| Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 1564 | 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] | 1565 | size_tflag = 1; | 
|  | 1566 | ++f; | 
|  | 1567 | } | 
|  | 1568 | if (p_longflag != NULL) | 
|  | 1569 | *p_longflag = longflag; | 
|  | 1570 | if (p_longlongflag != NULL) | 
|  | 1571 | *p_longlongflag = longlongflag; | 
|  | 1572 | if (p_size_tflag != NULL) | 
|  | 1573 | *p_size_tflag = size_tflag; | 
|  | 1574 | return f; | 
|  | 1575 | } | 
|  | 1576 |  | 
| Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1577 | /* maximum number of characters required for output of %ld.  21 characters | 
|  | 1578 | allows for 64-bit integers (in decimal) and an optional sign. */ | 
|  | 1579 | #define MAX_LONG_CHARS 21 | 
|  | 1580 | /* maximum number of characters required for output of %lld. | 
|  | 1581 | We need at most ceil(log10(256)*SIZEOF_LONG_LONG) digits, | 
|  | 1582 | plus 1 for the sign.  53/22 is an upper bound for log10(256). */ | 
|  | 1583 | #define MAX_LONG_LONG_CHARS (2 + (SIZEOF_LONG_LONG*53-1) / 22) | 
|  | 1584 |  | 
| Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1585 | PyObject * | 
|  | 1586 | PyUnicode_FromFormatV(const char *format, va_list vargs) | 
|  | 1587 | { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1588 | va_list count; | 
|  | 1589 | Py_ssize_t callcount = 0; | 
|  | 1590 | PyObject **callresults = NULL; | 
|  | 1591 | PyObject **callresult = NULL; | 
|  | 1592 | Py_ssize_t n = 0; | 
|  | 1593 | int width = 0; | 
|  | 1594 | int precision = 0; | 
|  | 1595 | int zeropad; | 
|  | 1596 | const char* f; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1597 | PyUnicodeObject *string; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1598 | /* used by sprintf */ | 
| Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1599 | char fmt[61]; /* should be enough for %0width.precisionlld */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1600 | Py_UCS4 maxchar = 127; /* result is ASCII by default */ | 
|  | 1601 | Py_UCS4 argmaxchar; | 
|  | 1602 | Py_ssize_t numbersize = 0; | 
|  | 1603 | char *numberresults = NULL; | 
|  | 1604 | char *numberresult = NULL; | 
|  | 1605 | Py_ssize_t i; | 
|  | 1606 | int kind; | 
|  | 1607 | void *data; | 
| Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1608 |  | 
| Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 1609 | Py_VA_COPY(count, vargs); | 
| Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 1610 | /* step 1: count the number of %S/%R/%A/%s format specifications | 
|  | 1611 | * (we call PyObject_Str()/PyObject_Repr()/PyObject_ASCII()/ | 
|  | 1612 | * PyUnicode_DecodeUTF8() for these objects once during step 3 and put the | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1613 | * result in an array) | 
|  | 1614 | * also esimate a upper bound for all the number formats in the string, | 
|  | 1615 | * numbers will be formated in step 3 and be keept in a '\0'-separated | 
|  | 1616 | * buffer before putting everything together. */ | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1617 | for (f = format; *f; f++) { | 
|  | 1618 | if (*f == '%') { | 
| Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 1619 | int longlongflag; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1620 | /* skip width or width.precision (eg. "1.2" of "%1.2f") */ | 
|  | 1621 | f = parse_format_flags(f, &width, NULL, NULL, &longlongflag, NULL); | 
|  | 1622 | if (*f == 's' || *f=='S' || *f=='R' || *f=='A' || *f=='V') | 
|  | 1623 | ++callcount; | 
| Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1624 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1625 | else if (*f == 'd' || *f=='u' || *f=='i' || *f=='x' || *f=='p') { | 
| Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1626 | #ifdef HAVE_LONG_LONG | 
|  | 1627 | if (longlongflag) { | 
|  | 1628 | if (width < MAX_LONG_LONG_CHARS) | 
|  | 1629 | width = MAX_LONG_LONG_CHARS; | 
|  | 1630 | } | 
|  | 1631 | else | 
|  | 1632 | #endif | 
|  | 1633 | /* MAX_LONG_CHARS is enough to hold a 64-bit integer, | 
|  | 1634 | including sign.  Decimal takes the most space.  This | 
|  | 1635 | isn't enough for octal.  If a width is specified we | 
|  | 1636 | need more (which we allocate later). */ | 
|  | 1637 | if (width < MAX_LONG_CHARS) | 
|  | 1638 | width = MAX_LONG_CHARS; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1639 |  | 
|  | 1640 | /* account for the size + '\0' to separate numbers | 
|  | 1641 | inside of the numberresults buffer */ | 
|  | 1642 | numbersize += (width + 1); | 
|  | 1643 | } | 
|  | 1644 | } | 
|  | 1645 | else if ((unsigned char)*f > 127) { | 
|  | 1646 | PyErr_Format(PyExc_ValueError, | 
|  | 1647 | "PyUnicode_FromFormatV() expects an ASCII-encoded format " | 
|  | 1648 | "string, got a non-ASCII byte: 0x%02x", | 
|  | 1649 | (unsigned char)*f); | 
|  | 1650 | return NULL; | 
|  | 1651 | } | 
|  | 1652 | } | 
|  | 1653 | /* step 2: allocate memory for the results of | 
|  | 1654 | * PyObject_Str()/PyObject_Repr()/PyUnicode_DecodeUTF8() calls */ | 
|  | 1655 | if (callcount) { | 
|  | 1656 | callresults = PyObject_Malloc(sizeof(PyObject *) * callcount); | 
|  | 1657 | if (!callresults) { | 
|  | 1658 | PyErr_NoMemory(); | 
|  | 1659 | return NULL; | 
|  | 1660 | } | 
|  | 1661 | callresult = callresults; | 
|  | 1662 | } | 
|  | 1663 | /* step 2.5: allocate memory for the results of formating numbers */ | 
|  | 1664 | if (numbersize) { | 
|  | 1665 | numberresults = PyObject_Malloc(numbersize); | 
|  | 1666 | if (!numberresults) { | 
|  | 1667 | PyErr_NoMemory(); | 
|  | 1668 | goto fail; | 
|  | 1669 | } | 
|  | 1670 | numberresult = numberresults; | 
|  | 1671 | } | 
|  | 1672 |  | 
|  | 1673 | /* step 3: format numbers and figure out how large a buffer we need */ | 
|  | 1674 | for (f = format; *f; f++) { | 
|  | 1675 | if (*f == '%') { | 
|  | 1676 | const char* p; | 
|  | 1677 | int longflag; | 
|  | 1678 | int longlongflag; | 
|  | 1679 | int size_tflag; | 
|  | 1680 | int numprinted; | 
|  | 1681 |  | 
|  | 1682 | p = f; | 
|  | 1683 | zeropad = (f[1] == '0'); | 
|  | 1684 | f = parse_format_flags(f, &width, &precision, | 
|  | 1685 | &longflag, &longlongflag, &size_tflag); | 
|  | 1686 | switch (*f) { | 
|  | 1687 | case 'c': | 
|  | 1688 | { | 
|  | 1689 | Py_UCS4 ordinal = va_arg(count, int); | 
| Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 1690 | maxchar = Py_MAX(maxchar, ordinal); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1691 | n++; | 
|  | 1692 | break; | 
|  | 1693 | } | 
|  | 1694 | case '%': | 
|  | 1695 | n++; | 
|  | 1696 | break; | 
|  | 1697 | case 'i': | 
|  | 1698 | case 'd': | 
|  | 1699 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, | 
|  | 1700 | width, precision, *f); | 
|  | 1701 | if (longflag) | 
|  | 1702 | numprinted = sprintf(numberresult, fmt, | 
|  | 1703 | va_arg(count, long)); | 
|  | 1704 | #ifdef HAVE_LONG_LONG | 
|  | 1705 | else if (longlongflag) | 
|  | 1706 | numprinted = sprintf(numberresult, fmt, | 
|  | 1707 | va_arg(count, PY_LONG_LONG)); | 
|  | 1708 | #endif | 
|  | 1709 | else if (size_tflag) | 
|  | 1710 | numprinted = sprintf(numberresult, fmt, | 
|  | 1711 | va_arg(count, Py_ssize_t)); | 
|  | 1712 | else | 
|  | 1713 | numprinted = sprintf(numberresult, fmt, | 
|  | 1714 | va_arg(count, int)); | 
|  | 1715 | n += numprinted; | 
|  | 1716 | /* advance by +1 to skip over the '\0' */ | 
|  | 1717 | numberresult += (numprinted + 1); | 
|  | 1718 | assert(*(numberresult - 1) == '\0'); | 
|  | 1719 | assert(*(numberresult - 2) != '\0'); | 
|  | 1720 | assert(numprinted >= 0); | 
|  | 1721 | assert(numberresult <= numberresults + numbersize); | 
|  | 1722 | break; | 
|  | 1723 | case 'u': | 
|  | 1724 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, | 
|  | 1725 | width, precision, 'u'); | 
|  | 1726 | if (longflag) | 
|  | 1727 | numprinted = sprintf(numberresult, fmt, | 
|  | 1728 | va_arg(count, unsigned long)); | 
|  | 1729 | #ifdef HAVE_LONG_LONG | 
|  | 1730 | else if (longlongflag) | 
|  | 1731 | numprinted = sprintf(numberresult, fmt, | 
|  | 1732 | va_arg(count, unsigned PY_LONG_LONG)); | 
|  | 1733 | #endif | 
|  | 1734 | else if (size_tflag) | 
|  | 1735 | numprinted = sprintf(numberresult, fmt, | 
|  | 1736 | va_arg(count, size_t)); | 
|  | 1737 | else | 
|  | 1738 | numprinted = sprintf(numberresult, fmt, | 
|  | 1739 | va_arg(count, unsigned int)); | 
|  | 1740 | n += numprinted; | 
|  | 1741 | numberresult += (numprinted + 1); | 
|  | 1742 | assert(*(numberresult - 1) == '\0'); | 
|  | 1743 | assert(*(numberresult - 2) != '\0'); | 
|  | 1744 | assert(numprinted >= 0); | 
|  | 1745 | assert(numberresult <= numberresults + numbersize); | 
|  | 1746 | break; | 
|  | 1747 | case 'x': | 
|  | 1748 | makefmt(fmt, 0, 0, 0, zeropad, width, precision, 'x'); | 
|  | 1749 | numprinted = sprintf(numberresult, fmt, va_arg(count, int)); | 
|  | 1750 | n += numprinted; | 
|  | 1751 | numberresult += (numprinted + 1); | 
|  | 1752 | assert(*(numberresult - 1) == '\0'); | 
|  | 1753 | assert(*(numberresult - 2) != '\0'); | 
|  | 1754 | assert(numprinted >= 0); | 
|  | 1755 | assert(numberresult <= numberresults + numbersize); | 
|  | 1756 | break; | 
|  | 1757 | case 'p': | 
|  | 1758 | numprinted = sprintf(numberresult, "%p", va_arg(count, void*)); | 
|  | 1759 | /* %p is ill-defined:  ensure leading 0x. */ | 
|  | 1760 | if (numberresult[1] == 'X') | 
|  | 1761 | numberresult[1] = 'x'; | 
|  | 1762 | else if (numberresult[1] != 'x') { | 
|  | 1763 | memmove(numberresult + 2, numberresult, | 
|  | 1764 | strlen(numberresult) + 1); | 
|  | 1765 | numberresult[0] = '0'; | 
|  | 1766 | numberresult[1] = 'x'; | 
|  | 1767 | numprinted += 2; | 
|  | 1768 | } | 
|  | 1769 | n += numprinted; | 
|  | 1770 | numberresult += (numprinted + 1); | 
|  | 1771 | assert(*(numberresult - 1) == '\0'); | 
|  | 1772 | assert(*(numberresult - 2) != '\0'); | 
|  | 1773 | assert(numprinted >= 0); | 
|  | 1774 | assert(numberresult <= numberresults + numbersize); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1775 | break; | 
|  | 1776 | case 's': | 
|  | 1777 | { | 
|  | 1778 | /* UTF-8 */ | 
| Georg Brandl | 780b2a6 | 2009-05-05 09:19:59 +0000 | [diff] [blame] | 1779 | const char *s = va_arg(count, const char*); | 
| Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 1780 | PyObject *str = PyUnicode_DecodeUTF8(s, strlen(s), "replace"); | 
|  | 1781 | if (!str) | 
|  | 1782 | goto fail; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1783 | /* since PyUnicode_DecodeUTF8 returns already flexible | 
|  | 1784 | unicode objects, there is no need to call ready on them */ | 
|  | 1785 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); | 
| Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 1786 | maxchar = Py_MAX(maxchar, argmaxchar); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1787 | n += PyUnicode_GET_LENGTH(str); | 
| Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 1788 | /* Remember the str and switch to the next slot */ | 
|  | 1789 | *callresult++ = str; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1790 | break; | 
|  | 1791 | } | 
|  | 1792 | case 'U': | 
|  | 1793 | { | 
|  | 1794 | PyObject *obj = va_arg(count, PyObject *); | 
|  | 1795 | assert(obj && PyUnicode_Check(obj)); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1796 | if (PyUnicode_READY(obj) == -1) | 
|  | 1797 | goto fail; | 
|  | 1798 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); | 
| Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 1799 | maxchar = Py_MAX(maxchar, argmaxchar); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1800 | n += PyUnicode_GET_LENGTH(obj); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1801 | break; | 
|  | 1802 | } | 
|  | 1803 | case 'V': | 
|  | 1804 | { | 
|  | 1805 | PyObject *obj = va_arg(count, PyObject *); | 
|  | 1806 | const char *str = va_arg(count, const char *); | 
| Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 1807 | PyObject *str_obj; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1808 | assert(obj || str); | 
|  | 1809 | assert(!obj || PyUnicode_Check(obj)); | 
| Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 1810 | if (obj) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1811 | if (PyUnicode_READY(obj) == -1) | 
|  | 1812 | goto fail; | 
|  | 1813 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); | 
| Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 1814 | maxchar = Py_MAX(maxchar, argmaxchar); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1815 | n += PyUnicode_GET_LENGTH(obj); | 
| Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 1816 | *callresult++ = NULL; | 
|  | 1817 | } | 
|  | 1818 | else { | 
|  | 1819 | str_obj = PyUnicode_DecodeUTF8(str, strlen(str), "replace"); | 
|  | 1820 | if (!str_obj) | 
|  | 1821 | goto fail; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1822 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str_obj); | 
| Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 1823 | maxchar = Py_MAX(maxchar, argmaxchar); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1824 | n += PyUnicode_GET_LENGTH(str_obj); | 
| Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 1825 | *callresult++ = str_obj; | 
|  | 1826 | } | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1827 | break; | 
|  | 1828 | } | 
|  | 1829 | case 'S': | 
|  | 1830 | { | 
|  | 1831 | PyObject *obj = va_arg(count, PyObject *); | 
|  | 1832 | PyObject *str; | 
|  | 1833 | assert(obj); | 
|  | 1834 | str = PyObject_Str(obj); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1835 | if (!str || PyUnicode_READY(str) == -1) | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1836 | goto fail; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1837 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); | 
| Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 1838 | maxchar = Py_MAX(maxchar, argmaxchar); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1839 | n += PyUnicode_GET_LENGTH(str); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1840 | /* Remember the str and switch to the next slot */ | 
|  | 1841 | *callresult++ = str; | 
|  | 1842 | break; | 
|  | 1843 | } | 
|  | 1844 | case 'R': | 
|  | 1845 | { | 
|  | 1846 | PyObject *obj = va_arg(count, PyObject *); | 
|  | 1847 | PyObject *repr; | 
|  | 1848 | assert(obj); | 
|  | 1849 | repr = PyObject_Repr(obj); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1850 | if (!repr || PyUnicode_READY(repr) == -1) | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1851 | goto fail; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1852 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(repr); | 
| Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 1853 | maxchar = Py_MAX(maxchar, argmaxchar); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1854 | n += PyUnicode_GET_LENGTH(repr); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1855 | /* Remember the repr and switch to the next slot */ | 
|  | 1856 | *callresult++ = repr; | 
|  | 1857 | break; | 
|  | 1858 | } | 
|  | 1859 | case 'A': | 
|  | 1860 | { | 
|  | 1861 | PyObject *obj = va_arg(count, PyObject *); | 
|  | 1862 | PyObject *ascii; | 
|  | 1863 | assert(obj); | 
|  | 1864 | ascii = PyObject_ASCII(obj); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1865 | if (!ascii || PyUnicode_READY(ascii) == -1) | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1866 | goto fail; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1867 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(ascii); | 
| Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 1868 | maxchar = Py_MAX(maxchar, argmaxchar); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1869 | n += PyUnicode_GET_LENGTH(ascii); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1870 | /* Remember the repr and switch to the next slot */ | 
|  | 1871 | *callresult++ = ascii; | 
|  | 1872 | break; | 
|  | 1873 | } | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1874 | default: | 
|  | 1875 | /* if we stumble upon an unknown | 
|  | 1876 | formatting code, copy the rest of | 
|  | 1877 | the format string to the output | 
|  | 1878 | string. (we cannot just skip the | 
|  | 1879 | code, since there's no way to know | 
|  | 1880 | what's in the argument list) */ | 
|  | 1881 | n += strlen(p); | 
|  | 1882 | goto expand; | 
|  | 1883 | } | 
|  | 1884 | } else | 
|  | 1885 | n++; | 
|  | 1886 | } | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1887 | expand: | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1888 | /* step 4: fill the buffer */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1889 | /* Since we've analyzed how much space we need, | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1890 | we don't have to resize the string. | 
|  | 1891 | There can be no errors beyond this point. */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1892 | string = (PyUnicodeObject *)PyUnicode_New(n, maxchar); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1893 | if (!string) | 
|  | 1894 | goto fail; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1895 | kind = PyUnicode_KIND(string); | 
|  | 1896 | data = PyUnicode_DATA(string); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1897 | callresult = callresults; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1898 | numberresult = numberresults; | 
| Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1899 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1900 | for (i = 0, f = format; *f; f++) { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1901 | if (*f == '%') { | 
| Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 1902 | const char* p; | 
| Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 1903 |  | 
|  | 1904 | p = f; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1905 | f = parse_format_flags(f, NULL, NULL, NULL, NULL, NULL); | 
|  | 1906 | /* checking for == because the last argument could be a empty | 
|  | 1907 | string, which causes i to point to end, the assert at the end of | 
|  | 1908 | the loop */ | 
|  | 1909 | assert(i <= PyUnicode_GET_LENGTH(string)); | 
| Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1910 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1911 | switch (*f) { | 
|  | 1912 | case 'c': | 
| Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 1913 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1914 | const int ordinal = va_arg(vargs, int); | 
|  | 1915 | PyUnicode_WRITE(kind, data, i++, ordinal); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1916 | break; | 
| Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 1917 | } | 
| Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 1918 | case 'i': | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1919 | case 'd': | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1920 | case 'u': | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1921 | case 'x': | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1922 | case 'p': | 
|  | 1923 | /* unused, since we already have the result */ | 
|  | 1924 | if (*f == 'p') | 
|  | 1925 | (void) va_arg(vargs, void *); | 
|  | 1926 | else | 
|  | 1927 | (void) va_arg(vargs, int); | 
|  | 1928 | /* extract the result from numberresults and append. */ | 
|  | 1929 | for (; *numberresult; ++i, ++numberresult) | 
|  | 1930 | PyUnicode_WRITE(kind, data, i, *numberresult); | 
|  | 1931 | /* skip over the separating '\0' */ | 
|  | 1932 | assert(*numberresult == '\0'); | 
|  | 1933 | numberresult++; | 
|  | 1934 | assert(numberresult <= numberresults + numbersize); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1935 | break; | 
|  | 1936 | case 's': | 
|  | 1937 | { | 
| Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 1938 | /* unused, since we already have the result */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1939 | Py_ssize_t size; | 
| Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 1940 | (void) va_arg(vargs, char *); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1941 | size = PyUnicode_GET_LENGTH(*callresult); | 
|  | 1942 | assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); | 
| Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 1943 | if (PyUnicode_CopyCharacters((PyObject*)string, i, | 
|  | 1944 | *callresult, 0, | 
|  | 1945 | size) < 0) | 
|  | 1946 | goto fail; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1947 | i += size; | 
| Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 1948 | /* We're done with the unicode()/repr() => forget it */ | 
|  | 1949 | Py_DECREF(*callresult); | 
|  | 1950 | /* switch to next unicode()/repr() result */ | 
|  | 1951 | ++callresult; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1952 | break; | 
|  | 1953 | } | 
|  | 1954 | case 'U': | 
|  | 1955 | { | 
|  | 1956 | PyObject *obj = va_arg(vargs, PyObject *); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1957 | Py_ssize_t size; | 
|  | 1958 | assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); | 
|  | 1959 | size = PyUnicode_GET_LENGTH(obj); | 
| Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 1960 | if (PyUnicode_CopyCharacters((PyObject*)string, i, | 
|  | 1961 | obj, 0, | 
|  | 1962 | size) < 0) | 
|  | 1963 | goto fail; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1964 | i += size; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1965 | break; | 
|  | 1966 | } | 
|  | 1967 | case 'V': | 
|  | 1968 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1969 | Py_ssize_t size; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1970 | PyObject *obj = va_arg(vargs, PyObject *); | 
| Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 1971 | va_arg(vargs, const char *); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1972 | if (obj) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1973 | size = PyUnicode_GET_LENGTH(obj); | 
|  | 1974 | assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); | 
| Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 1975 | if (PyUnicode_CopyCharacters((PyObject*)string, i, | 
|  | 1976 | obj, 0, | 
|  | 1977 | size) < 0) | 
|  | 1978 | goto fail; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1979 | i += size; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1980 | } else { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1981 | size = PyUnicode_GET_LENGTH(*callresult); | 
|  | 1982 | assert(PyUnicode_KIND(*callresult) <= | 
|  | 1983 | PyUnicode_KIND(string)); | 
| Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 1984 | if (PyUnicode_CopyCharacters((PyObject*)string, i, | 
|  | 1985 | *callresult, | 
|  | 1986 | 0, size) < 0) | 
|  | 1987 | goto fail; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1988 | i += size; | 
| Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 1989 | Py_DECREF(*callresult); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1990 | } | 
| Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 1991 | ++callresult; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1992 | break; | 
|  | 1993 | } | 
|  | 1994 | case 'S': | 
|  | 1995 | case 'R': | 
| Victor Stinner | 9a90900 | 2010-10-18 20:59:24 +0000 | [diff] [blame] | 1996 | case 'A': | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1997 | { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1998 | /* unused, since we already have the result */ | 
|  | 1999 | (void) va_arg(vargs, PyObject *); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2000 | assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); | 
| Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 2001 | if (PyUnicode_CopyCharacters((PyObject*)string, i, | 
|  | 2002 | *callresult, 0, | 
|  | 2003 | PyUnicode_GET_LENGTH(*callresult)) < 0) | 
|  | 2004 | goto fail; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2005 | i += PyUnicode_GET_LENGTH(*callresult); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2006 | /* We're done with the unicode()/repr() => forget it */ | 
|  | 2007 | Py_DECREF(*callresult); | 
|  | 2008 | /* switch to next unicode()/repr() result */ | 
|  | 2009 | ++callresult; | 
|  | 2010 | break; | 
|  | 2011 | } | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2012 | case '%': | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2013 | PyUnicode_WRITE(kind, data, i++, '%'); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2014 | break; | 
|  | 2015 | default: | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2016 | for (; *p; ++p, ++i) | 
|  | 2017 | PyUnicode_WRITE(kind, data, i, *p); | 
|  | 2018 | assert(i == PyUnicode_GET_LENGTH(string)); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2019 | goto end; | 
|  | 2020 | } | 
| Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 2021 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2022 | else { | 
|  | 2023 | assert(i < PyUnicode_GET_LENGTH(string)); | 
|  | 2024 | PyUnicode_WRITE(kind, data, i++, *f); | 
|  | 2025 | } | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2026 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2027 | assert(i == PyUnicode_GET_LENGTH(string)); | 
| Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2028 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2029 | end: | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2030 | if (callresults) | 
|  | 2031 | PyObject_Free(callresults); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2032 | if (numberresults) | 
|  | 2033 | PyObject_Free(numberresults); | 
|  | 2034 | return (PyObject *)string; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2035 | fail: | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2036 | if (callresults) { | 
|  | 2037 | PyObject **callresult2 = callresults; | 
|  | 2038 | while (callresult2 < callresult) { | 
| Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2039 | Py_XDECREF(*callresult2); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2040 | ++callresult2; | 
|  | 2041 | } | 
|  | 2042 | PyObject_Free(callresults); | 
|  | 2043 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2044 | if (numberresults) | 
|  | 2045 | PyObject_Free(numberresults); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2046 | return NULL; | 
| Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2047 | } | 
|  | 2048 |  | 
| Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2049 | PyObject * | 
|  | 2050 | PyUnicode_FromFormat(const char *format, ...) | 
|  | 2051 | { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2052 | PyObject* ret; | 
|  | 2053 | va_list vargs; | 
| Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2054 |  | 
|  | 2055 | #ifdef HAVE_STDARG_PROTOTYPES | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2056 | va_start(vargs, format); | 
| Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2057 | #else | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2058 | va_start(vargs); | 
| Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2059 | #endif | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2060 | ret = PyUnicode_FromFormatV(format, vargs); | 
|  | 2061 | va_end(vargs); | 
|  | 2062 | return ret; | 
| Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2063 | } | 
|  | 2064 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2065 | #ifdef HAVE_WCHAR_H | 
|  | 2066 |  | 
| Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2067 | /* Helper function for PyUnicode_AsWideChar() and PyUnicode_AsWideCharString(): | 
|  | 2068 | convert a Unicode object to a wide character string. | 
|  | 2069 |  | 
| Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2070 | - 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] | 2071 | character) required to convert the unicode object. Ignore size argument. | 
|  | 2072 |  | 
| Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2073 | - Otherwise: return the number of wide characters (excluding the null | 
| Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2074 | character) written into w. Write at most size wide characters (including | 
| Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2075 | the null character). */ | 
| Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2076 | static Py_ssize_t | 
| Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2077 | unicode_aswidechar(PyUnicodeObject *unicode, | 
|  | 2078 | wchar_t *w, | 
|  | 2079 | Py_ssize_t size) | 
|  | 2080 | { | 
| Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2081 | Py_ssize_t res; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2082 | const wchar_t *wstr; | 
|  | 2083 |  | 
|  | 2084 | wstr = PyUnicode_AsUnicodeAndSize((PyObject *)unicode, &res); | 
|  | 2085 | if (wstr == NULL) | 
|  | 2086 | return -1; | 
|  | 2087 |  | 
| Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2088 | if (w != NULL) { | 
| Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2089 | if (size > res) | 
|  | 2090 | size = res + 1; | 
|  | 2091 | else | 
|  | 2092 | res = size; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2093 | Py_MEMCPY(w, wstr, size * sizeof(wchar_t)); | 
| Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2094 | return res; | 
|  | 2095 | } | 
|  | 2096 | else | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2097 | return res + 1; | 
| Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2098 | } | 
|  | 2099 |  | 
|  | 2100 | Py_ssize_t | 
| Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2101 | PyUnicode_AsWideChar(PyObject *unicode, | 
| Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2102 | wchar_t *w, | 
|  | 2103 | Py_ssize_t size) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2104 | { | 
|  | 2105 | if (unicode == NULL) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2106 | PyErr_BadInternalCall(); | 
|  | 2107 | return -1; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2108 | } | 
| Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2109 | return unicode_aswidechar((PyUnicodeObject*)unicode, w, size); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2110 | } | 
|  | 2111 |  | 
| Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2112 | wchar_t* | 
| Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2113 | PyUnicode_AsWideCharString(PyObject *unicode, | 
| Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2114 | Py_ssize_t *size) | 
|  | 2115 | { | 
|  | 2116 | wchar_t* buffer; | 
|  | 2117 | Py_ssize_t buflen; | 
|  | 2118 |  | 
|  | 2119 | if (unicode == NULL) { | 
|  | 2120 | PyErr_BadInternalCall(); | 
|  | 2121 | return NULL; | 
|  | 2122 | } | 
|  | 2123 |  | 
| Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2124 | buflen = unicode_aswidechar((PyUnicodeObject *)unicode, NULL, 0); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2125 | if (buflen == -1) | 
|  | 2126 | return NULL; | 
| Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2127 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < buflen) { | 
| Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2128 | PyErr_NoMemory(); | 
|  | 2129 | return NULL; | 
|  | 2130 | } | 
|  | 2131 |  | 
| Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2132 | buffer = PyMem_MALLOC(buflen * sizeof(wchar_t)); | 
|  | 2133 | if (buffer == NULL) { | 
|  | 2134 | PyErr_NoMemory(); | 
|  | 2135 | return NULL; | 
|  | 2136 | } | 
| Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2137 | buflen = unicode_aswidechar((PyUnicodeObject *)unicode, buffer, buflen); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2138 | if (buflen == -1) | 
|  | 2139 | return NULL; | 
| Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2140 | if (size != NULL) | 
|  | 2141 | *size = buflen; | 
| Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2142 | return buffer; | 
|  | 2143 | } | 
|  | 2144 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2145 | #endif /* HAVE_WCHAR_H */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2146 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2147 | PyObject * | 
|  | 2148 | PyUnicode_FromOrdinal(int ordinal) | 
| Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2149 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2150 | PyObject *v; | 
| Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2151 | if (ordinal < 0 || ordinal > 0x10ffff) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2152 | PyErr_SetString(PyExc_ValueError, | 
|  | 2153 | "chr() arg not in range(0x110000)"); | 
|  | 2154 | return NULL; | 
| Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2155 | } | 
| Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 2156 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2157 | if (ordinal < 256) | 
|  | 2158 | return get_latin1_char(ordinal); | 
| Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2159 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2160 | v = PyUnicode_New(1, ordinal); | 
|  | 2161 | if (v == NULL) | 
|  | 2162 | return NULL; | 
|  | 2163 | PyUnicode_WRITE(PyUnicode_KIND(v), PyUnicode_DATA(v), 0, ordinal); | 
|  | 2164 | return v; | 
| Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2165 | } | 
|  | 2166 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2167 | PyObject * | 
|  | 2168 | PyUnicode_FromObject(register PyObject *obj) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2169 | { | 
| Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2170 | /* XXX Perhaps we should make this API an alias of | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2171 | PyObject_Str() instead ?! */ | 
| Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2172 | if (PyUnicode_CheckExact(obj)) { | 
| Victor Stinner | d3a83d5 | 2011-10-01 03:09:33 +0200 | [diff] [blame] | 2173 | if (PyUnicode_READY(obj)) | 
|  | 2174 | return NULL; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2175 | Py_INCREF(obj); | 
|  | 2176 | return obj; | 
| Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2177 | } | 
|  | 2178 | if (PyUnicode_Check(obj)) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2179 | /* For a Unicode subtype that's not a Unicode object, | 
|  | 2180 | return a true Unicode object with the same data. */ | 
| Victor Stinner | 2219e0a | 2011-10-01 01:16:59 +0200 | [diff] [blame] | 2181 | return PyUnicode_Copy(obj); | 
| Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2182 | } | 
| Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2183 | PyErr_Format(PyExc_TypeError, | 
|  | 2184 | "Can't convert '%.100s' object to str implicitly", | 
| Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2185 | Py_TYPE(obj)->tp_name); | 
| Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2186 | return NULL; | 
| Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2187 | } | 
|  | 2188 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2189 | PyObject * | 
|  | 2190 | PyUnicode_FromEncodedObject(register PyObject *obj, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2191 | const char *encoding, | 
|  | 2192 | const char *errors) | 
| Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2193 | { | 
| Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2194 | Py_buffer buffer; | 
| Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2195 | PyObject *v; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2196 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2197 | if (obj == NULL) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2198 | PyErr_BadInternalCall(); | 
|  | 2199 | return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2200 | } | 
| Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2201 |  | 
| Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2202 | /* Decoding bytes objects is the most common case and should be fast */ | 
|  | 2203 | if (PyBytes_Check(obj)) { | 
|  | 2204 | if (PyBytes_GET_SIZE(obj) == 0) { | 
|  | 2205 | Py_INCREF(unicode_empty); | 
| Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 2206 | v = unicode_empty; | 
| Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2207 | } | 
|  | 2208 | else { | 
|  | 2209 | v = PyUnicode_Decode( | 
|  | 2210 | PyBytes_AS_STRING(obj), PyBytes_GET_SIZE(obj), | 
|  | 2211 | encoding, errors); | 
|  | 2212 | } | 
|  | 2213 | return v; | 
|  | 2214 | } | 
|  | 2215 |  | 
| Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2216 | if (PyUnicode_Check(obj)) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2217 | PyErr_SetString(PyExc_TypeError, | 
|  | 2218 | "decoding str is not supported"); | 
|  | 2219 | return NULL; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2220 | } | 
| Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2221 |  | 
| Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2222 | /* Retrieve a bytes buffer view through the PEP 3118 buffer interface */ | 
|  | 2223 | if (PyObject_GetBuffer(obj, &buffer, PyBUF_SIMPLE) < 0) { | 
|  | 2224 | PyErr_Format(PyExc_TypeError, | 
|  | 2225 | "coercing to str: need bytes, bytearray " | 
|  | 2226 | "or buffer-like object, %.80s found", | 
|  | 2227 | Py_TYPE(obj)->tp_name); | 
|  | 2228 | return NULL; | 
| Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 2229 | } | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2230 |  | 
| Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2231 | if (buffer.len == 0) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2232 | Py_INCREF(unicode_empty); | 
| Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 2233 | v = unicode_empty; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2234 | } | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2235 | else | 
| Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2236 | v = PyUnicode_Decode((char*) buffer.buf, buffer.len, encoding, errors); | 
| Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 2237 |  | 
| Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2238 | PyBuffer_Release(&buffer); | 
| Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2239 | return v; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2240 | } | 
|  | 2241 |  | 
| Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2242 | /* Convert encoding to lower case and replace '_' with '-' in order to | 
| Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2243 | catch e.g. UTF_8. Return 0 on error (encoding is longer than lower_len-1), | 
|  | 2244 | 1 on success. */ | 
|  | 2245 | static int | 
|  | 2246 | normalize_encoding(const char *encoding, | 
|  | 2247 | char *lower, | 
|  | 2248 | size_t lower_len) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2249 | { | 
| Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2250 | const char *e; | 
| Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2251 | char *l; | 
|  | 2252 | char *l_end; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2253 |  | 
| Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2254 | e = encoding; | 
|  | 2255 | l = lower; | 
| Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2256 | l_end = &lower[lower_len - 1]; | 
| Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2257 | while (*e) { | 
|  | 2258 | if (l == l_end) | 
|  | 2259 | return 0; | 
| David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 2260 | if (Py_ISUPPER(*e)) { | 
|  | 2261 | *l++ = Py_TOLOWER(*e++); | 
| Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2262 | } | 
|  | 2263 | else if (*e == '_') { | 
|  | 2264 | *l++ = '-'; | 
|  | 2265 | e++; | 
|  | 2266 | } | 
|  | 2267 | else { | 
|  | 2268 | *l++ = *e++; | 
|  | 2269 | } | 
|  | 2270 | } | 
|  | 2271 | *l = '\0'; | 
| Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2272 | return 1; | 
| Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2273 | } | 
|  | 2274 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2275 | PyObject * | 
|  | 2276 | PyUnicode_Decode(const char *s, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2277 | Py_ssize_t size, | 
|  | 2278 | const char *encoding, | 
|  | 2279 | const char *errors) | 
| Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2280 | { | 
|  | 2281 | PyObject *buffer = NULL, *unicode; | 
|  | 2282 | Py_buffer info; | 
|  | 2283 | char lower[11];  /* Enough for any encoding shortcut */ | 
|  | 2284 |  | 
|  | 2285 | if (encoding == NULL) | 
| Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2286 | return PyUnicode_DecodeUTF8(s, size, errors); | 
| Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 2287 |  | 
|  | 2288 | /* Shortcuts for common default encodings */ | 
| Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2289 | if (normalize_encoding(encoding, lower, sizeof(lower))) { | 
| Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2290 | if ((strcmp(lower, "utf-8") == 0) || | 
|  | 2291 | (strcmp(lower, "utf8") == 0)) | 
| Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2292 | return PyUnicode_DecodeUTF8(s, size, errors); | 
|  | 2293 | else if ((strcmp(lower, "latin-1") == 0) || | 
| Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2294 | (strcmp(lower, "latin1") == 0) || | 
| Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2295 | (strcmp(lower, "iso-8859-1") == 0)) | 
|  | 2296 | return PyUnicode_DecodeLatin1(s, size, errors); | 
| Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 2297 | #ifdef HAVE_MBCS | 
| Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2298 | else if (strcmp(lower, "mbcs") == 0) | 
|  | 2299 | return PyUnicode_DecodeMBCS(s, size, errors); | 
| Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 2300 | #endif | 
| Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2301 | else if (strcmp(lower, "ascii") == 0) | 
|  | 2302 | return PyUnicode_DecodeASCII(s, size, errors); | 
|  | 2303 | else if (strcmp(lower, "utf-16") == 0) | 
|  | 2304 | return PyUnicode_DecodeUTF16(s, size, errors, 0); | 
|  | 2305 | else if (strcmp(lower, "utf-32") == 0) | 
|  | 2306 | return PyUnicode_DecodeUTF32(s, size, errors, 0); | 
|  | 2307 | } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2308 |  | 
|  | 2309 | /* Decode via the codec registry */ | 
| Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 2310 | buffer = NULL; | 
| Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 2311 | 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] | 2312 | goto onError; | 
| Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 2313 | buffer = PyMemoryView_FromBuffer(&info); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2314 | if (buffer == NULL) | 
|  | 2315 | goto onError; | 
|  | 2316 | unicode = PyCodec_Decode(buffer, encoding, errors); | 
|  | 2317 | if (unicode == NULL) | 
|  | 2318 | goto onError; | 
|  | 2319 | if (!PyUnicode_Check(unicode)) { | 
|  | 2320 | PyErr_Format(PyExc_TypeError, | 
| Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 2321 | "decoder did not return a str object (type=%.400s)", | 
| Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2322 | Py_TYPE(unicode)->tp_name); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2323 | Py_DECREF(unicode); | 
|  | 2324 | goto onError; | 
|  | 2325 | } | 
|  | 2326 | Py_DECREF(buffer); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2327 | if (PyUnicode_READY(unicode)) { | 
|  | 2328 | Py_DECREF(unicode); | 
|  | 2329 | return NULL; | 
|  | 2330 | } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2331 | return unicode; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2332 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2333 | onError: | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2334 | Py_XDECREF(buffer); | 
|  | 2335 | return NULL; | 
|  | 2336 | } | 
|  | 2337 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2338 | PyObject * | 
|  | 2339 | PyUnicode_AsDecodedObject(PyObject *unicode, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2340 | const char *encoding, | 
|  | 2341 | const char *errors) | 
| Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2342 | { | 
|  | 2343 | PyObject *v; | 
|  | 2344 |  | 
|  | 2345 | if (!PyUnicode_Check(unicode)) { | 
|  | 2346 | PyErr_BadArgument(); | 
|  | 2347 | goto onError; | 
|  | 2348 | } | 
|  | 2349 |  | 
|  | 2350 | if (encoding == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2351 | encoding = PyUnicode_GetDefaultEncoding(); | 
| Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2352 |  | 
|  | 2353 | /* Decode via the codec registry */ | 
|  | 2354 | v = PyCodec_Decode(unicode, encoding, errors); | 
|  | 2355 | if (v == NULL) | 
|  | 2356 | goto onError; | 
|  | 2357 | return v; | 
|  | 2358 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2359 | onError: | 
| Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2360 | return NULL; | 
|  | 2361 | } | 
|  | 2362 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2363 | PyObject * | 
|  | 2364 | PyUnicode_AsDecodedUnicode(PyObject *unicode, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2365 | const char *encoding, | 
|  | 2366 | const char *errors) | 
| Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2367 | { | 
|  | 2368 | PyObject *v; | 
|  | 2369 |  | 
|  | 2370 | if (!PyUnicode_Check(unicode)) { | 
|  | 2371 | PyErr_BadArgument(); | 
|  | 2372 | goto onError; | 
|  | 2373 | } | 
|  | 2374 |  | 
|  | 2375 | if (encoding == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2376 | encoding = PyUnicode_GetDefaultEncoding(); | 
| Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2377 |  | 
|  | 2378 | /* Decode via the codec registry */ | 
|  | 2379 | v = PyCodec_Decode(unicode, encoding, errors); | 
|  | 2380 | if (v == NULL) | 
|  | 2381 | goto onError; | 
|  | 2382 | if (!PyUnicode_Check(v)) { | 
|  | 2383 | PyErr_Format(PyExc_TypeError, | 
| Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 2384 | "decoder did not return a str object (type=%.400s)", | 
| Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2385 | Py_TYPE(v)->tp_name); | 
|  | 2386 | Py_DECREF(v); | 
|  | 2387 | goto onError; | 
|  | 2388 | } | 
|  | 2389 | return v; | 
|  | 2390 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2391 | onError: | 
| Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2392 | return NULL; | 
|  | 2393 | } | 
|  | 2394 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2395 | PyObject * | 
|  | 2396 | PyUnicode_Encode(const Py_UNICODE *s, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2397 | Py_ssize_t size, | 
|  | 2398 | const char *encoding, | 
|  | 2399 | const char *errors) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2400 | { | 
|  | 2401 | PyObject *v, *unicode; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2402 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2403 | unicode = PyUnicode_FromUnicode(s, size); | 
|  | 2404 | if (unicode == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2405 | return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2406 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); | 
|  | 2407 | Py_DECREF(unicode); | 
|  | 2408 | return v; | 
|  | 2409 | } | 
|  | 2410 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2411 | PyObject * | 
|  | 2412 | PyUnicode_AsEncodedObject(PyObject *unicode, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2413 | const char *encoding, | 
|  | 2414 | const char *errors) | 
| Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2415 | { | 
|  | 2416 | PyObject *v; | 
|  | 2417 |  | 
|  | 2418 | if (!PyUnicode_Check(unicode)) { | 
|  | 2419 | PyErr_BadArgument(); | 
|  | 2420 | goto onError; | 
|  | 2421 | } | 
|  | 2422 |  | 
|  | 2423 | if (encoding == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2424 | encoding = PyUnicode_GetDefaultEncoding(); | 
| Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2425 |  | 
|  | 2426 | /* Encode via the codec registry */ | 
|  | 2427 | v = PyCodec_Encode(unicode, encoding, errors); | 
|  | 2428 | if (v == NULL) | 
|  | 2429 | goto onError; | 
|  | 2430 | return v; | 
|  | 2431 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2432 | onError: | 
| Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2433 | return NULL; | 
|  | 2434 | } | 
|  | 2435 |  | 
| Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2436 | PyObject * | 
|  | 2437 | PyUnicode_EncodeFSDefault(PyObject *unicode) | 
| Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 2438 | { | 
| Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 2439 | #ifdef HAVE_MBCS | 
| Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2440 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), | 
|  | 2441 | PyUnicode_GET_SIZE(unicode), | 
|  | 2442 | NULL); | 
|  | 2443 | #elif defined(__APPLE__) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2444 | return _PyUnicode_AsUTF8String(unicode, "surrogateescape"); | 
| Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2445 | #else | 
| Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 2446 | PyInterpreterState *interp = PyThreadState_GET()->interp; | 
|  | 2447 | /* Bootstrap check: if the filesystem codec is implemented in Python, we | 
|  | 2448 | cannot use it to encode and decode filenames before it is loaded. Load | 
|  | 2449 | the Python codec requires to encode at least its own filename. Use the C | 
|  | 2450 | version of the locale codec until the codec registry is initialized and | 
|  | 2451 | the Python codec is loaded. | 
|  | 2452 |  | 
|  | 2453 | Py_FileSystemDefaultEncoding is shared between all interpreters, we | 
|  | 2454 | cannot only rely on it: check also interp->fscodec_initialized for | 
|  | 2455 | subinterpreters. */ | 
|  | 2456 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { | 
| Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 2457 | return PyUnicode_AsEncodedString(unicode, | 
|  | 2458 | Py_FileSystemDefaultEncoding, | 
|  | 2459 | "surrogateescape"); | 
| Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 2460 | } | 
|  | 2461 | else { | 
| Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2462 | /* locale encoding with surrogateescape */ | 
|  | 2463 | wchar_t *wchar; | 
|  | 2464 | char *bytes; | 
|  | 2465 | PyObject *bytes_obj; | 
| Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 2466 | size_t error_pos; | 
| Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2467 |  | 
|  | 2468 | wchar = PyUnicode_AsWideCharString(unicode, NULL); | 
|  | 2469 | if (wchar == NULL) | 
|  | 2470 | return NULL; | 
| Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 2471 | bytes = _Py_wchar2char(wchar, &error_pos); | 
|  | 2472 | if (bytes == NULL) { | 
|  | 2473 | if (error_pos != (size_t)-1) { | 
|  | 2474 | char *errmsg = strerror(errno); | 
|  | 2475 | PyObject *exc = NULL; | 
|  | 2476 | if (errmsg == NULL) | 
|  | 2477 | errmsg = "Py_wchar2char() failed"; | 
|  | 2478 | raise_encode_exception(&exc, | 
|  | 2479 | "filesystemencoding", | 
|  | 2480 | PyUnicode_AS_UNICODE(unicode), PyUnicode_GET_SIZE(unicode), | 
|  | 2481 | error_pos, error_pos+1, | 
|  | 2482 | errmsg); | 
|  | 2483 | Py_XDECREF(exc); | 
|  | 2484 | } | 
|  | 2485 | else | 
|  | 2486 | PyErr_NoMemory(); | 
|  | 2487 | PyMem_Free(wchar); | 
| Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2488 | return NULL; | 
| Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 2489 | } | 
|  | 2490 | PyMem_Free(wchar); | 
| Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2491 |  | 
|  | 2492 | bytes_obj = PyBytes_FromString(bytes); | 
|  | 2493 | PyMem_Free(bytes); | 
|  | 2494 | return bytes_obj; | 
| Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 2495 | } | 
| Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2496 | #endif | 
| Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 2497 | } | 
|  | 2498 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2499 | PyObject * | 
|  | 2500 | PyUnicode_AsEncodedString(PyObject *unicode, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2501 | const char *encoding, | 
|  | 2502 | const char *errors) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2503 | { | 
|  | 2504 | PyObject *v; | 
| Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2505 | char lower[11];  /* Enough for any encoding shortcut */ | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2506 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2507 | if (!PyUnicode_Check(unicode)) { | 
|  | 2508 | PyErr_BadArgument(); | 
| Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 2509 | return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2510 | } | 
| Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 2511 |  | 
| Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 2512 | if (encoding == NULL) { | 
|  | 2513 | if (errors == NULL || strcmp(errors, "strict") == 0) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2514 | return _PyUnicode_AsUTF8String(unicode, NULL); | 
| Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 2515 | else | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2516 | return _PyUnicode_AsUTF8String(unicode, errors); | 
| Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 2517 | } | 
| Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 2518 |  | 
|  | 2519 | /* Shortcuts for common default encodings */ | 
| Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2520 | if (normalize_encoding(encoding, lower, sizeof(lower))) { | 
| Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2521 | if ((strcmp(lower, "utf-8") == 0) || | 
|  | 2522 | (strcmp(lower, "utf8") == 0)) | 
| Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 2523 | { | 
| Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 2524 | if (errors == NULL || strcmp(errors, "strict") == 0) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2525 | return _PyUnicode_AsUTF8String(unicode, NULL); | 
| Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 2526 | else | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2527 | return _PyUnicode_AsUTF8String(unicode, errors); | 
| Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 2528 | } | 
| Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2529 | else if ((strcmp(lower, "latin-1") == 0) || | 
| Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2530 | (strcmp(lower, "latin1") == 0) || | 
| Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2531 | (strcmp(lower, "iso-8859-1") == 0)) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2532 | return _PyUnicode_AsLatin1String(unicode, errors); | 
| Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 2533 | #ifdef HAVE_MBCS | 
| Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2534 | else if (strcmp(lower, "mbcs") == 0) | 
|  | 2535 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), | 
|  | 2536 | PyUnicode_GET_SIZE(unicode), | 
|  | 2537 | errors); | 
| Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 2538 | #endif | 
| Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2539 | else if (strcmp(lower, "ascii") == 0) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2540 | return _PyUnicode_AsASCIIString(unicode, errors); | 
| Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2541 | } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2542 |  | 
|  | 2543 | /* Encode via the codec registry */ | 
|  | 2544 | v = PyCodec_Encode(unicode, encoding, errors); | 
|  | 2545 | if (v == NULL) | 
| Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 2546 | return NULL; | 
|  | 2547 |  | 
|  | 2548 | /* The normal path */ | 
|  | 2549 | if (PyBytes_Check(v)) | 
|  | 2550 | return v; | 
|  | 2551 |  | 
|  | 2552 | /* 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] | 2553 | if (PyByteArray_Check(v)) { | 
| Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 2554 | int error; | 
| Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 2555 | PyObject *b; | 
| Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 2556 |  | 
|  | 2557 | error = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, | 
|  | 2558 | "encoder %s returned bytearray instead of bytes", | 
|  | 2559 | encoding); | 
|  | 2560 | if (error) { | 
| Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 2561 | Py_DECREF(v); | 
|  | 2562 | return NULL; | 
| Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2563 | } | 
| Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2564 |  | 
| Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 2565 | b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); | 
|  | 2566 | Py_DECREF(v); | 
|  | 2567 | return b; | 
|  | 2568 | } | 
|  | 2569 |  | 
|  | 2570 | PyErr_Format(PyExc_TypeError, | 
|  | 2571 | "encoder did not return a bytes object (type=%.400s)", | 
|  | 2572 | Py_TYPE(v)->tp_name); | 
|  | 2573 | Py_DECREF(v); | 
| Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2574 | return NULL; | 
|  | 2575 | } | 
|  | 2576 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2577 | PyObject * | 
|  | 2578 | PyUnicode_AsEncodedUnicode(PyObject *unicode, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2579 | const char *encoding, | 
|  | 2580 | const char *errors) | 
| Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2581 | { | 
|  | 2582 | PyObject *v; | 
|  | 2583 |  | 
|  | 2584 | if (!PyUnicode_Check(unicode)) { | 
|  | 2585 | PyErr_BadArgument(); | 
|  | 2586 | goto onError; | 
|  | 2587 | } | 
|  | 2588 |  | 
|  | 2589 | if (encoding == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2590 | encoding = PyUnicode_GetDefaultEncoding(); | 
| Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2591 |  | 
|  | 2592 | /* Encode via the codec registry */ | 
|  | 2593 | v = PyCodec_Encode(unicode, encoding, errors); | 
|  | 2594 | if (v == NULL) | 
|  | 2595 | goto onError; | 
|  | 2596 | if (!PyUnicode_Check(v)) { | 
|  | 2597 | PyErr_Format(PyExc_TypeError, | 
| Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 2598 | "encoder did not return an str object (type=%.400s)", | 
| Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2599 | Py_TYPE(v)->tp_name); | 
|  | 2600 | Py_DECREF(v); | 
|  | 2601 | goto onError; | 
|  | 2602 | } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2603 | return v; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2604 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2605 | onError: | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2606 | return NULL; | 
|  | 2607 | } | 
|  | 2608 |  | 
| Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 2609 | PyObject* | 
| Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 2610 | PyUnicode_DecodeFSDefault(const char *s) { | 
| Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 2611 | Py_ssize_t size = (Py_ssize_t)strlen(s); | 
| Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 2612 | return PyUnicode_DecodeFSDefaultAndSize(s, size); | 
|  | 2613 | } | 
| Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 2614 |  | 
| Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 2615 | PyObject* | 
|  | 2616 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) | 
|  | 2617 | { | 
| Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 2618 | #ifdef HAVE_MBCS | 
| Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2619 | return PyUnicode_DecodeMBCS(s, size, NULL); | 
|  | 2620 | #elif defined(__APPLE__) | 
|  | 2621 | return PyUnicode_DecodeUTF8(s, size, "surrogateescape"); | 
|  | 2622 | #else | 
| Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 2623 | PyInterpreterState *interp = PyThreadState_GET()->interp; | 
|  | 2624 | /* Bootstrap check: if the filesystem codec is implemented in Python, we | 
|  | 2625 | cannot use it to encode and decode filenames before it is loaded. Load | 
|  | 2626 | the Python codec requires to encode at least its own filename. Use the C | 
|  | 2627 | version of the locale codec until the codec registry is initialized and | 
|  | 2628 | the Python codec is loaded. | 
|  | 2629 |  | 
|  | 2630 | Py_FileSystemDefaultEncoding is shared between all interpreters, we | 
|  | 2631 | cannot only rely on it: check also interp->fscodec_initialized for | 
|  | 2632 | subinterpreters. */ | 
|  | 2633 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { | 
| Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 2634 | return PyUnicode_Decode(s, size, | 
|  | 2635 | Py_FileSystemDefaultEncoding, | 
| Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 2636 | "surrogateescape"); | 
| Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 2637 | } | 
|  | 2638 | else { | 
| Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2639 | /* locale encoding with surrogateescape */ | 
|  | 2640 | wchar_t *wchar; | 
|  | 2641 | PyObject *unicode; | 
| Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 2642 | size_t len; | 
| Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2643 |  | 
|  | 2644 | if (s[size] != '\0' || size != strlen(s)) { | 
|  | 2645 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); | 
|  | 2646 | return NULL; | 
|  | 2647 | } | 
|  | 2648 |  | 
| Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 2649 | wchar = _Py_char2wchar(s, &len); | 
| Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2650 | if (wchar == NULL) | 
| Victor Stinner | d5af0a5 | 2010-11-08 23:34:29 +0000 | [diff] [blame] | 2651 | return PyErr_NoMemory(); | 
| Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2652 |  | 
| Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 2653 | unicode = PyUnicode_FromWideChar(wchar, len); | 
| Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2654 | PyMem_Free(wchar); | 
|  | 2655 | return unicode; | 
| Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 2656 | } | 
| Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2657 | #endif | 
| Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 2658 | } | 
|  | 2659 |  | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2660 |  | 
|  | 2661 | int | 
|  | 2662 | PyUnicode_FSConverter(PyObject* arg, void* addr) | 
|  | 2663 | { | 
|  | 2664 | PyObject *output = NULL; | 
|  | 2665 | Py_ssize_t size; | 
|  | 2666 | void *data; | 
| Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 2667 | if (arg == NULL) { | 
|  | 2668 | Py_DECREF(*(PyObject**)addr); | 
|  | 2669 | return 1; | 
|  | 2670 | } | 
| Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 2671 | if (PyBytes_Check(arg)) { | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2672 | output = arg; | 
|  | 2673 | Py_INCREF(output); | 
|  | 2674 | } | 
|  | 2675 | else { | 
|  | 2676 | arg = PyUnicode_FromObject(arg); | 
|  | 2677 | if (!arg) | 
|  | 2678 | return 0; | 
| Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 2679 | output = PyUnicode_EncodeFSDefault(arg); | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2680 | Py_DECREF(arg); | 
|  | 2681 | if (!output) | 
|  | 2682 | return 0; | 
|  | 2683 | if (!PyBytes_Check(output)) { | 
|  | 2684 | Py_DECREF(output); | 
|  | 2685 | PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes"); | 
|  | 2686 | return 0; | 
|  | 2687 | } | 
|  | 2688 | } | 
| Victor Stinner | 0ea2a46 | 2010-04-30 00:22:08 +0000 | [diff] [blame] | 2689 | size = PyBytes_GET_SIZE(output); | 
|  | 2690 | data = PyBytes_AS_STRING(output); | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2691 | if (size != strlen(data)) { | 
| Benjamin Peterson | 7a6b44a | 2011-08-18 13:51:47 -0500 | [diff] [blame] | 2692 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2693 | Py_DECREF(output); | 
|  | 2694 | return 0; | 
|  | 2695 | } | 
|  | 2696 | *(PyObject**)addr = output; | 
| Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 2697 | return Py_CLEANUP_SUPPORTED; | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2698 | } | 
|  | 2699 |  | 
|  | 2700 |  | 
| Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 2701 | int | 
|  | 2702 | PyUnicode_FSDecoder(PyObject* arg, void* addr) | 
|  | 2703 | { | 
|  | 2704 | PyObject *output = NULL; | 
| Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 2705 | if (arg == NULL) { | 
|  | 2706 | Py_DECREF(*(PyObject**)addr); | 
|  | 2707 | return 1; | 
|  | 2708 | } | 
|  | 2709 | if (PyUnicode_Check(arg)) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2710 | if (PyUnicode_READY(arg)) | 
|  | 2711 | return 0; | 
| Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 2712 | output = arg; | 
|  | 2713 | Py_INCREF(output); | 
|  | 2714 | } | 
|  | 2715 | else { | 
|  | 2716 | arg = PyBytes_FromObject(arg); | 
|  | 2717 | if (!arg) | 
|  | 2718 | return 0; | 
|  | 2719 | output = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AS_STRING(arg), | 
|  | 2720 | PyBytes_GET_SIZE(arg)); | 
|  | 2721 | Py_DECREF(arg); | 
|  | 2722 | if (!output) | 
|  | 2723 | return 0; | 
|  | 2724 | if (!PyUnicode_Check(output)) { | 
|  | 2725 | Py_DECREF(output); | 
|  | 2726 | PyErr_SetString(PyExc_TypeError, "decoder failed to return unicode"); | 
|  | 2727 | return 0; | 
|  | 2728 | } | 
|  | 2729 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2730 | if (findchar(PyUnicode_DATA(output), PyUnicode_KIND(output), | 
|  | 2731 | PyUnicode_GET_LENGTH(output), 0, 1)) { | 
| Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 2732 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); | 
|  | 2733 | Py_DECREF(output); | 
|  | 2734 | return 0; | 
|  | 2735 | } | 
|  | 2736 | *(PyObject**)addr = output; | 
|  | 2737 | return Py_CLEANUP_SUPPORTED; | 
|  | 2738 | } | 
|  | 2739 |  | 
|  | 2740 |  | 
| Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 2741 | char* | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2742 | PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize) | 
| Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 2743 | { | 
| Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 2744 | PyObject *bytes; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2745 | PyUnicodeObject *u = (PyUnicodeObject *)unicode; | 
|  | 2746 |  | 
| Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 2747 | if (!PyUnicode_Check(unicode)) { | 
|  | 2748 | PyErr_BadArgument(); | 
|  | 2749 | return NULL; | 
|  | 2750 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2751 | if (PyUnicode_READY(u) == -1) | 
| Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 2752 | return NULL; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2753 |  | 
| Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 2754 | if (PyUnicode_UTF8(unicode) == NULL) { | 
|  | 2755 | assert(!PyUnicode_IS_COMPACT_ASCII(unicode)); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2756 | bytes = _PyUnicode_AsUTF8String(unicode, "strict"); | 
|  | 2757 | if (bytes == NULL) | 
|  | 2758 | return NULL; | 
| Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 2759 | _PyUnicode_UTF8(u) = PyObject_MALLOC(PyBytes_GET_SIZE(bytes) + 1); | 
|  | 2760 | if (_PyUnicode_UTF8(u) == NULL) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2761 | Py_DECREF(bytes); | 
|  | 2762 | return NULL; | 
|  | 2763 | } | 
| Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 2764 | _PyUnicode_UTF8_LENGTH(u) = PyBytes_GET_SIZE(bytes); | 
|  | 2765 | Py_MEMCPY(_PyUnicode_UTF8(u), PyBytes_AS_STRING(bytes), _PyUnicode_UTF8_LENGTH(u) + 1); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2766 | Py_DECREF(bytes); | 
|  | 2767 | } | 
|  | 2768 |  | 
|  | 2769 | if (psize) | 
| Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 2770 | *psize = PyUnicode_UTF8_LENGTH(unicode); | 
|  | 2771 | return PyUnicode_UTF8(unicode); | 
| Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 2772 | } | 
|  | 2773 |  | 
|  | 2774 | char* | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2775 | PyUnicode_AsUTF8(PyObject *unicode) | 
| Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 2776 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2777 | return PyUnicode_AsUTF8AndSize(unicode, NULL); | 
|  | 2778 | } | 
|  | 2779 |  | 
|  | 2780 | #ifdef Py_DEBUG | 
|  | 2781 | int unicode_as_unicode_calls = 0; | 
|  | 2782 | #endif | 
|  | 2783 |  | 
|  | 2784 |  | 
|  | 2785 | Py_UNICODE * | 
|  | 2786 | PyUnicode_AsUnicodeAndSize(PyObject *unicode, Py_ssize_t *size) | 
|  | 2787 | { | 
|  | 2788 | PyUnicodeObject *u; | 
|  | 2789 | const unsigned char *one_byte; | 
|  | 2790 | #if SIZEOF_WCHAR_T == 4 | 
|  | 2791 | const Py_UCS2 *two_bytes; | 
|  | 2792 | #else | 
|  | 2793 | const Py_UCS4 *four_bytes; | 
|  | 2794 | const Py_UCS4 *ucs4_end; | 
|  | 2795 | Py_ssize_t num_surrogates; | 
|  | 2796 | #endif | 
|  | 2797 | wchar_t *w; | 
|  | 2798 | wchar_t *wchar_end; | 
|  | 2799 |  | 
|  | 2800 | if (!PyUnicode_Check(unicode)) { | 
|  | 2801 | PyErr_BadArgument(); | 
|  | 2802 | return NULL; | 
|  | 2803 | } | 
|  | 2804 | u = (PyUnicodeObject*)unicode; | 
|  | 2805 | if (_PyUnicode_WSTR(u) == NULL) { | 
|  | 2806 | /* Non-ASCII compact unicode object */ | 
|  | 2807 | assert(_PyUnicode_KIND(u) != 0); | 
|  | 2808 | assert(PyUnicode_IS_READY(u)); | 
|  | 2809 |  | 
|  | 2810 | #ifdef Py_DEBUG | 
|  | 2811 | ++unicode_as_unicode_calls; | 
|  | 2812 | #endif | 
|  | 2813 |  | 
|  | 2814 | if (PyUnicode_KIND(u) == PyUnicode_4BYTE_KIND) { | 
|  | 2815 | #if SIZEOF_WCHAR_T == 2 | 
|  | 2816 | four_bytes = PyUnicode_4BYTE_DATA(u); | 
|  | 2817 | ucs4_end = four_bytes + _PyUnicode_LENGTH(u); | 
|  | 2818 | num_surrogates = 0; | 
|  | 2819 |  | 
|  | 2820 | for (; four_bytes < ucs4_end; ++four_bytes) { | 
|  | 2821 | if (*four_bytes > 0xFFFF) | 
|  | 2822 | ++num_surrogates; | 
|  | 2823 | } | 
|  | 2824 |  | 
|  | 2825 | _PyUnicode_WSTR(u) = (wchar_t *) PyObject_MALLOC( | 
|  | 2826 | sizeof(wchar_t) * (_PyUnicode_LENGTH(u) + 1 + num_surrogates)); | 
|  | 2827 | if (!_PyUnicode_WSTR(u)) { | 
|  | 2828 | PyErr_NoMemory(); | 
|  | 2829 | return NULL; | 
|  | 2830 | } | 
|  | 2831 | _PyUnicode_WSTR_LENGTH(u) = _PyUnicode_LENGTH(u) + num_surrogates; | 
|  | 2832 |  | 
|  | 2833 | w = _PyUnicode_WSTR(u); | 
|  | 2834 | wchar_end = w + _PyUnicode_WSTR_LENGTH(u); | 
|  | 2835 | four_bytes = PyUnicode_4BYTE_DATA(u); | 
|  | 2836 | for (; four_bytes < ucs4_end; ++four_bytes, ++w) { | 
|  | 2837 | if (*four_bytes > 0xFFFF) { | 
|  | 2838 | /* encode surrogate pair in this case */ | 
|  | 2839 | *w++ = 0xD800 | ((*four_bytes - 0x10000) >> 10); | 
|  | 2840 | *w   = 0xDC00 | ((*four_bytes - 0x10000) & 0x3FF); | 
|  | 2841 | } | 
|  | 2842 | else | 
|  | 2843 | *w = *four_bytes; | 
|  | 2844 |  | 
|  | 2845 | if (w > wchar_end) { | 
|  | 2846 | assert(0 && "Miscalculated string end"); | 
|  | 2847 | } | 
|  | 2848 | } | 
|  | 2849 | *w = 0; | 
|  | 2850 | #else | 
|  | 2851 | /* sizeof(wchar_t) == 4 */ | 
|  | 2852 | Py_FatalError("Impossible unicode object state, wstr and str " | 
|  | 2853 | "should share memory already."); | 
|  | 2854 | return NULL; | 
|  | 2855 | #endif | 
|  | 2856 | } | 
|  | 2857 | else { | 
|  | 2858 | _PyUnicode_WSTR(u) = (wchar_t *) PyObject_MALLOC(sizeof(wchar_t) * | 
|  | 2859 | (_PyUnicode_LENGTH(u) + 1)); | 
|  | 2860 | if (!_PyUnicode_WSTR(u)) { | 
|  | 2861 | PyErr_NoMemory(); | 
|  | 2862 | return NULL; | 
|  | 2863 | } | 
|  | 2864 | if (!PyUnicode_IS_COMPACT_ASCII(u)) | 
|  | 2865 | _PyUnicode_WSTR_LENGTH(u) = _PyUnicode_LENGTH(u); | 
|  | 2866 | w = _PyUnicode_WSTR(u); | 
|  | 2867 | wchar_end = w + _PyUnicode_LENGTH(u); | 
|  | 2868 |  | 
|  | 2869 | if (PyUnicode_KIND(u) == PyUnicode_1BYTE_KIND) { | 
|  | 2870 | one_byte = PyUnicode_1BYTE_DATA(u); | 
|  | 2871 | for (; w < wchar_end; ++one_byte, ++w) | 
|  | 2872 | *w = *one_byte; | 
|  | 2873 | /* null-terminate the wstr */ | 
|  | 2874 | *w = 0; | 
|  | 2875 | } | 
|  | 2876 | else if (PyUnicode_KIND(u) == PyUnicode_2BYTE_KIND) { | 
|  | 2877 | #if SIZEOF_WCHAR_T == 4 | 
|  | 2878 | two_bytes = PyUnicode_2BYTE_DATA(u); | 
|  | 2879 | for (; w < wchar_end; ++two_bytes, ++w) | 
|  | 2880 | *w = *two_bytes; | 
|  | 2881 | /* null-terminate the wstr */ | 
|  | 2882 | *w = 0; | 
|  | 2883 | #else | 
|  | 2884 | /* sizeof(wchar_t) == 2 */ | 
|  | 2885 | PyObject_FREE(_PyUnicode_WSTR(u)); | 
|  | 2886 | _PyUnicode_WSTR(u) = NULL; | 
|  | 2887 | Py_FatalError("Impossible unicode object state, wstr " | 
|  | 2888 | "and str should share memory already."); | 
|  | 2889 | return NULL; | 
|  | 2890 | #endif | 
|  | 2891 | } | 
|  | 2892 | else { | 
|  | 2893 | assert(0 && "This should never happen."); | 
|  | 2894 | } | 
|  | 2895 | } | 
|  | 2896 | } | 
|  | 2897 | if (size != NULL) | 
|  | 2898 | *size = PyUnicode_WSTR_LENGTH(u); | 
|  | 2899 | return _PyUnicode_WSTR(u); | 
| Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 2900 | } | 
|  | 2901 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2902 | Py_UNICODE * | 
|  | 2903 | PyUnicode_AsUnicode(PyObject *unicode) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2904 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2905 | return PyUnicode_AsUnicodeAndSize(unicode, NULL); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2906 | } | 
|  | 2907 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2908 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2909 | Py_ssize_t | 
|  | 2910 | PyUnicode_GetSize(PyObject *unicode) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2911 | { | 
|  | 2912 | if (!PyUnicode_Check(unicode)) { | 
|  | 2913 | PyErr_BadArgument(); | 
|  | 2914 | goto onError; | 
|  | 2915 | } | 
|  | 2916 | return PyUnicode_GET_SIZE(unicode); | 
|  | 2917 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2918 | onError: | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2919 | return -1; | 
|  | 2920 | } | 
|  | 2921 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2922 | Py_ssize_t | 
|  | 2923 | PyUnicode_GetLength(PyObject *unicode) | 
|  | 2924 | { | 
| Victor Stinner | 5a706cf | 2011-10-02 00:36:53 +0200 | [diff] [blame] | 2925 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2926 | PyErr_BadArgument(); | 
|  | 2927 | return -1; | 
|  | 2928 | } | 
|  | 2929 |  | 
|  | 2930 | return PyUnicode_GET_LENGTH(unicode); | 
|  | 2931 | } | 
|  | 2932 |  | 
|  | 2933 | Py_UCS4 | 
|  | 2934 | PyUnicode_ReadChar(PyObject *unicode, Py_ssize_t index) | 
|  | 2935 | { | 
| Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 2936 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { | 
|  | 2937 | PyErr_BadArgument(); | 
|  | 2938 | return (Py_UCS4)-1; | 
|  | 2939 | } | 
|  | 2940 | if (index < 0 || index >= _PyUnicode_LENGTH(unicode)) { | 
|  | 2941 | PyErr_SetString(PyExc_IndexError, "string index out of range"); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2942 | return (Py_UCS4)-1; | 
|  | 2943 | } | 
|  | 2944 | return PyUnicode_READ_CHAR(unicode, index); | 
|  | 2945 | } | 
|  | 2946 |  | 
|  | 2947 | int | 
|  | 2948 | PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, Py_UCS4 ch) | 
|  | 2949 | { | 
|  | 2950 | if (!PyUnicode_Check(unicode) || !PyUnicode_IS_COMPACT(unicode)) { | 
| Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 2951 | PyErr_BadArgument(); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2952 | return -1; | 
|  | 2953 | } | 
| Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 2954 | if (index < 0 || index >= _PyUnicode_LENGTH(unicode)) { | 
|  | 2955 | PyErr_SetString(PyExc_IndexError, "string index out of range"); | 
|  | 2956 | return -1; | 
|  | 2957 | } | 
|  | 2958 | if (_PyUnicode_Dirty(unicode)) | 
|  | 2959 | return -1; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2960 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), | 
|  | 2961 | index, ch); | 
|  | 2962 | return 0; | 
|  | 2963 | } | 
|  | 2964 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2965 | const char * | 
|  | 2966 | PyUnicode_GetDefaultEncoding(void) | 
| Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 2967 | { | 
| Victor Stinner | 42cb462 | 2010-09-01 19:39:01 +0000 | [diff] [blame] | 2968 | return "utf-8"; | 
| Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 2969 | } | 
|  | 2970 |  | 
| Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 2971 | /* create or adjust a UnicodeDecodeError */ | 
|  | 2972 | static void | 
|  | 2973 | make_decode_exception(PyObject **exceptionObject, | 
|  | 2974 | const char *encoding, | 
|  | 2975 | const char *input, Py_ssize_t length, | 
|  | 2976 | Py_ssize_t startpos, Py_ssize_t endpos, | 
|  | 2977 | const char *reason) | 
|  | 2978 | { | 
|  | 2979 | if (*exceptionObject == NULL) { | 
|  | 2980 | *exceptionObject = PyUnicodeDecodeError_Create( | 
|  | 2981 | encoding, input, length, startpos, endpos, reason); | 
|  | 2982 | } | 
|  | 2983 | else { | 
|  | 2984 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, startpos)) | 
|  | 2985 | goto onError; | 
|  | 2986 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, endpos)) | 
|  | 2987 | goto onError; | 
|  | 2988 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) | 
|  | 2989 | goto onError; | 
|  | 2990 | } | 
|  | 2991 | return; | 
|  | 2992 |  | 
|  | 2993 | onError: | 
|  | 2994 | Py_DECREF(*exceptionObject); | 
|  | 2995 | *exceptionObject = NULL; | 
|  | 2996 | } | 
|  | 2997 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2998 | /* error handling callback helper: | 
|  | 2999 | build arguments, call the callback and check the arguments, | 
| Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 3000 | if no exception occurred, copy the replacement to the output | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3001 | and adjust various state variables. | 
|  | 3002 | return 0 on success, -1 on error | 
|  | 3003 | */ | 
|  | 3004 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3005 | static int | 
|  | 3006 | unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3007 | const char *encoding, const char *reason, | 
|  | 3008 | const char **input, const char **inend, Py_ssize_t *startinpos, | 
|  | 3009 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, | 
|  | 3010 | PyUnicodeObject **output, Py_ssize_t *outpos, Py_UNICODE **outptr) | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3011 | { | 
| Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3012 | 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] | 3013 |  | 
|  | 3014 | PyObject *restuple = NULL; | 
|  | 3015 | PyObject *repunicode = NULL; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3016 | Py_ssize_t outsize = PyUnicode_GET_SIZE(*output); | 
| Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3017 | Py_ssize_t insize; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3018 | Py_ssize_t requiredsize; | 
|  | 3019 | Py_ssize_t newpos; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3020 | const Py_UNICODE *repptr; | 
| Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3021 | PyObject *inputobj = NULL; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3022 | Py_ssize_t repsize; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3023 | int res = -1; | 
|  | 3024 |  | 
|  | 3025 | if (*errorHandler == NULL) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3026 | *errorHandler = PyCodec_LookupError(errors); | 
|  | 3027 | if (*errorHandler == NULL) | 
|  | 3028 | goto onError; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3029 | } | 
|  | 3030 |  | 
| Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 3031 | make_decode_exception(exceptionObject, | 
|  | 3032 | encoding, | 
|  | 3033 | *input, *inend - *input, | 
|  | 3034 | *startinpos, *endinpos, | 
|  | 3035 | reason); | 
|  | 3036 | if (*exceptionObject == NULL) | 
|  | 3037 | goto onError; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3038 |  | 
|  | 3039 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); | 
|  | 3040 | if (restuple == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3041 | goto onError; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3042 | if (!PyTuple_Check(restuple)) { | 
| Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 3043 | PyErr_SetString(PyExc_TypeError, &argparse[4]); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3044 | goto onError; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3045 | } | 
|  | 3046 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3047 | goto onError; | 
| Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3048 |  | 
|  | 3049 | /* Copy back the bytes variables, which might have been modified by the | 
|  | 3050 | callback */ | 
|  | 3051 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); | 
|  | 3052 | if (!inputobj) | 
|  | 3053 | goto onError; | 
| Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3054 | if (!PyBytes_Check(inputobj)) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3055 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); | 
| Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3056 | } | 
| Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3057 | *input = PyBytes_AS_STRING(inputobj); | 
|  | 3058 | insize = PyBytes_GET_SIZE(inputobj); | 
| Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3059 | *inend = *input + insize; | 
| Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 3060 | /* we can DECREF safely, as the exception has another reference, | 
|  | 3061 | so the object won't go away. */ | 
|  | 3062 | Py_DECREF(inputobj); | 
| Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3063 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3064 | if (newpos<0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3065 | newpos = insize+newpos; | 
| Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3066 | if (newpos<0 || newpos>insize) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3067 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); | 
|  | 3068 | goto onError; | 
| Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3069 | } | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3070 |  | 
|  | 3071 | /* need more space? (at least enough for what we | 
|  | 3072 | have+the replacement+the rest of the string (starting | 
|  | 3073 | at the new input position), so we won't have to check space | 
|  | 3074 | when there are no errors in the rest of the string) */ | 
|  | 3075 | repptr = PyUnicode_AS_UNICODE(repunicode); | 
|  | 3076 | repsize = PyUnicode_GET_SIZE(repunicode); | 
|  | 3077 | requiredsize = *outpos + repsize + insize-newpos; | 
|  | 3078 | if (requiredsize > outsize) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3079 | if (requiredsize<2*outsize) | 
|  | 3080 | requiredsize = 2*outsize; | 
|  | 3081 | if (_PyUnicode_Resize(output, requiredsize) < 0) | 
|  | 3082 | goto onError; | 
|  | 3083 | *outptr = PyUnicode_AS_UNICODE(*output) + *outpos; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3084 | } | 
|  | 3085 | *endinpos = newpos; | 
| Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3086 | *inptr = *input + newpos; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3087 | Py_UNICODE_COPY(*outptr, repptr, repsize); | 
|  | 3088 | *outptr += repsize; | 
|  | 3089 | *outpos += repsize; | 
| Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3090 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3091 | /* we made it! */ | 
|  | 3092 | res = 0; | 
|  | 3093 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3094 | onError: | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3095 | Py_XDECREF(restuple); | 
|  | 3096 | return res; | 
|  | 3097 | } | 
|  | 3098 |  | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3099 | /* --- UTF-7 Codec -------------------------------------------------------- */ | 
|  | 3100 |  | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3101 | /* See RFC2152 for details.  We encode conservatively and decode liberally. */ | 
|  | 3102 |  | 
|  | 3103 | /* Three simple macros defining base-64. */ | 
|  | 3104 |  | 
|  | 3105 | /* Is c a base-64 character? */ | 
|  | 3106 |  | 
|  | 3107 | #define IS_BASE64(c) \ | 
|  | 3108 | (((c) >= 'A' && (c) <= 'Z') ||     \ | 
|  | 3109 | ((c) >= 'a' && (c) <= 'z') ||     \ | 
|  | 3110 | ((c) >= '0' && (c) <= '9') ||     \ | 
|  | 3111 | (c) == '+' || (c) == '/') | 
|  | 3112 |  | 
|  | 3113 | /* given that c is a base-64 character, what is its base-64 value? */ | 
|  | 3114 |  | 
|  | 3115 | #define FROM_BASE64(c)                                                  \ | 
|  | 3116 | (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' :                           \ | 
|  | 3117 | ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 :                      \ | 
|  | 3118 | ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 :                      \ | 
|  | 3119 | (c) == '+' ? 62 : 63) | 
|  | 3120 |  | 
|  | 3121 | /* What is the base-64 character of the bottom 6 bits of n? */ | 
|  | 3122 |  | 
|  | 3123 | #define TO_BASE64(n)  \ | 
|  | 3124 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) | 
|  | 3125 |  | 
|  | 3126 | /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be | 
|  | 3127 | * decoded as itself.  We are permissive on decoding; the only ASCII | 
|  | 3128 | * byte not decoding to itself is the + which begins a base64 | 
|  | 3129 | * string. */ | 
|  | 3130 |  | 
|  | 3131 | #define DECODE_DIRECT(c)                                \ | 
|  | 3132 | ((c) <= 127 && (c) != '+') | 
|  | 3133 |  | 
|  | 3134 | /* The UTF-7 encoder treats ASCII characters differently according to | 
|  | 3135 | * whether they are Set D, Set O, Whitespace, or special (i.e. none of | 
|  | 3136 | * the above).  See RFC2152.  This array identifies these different | 
|  | 3137 | * sets: | 
|  | 3138 | * 0 : "Set D" | 
|  | 3139 | *     alphanumeric and '(),-./:? | 
|  | 3140 | * 1 : "Set O" | 
|  | 3141 | *     !"#$%&*;<=>@[]^_`{|} | 
|  | 3142 | * 2 : "whitespace" | 
|  | 3143 | *     ht nl cr sp | 
|  | 3144 | * 3 : special (must be base64 encoded) | 
|  | 3145 | *     everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) | 
|  | 3146 | */ | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3147 |  | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3148 | static | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3149 | char utf7_category[128] = { | 
|  | 3150 | /* nul soh stx etx eot enq ack bel bs  ht  nl  vt  np  cr  so  si  */ | 
|  | 3151 | 3,  3,  3,  3,  3,  3,  3,  3,  3,  2,  2,  3,  3,  2,  3,  3, | 
|  | 3152 | /* dle dc1 dc2 dc3 dc4 nak syn etb can em  sub esc fs  gs  rs  us  */ | 
|  | 3153 | 3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3, | 
|  | 3154 | /* sp   !   "   #   $   %   &   '   (   )   *   +   ,   -   .   /  */ | 
|  | 3155 | 2,  1,  1,  1,  1,  1,  1,  0,  0,  0,  1,  3,  0,  0,  0,  0, | 
|  | 3156 | /*  0   1   2   3   4   5   6   7   8   9   :   ;   <   =   >   ?  */ | 
|  | 3157 | 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  1,  1,  1,  1,  0, | 
|  | 3158 | /*  @   A   B   C   D   E   F   G   H   I   J   K   L   M   N   O  */ | 
|  | 3159 | 1,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, | 
|  | 3160 | /*  P   Q   R   S   T   U   V   W   X   Y   Z   [   \   ]   ^   _  */ | 
|  | 3161 | 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  1,  3,  1,  1,  1, | 
|  | 3162 | /*  `   a   b   c   d   e   f   g   h   i   j   k   l   m   n   o  */ | 
|  | 3163 | 1,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, | 
|  | 3164 | /*  p   q   r   s   t   u   v   w   x   y   z   {   |   }   ~  del */ | 
|  | 3165 | 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] | 3166 | }; | 
|  | 3167 |  | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3168 | /* ENCODE_DIRECT: this character should be encoded as itself.  The | 
|  | 3169 | * answer depends on whether we are encoding set O as itself, and also | 
|  | 3170 | * on whether we are encoding whitespace as itself.  RFC2152 makes it | 
|  | 3171 | * clear that the answers to these questions vary between | 
|  | 3172 | * applications, so this code needs to be flexible.  */ | 
| Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 3173 |  | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3174 | #define ENCODE_DIRECT(c, directO, directWS)             \ | 
|  | 3175 | ((c) < 128 && (c) > 0 &&                            \ | 
|  | 3176 | ((utf7_category[(c)] == 0) ||                      \ | 
|  | 3177 | (directWS && (utf7_category[(c)] == 2)) ||        \ | 
|  | 3178 | (directO && (utf7_category[(c)] == 1)))) | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3179 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3180 | PyObject * | 
|  | 3181 | PyUnicode_DecodeUTF7(const char *s, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3182 | Py_ssize_t size, | 
|  | 3183 | const char *errors) | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3184 | { | 
| Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3185 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); | 
|  | 3186 | } | 
|  | 3187 |  | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3188 | /* The decoder.  The only state we preserve is our read position, | 
|  | 3189 | * i.e. how many characters we have consumed.  So if we end in the | 
|  | 3190 | * middle of a shift sequence we have to back off the read position | 
|  | 3191 | * and the output to the beginning of the sequence, otherwise we lose | 
|  | 3192 | * all the shift state (seen bits, number of bits seen, high | 
|  | 3193 | * surrogate). */ | 
|  | 3194 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3195 | PyObject * | 
|  | 3196 | PyUnicode_DecodeUTF7Stateful(const char *s, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3197 | Py_ssize_t size, | 
|  | 3198 | const char *errors, | 
|  | 3199 | Py_ssize_t *consumed) | 
| Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3200 | { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3201 | const char *starts = s; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3202 | Py_ssize_t startinpos; | 
|  | 3203 | Py_ssize_t endinpos; | 
|  | 3204 | Py_ssize_t outpos; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3205 | const char *e; | 
|  | 3206 | PyUnicodeObject *unicode; | 
|  | 3207 | Py_UNICODE *p; | 
|  | 3208 | const char *errmsg = ""; | 
|  | 3209 | int inShift = 0; | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3210 | Py_UNICODE *shiftOutStart; | 
|  | 3211 | unsigned int base64bits = 0; | 
|  | 3212 | unsigned long base64buffer = 0; | 
|  | 3213 | Py_UNICODE surrogate = 0; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3214 | PyObject *errorHandler = NULL; | 
|  | 3215 | PyObject *exc = NULL; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3216 |  | 
|  | 3217 | unicode = _PyUnicode_New(size); | 
|  | 3218 | if (!unicode) | 
|  | 3219 | return NULL; | 
| Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3220 | if (size == 0) { | 
|  | 3221 | if (consumed) | 
|  | 3222 | *consumed = 0; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3223 | return (PyObject *)unicode; | 
| Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3224 | } | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3225 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3226 | p = PyUnicode_AS_UNICODE(unicode); | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3227 | shiftOutStart = p; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3228 | e = s + size; | 
|  | 3229 |  | 
|  | 3230 | while (s < e) { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3231 | Py_UNICODE ch; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3232 | restart: | 
| Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 3233 | ch = (unsigned char) *s; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3234 |  | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3235 | if (inShift) { /* in a base-64 section */ | 
|  | 3236 | if (IS_BASE64(ch)) { /* consume a base-64 character */ | 
|  | 3237 | base64buffer = (base64buffer << 6) | FROM_BASE64(ch); | 
|  | 3238 | base64bits += 6; | 
|  | 3239 | s++; | 
|  | 3240 | if (base64bits >= 16) { | 
|  | 3241 | /* we have enough bits for a UTF-16 value */ | 
|  | 3242 | Py_UNICODE outCh = (Py_UNICODE) | 
|  | 3243 | (base64buffer >> (base64bits-16)); | 
|  | 3244 | base64bits -= 16; | 
|  | 3245 | base64buffer &= (1 << base64bits) - 1; /* clear high bits */ | 
|  | 3246 | if (surrogate) { | 
|  | 3247 | /* expecting a second surrogate */ | 
|  | 3248 | if (outCh >= 0xDC00 && outCh <= 0xDFFF) { | 
|  | 3249 | #ifdef Py_UNICODE_WIDE | 
|  | 3250 | *p++ = (((surrogate & 0x3FF)<<10) | 
|  | 3251 | | (outCh & 0x3FF)) + 0x10000; | 
|  | 3252 | #else | 
|  | 3253 | *p++ = surrogate; | 
|  | 3254 | *p++ = outCh; | 
|  | 3255 | #endif | 
|  | 3256 | surrogate = 0; | 
|  | 3257 | } | 
|  | 3258 | else { | 
|  | 3259 | surrogate = 0; | 
|  | 3260 | errmsg = "second surrogate missing"; | 
|  | 3261 | goto utf7Error; | 
|  | 3262 | } | 
|  | 3263 | } | 
|  | 3264 | else if (outCh >= 0xD800 && outCh <= 0xDBFF) { | 
|  | 3265 | /* first surrogate */ | 
|  | 3266 | surrogate = outCh; | 
|  | 3267 | } | 
|  | 3268 | else if (outCh >= 0xDC00 && outCh <= 0xDFFF) { | 
|  | 3269 | errmsg = "unexpected second surrogate"; | 
|  | 3270 | goto utf7Error; | 
|  | 3271 | } | 
|  | 3272 | else { | 
|  | 3273 | *p++ = outCh; | 
|  | 3274 | } | 
|  | 3275 | } | 
|  | 3276 | } | 
|  | 3277 | else { /* now leaving a base-64 section */ | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3278 | inShift = 0; | 
|  | 3279 | s++; | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3280 | if (surrogate) { | 
|  | 3281 | errmsg = "second surrogate missing at end of shift sequence"; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3282 | goto utf7Error; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3283 | } | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3284 | if (base64bits > 0) { /* left-over bits */ | 
|  | 3285 | if (base64bits >= 6) { | 
|  | 3286 | /* We've seen at least one base-64 character */ | 
|  | 3287 | errmsg = "partial character in shift sequence"; | 
|  | 3288 | goto utf7Error; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3289 | } | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3290 | else { | 
|  | 3291 | /* Some bits remain; they should be zero */ | 
|  | 3292 | if (base64buffer != 0) { | 
|  | 3293 | errmsg = "non-zero padding bits in shift sequence"; | 
|  | 3294 | goto utf7Error; | 
|  | 3295 | } | 
|  | 3296 | } | 
|  | 3297 | } | 
|  | 3298 | if (ch != '-') { | 
|  | 3299 | /* '-' is absorbed; other terminating | 
|  | 3300 | characters are preserved */ | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3301 | *p++ = ch; | 
|  | 3302 | } | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3303 | } | 
|  | 3304 | } | 
|  | 3305 | else if ( ch == '+' ) { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3306 | startinpos = s-starts; | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3307 | s++; /* consume '+' */ | 
|  | 3308 | if (s < e && *s == '-') { /* '+-' encodes '+' */ | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3309 | s++; | 
|  | 3310 | *p++ = '+'; | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3311 | } | 
|  | 3312 | else { /* begin base64-encoded section */ | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3313 | inShift = 1; | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3314 | shiftOutStart = p; | 
|  | 3315 | base64bits = 0; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3316 | } | 
|  | 3317 | } | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3318 | else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3319 | *p++ = ch; | 
|  | 3320 | s++; | 
|  | 3321 | } | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3322 | else { | 
|  | 3323 | startinpos = s-starts; | 
|  | 3324 | s++; | 
|  | 3325 | errmsg = "unexpected special character"; | 
|  | 3326 | goto utf7Error; | 
|  | 3327 | } | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3328 | continue; | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3329 | utf7Error: | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3330 | outpos = p-PyUnicode_AS_UNICODE(unicode); | 
|  | 3331 | endinpos = s-starts; | 
|  | 3332 | if (unicode_decode_call_errorhandler( | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3333 | errors, &errorHandler, | 
|  | 3334 | "utf7", errmsg, | 
|  | 3335 | &starts, &e, &startinpos, &endinpos, &exc, &s, | 
|  | 3336 | &unicode, &outpos, &p)) | 
|  | 3337 | goto onError; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3338 | } | 
|  | 3339 |  | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3340 | /* end of string */ | 
|  | 3341 |  | 
|  | 3342 | if (inShift && !consumed) { /* in shift sequence, no more to follow */ | 
|  | 3343 | /* if we're in an inconsistent state, that's an error */ | 
|  | 3344 | if (surrogate || | 
|  | 3345 | (base64bits >= 6) || | 
|  | 3346 | (base64bits > 0 && base64buffer != 0)) { | 
|  | 3347 | outpos = p-PyUnicode_AS_UNICODE(unicode); | 
|  | 3348 | endinpos = size; | 
|  | 3349 | if (unicode_decode_call_errorhandler( | 
|  | 3350 | errors, &errorHandler, | 
|  | 3351 | "utf7", "unterminated shift sequence", | 
|  | 3352 | &starts, &e, &startinpos, &endinpos, &exc, &s, | 
|  | 3353 | &unicode, &outpos, &p)) | 
|  | 3354 | goto onError; | 
|  | 3355 | if (s < e) | 
|  | 3356 | goto restart; | 
|  | 3357 | } | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3358 | } | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3359 |  | 
|  | 3360 | /* return state */ | 
| Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3361 | if (consumed) { | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3362 | if (inShift) { | 
|  | 3363 | p = shiftOutStart; /* back off output */ | 
| Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3364 | *consumed = startinpos; | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3365 | } | 
|  | 3366 | else { | 
| Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3367 | *consumed = s-starts; | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3368 | } | 
| Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3369 | } | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3370 |  | 
| Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 3371 | if (_PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3372 | goto onError; | 
|  | 3373 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3374 | Py_XDECREF(errorHandler); | 
|  | 3375 | Py_XDECREF(exc); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3376 | if (PyUnicode_READY(unicode) == -1) { | 
|  | 3377 | Py_DECREF(unicode); | 
|  | 3378 | return NULL; | 
|  | 3379 | } | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3380 | return (PyObject *)unicode; | 
|  | 3381 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3382 | onError: | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3383 | Py_XDECREF(errorHandler); | 
|  | 3384 | Py_XDECREF(exc); | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3385 | Py_DECREF(unicode); | 
|  | 3386 | return NULL; | 
|  | 3387 | } | 
|  | 3388 |  | 
|  | 3389 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3390 | PyObject * | 
|  | 3391 | PyUnicode_EncodeUTF7(const Py_UNICODE *s, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3392 | Py_ssize_t size, | 
|  | 3393 | int base64SetO, | 
|  | 3394 | int base64WhiteSpace, | 
|  | 3395 | const char *errors) | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3396 | { | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3397 | PyObject *v; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3398 | /* It might be possible to tighten this worst case */ | 
| Alexandre Vassalotti | e85bd98 | 2009-07-21 00:39:03 +0000 | [diff] [blame] | 3399 | Py_ssize_t allocated = 8 * size; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3400 | int inShift = 0; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3401 | Py_ssize_t i = 0; | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3402 | unsigned int base64bits = 0; | 
|  | 3403 | unsigned long base64buffer = 0; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3404 | char * out; | 
|  | 3405 | char * start; | 
|  | 3406 |  | 
|  | 3407 | if (size == 0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3408 | return PyBytes_FromStringAndSize(NULL, 0); | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3409 |  | 
| Alexandre Vassalotti | e85bd98 | 2009-07-21 00:39:03 +0000 | [diff] [blame] | 3410 | if (allocated / 8 != size) | 
| Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3411 | return PyErr_NoMemory(); | 
|  | 3412 |  | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3413 | v = PyBytes_FromStringAndSize(NULL, allocated); | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3414 | if (v == NULL) | 
|  | 3415 | return NULL; | 
|  | 3416 |  | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3417 | start = out = PyBytes_AS_STRING(v); | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3418 | for (;i < size; ++i) { | 
|  | 3419 | Py_UNICODE ch = s[i]; | 
|  | 3420 |  | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3421 | if (inShift) { | 
|  | 3422 | if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { | 
|  | 3423 | /* shifting out */ | 
|  | 3424 | if (base64bits) { /* output remaining bits */ | 
|  | 3425 | *out++ = TO_BASE64(base64buffer << (6-base64bits)); | 
|  | 3426 | base64buffer = 0; | 
|  | 3427 | base64bits = 0; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3428 | } | 
|  | 3429 | inShift = 0; | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3430 | /* Characters not in the BASE64 set implicitly unshift the sequence | 
|  | 3431 | so no '-' is required, except if the character is itself a '-' */ | 
|  | 3432 | if (IS_BASE64(ch) || ch == '-') { | 
|  | 3433 | *out++ = '-'; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3434 | } | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3435 | *out++ = (char) ch; | 
|  | 3436 | } | 
|  | 3437 | else { | 
|  | 3438 | goto encode_char; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3439 | } | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3440 | } | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3441 | else { /* not in a shift sequence */ | 
|  | 3442 | if (ch == '+') { | 
|  | 3443 | *out++ = '+'; | 
|  | 3444 | *out++ = '-'; | 
|  | 3445 | } | 
|  | 3446 | else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { | 
|  | 3447 | *out++ = (char) ch; | 
|  | 3448 | } | 
|  | 3449 | else { | 
|  | 3450 | *out++ = '+'; | 
|  | 3451 | inShift = 1; | 
|  | 3452 | goto encode_char; | 
|  | 3453 | } | 
|  | 3454 | } | 
|  | 3455 | continue; | 
|  | 3456 | encode_char: | 
|  | 3457 | #ifdef Py_UNICODE_WIDE | 
|  | 3458 | if (ch >= 0x10000) { | 
|  | 3459 | /* code first surrogate */ | 
|  | 3460 | base64bits += 16; | 
|  | 3461 | base64buffer = (base64buffer << 16) | 0xd800 | ((ch-0x10000) >> 10); | 
|  | 3462 | while (base64bits >= 6) { | 
|  | 3463 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); | 
|  | 3464 | base64bits -= 6; | 
|  | 3465 | } | 
|  | 3466 | /* prepare second surrogate */ | 
|  | 3467 | ch =  0xDC00 | ((ch-0x10000) & 0x3FF); | 
|  | 3468 | } | 
|  | 3469 | #endif | 
|  | 3470 | base64bits += 16; | 
|  | 3471 | base64buffer = (base64buffer << 16) | ch; | 
|  | 3472 | while (base64bits >= 6) { | 
|  | 3473 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); | 
|  | 3474 | base64bits -= 6; | 
|  | 3475 | } | 
| Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 3476 | } | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3477 | if (base64bits) | 
|  | 3478 | *out++= TO_BASE64(base64buffer << (6-base64bits) ); | 
|  | 3479 | if (inShift) | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3480 | *out++ = '-'; | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3481 | if (_PyBytes_Resize(&v, out - start) < 0) | 
|  | 3482 | return NULL; | 
|  | 3483 | return v; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3484 | } | 
|  | 3485 |  | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3486 | #undef IS_BASE64 | 
|  | 3487 | #undef FROM_BASE64 | 
|  | 3488 | #undef TO_BASE64 | 
|  | 3489 | #undef DECODE_DIRECT | 
|  | 3490 | #undef ENCODE_DIRECT | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3491 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3492 | /* --- UTF-8 Codec -------------------------------------------------------- */ | 
|  | 3493 |  | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3494 | static | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3495 | char utf8_code_length[256] = { | 
| Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3496 | /* Map UTF-8 encoded prefix byte to sequence length.  Zero means | 
|  | 3497 | illegal prefix.  See RFC 3629 for details */ | 
|  | 3498 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00-0F */ | 
|  | 3499 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | 
| Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3500 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3501 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | 
|  | 3502 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | 
|  | 3503 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | 
|  | 3504 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | 
| Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3505 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 70-7F */ | 
|  | 3506 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 80-8F */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3507 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | 
|  | 3508 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | 
| Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3509 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0-BF */ | 
|  | 3510 | 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* C0-C1 + C2-CF */ | 
|  | 3511 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* D0-DF */ | 
|  | 3512 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* E0-EF */ | 
|  | 3513 | 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  /* F0-F4 + F5-FF */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3514 | }; | 
|  | 3515 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3516 | PyObject * | 
|  | 3517 | PyUnicode_DecodeUTF8(const char *s, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3518 | Py_ssize_t size, | 
|  | 3519 | const char *errors) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3520 | { | 
| Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3521 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); | 
|  | 3522 | } | 
|  | 3523 |  | 
| Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3524 | /* Mask to check or force alignment of a pointer to C 'long' boundaries */ | 
|  | 3525 | #define LONG_PTR_MASK (size_t) (SIZEOF_LONG - 1) | 
|  | 3526 |  | 
|  | 3527 | /* Mask to quickly check whether a C 'long' contains a | 
|  | 3528 | non-ASCII, UTF8-encoded char. */ | 
|  | 3529 | #if (SIZEOF_LONG == 8) | 
|  | 3530 | # define ASCII_CHAR_MASK 0x8080808080808080L | 
|  | 3531 | #elif (SIZEOF_LONG == 4) | 
|  | 3532 | # define ASCII_CHAR_MASK 0x80808080L | 
|  | 3533 | #else | 
|  | 3534 | # error C 'long' size should be either 4 or 8! | 
|  | 3535 | #endif | 
|  | 3536 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3537 | /* Scans a UTF-8 string and returns the maximum character to be expected, | 
|  | 3538 | the size of the decoded unicode string and if any major errors were | 
|  | 3539 | encountered. | 
|  | 3540 |  | 
|  | 3541 | This function does check basic UTF-8 sanity, it does however NOT CHECK | 
|  | 3542 | if the string contains surrogates, and if all continuation bytes are | 
|  | 3543 | within the correct ranges, these checks are performed in | 
|  | 3544 | PyUnicode_DecodeUTF8Stateful. | 
|  | 3545 |  | 
|  | 3546 | If it sets has_errors to 1, it means the value of unicode_size and max_char | 
|  | 3547 | will be bogus and you should not rely on useful information in them. | 
|  | 3548 | */ | 
|  | 3549 | static Py_UCS4 | 
|  | 3550 | utf8_max_char_size_and_has_errors(const char *s, Py_ssize_t string_size, | 
|  | 3551 | Py_ssize_t *unicode_size, Py_ssize_t* consumed, | 
|  | 3552 | int *has_errors) | 
|  | 3553 | { | 
|  | 3554 | Py_ssize_t n; | 
|  | 3555 | Py_ssize_t char_count = 0; | 
|  | 3556 | Py_UCS4 max_char = 127, new_max; | 
|  | 3557 | Py_UCS4 upper_bound; | 
|  | 3558 | const unsigned char *p = (const unsigned char *)s; | 
|  | 3559 | const unsigned char *end = p + string_size; | 
|  | 3560 | const unsigned char *aligned_end = (const unsigned char *) ((size_t) end & ~LONG_PTR_MASK); | 
|  | 3561 | int err = 0; | 
|  | 3562 |  | 
|  | 3563 | for (; p < end && !err; ++p, ++char_count) { | 
|  | 3564 | /* Only check value if it's not a ASCII char... */ | 
|  | 3565 | if (*p < 0x80) { | 
|  | 3566 | /* Fast path, see below in PyUnicode_DecodeUTF8Stateful for | 
|  | 3567 | an explanation. */ | 
|  | 3568 | if (!((size_t) p & LONG_PTR_MASK)) { | 
|  | 3569 | /* Help register allocation */ | 
|  | 3570 | register const unsigned char *_p = p; | 
|  | 3571 | while (_p < aligned_end) { | 
|  | 3572 | unsigned long value = *(unsigned long *) _p; | 
|  | 3573 | if (value & ASCII_CHAR_MASK) | 
|  | 3574 | break; | 
|  | 3575 | _p += SIZEOF_LONG; | 
|  | 3576 | char_count += SIZEOF_LONG; | 
|  | 3577 | } | 
|  | 3578 | p = _p; | 
|  | 3579 | if (p == end) | 
|  | 3580 | break; | 
|  | 3581 | } | 
|  | 3582 | } | 
|  | 3583 | if (*p >= 0x80) { | 
|  | 3584 | n = utf8_code_length[*p]; | 
|  | 3585 | new_max = max_char; | 
|  | 3586 | switch (n) { | 
|  | 3587 | /* invalid start byte */ | 
|  | 3588 | case 0: | 
|  | 3589 | err = 1; | 
|  | 3590 | break; | 
|  | 3591 | case 2: | 
|  | 3592 | /* Code points between 0x00FF and 0x07FF inclusive. | 
|  | 3593 | Approximate the upper bound of the code point, | 
|  | 3594 | if this flips over 255 we can be sure it will be more | 
|  | 3595 | than 255 and the string will need 2 bytes per code coint, | 
|  | 3596 | if it stays under or equal to 255, we can be sure 1 byte | 
|  | 3597 | is enough. | 
|  | 3598 | ((*p & 0b00011111) << 6) | 0b00111111 */ | 
|  | 3599 | upper_bound = ((*p & 0x1F) << 6) | 0x3F; | 
|  | 3600 | if (max_char < upper_bound) | 
|  | 3601 | new_max = upper_bound; | 
|  | 3602 | /* Ensure we track at least that we left ASCII space. */ | 
|  | 3603 | if (new_max < 128) | 
|  | 3604 | new_max = 128; | 
|  | 3605 | break; | 
|  | 3606 | case 3: | 
|  | 3607 | /* Between 0x0FFF and 0xFFFF inclusive, so values are | 
|  | 3608 | always > 255 and <= 65535 and will always need 2 bytes. */ | 
|  | 3609 | if (max_char < 65535) | 
|  | 3610 | new_max = 65535; | 
|  | 3611 | break; | 
|  | 3612 | case 4: | 
|  | 3613 | /* Code point will be above 0xFFFF for sure in this case. */ | 
|  | 3614 | new_max = 65537; | 
|  | 3615 | break; | 
|  | 3616 | /* Internal error, this should be caught by the first if */ | 
|  | 3617 | case 1: | 
|  | 3618 | default: | 
|  | 3619 | assert(0 && "Impossible case in utf8_max_char_and_size"); | 
|  | 3620 | err = 1; | 
|  | 3621 | } | 
|  | 3622 | /* Instead of number of overall bytes for this code point, | 
|  | 3623 | n containts the number of following bytes: */ | 
|  | 3624 | --n; | 
|  | 3625 | /* Check if the follow up chars are all valid continuation bytes */ | 
|  | 3626 | if (n >= 1) { | 
|  | 3627 | const unsigned char *cont; | 
|  | 3628 | if ((p + n) >= end) { | 
|  | 3629 | if (consumed == 0) | 
|  | 3630 | /* incomplete data, non-incremental decoding */ | 
|  | 3631 | err = 1; | 
|  | 3632 | break; | 
|  | 3633 | } | 
|  | 3634 | for (cont = p + 1; cont < (p + n); ++cont) { | 
|  | 3635 | if ((*cont & 0xc0) != 0x80) { | 
|  | 3636 | err = 1; | 
|  | 3637 | break; | 
|  | 3638 | } | 
|  | 3639 | } | 
|  | 3640 | p += n; | 
|  | 3641 | } | 
|  | 3642 | else | 
|  | 3643 | err = 1; | 
|  | 3644 | max_char = new_max; | 
|  | 3645 | } | 
|  | 3646 | } | 
|  | 3647 |  | 
|  | 3648 | if (unicode_size) | 
|  | 3649 | *unicode_size = char_count; | 
|  | 3650 | if (has_errors) | 
|  | 3651 | *has_errors = err; | 
|  | 3652 | return max_char; | 
|  | 3653 | } | 
|  | 3654 |  | 
|  | 3655 | /* Similar to PyUnicode_WRITE but can also write into wstr field | 
|  | 3656 | of the legacy unicode representation */ | 
|  | 3657 | #define WRITE_FLEXIBLE_OR_WSTR(kind, buf, index, value) \ | 
|  | 3658 | do { \ | 
|  | 3659 | const int k_ = (kind); \ | 
|  | 3660 | if (k_ == PyUnicode_WCHAR_KIND) \ | 
|  | 3661 | ((Py_UNICODE *)(buf))[(index)] = (Py_UNICODE)(value); \ | 
|  | 3662 | else if (k_ == PyUnicode_1BYTE_KIND) \ | 
|  | 3663 | ((unsigned char *)(buf))[(index)] = (unsigned char)(value); \ | 
|  | 3664 | else if (k_ == PyUnicode_2BYTE_KIND) \ | 
|  | 3665 | ((Py_UCS2 *)(buf))[(index)] = (Py_UCS2)(value); \ | 
|  | 3666 | else \ | 
|  | 3667 | ((Py_UCS4 *)(buf))[(index)] = (Py_UCS4)(value); \ | 
|  | 3668 | } while (0) | 
|  | 3669 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3670 | PyObject * | 
|  | 3671 | PyUnicode_DecodeUTF8Stateful(const char *s, | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3672 | Py_ssize_t size, | 
|  | 3673 | const char *errors, | 
|  | 3674 | Py_ssize_t *consumed) | 
| Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3675 | { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3676 | const char *starts = s; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3677 | int n; | 
| Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3678 | int k; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3679 | Py_ssize_t startinpos; | 
|  | 3680 | Py_ssize_t endinpos; | 
| Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3681 | const char *e, *aligned_end; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3682 | PyUnicodeObject *unicode; | 
| Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 3683 | const char *errmsg = ""; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3684 | PyObject *errorHandler = NULL; | 
|  | 3685 | PyObject *exc = NULL; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3686 | Py_UCS4 maxchar = 0; | 
|  | 3687 | Py_ssize_t unicode_size; | 
|  | 3688 | Py_ssize_t i; | 
|  | 3689 | int kind; | 
|  | 3690 | void *data; | 
|  | 3691 | int has_errors; | 
|  | 3692 | Py_UNICODE *error_outptr; | 
|  | 3693 | #if SIZEOF_WCHAR_T == 2 | 
|  | 3694 | Py_ssize_t wchar_offset = 0; | 
|  | 3695 | #endif | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3696 |  | 
| Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3697 | if (size == 0) { | 
|  | 3698 | if (consumed) | 
|  | 3699 | *consumed = 0; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3700 | return (PyObject *)PyUnicode_New(0, 0); | 
| Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3701 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3702 | maxchar = utf8_max_char_size_and_has_errors(s, size, &unicode_size, | 
|  | 3703 | consumed, &has_errors); | 
|  | 3704 | if (has_errors) { | 
|  | 3705 | unicode = _PyUnicode_New(size); | 
|  | 3706 | if (!unicode) | 
|  | 3707 | return NULL; | 
|  | 3708 | kind = PyUnicode_WCHAR_KIND; | 
|  | 3709 | data = PyUnicode_AS_UNICODE(unicode); | 
|  | 3710 | assert(data != NULL); | 
|  | 3711 | } | 
|  | 3712 | else { | 
|  | 3713 | unicode = (PyUnicodeObject *)PyUnicode_New(unicode_size, maxchar); | 
|  | 3714 | if (!unicode) | 
|  | 3715 | return NULL; | 
|  | 3716 | /* When the string is ASCII only, just use memcpy and return. | 
|  | 3717 | unicode_size may be != size if there is an incomplete UTF-8 | 
|  | 3718 | sequence at the end of the ASCII block.  */ | 
|  | 3719 | if (maxchar < 128 && size == unicode_size) { | 
|  | 3720 | Py_MEMCPY(PyUnicode_1BYTE_DATA(unicode), s, unicode_size); | 
|  | 3721 | return (PyObject *)unicode; | 
|  | 3722 | } | 
|  | 3723 | kind = PyUnicode_KIND(unicode); | 
|  | 3724 | data = PyUnicode_DATA(unicode); | 
|  | 3725 | } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3726 | /* Unpack UTF-8 encoded data */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3727 | i = 0; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3728 | e = s + size; | 
| Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3729 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3730 |  | 
|  | 3731 | while (s < e) { | 
| Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 3732 | Py_UCS4 ch = (unsigned char)*s; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3733 |  | 
|  | 3734 | if (ch < 0x80) { | 
| Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3735 | /* Fast path for runs of ASCII characters. Given that common UTF-8 | 
|  | 3736 | input will consist of an overwhelming majority of ASCII | 
|  | 3737 | characters, we try to optimize for this case by checking | 
|  | 3738 | as many characters as a C 'long' can contain. | 
|  | 3739 | First, check if we can do an aligned read, as most CPUs have | 
|  | 3740 | a penalty for unaligned reads. | 
|  | 3741 | */ | 
|  | 3742 | if (!((size_t) s & LONG_PTR_MASK)) { | 
|  | 3743 | /* Help register allocation */ | 
|  | 3744 | register const char *_s = s; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3745 | register Py_ssize_t _i = i; | 
| Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3746 | while (_s < aligned_end) { | 
|  | 3747 | /* Read a whole long at a time (either 4 or 8 bytes), | 
|  | 3748 | and do a fast unrolled copy if it only contains ASCII | 
|  | 3749 | characters. */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3750 | unsigned long value = *(unsigned long *) _s; | 
|  | 3751 | if (value & ASCII_CHAR_MASK) | 
| Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3752 | break; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3753 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+0, _s[0]); | 
|  | 3754 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+1, _s[1]); | 
|  | 3755 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+2, _s[2]); | 
|  | 3756 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+3, _s[3]); | 
| Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3757 | #if (SIZEOF_LONG == 8) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3758 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+4, _s[4]); | 
|  | 3759 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+5, _s[5]); | 
|  | 3760 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+6, _s[6]); | 
|  | 3761 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+7, _s[7]); | 
| Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3762 | #endif | 
|  | 3763 | _s += SIZEOF_LONG; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3764 | _i += SIZEOF_LONG; | 
| Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3765 | } | 
|  | 3766 | s = _s; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3767 | i = _i; | 
| Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3768 | if (s == e) | 
|  | 3769 | break; | 
|  | 3770 | ch = (unsigned char)*s; | 
|  | 3771 | } | 
|  | 3772 | } | 
|  | 3773 |  | 
|  | 3774 | if (ch < 0x80) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3775 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3776 | s++; | 
|  | 3777 | continue; | 
|  | 3778 | } | 
|  | 3779 |  | 
|  | 3780 | n = utf8_code_length[ch]; | 
|  | 3781 |  | 
| Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 3782 | if (s + n > e) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3783 | if (consumed) | 
|  | 3784 | break; | 
|  | 3785 | else { | 
|  | 3786 | errmsg = "unexpected end of data"; | 
|  | 3787 | startinpos = s-starts; | 
| Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3788 | endinpos = startinpos+1; | 
|  | 3789 | for (k=1; (k < size-startinpos) && ((s[k]&0xC0) == 0x80); k++) | 
|  | 3790 | endinpos++; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3791 | goto utf8Error; | 
|  | 3792 | } | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3793 | } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3794 |  | 
|  | 3795 | switch (n) { | 
|  | 3796 |  | 
|  | 3797 | case 0: | 
| Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3798 | errmsg = "invalid start byte"; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3799 | startinpos = s-starts; | 
|  | 3800 | endinpos = startinpos+1; | 
|  | 3801 | goto utf8Error; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3802 |  | 
|  | 3803 | case 1: | 
| Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 3804 | errmsg = "internal error"; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3805 | startinpos = s-starts; | 
|  | 3806 | endinpos = startinpos+1; | 
|  | 3807 | goto utf8Error; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3808 |  | 
|  | 3809 | case 2: | 
| Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 3810 | if ((s[1] & 0xc0) != 0x80) { | 
| Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3811 | errmsg = "invalid continuation byte"; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3812 | startinpos = s-starts; | 
| Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3813 | endinpos = startinpos + 1; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3814 | goto utf8Error; | 
|  | 3815 | } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3816 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); | 
| Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3817 | assert ((ch > 0x007F) && (ch <= 0x07FF)); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3818 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3819 | break; | 
|  | 3820 |  | 
|  | 3821 | case 3: | 
| Ezio Melotti | 9bf2b3a | 2010-07-03 04:52:19 +0000 | [diff] [blame] | 3822 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf | 
|  | 3823 | will result in surrogates in range d800-dfff. Surrogates are | 
|  | 3824 | not valid UTF-8 so they are rejected. | 
|  | 3825 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf | 
|  | 3826 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3827 | if ((s[1] & 0xc0) != 0x80 || | 
| Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3828 | (s[2] & 0xc0) != 0x80 || | 
|  | 3829 | ((unsigned char)s[0] == 0xE0 && | 
|  | 3830 | (unsigned char)s[1] < 0xA0) || | 
|  | 3831 | ((unsigned char)s[0] == 0xED && | 
|  | 3832 | (unsigned char)s[1] > 0x9F)) { | 
|  | 3833 | errmsg = "invalid continuation byte"; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3834 | startinpos = s-starts; | 
| Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3835 | endinpos = startinpos + 1; | 
|  | 3836 |  | 
|  | 3837 | /* if s[1] first two bits are 1 and 0, then the invalid | 
|  | 3838 | continuation byte is s[2], so increment endinpos by 1, | 
|  | 3839 | if not, s[1] is invalid and endinpos doesn't need to | 
|  | 3840 | be incremented. */ | 
|  | 3841 | if ((s[1] & 0xC0) == 0x80) | 
|  | 3842 | endinpos++; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3843 | goto utf8Error; | 
|  | 3844 | } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3845 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); | 
| Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3846 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3847 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); | 
| Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 3848 | break; | 
|  | 3849 |  | 
|  | 3850 | case 4: | 
|  | 3851 | if ((s[1] & 0xc0) != 0x80 || | 
|  | 3852 | (s[2] & 0xc0) != 0x80 || | 
| Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3853 | (s[3] & 0xc0) != 0x80 || | 
|  | 3854 | ((unsigned char)s[0] == 0xF0 && | 
|  | 3855 | (unsigned char)s[1] < 0x90) || | 
|  | 3856 | ((unsigned char)s[0] == 0xF4 && | 
|  | 3857 | (unsigned char)s[1] > 0x8F)) { | 
|  | 3858 | errmsg = "invalid continuation byte"; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3859 | startinpos = s-starts; | 
| Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3860 | endinpos = startinpos + 1; | 
|  | 3861 | if ((s[1] & 0xC0) == 0x80) { | 
|  | 3862 | endinpos++; | 
|  | 3863 | if ((s[2] & 0xC0) == 0x80) | 
|  | 3864 | endinpos++; | 
|  | 3865 | } | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3866 | goto utf8Error; | 
|  | 3867 | } | 
| Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 3868 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + | 
| Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3869 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); | 
|  | 3870 | assert ((ch > 0xFFFF) && (ch <= 0x10ffff)); | 
|  | 3871 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3872 | /* If the string is flexible or we have native UCS-4, write | 
|  | 3873 | directly.. */ | 
|  | 3874 | if (sizeof(Py_UNICODE) > 2 || kind != PyUnicode_WCHAR_KIND) | 
|  | 3875 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3876 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3877 | else { | 
|  | 3878 | /* compute and append the two surrogates: */ | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3879 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3880 | /* translate from 10000..10FFFF to 0..FFFF */ | 
|  | 3881 | ch -= 0x10000; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3882 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3883 | /* high surrogate = top 10 bits added to D800 */ | 
|  | 3884 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, | 
|  | 3885 | (Py_UNICODE)(0xD800 + (ch >> 10))); | 
|  | 3886 |  | 
|  | 3887 | /* low surrogate = bottom 10 bits added to DC00 */ | 
|  | 3888 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, | 
|  | 3889 | (Py_UNICODE)(0xDC00 + (ch & 0x03FF))); | 
|  | 3890 | } | 
|  | 3891 | #if SIZEOF_WCHAR_T == 2 | 
|  | 3892 | wchar_offset++; | 
| Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3893 | #endif | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3894 | break; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3895 | } | 
|  | 3896 | s += n; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3897 | continue; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3898 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3899 | utf8Error: | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3900 | /* If this is not yet a resizable string, make it one.. */ | 
|  | 3901 | if (kind != PyUnicode_WCHAR_KIND) { | 
|  | 3902 | const Py_UNICODE *u; | 
|  | 3903 | PyUnicodeObject *new_unicode = _PyUnicode_New(size); | 
|  | 3904 | if (!new_unicode) | 
|  | 3905 | goto onError; | 
|  | 3906 | u = PyUnicode_AsUnicode((PyObject *)unicode); | 
|  | 3907 | if (!u) | 
|  | 3908 | goto onError; | 
|  | 3909 | #if SIZEOF_WCHAR_T == 2 | 
|  | 3910 | i += wchar_offset; | 
|  | 3911 | #endif | 
|  | 3912 | Py_UNICODE_COPY(PyUnicode_AS_UNICODE(new_unicode), u, i); | 
|  | 3913 | Py_DECREF(unicode); | 
|  | 3914 | unicode = new_unicode; | 
|  | 3915 | kind = 0; | 
|  | 3916 | data = PyUnicode_AS_UNICODE(new_unicode); | 
|  | 3917 | assert(data != NULL); | 
|  | 3918 | } | 
|  | 3919 | error_outptr = PyUnicode_AS_UNICODE(unicode) + i; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3920 | if (unicode_decode_call_errorhandler( | 
|  | 3921 | errors, &errorHandler, | 
|  | 3922 | "utf8", errmsg, | 
|  | 3923 | &starts, &e, &startinpos, &endinpos, &exc, &s, | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3924 | &unicode, &i, &error_outptr)) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3925 | goto onError; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3926 | /* Update data because unicode_decode_call_errorhandler might have | 
|  | 3927 | re-created or resized the unicode object. */ | 
|  | 3928 | data = PyUnicode_AS_UNICODE(unicode); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3929 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3930 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3931 | /* Ensure the unicode_size calculation above was correct: */ | 
|  | 3932 | assert(kind == PyUnicode_WCHAR_KIND || i == unicode_size); | 
|  | 3933 |  | 
| Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3934 | if (consumed) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3935 | *consumed = s-starts; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3936 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3937 | /* Adjust length and ready string when it contained errors and | 
|  | 3938 | is of the old resizable kind. */ | 
|  | 3939 | if (kind == PyUnicode_WCHAR_KIND) { | 
|  | 3940 | if (_PyUnicode_Resize(&unicode, i) < 0 || | 
|  | 3941 | PyUnicode_READY(unicode) == -1) | 
|  | 3942 | goto onError; | 
|  | 3943 | } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3944 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3945 | Py_XDECREF(errorHandler); | 
|  | 3946 | Py_XDECREF(exc); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3947 | if (PyUnicode_READY(unicode) == -1) { | 
|  | 3948 | Py_DECREF(unicode); | 
|  | 3949 | return NULL; | 
|  | 3950 | } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3951 | return (PyObject *)unicode; | 
|  | 3952 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3953 | onError: | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3954 | Py_XDECREF(errorHandler); | 
|  | 3955 | Py_XDECREF(exc); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3956 | Py_DECREF(unicode); | 
|  | 3957 | return NULL; | 
|  | 3958 | } | 
|  | 3959 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3960 | #undef WRITE_FLEXIBLE_OR_WSTR | 
| Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3961 |  | 
| Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 3962 | #ifdef __APPLE__ | 
|  | 3963 |  | 
|  | 3964 | /* Simplified UTF-8 decoder using surrogateescape error handler, | 
|  | 3965 | used to decode the command line arguments on Mac OS X. */ | 
|  | 3966 |  | 
|  | 3967 | wchar_t* | 
|  | 3968 | _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size) | 
|  | 3969 | { | 
|  | 3970 | int n; | 
|  | 3971 | const char *e; | 
|  | 3972 | wchar_t *unicode, *p; | 
|  | 3973 |  | 
|  | 3974 | /* Note: size will always be longer than the resulting Unicode | 
|  | 3975 | character count */ | 
|  | 3976 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < (size + 1)) { | 
|  | 3977 | PyErr_NoMemory(); | 
|  | 3978 | return NULL; | 
|  | 3979 | } | 
|  | 3980 | unicode = PyMem_Malloc((size + 1) * sizeof(wchar_t)); | 
|  | 3981 | if (!unicode) | 
|  | 3982 | return NULL; | 
|  | 3983 |  | 
|  | 3984 | /* Unpack UTF-8 encoded data */ | 
|  | 3985 | p = unicode; | 
|  | 3986 | e = s + size; | 
|  | 3987 | while (s < e) { | 
|  | 3988 | Py_UCS4 ch = (unsigned char)*s; | 
|  | 3989 |  | 
|  | 3990 | if (ch < 0x80) { | 
|  | 3991 | *p++ = (wchar_t)ch; | 
|  | 3992 | s++; | 
|  | 3993 | continue; | 
|  | 3994 | } | 
|  | 3995 |  | 
|  | 3996 | n = utf8_code_length[ch]; | 
|  | 3997 | if (s + n > e) { | 
|  | 3998 | goto surrogateescape; | 
|  | 3999 | } | 
|  | 4000 |  | 
|  | 4001 | switch (n) { | 
|  | 4002 | case 0: | 
|  | 4003 | case 1: | 
|  | 4004 | goto surrogateescape; | 
|  | 4005 |  | 
|  | 4006 | case 2: | 
|  | 4007 | if ((s[1] & 0xc0) != 0x80) | 
|  | 4008 | goto surrogateescape; | 
|  | 4009 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); | 
|  | 4010 | assert ((ch > 0x007F) && (ch <= 0x07FF)); | 
|  | 4011 | *p++ = (wchar_t)ch; | 
|  | 4012 | break; | 
|  | 4013 |  | 
|  | 4014 | case 3: | 
|  | 4015 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf | 
|  | 4016 | will result in surrogates in range d800-dfff. Surrogates are | 
|  | 4017 | not valid UTF-8 so they are rejected. | 
|  | 4018 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf | 
|  | 4019 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ | 
|  | 4020 | if ((s[1] & 0xc0) != 0x80 || | 
|  | 4021 | (s[2] & 0xc0) != 0x80 || | 
|  | 4022 | ((unsigned char)s[0] == 0xE0 && | 
|  | 4023 | (unsigned char)s[1] < 0xA0) || | 
|  | 4024 | ((unsigned char)s[0] == 0xED && | 
|  | 4025 | (unsigned char)s[1] > 0x9F)) { | 
|  | 4026 |  | 
|  | 4027 | goto surrogateescape; | 
|  | 4028 | } | 
|  | 4029 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); | 
|  | 4030 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4031 | *p++ = (wchar_t)ch; | 
| Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4032 | break; | 
|  | 4033 |  | 
|  | 4034 | case 4: | 
|  | 4035 | if ((s[1] & 0xc0) != 0x80 || | 
|  | 4036 | (s[2] & 0xc0) != 0x80 || | 
|  | 4037 | (s[3] & 0xc0) != 0x80 || | 
|  | 4038 | ((unsigned char)s[0] == 0xF0 && | 
|  | 4039 | (unsigned char)s[1] < 0x90) || | 
|  | 4040 | ((unsigned char)s[0] == 0xF4 && | 
|  | 4041 | (unsigned char)s[1] > 0x8F)) { | 
|  | 4042 | goto surrogateescape; | 
|  | 4043 | } | 
|  | 4044 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + | 
|  | 4045 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); | 
|  | 4046 | assert ((ch > 0xFFFF) && (ch <= 0x10ffff)); | 
|  | 4047 |  | 
|  | 4048 | #if SIZEOF_WCHAR_T == 4 | 
|  | 4049 | *p++ = (wchar_t)ch; | 
|  | 4050 | #else | 
|  | 4051 | /*  compute and append the two surrogates: */ | 
|  | 4052 |  | 
|  | 4053 | /*  translate from 10000..10FFFF to 0..FFFF */ | 
|  | 4054 | ch -= 0x10000; | 
|  | 4055 |  | 
|  | 4056 | /*  high surrogate = top 10 bits added to D800 */ | 
|  | 4057 | *p++ = (wchar_t)(0xD800 + (ch >> 10)); | 
|  | 4058 |  | 
|  | 4059 | /*  low surrogate = bottom 10 bits added to DC00 */ | 
|  | 4060 | *p++ = (wchar_t)(0xDC00 + (ch & 0x03FF)); | 
|  | 4061 | #endif | 
|  | 4062 | break; | 
|  | 4063 | } | 
|  | 4064 | s += n; | 
|  | 4065 | continue; | 
|  | 4066 |  | 
|  | 4067 | surrogateescape: | 
|  | 4068 | *p++ = 0xDC00 + ch; | 
|  | 4069 | s++; | 
|  | 4070 | } | 
|  | 4071 | *p = L'\0'; | 
|  | 4072 | return unicode; | 
|  | 4073 | } | 
|  | 4074 |  | 
|  | 4075 | #endif /* __APPLE__ */ | 
| Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4076 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4077 | /* Primary internal function which creates utf8 encoded bytes objects. | 
|  | 4078 |  | 
|  | 4079 | Allocation strategy:  if the string is short, convert into a stack buffer | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4080 | and allocate exactly as much space needed at the end.  Else allocate the | 
|  | 4081 | maximum possible needed (4 result bytes per Unicode character), and return | 
|  | 4082 | the excess memory at the end. | 
| Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4083 | */ | 
| Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 4084 | PyObject * | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4085 | _PyUnicode_AsUTF8String(PyObject *obj, const char *errors) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4086 | { | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4087 | #define MAX_SHORT_UNICHARS 300  /* largest size we'll do on the stack */ | 
| Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 4088 |  | 
| Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4089 | Py_ssize_t i;                /* index into s of next input byte */ | 
|  | 4090 | PyObject *result;            /* result string object */ | 
|  | 4091 | char *p;                     /* next free byte in output buffer */ | 
|  | 4092 | Py_ssize_t nallocated;      /* number of result bytes allocated */ | 
|  | 4093 | Py_ssize_t nneeded;            /* number of result bytes needed */ | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4094 | char stackbuf[MAX_SHORT_UNICHARS * 4]; | 
| Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4095 | PyObject *errorHandler = NULL; | 
|  | 4096 | PyObject *exc = NULL; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4097 | int kind; | 
|  | 4098 | void *data; | 
|  | 4099 | Py_ssize_t size; | 
|  | 4100 | PyUnicodeObject *unicode = (PyUnicodeObject *)obj; | 
|  | 4101 | #if SIZEOF_WCHAR_T == 2 | 
|  | 4102 | Py_ssize_t wchar_offset = 0; | 
|  | 4103 | #endif | 
| Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 4104 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4105 | if (!PyUnicode_Check(unicode)) { | 
|  | 4106 | PyErr_BadArgument(); | 
|  | 4107 | return NULL; | 
|  | 4108 | } | 
|  | 4109 |  | 
|  | 4110 | if (PyUnicode_READY(unicode) == -1) | 
|  | 4111 | return NULL; | 
|  | 4112 |  | 
| Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 4113 | if (PyUnicode_UTF8(unicode)) | 
|  | 4114 | return PyBytes_FromStringAndSize(PyUnicode_UTF8(unicode), | 
|  | 4115 | PyUnicode_UTF8_LENGTH(unicode)); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4116 |  | 
|  | 4117 | kind = PyUnicode_KIND(unicode); | 
|  | 4118 | data = PyUnicode_DATA(unicode); | 
|  | 4119 | size = PyUnicode_GET_LENGTH(unicode); | 
|  | 4120 |  | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4121 | assert(size >= 0); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4122 |  | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4123 | if (size <= MAX_SHORT_UNICHARS) { | 
|  | 4124 | /* Write into the stack buffer; nallocated can't overflow. | 
|  | 4125 | * At the end, we'll allocate exactly as much heap space as it | 
|  | 4126 | * turns out we need. | 
|  | 4127 | */ | 
|  | 4128 | nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); | 
| Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4129 | result = NULL;   /* will allocate after we're done */ | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4130 | p = stackbuf; | 
|  | 4131 | } | 
|  | 4132 | else { | 
|  | 4133 | /* Overallocate on the heap, and give the excess back at the end. */ | 
|  | 4134 | nallocated = size * 4; | 
|  | 4135 | if (nallocated / 4 != size)  /* overflow! */ | 
|  | 4136 | return PyErr_NoMemory(); | 
| Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4137 | result = PyBytes_FromStringAndSize(NULL, nallocated); | 
| Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4138 | if (result == NULL) | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4139 | return NULL; | 
| Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4140 | p = PyBytes_AS_STRING(result); | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4141 | } | 
| Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4142 |  | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4143 | for (i = 0; i < size;) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4144 | Py_UCS4 ch = PyUnicode_READ(kind, data, i++); | 
| Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 4145 |  | 
| Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4146 | if (ch < 0x80) | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4147 | /* Encode ASCII */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4148 | *p++ = (char) ch; | 
| Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 4149 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4150 | else if (ch < 0x0800) { | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4151 | /* Encode Latin-1 */ | 
| Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 4152 | *p++ = (char)(0xc0 | (ch >> 6)); | 
|  | 4153 | *p++ = (char)(0x80 | (ch & 0x3f)); | 
| Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4154 | } else if (0xD800 <= ch && ch <= 0xDFFF) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4155 | Py_ssize_t newpos; | 
|  | 4156 | PyObject *rep; | 
|  | 4157 | Py_ssize_t repsize, k, startpos; | 
|  | 4158 | startpos = i-1; | 
|  | 4159 | #if SIZEOF_WCHAR_T == 2 | 
|  | 4160 | startpos += wchar_offset; | 
| Victor Stinner | 445a623 | 2010-04-22 20:01:57 +0000 | [diff] [blame] | 4161 | #endif | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4162 | rep = unicode_encode_call_errorhandler( | 
|  | 4163 | errors, &errorHandler, "utf-8", "surrogates not allowed", | 
|  | 4164 | PyUnicode_AS_UNICODE(unicode), PyUnicode_GET_SIZE(unicode), | 
|  | 4165 | &exc, startpos, startpos+1, &newpos); | 
|  | 4166 | if (!rep) | 
|  | 4167 | goto error; | 
| Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4168 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4169 | if (PyBytes_Check(rep)) | 
|  | 4170 | repsize = PyBytes_GET_SIZE(rep); | 
|  | 4171 | else | 
|  | 4172 | repsize = PyUnicode_GET_SIZE(rep); | 
|  | 4173 |  | 
|  | 4174 | if (repsize > 4) { | 
|  | 4175 | Py_ssize_t offset; | 
|  | 4176 |  | 
|  | 4177 | if (result == NULL) | 
|  | 4178 | offset = p - stackbuf; | 
| Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4179 | else | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4180 | offset = p - PyBytes_AS_STRING(result); | 
| Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4181 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4182 | if (nallocated > PY_SSIZE_T_MAX - repsize + 4) { | 
|  | 4183 | /* integer overflow */ | 
|  | 4184 | PyErr_NoMemory(); | 
|  | 4185 | goto error; | 
|  | 4186 | } | 
|  | 4187 | nallocated += repsize - 4; | 
|  | 4188 | if (result != NULL) { | 
|  | 4189 | if (_PyBytes_Resize(&result, nallocated) < 0) | 
|  | 4190 | goto error; | 
|  | 4191 | } else { | 
|  | 4192 | result = PyBytes_FromStringAndSize(NULL, nallocated); | 
| Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4193 | if (result == NULL) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4194 | goto error; | 
|  | 4195 | Py_MEMCPY(PyBytes_AS_STRING(result), stackbuf, offset); | 
|  | 4196 | } | 
|  | 4197 | p = PyBytes_AS_STRING(result) + offset; | 
|  | 4198 | } | 
| Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4199 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4200 | if (PyBytes_Check(rep)) { | 
|  | 4201 | char *prep = PyBytes_AS_STRING(rep); | 
|  | 4202 | for(k = repsize; k > 0; k--) | 
|  | 4203 | *p++ = *prep++; | 
|  | 4204 | } else /* rep is unicode */ { | 
|  | 4205 | const Py_UNICODE *prep = PyUnicode_AS_UNICODE(rep); | 
|  | 4206 | Py_UNICODE c; | 
|  | 4207 |  | 
|  | 4208 | for(k=0; k<repsize; k++) { | 
|  | 4209 | c = prep[k]; | 
|  | 4210 | if (0x80 <= c) { | 
|  | 4211 | raise_encode_exception(&exc, "utf-8", | 
|  | 4212 | PyUnicode_AS_UNICODE(unicode), | 
|  | 4213 | size, i-1, i, | 
|  | 4214 | "surrogates not allowed"); | 
| Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4215 | goto error; | 
|  | 4216 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4217 | *p++ = (char)prep[k]; | 
| Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4218 | } | 
| Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4219 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4220 | Py_DECREF(rep); | 
| Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4221 | } else if (ch < 0x10000) { | 
|  | 4222 | *p++ = (char)(0xe0 | (ch >> 12)); | 
|  | 4223 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); | 
|  | 4224 | *p++ = (char)(0x80 | (ch & 0x3f)); | 
|  | 4225 | } else /* ch >= 0x10000 */ { | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4226 | /* Encode UCS4 Unicode ordinals */ | 
|  | 4227 | *p++ = (char)(0xf0 | (ch >> 18)); | 
|  | 4228 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); | 
|  | 4229 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); | 
|  | 4230 | *p++ = (char)(0x80 | (ch & 0x3f)); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4231 | #if SIZEOF_WCHAR_T == 2 | 
|  | 4232 | wchar_offset++; | 
|  | 4233 | #endif | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4234 | } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4235 | } | 
| Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 4236 |  | 
| Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4237 | if (result == NULL) { | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4238 | /* This was stack allocated. */ | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4239 | nneeded = p - stackbuf; | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4240 | assert(nneeded <= nallocated); | 
| Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4241 | result = PyBytes_FromStringAndSize(stackbuf, nneeded); | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4242 | } | 
|  | 4243 | else { | 
| Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 4244 | /* Cut back to size actually needed. */ | 
| Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4245 | nneeded = p - PyBytes_AS_STRING(result); | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4246 | assert(nneeded <= nallocated); | 
| Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4247 | _PyBytes_Resize(&result, nneeded); | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4248 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4249 |  | 
| Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4250 | Py_XDECREF(errorHandler); | 
|  | 4251 | Py_XDECREF(exc); | 
| Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4252 | return result; | 
| Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4253 | error: | 
|  | 4254 | Py_XDECREF(errorHandler); | 
|  | 4255 | Py_XDECREF(exc); | 
|  | 4256 | Py_XDECREF(result); | 
|  | 4257 | return NULL; | 
| Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4258 |  | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4259 | #undef MAX_SHORT_UNICHARS | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4260 | } | 
|  | 4261 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4262 | PyObject * | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4263 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, | 
|  | 4264 | Py_ssize_t size, | 
|  | 4265 | const char *errors) | 
|  | 4266 | { | 
|  | 4267 | PyObject *v, *unicode; | 
|  | 4268 |  | 
|  | 4269 | unicode = PyUnicode_FromUnicode(s, size); | 
|  | 4270 | if (unicode == NULL) | 
|  | 4271 | return NULL; | 
|  | 4272 | v = _PyUnicode_AsUTF8String(unicode, errors); | 
|  | 4273 | Py_DECREF(unicode); | 
|  | 4274 | return v; | 
|  | 4275 | } | 
|  | 4276 |  | 
|  | 4277 | PyObject * | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4278 | PyUnicode_AsUTF8String(PyObject *unicode) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4279 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4280 | return _PyUnicode_AsUTF8String(unicode, NULL); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4281 | } | 
|  | 4282 |  | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4283 | /* --- UTF-32 Codec ------------------------------------------------------- */ | 
|  | 4284 |  | 
|  | 4285 | PyObject * | 
|  | 4286 | PyUnicode_DecodeUTF32(const char *s, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4287 | Py_ssize_t size, | 
|  | 4288 | const char *errors, | 
|  | 4289 | int *byteorder) | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4290 | { | 
|  | 4291 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); | 
|  | 4292 | } | 
|  | 4293 |  | 
|  | 4294 | PyObject * | 
|  | 4295 | PyUnicode_DecodeUTF32Stateful(const char *s, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4296 | Py_ssize_t size, | 
|  | 4297 | const char *errors, | 
|  | 4298 | int *byteorder, | 
|  | 4299 | Py_ssize_t *consumed) | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4300 | { | 
|  | 4301 | const char *starts = s; | 
|  | 4302 | Py_ssize_t startinpos; | 
|  | 4303 | Py_ssize_t endinpos; | 
|  | 4304 | Py_ssize_t outpos; | 
|  | 4305 | PyUnicodeObject *unicode; | 
|  | 4306 | Py_UNICODE *p; | 
|  | 4307 | #ifndef Py_UNICODE_WIDE | 
| Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4308 | int pairs = 0; | 
| Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 4309 | const unsigned char *qq; | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4310 | #else | 
|  | 4311 | const int pairs = 0; | 
|  | 4312 | #endif | 
| Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 4313 | const unsigned char *q, *e; | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4314 | int bo = 0;       /* assume native ordering by default */ | 
|  | 4315 | const char *errmsg = ""; | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4316 | /* Offsets from q for retrieving bytes in the right order. */ | 
|  | 4317 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN | 
|  | 4318 | int iorder[] = {0, 1, 2, 3}; | 
|  | 4319 | #else | 
|  | 4320 | int iorder[] = {3, 2, 1, 0}; | 
|  | 4321 | #endif | 
|  | 4322 | PyObject *errorHandler = NULL; | 
|  | 4323 | PyObject *exc = NULL; | 
| Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 4324 |  | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4325 | q = (unsigned char *)s; | 
|  | 4326 | e = q + size; | 
|  | 4327 |  | 
|  | 4328 | if (byteorder) | 
|  | 4329 | bo = *byteorder; | 
|  | 4330 |  | 
|  | 4331 | /* Check for BOM marks (U+FEFF) in the input and adjust current | 
|  | 4332 | byte order setting accordingly. In native mode, the leading BOM | 
|  | 4333 | mark is skipped, in all other modes, it is copied to the output | 
|  | 4334 | stream as-is (giving a ZWNBSP character). */ | 
|  | 4335 | if (bo == 0) { | 
|  | 4336 | if (size >= 4) { | 
|  | 4337 | const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4338 | (q[iorder[1]] << 8) | q[iorder[0]]; | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4339 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4340 | if (bom == 0x0000FEFF) { | 
|  | 4341 | q += 4; | 
|  | 4342 | bo = -1; | 
|  | 4343 | } | 
|  | 4344 | else if (bom == 0xFFFE0000) { | 
|  | 4345 | q += 4; | 
|  | 4346 | bo = 1; | 
|  | 4347 | } | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4348 | #else | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4349 | if (bom == 0x0000FEFF) { | 
|  | 4350 | q += 4; | 
|  | 4351 | bo = 1; | 
|  | 4352 | } | 
|  | 4353 | else if (bom == 0xFFFE0000) { | 
|  | 4354 | q += 4; | 
|  | 4355 | bo = -1; | 
|  | 4356 | } | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4357 | #endif | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4358 | } | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4359 | } | 
|  | 4360 |  | 
|  | 4361 | if (bo == -1) { | 
|  | 4362 | /* force LE */ | 
|  | 4363 | iorder[0] = 0; | 
|  | 4364 | iorder[1] = 1; | 
|  | 4365 | iorder[2] = 2; | 
|  | 4366 | iorder[3] = 3; | 
|  | 4367 | } | 
|  | 4368 | else if (bo == 1) { | 
|  | 4369 | /* force BE */ | 
|  | 4370 | iorder[0] = 3; | 
|  | 4371 | iorder[1] = 2; | 
|  | 4372 | iorder[2] = 1; | 
|  | 4373 | iorder[3] = 0; | 
|  | 4374 | } | 
|  | 4375 |  | 
| Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4376 | /* On narrow builds we split characters outside the BMP into two | 
|  | 4377 | codepoints => count how much extra space we need. */ | 
|  | 4378 | #ifndef Py_UNICODE_WIDE | 
|  | 4379 | for (qq = q; qq < e; qq += 4) | 
|  | 4380 | if (qq[iorder[2]] != 0 || qq[iorder[3]] != 0) | 
|  | 4381 | pairs++; | 
|  | 4382 | #endif | 
|  | 4383 |  | 
|  | 4384 | /* This might be one to much, because of a BOM */ | 
|  | 4385 | unicode = _PyUnicode_New((size+3)/4+pairs); | 
|  | 4386 | if (!unicode) | 
|  | 4387 | return NULL; | 
|  | 4388 | if (size == 0) | 
|  | 4389 | return (PyObject *)unicode; | 
|  | 4390 |  | 
|  | 4391 | /* Unpack UTF-32 encoded data */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4392 | p = PyUnicode_AS_UNICODE(unicode); | 
| Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4393 |  | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4394 | while (q < e) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4395 | Py_UCS4 ch; | 
|  | 4396 | /* remaining bytes at the end? (size should be divisible by 4) */ | 
|  | 4397 | if (e-q<4) { | 
|  | 4398 | if (consumed) | 
|  | 4399 | break; | 
|  | 4400 | errmsg = "truncated data"; | 
|  | 4401 | startinpos = ((const char *)q)-starts; | 
|  | 4402 | endinpos = ((const char *)e)-starts; | 
|  | 4403 | goto utf32Error; | 
|  | 4404 | /* The remaining input chars are ignored if the callback | 
|  | 4405 | chooses to skip the input */ | 
|  | 4406 | } | 
|  | 4407 | ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | | 
|  | 4408 | (q[iorder[1]] << 8) | q[iorder[0]]; | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4409 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4410 | if (ch >= 0x110000) | 
|  | 4411 | { | 
|  | 4412 | errmsg = "codepoint not in range(0x110000)"; | 
|  | 4413 | startinpos = ((const char *)q)-starts; | 
|  | 4414 | endinpos = startinpos+4; | 
|  | 4415 | goto utf32Error; | 
|  | 4416 | } | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4417 | #ifndef Py_UNICODE_WIDE | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4418 | if (ch >= 0x10000) | 
|  | 4419 | { | 
|  | 4420 | *p++ = 0xD800 | ((ch-0x10000) >> 10); | 
|  | 4421 | *p++ = 0xDC00 | ((ch-0x10000) & 0x3FF); | 
|  | 4422 | } | 
|  | 4423 | else | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4424 | #endif | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4425 | *p++ = ch; | 
|  | 4426 | q += 4; | 
|  | 4427 | continue; | 
|  | 4428 | utf32Error: | 
|  | 4429 | outpos = p-PyUnicode_AS_UNICODE(unicode); | 
|  | 4430 | if (unicode_decode_call_errorhandler( | 
|  | 4431 | errors, &errorHandler, | 
|  | 4432 | "utf32", errmsg, | 
|  | 4433 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, | 
|  | 4434 | &unicode, &outpos, &p)) | 
|  | 4435 | goto onError; | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4436 | } | 
|  | 4437 |  | 
|  | 4438 | if (byteorder) | 
|  | 4439 | *byteorder = bo; | 
|  | 4440 |  | 
|  | 4441 | if (consumed) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4442 | *consumed = (const char *)q-starts; | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4443 |  | 
|  | 4444 | /* Adjust length */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4445 | if (_PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4446 | goto onError; | 
|  | 4447 |  | 
|  | 4448 | Py_XDECREF(errorHandler); | 
|  | 4449 | Py_XDECREF(exc); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4450 | if (PyUnicode_READY(unicode) == -1) { | 
|  | 4451 | Py_DECREF(unicode); | 
|  | 4452 | return NULL; | 
|  | 4453 | } | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4454 | return (PyObject *)unicode; | 
|  | 4455 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4456 | onError: | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4457 | Py_DECREF(unicode); | 
|  | 4458 | Py_XDECREF(errorHandler); | 
|  | 4459 | Py_XDECREF(exc); | 
|  | 4460 | return NULL; | 
|  | 4461 | } | 
|  | 4462 |  | 
|  | 4463 | PyObject * | 
|  | 4464 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4465 | Py_ssize_t size, | 
|  | 4466 | const char *errors, | 
|  | 4467 | int byteorder) | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4468 | { | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4469 | PyObject *v; | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4470 | unsigned char *p; | 
| Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4471 | Py_ssize_t nsize, bytesize; | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4472 | #ifndef Py_UNICODE_WIDE | 
| Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4473 | Py_ssize_t i, pairs; | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4474 | #else | 
|  | 4475 | const int pairs = 0; | 
|  | 4476 | #endif | 
|  | 4477 | /* Offsets from p for storing byte pairs in the right order. */ | 
|  | 4478 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN | 
|  | 4479 | int iorder[] = {0, 1, 2, 3}; | 
|  | 4480 | #else | 
|  | 4481 | int iorder[] = {3, 2, 1, 0}; | 
|  | 4482 | #endif | 
|  | 4483 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4484 | #define STORECHAR(CH)                           \ | 
|  | 4485 | do {                                        \ | 
|  | 4486 | p[iorder[3]] = ((CH) >> 24) & 0xff;     \ | 
|  | 4487 | p[iorder[2]] = ((CH) >> 16) & 0xff;     \ | 
|  | 4488 | p[iorder[1]] = ((CH) >> 8) & 0xff;      \ | 
|  | 4489 | p[iorder[0]] = (CH) & 0xff;             \ | 
|  | 4490 | p += 4;                                 \ | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4491 | } while(0) | 
|  | 4492 |  | 
|  | 4493 | /* In narrow builds we can output surrogate pairs as one codepoint, | 
|  | 4494 | so we need less space. */ | 
|  | 4495 | #ifndef Py_UNICODE_WIDE | 
|  | 4496 | for (i = pairs = 0; i < size-1; i++) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4497 | if (0xD800 <= s[i] && s[i] <= 0xDBFF && | 
|  | 4498 | 0xDC00 <= s[i+1] && s[i+1] <= 0xDFFF) | 
|  | 4499 | pairs++; | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4500 | #endif | 
| Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4501 | nsize = (size - pairs + (byteorder == 0)); | 
|  | 4502 | bytesize = nsize * 4; | 
|  | 4503 | if (bytesize / 4 != nsize) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4504 | return PyErr_NoMemory(); | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4505 | v = PyBytes_FromStringAndSize(NULL, bytesize); | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4506 | if (v == NULL) | 
|  | 4507 | return NULL; | 
|  | 4508 |  | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4509 | p = (unsigned char *)PyBytes_AS_STRING(v); | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4510 | if (byteorder == 0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4511 | STORECHAR(0xFEFF); | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4512 | if (size == 0) | 
| Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4513 | goto done; | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4514 |  | 
|  | 4515 | if (byteorder == -1) { | 
|  | 4516 | /* force LE */ | 
|  | 4517 | iorder[0] = 0; | 
|  | 4518 | iorder[1] = 1; | 
|  | 4519 | iorder[2] = 2; | 
|  | 4520 | iorder[3] = 3; | 
|  | 4521 | } | 
|  | 4522 | else if (byteorder == 1) { | 
|  | 4523 | /* force BE */ | 
|  | 4524 | iorder[0] = 3; | 
|  | 4525 | iorder[1] = 2; | 
|  | 4526 | iorder[2] = 1; | 
|  | 4527 | iorder[3] = 0; | 
|  | 4528 | } | 
|  | 4529 |  | 
|  | 4530 | while (size-- > 0) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4531 | Py_UCS4 ch = *s++; | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4532 | #ifndef Py_UNICODE_WIDE | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4533 | if (0xD800 <= ch && ch <= 0xDBFF && size > 0) { | 
|  | 4534 | Py_UCS4 ch2 = *s; | 
|  | 4535 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { | 
|  | 4536 | ch = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; | 
|  | 4537 | s++; | 
|  | 4538 | size--; | 
|  | 4539 | } | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4540 | } | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4541 | #endif | 
|  | 4542 | STORECHAR(ch); | 
|  | 4543 | } | 
| Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4544 |  | 
|  | 4545 | done: | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4546 | return v; | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4547 | #undef STORECHAR | 
|  | 4548 | } | 
|  | 4549 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4550 | PyObject * | 
|  | 4551 | PyUnicode_AsUTF32String(PyObject *unicode) | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4552 | { | 
|  | 4553 | if (!PyUnicode_Check(unicode)) { | 
|  | 4554 | PyErr_BadArgument(); | 
|  | 4555 | return NULL; | 
|  | 4556 | } | 
|  | 4557 | return PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(unicode), | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4558 | PyUnicode_GET_SIZE(unicode), | 
|  | 4559 | NULL, | 
|  | 4560 | 0); | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4561 | } | 
|  | 4562 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4563 | /* --- UTF-16 Codec ------------------------------------------------------- */ | 
|  | 4564 |  | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4565 | PyObject * | 
|  | 4566 | PyUnicode_DecodeUTF16(const char *s, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4567 | Py_ssize_t size, | 
|  | 4568 | const char *errors, | 
|  | 4569 | int *byteorder) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4570 | { | 
| Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4571 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); | 
|  | 4572 | } | 
|  | 4573 |  | 
| Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4574 | /* Two masks for fast checking of whether a C 'long' may contain | 
|  | 4575 | UTF16-encoded surrogate characters. This is an efficient heuristic, | 
|  | 4576 | assuming that non-surrogate characters with a code point >= 0x8000 are | 
|  | 4577 | rare in most input. | 
|  | 4578 | FAST_CHAR_MASK is used when the input is in native byte ordering, | 
|  | 4579 | SWAPPED_FAST_CHAR_MASK when the input is in byteswapped ordering. | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4580 | */ | 
| Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4581 | #if (SIZEOF_LONG == 8) | 
|  | 4582 | # define FAST_CHAR_MASK         0x8000800080008000L | 
|  | 4583 | # define SWAPPED_FAST_CHAR_MASK 0x0080008000800080L | 
|  | 4584 | #elif (SIZEOF_LONG == 4) | 
|  | 4585 | # define FAST_CHAR_MASK         0x80008000L | 
|  | 4586 | # define SWAPPED_FAST_CHAR_MASK 0x00800080L | 
|  | 4587 | #else | 
|  | 4588 | # error C 'long' size should be either 4 or 8! | 
|  | 4589 | #endif | 
|  | 4590 |  | 
| Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4591 | PyObject * | 
|  | 4592 | PyUnicode_DecodeUTF16Stateful(const char *s, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4593 | Py_ssize_t size, | 
|  | 4594 | const char *errors, | 
|  | 4595 | int *byteorder, | 
|  | 4596 | Py_ssize_t *consumed) | 
| Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4597 | { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4598 | const char *starts = s; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4599 | Py_ssize_t startinpos; | 
|  | 4600 | Py_ssize_t endinpos; | 
|  | 4601 | Py_ssize_t outpos; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4602 | PyUnicodeObject *unicode; | 
|  | 4603 | Py_UNICODE *p; | 
| Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4604 | const unsigned char *q, *e, *aligned_end; | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4605 | int bo = 0;       /* assume native ordering by default */ | 
| Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4606 | int native_ordering = 0; | 
| Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4607 | const char *errmsg = ""; | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4608 | /* Offsets from q for retrieving byte pairs in the right order. */ | 
|  | 4609 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN | 
|  | 4610 | int ihi = 1, ilo = 0; | 
|  | 4611 | #else | 
|  | 4612 | int ihi = 0, ilo = 1; | 
|  | 4613 | #endif | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4614 | PyObject *errorHandler = NULL; | 
|  | 4615 | PyObject *exc = NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4616 |  | 
|  | 4617 | /* Note: size will always be longer than the resulting Unicode | 
|  | 4618 | character count */ | 
|  | 4619 | unicode = _PyUnicode_New(size); | 
|  | 4620 | if (!unicode) | 
|  | 4621 | return NULL; | 
|  | 4622 | if (size == 0) | 
|  | 4623 | return (PyObject *)unicode; | 
|  | 4624 |  | 
|  | 4625 | /* Unpack UTF-16 encoded data */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4626 | p = PyUnicode_AS_UNICODE(unicode); | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4627 | q = (unsigned char *)s; | 
| Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4628 | e = q + size - 1; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4629 |  | 
|  | 4630 | if (byteorder) | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4631 | bo = *byteorder; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4632 |  | 
| Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 4633 | /* Check for BOM marks (U+FEFF) in the input and adjust current | 
|  | 4634 | byte order setting accordingly. In native mode, the leading BOM | 
|  | 4635 | mark is skipped, in all other modes, it is copied to the output | 
|  | 4636 | stream as-is (giving a ZWNBSP character). */ | 
|  | 4637 | if (bo == 0) { | 
| Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4638 | if (size >= 2) { | 
|  | 4639 | const Py_UNICODE bom = (q[ihi] << 8) | q[ilo]; | 
| Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 4640 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4641 | if (bom == 0xFEFF) { | 
|  | 4642 | q += 2; | 
|  | 4643 | bo = -1; | 
|  | 4644 | } | 
|  | 4645 | else if (bom == 0xFFFE) { | 
|  | 4646 | q += 2; | 
|  | 4647 | bo = 1; | 
|  | 4648 | } | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4649 | #else | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4650 | if (bom == 0xFEFF) { | 
|  | 4651 | q += 2; | 
|  | 4652 | bo = 1; | 
|  | 4653 | } | 
|  | 4654 | else if (bom == 0xFFFE) { | 
|  | 4655 | q += 2; | 
|  | 4656 | bo = -1; | 
|  | 4657 | } | 
| Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 4658 | #endif | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4659 | } | 
| Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 4660 | } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4661 |  | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4662 | if (bo == -1) { | 
|  | 4663 | /* force LE */ | 
|  | 4664 | ihi = 1; | 
|  | 4665 | ilo = 0; | 
|  | 4666 | } | 
|  | 4667 | else if (bo == 1) { | 
|  | 4668 | /* force BE */ | 
|  | 4669 | ihi = 0; | 
|  | 4670 | ilo = 1; | 
|  | 4671 | } | 
| Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4672 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN | 
|  | 4673 | native_ordering = ilo < ihi; | 
|  | 4674 | #else | 
|  | 4675 | native_ordering = ilo > ihi; | 
|  | 4676 | #endif | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4677 |  | 
| Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4678 | aligned_end = (const unsigned char *) ((size_t) e & ~LONG_PTR_MASK); | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4679 | while (q < e) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4680 | Py_UNICODE ch; | 
| Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4681 | /* First check for possible aligned read of a C 'long'. Unaligned | 
|  | 4682 | reads are more expensive, better to defer to another iteration. */ | 
|  | 4683 | if (!((size_t) q & LONG_PTR_MASK)) { | 
|  | 4684 | /* Fast path for runs of non-surrogate chars. */ | 
|  | 4685 | register const unsigned char *_q = q; | 
|  | 4686 | Py_UNICODE *_p = p; | 
|  | 4687 | if (native_ordering) { | 
|  | 4688 | /* Native ordering is simple: as long as the input cannot | 
|  | 4689 | possibly contain a surrogate char, do an unrolled copy | 
|  | 4690 | of several 16-bit code points to the target object. | 
|  | 4691 | The non-surrogate check is done on several input bytes | 
|  | 4692 | at a time (as many as a C 'long' can contain). */ | 
|  | 4693 | while (_q < aligned_end) { | 
|  | 4694 | unsigned long data = * (unsigned long *) _q; | 
|  | 4695 | if (data & FAST_CHAR_MASK) | 
|  | 4696 | break; | 
|  | 4697 | _p[0] = ((unsigned short *) _q)[0]; | 
|  | 4698 | _p[1] = ((unsigned short *) _q)[1]; | 
|  | 4699 | #if (SIZEOF_LONG == 8) | 
|  | 4700 | _p[2] = ((unsigned short *) _q)[2]; | 
|  | 4701 | _p[3] = ((unsigned short *) _q)[3]; | 
|  | 4702 | #endif | 
|  | 4703 | _q += SIZEOF_LONG; | 
|  | 4704 | _p += SIZEOF_LONG / 2; | 
|  | 4705 | } | 
|  | 4706 | } | 
|  | 4707 | else { | 
|  | 4708 | /* Byteswapped ordering is similar, but we must decompose | 
|  | 4709 | the copy bytewise, and take care of zero'ing out the | 
|  | 4710 | upper bytes if the target object is in 32-bit units | 
|  | 4711 | (that is, in UCS-4 builds). */ | 
|  | 4712 | while (_q < aligned_end) { | 
|  | 4713 | unsigned long data = * (unsigned long *) _q; | 
|  | 4714 | if (data & SWAPPED_FAST_CHAR_MASK) | 
|  | 4715 | break; | 
|  | 4716 | /* Zero upper bytes in UCS-4 builds */ | 
|  | 4717 | #if (Py_UNICODE_SIZE > 2) | 
|  | 4718 | _p[0] = 0; | 
|  | 4719 | _p[1] = 0; | 
|  | 4720 | #if (SIZEOF_LONG == 8) | 
|  | 4721 | _p[2] = 0; | 
|  | 4722 | _p[3] = 0; | 
|  | 4723 | #endif | 
|  | 4724 | #endif | 
| Antoine Pitrou | d6e8de1 | 2009-01-11 23:56:55 +0000 | [diff] [blame] | 4725 | /* Issue #4916; UCS-4 builds on big endian machines must | 
|  | 4726 | fill the two last bytes of each 4-byte unit. */ | 
|  | 4727 | #if (!defined(BYTEORDER_IS_LITTLE_ENDIAN) && Py_UNICODE_SIZE > 2) | 
|  | 4728 | # define OFF 2 | 
|  | 4729 | #else | 
|  | 4730 | # define OFF 0 | 
| Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4731 | #endif | 
| Antoine Pitrou | d6e8de1 | 2009-01-11 23:56:55 +0000 | [diff] [blame] | 4732 | ((unsigned char *) _p)[OFF + 1] = _q[0]; | 
|  | 4733 | ((unsigned char *) _p)[OFF + 0] = _q[1]; | 
|  | 4734 | ((unsigned char *) _p)[OFF + 1 + Py_UNICODE_SIZE] = _q[2]; | 
|  | 4735 | ((unsigned char *) _p)[OFF + 0 + Py_UNICODE_SIZE] = _q[3]; | 
|  | 4736 | #if (SIZEOF_LONG == 8) | 
|  | 4737 | ((unsigned char *) _p)[OFF + 1 + 2 * Py_UNICODE_SIZE] = _q[4]; | 
|  | 4738 | ((unsigned char *) _p)[OFF + 0 + 2 * Py_UNICODE_SIZE] = _q[5]; | 
|  | 4739 | ((unsigned char *) _p)[OFF + 1 + 3 * Py_UNICODE_SIZE] = _q[6]; | 
|  | 4740 | ((unsigned char *) _p)[OFF + 0 + 3 * Py_UNICODE_SIZE] = _q[7]; | 
|  | 4741 | #endif | 
|  | 4742 | #undef OFF | 
| Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4743 | _q += SIZEOF_LONG; | 
|  | 4744 | _p += SIZEOF_LONG / 2; | 
|  | 4745 | } | 
|  | 4746 | } | 
|  | 4747 | p = _p; | 
|  | 4748 | q = _q; | 
|  | 4749 | if (q >= e) | 
|  | 4750 | break; | 
|  | 4751 | } | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4752 | ch = (q[ihi] << 8) | q[ilo]; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4753 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4754 | q += 2; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4755 |  | 
|  | 4756 | if (ch < 0xD800 || ch > 0xDFFF) { | 
|  | 4757 | *p++ = ch; | 
|  | 4758 | continue; | 
|  | 4759 | } | 
|  | 4760 |  | 
|  | 4761 | /* UTF-16 code pair: */ | 
|  | 4762 | if (q > e) { | 
|  | 4763 | errmsg = "unexpected end of data"; | 
|  | 4764 | startinpos = (((const char *)q) - 2) - starts; | 
|  | 4765 | endinpos = ((const char *)e) + 1 - starts; | 
|  | 4766 | goto utf16Error; | 
|  | 4767 | } | 
|  | 4768 | if (0xD800 <= ch && ch <= 0xDBFF) { | 
|  | 4769 | Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; | 
|  | 4770 | q += 2; | 
|  | 4771 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { | 
| Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 4772 | #ifndef Py_UNICODE_WIDE | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4773 | *p++ = ch; | 
|  | 4774 | *p++ = ch2; | 
| Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 4775 | #else | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4776 | *p++ = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; | 
| Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 4777 | #endif | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4778 | continue; | 
|  | 4779 | } | 
|  | 4780 | else { | 
| Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 4781 | errmsg = "illegal UTF-16 surrogate"; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4782 | startinpos = (((const char *)q)-4)-starts; | 
|  | 4783 | endinpos = startinpos+2; | 
|  | 4784 | goto utf16Error; | 
|  | 4785 | } | 
|  | 4786 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4787 | } | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4788 | errmsg = "illegal encoding"; | 
|  | 4789 | startinpos = (((const char *)q)-2)-starts; | 
|  | 4790 | endinpos = startinpos+2; | 
|  | 4791 | /* Fall through to report the error */ | 
| Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 4792 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4793 | utf16Error: | 
|  | 4794 | outpos = p - PyUnicode_AS_UNICODE(unicode); | 
|  | 4795 | if (unicode_decode_call_errorhandler( | 
| Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4796 | errors, | 
|  | 4797 | &errorHandler, | 
|  | 4798 | "utf16", errmsg, | 
|  | 4799 | &starts, | 
|  | 4800 | (const char **)&e, | 
|  | 4801 | &startinpos, | 
|  | 4802 | &endinpos, | 
|  | 4803 | &exc, | 
|  | 4804 | (const char **)&q, | 
|  | 4805 | &unicode, | 
|  | 4806 | &outpos, | 
|  | 4807 | &p)) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4808 | goto onError; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4809 | } | 
| Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4810 | /* remaining byte at the end? (size should be even) */ | 
|  | 4811 | if (e == q) { | 
|  | 4812 | if (!consumed) { | 
|  | 4813 | errmsg = "truncated data"; | 
|  | 4814 | startinpos = ((const char *)q) - starts; | 
|  | 4815 | endinpos = ((const char *)e) + 1 - starts; | 
|  | 4816 | outpos = p - PyUnicode_AS_UNICODE(unicode); | 
|  | 4817 | if (unicode_decode_call_errorhandler( | 
|  | 4818 | errors, | 
|  | 4819 | &errorHandler, | 
|  | 4820 | "utf16", errmsg, | 
|  | 4821 | &starts, | 
|  | 4822 | (const char **)&e, | 
|  | 4823 | &startinpos, | 
|  | 4824 | &endinpos, | 
|  | 4825 | &exc, | 
|  | 4826 | (const char **)&q, | 
|  | 4827 | &unicode, | 
|  | 4828 | &outpos, | 
|  | 4829 | &p)) | 
|  | 4830 | goto onError; | 
|  | 4831 | /* The remaining input chars are ignored if the callback | 
|  | 4832 | chooses to skip the input */ | 
|  | 4833 | } | 
|  | 4834 | } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4835 |  | 
|  | 4836 | if (byteorder) | 
|  | 4837 | *byteorder = bo; | 
|  | 4838 |  | 
| Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4839 | if (consumed) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4840 | *consumed = (const char *)q-starts; | 
| Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4841 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4842 | /* Adjust length */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4843 | if (_PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4844 | goto onError; | 
|  | 4845 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4846 | Py_XDECREF(errorHandler); | 
|  | 4847 | Py_XDECREF(exc); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4848 | if (PyUnicode_READY(unicode) == -1) { | 
|  | 4849 | Py_DECREF(unicode); | 
|  | 4850 | return NULL; | 
|  | 4851 | } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4852 | return (PyObject *)unicode; | 
|  | 4853 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4854 | onError: | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4855 | Py_DECREF(unicode); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4856 | Py_XDECREF(errorHandler); | 
|  | 4857 | Py_XDECREF(exc); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4858 | return NULL; | 
|  | 4859 | } | 
|  | 4860 |  | 
| Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4861 | #undef FAST_CHAR_MASK | 
|  | 4862 | #undef SWAPPED_FAST_CHAR_MASK | 
|  | 4863 |  | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4864 | PyObject * | 
|  | 4865 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4866 | Py_ssize_t size, | 
|  | 4867 | const char *errors, | 
|  | 4868 | int byteorder) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4869 | { | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4870 | PyObject *v; | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4871 | unsigned char *p; | 
| Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4872 | Py_ssize_t nsize, bytesize; | 
| Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 4873 | #ifdef Py_UNICODE_WIDE | 
| Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4874 | Py_ssize_t i, pairs; | 
| Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 4875 | #else | 
|  | 4876 | const int pairs = 0; | 
|  | 4877 | #endif | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4878 | /* Offsets from p for storing byte pairs in the right order. */ | 
|  | 4879 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN | 
|  | 4880 | int ihi = 1, ilo = 0; | 
|  | 4881 | #else | 
|  | 4882 | int ihi = 0, ilo = 1; | 
|  | 4883 | #endif | 
|  | 4884 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4885 | #define STORECHAR(CH)                           \ | 
|  | 4886 | do {                                        \ | 
|  | 4887 | p[ihi] = ((CH) >> 8) & 0xff;            \ | 
|  | 4888 | p[ilo] = (CH) & 0xff;                   \ | 
|  | 4889 | p += 2;                                 \ | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4890 | } while(0) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4891 |  | 
| Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 4892 | #ifdef Py_UNICODE_WIDE | 
| Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 4893 | for (i = pairs = 0; i < size; i++) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4894 | if (s[i] >= 0x10000) | 
|  | 4895 | pairs++; | 
| Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 4896 | #endif | 
| Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4897 | /* 2 * (size + pairs + (byteorder == 0)) */ | 
|  | 4898 | if (size > PY_SSIZE_T_MAX || | 
|  | 4899 | size > PY_SSIZE_T_MAX - pairs - (byteorder == 0)) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4900 | return PyErr_NoMemory(); | 
| Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4901 | nsize = size + pairs + (byteorder == 0); | 
|  | 4902 | bytesize = nsize * 2; | 
|  | 4903 | if (bytesize / 2 != nsize) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4904 | return PyErr_NoMemory(); | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4905 | v = PyBytes_FromStringAndSize(NULL, bytesize); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4906 | if (v == NULL) | 
|  | 4907 | return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4908 |  | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4909 | p = (unsigned char *)PyBytes_AS_STRING(v); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4910 | if (byteorder == 0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4911 | STORECHAR(0xFEFF); | 
| Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 4912 | if (size == 0) | 
| Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4913 | goto done; | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4914 |  | 
|  | 4915 | if (byteorder == -1) { | 
|  | 4916 | /* force LE */ | 
|  | 4917 | ihi = 1; | 
|  | 4918 | ilo = 0; | 
|  | 4919 | } | 
|  | 4920 | else if (byteorder == 1) { | 
|  | 4921 | /* force BE */ | 
|  | 4922 | ihi = 0; | 
|  | 4923 | ilo = 1; | 
|  | 4924 | } | 
|  | 4925 |  | 
| Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 4926 | while (size-- > 0) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4927 | Py_UNICODE ch = *s++; | 
|  | 4928 | Py_UNICODE ch2 = 0; | 
| Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 4929 | #ifdef Py_UNICODE_WIDE | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4930 | if (ch >= 0x10000) { | 
|  | 4931 | ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); | 
|  | 4932 | ch  = 0xD800 | ((ch-0x10000) >> 10); | 
|  | 4933 | } | 
| Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 4934 | #endif | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4935 | STORECHAR(ch); | 
|  | 4936 | if (ch2) | 
|  | 4937 | STORECHAR(ch2); | 
| Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 4938 | } | 
| Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4939 |  | 
|  | 4940 | done: | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4941 | return v; | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4942 | #undef STORECHAR | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4943 | } | 
|  | 4944 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4945 | PyObject * | 
|  | 4946 | PyUnicode_AsUTF16String(PyObject *unicode) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4947 | { | 
|  | 4948 | if (!PyUnicode_Check(unicode)) { | 
|  | 4949 | PyErr_BadArgument(); | 
|  | 4950 | return NULL; | 
|  | 4951 | } | 
|  | 4952 | return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode), | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4953 | PyUnicode_GET_SIZE(unicode), | 
|  | 4954 | NULL, | 
|  | 4955 | 0); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4956 | } | 
|  | 4957 |  | 
|  | 4958 | /* --- Unicode Escape Codec ----------------------------------------------- */ | 
|  | 4959 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4960 | /* Helper function for PyUnicode_DecodeUnicodeEscape, determines | 
|  | 4961 | if all the escapes in the string make it still a valid ASCII string. | 
|  | 4962 | Returns -1 if any escapes were found which cause the string to | 
|  | 4963 | pop out of ASCII range.  Otherwise returns the length of the | 
|  | 4964 | required buffer to hold the string. | 
|  | 4965 | */ | 
|  | 4966 | Py_ssize_t | 
|  | 4967 | length_of_escaped_ascii_string(const char *s, Py_ssize_t size) | 
|  | 4968 | { | 
|  | 4969 | const unsigned char *p = (const unsigned char *)s; | 
|  | 4970 | const unsigned char *end = p + size; | 
|  | 4971 | Py_ssize_t length = 0; | 
|  | 4972 |  | 
|  | 4973 | if (size < 0) | 
|  | 4974 | return -1; | 
|  | 4975 |  | 
|  | 4976 | for (; p < end; ++p) { | 
|  | 4977 | if (*p > 127) { | 
|  | 4978 | /* Non-ASCII */ | 
|  | 4979 | return -1; | 
|  | 4980 | } | 
|  | 4981 | else if (*p != '\\') { | 
|  | 4982 | /* Normal character */ | 
|  | 4983 | ++length; | 
|  | 4984 | } | 
|  | 4985 | else { | 
|  | 4986 | /* Backslash-escape, check next char */ | 
|  | 4987 | ++p; | 
|  | 4988 | /* Escape sequence reaches till end of string or | 
|  | 4989 | non-ASCII follow-up. */ | 
|  | 4990 | if (p >= end || *p > 127) | 
|  | 4991 | return -1; | 
|  | 4992 | switch (*p) { | 
|  | 4993 | case '\n': | 
|  | 4994 | /* backslash + \n result in zero characters */ | 
|  | 4995 | break; | 
|  | 4996 | case '\\': case '\'': case '\"': | 
|  | 4997 | case 'b': case 'f': case 't': | 
|  | 4998 | case 'n': case 'r': case 'v': case 'a': | 
|  | 4999 | ++length; | 
|  | 5000 | break; | 
|  | 5001 | case '0': case '1': case '2': case '3': | 
|  | 5002 | case '4': case '5': case '6': case '7': | 
|  | 5003 | case 'x': case 'u': case 'U': case 'N': | 
|  | 5004 | /* these do not guarantee ASCII characters */ | 
|  | 5005 | return -1; | 
|  | 5006 | default: | 
|  | 5007 | /* count the backslash + the other character */ | 
|  | 5008 | length += 2; | 
|  | 5009 | } | 
|  | 5010 | } | 
|  | 5011 | } | 
|  | 5012 | return length; | 
|  | 5013 | } | 
|  | 5014 |  | 
|  | 5015 | /* Similar to PyUnicode_WRITE but either write into wstr field | 
|  | 5016 | or treat string as ASCII. */ | 
|  | 5017 | #define WRITE_ASCII_OR_WSTR(kind, buf, index, value) \ | 
|  | 5018 | do { \ | 
|  | 5019 | if ((kind) != PyUnicode_WCHAR_KIND) \ | 
|  | 5020 | ((unsigned char *)(buf))[(index)] = (unsigned char)(value); \ | 
|  | 5021 | else \ | 
|  | 5022 | ((Py_UNICODE *)(buf))[(index)] = (Py_UNICODE)(value); \ | 
|  | 5023 | } while (0) | 
|  | 5024 |  | 
|  | 5025 | #define WRITE_WSTR(buf, index, value) \ | 
|  | 5026 | assert(kind == PyUnicode_WCHAR_KIND), \ | 
|  | 5027 | ((Py_UNICODE *)(buf))[(index)] = (Py_UNICODE)(value) | 
|  | 5028 |  | 
|  | 5029 |  | 
| Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5030 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; | 
| Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 5031 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5032 | PyObject * | 
|  | 5033 | PyUnicode_DecodeUnicodeEscape(const char *s, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5034 | Py_ssize_t size, | 
| Victor Stinner | c17f540 | 2011-09-29 00:16:58 +0200 | [diff] [blame] | 5035 | const char *errors) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5036 | { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5037 | const char *starts = s; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5038 | Py_ssize_t startinpos; | 
|  | 5039 | Py_ssize_t endinpos; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5040 | int j; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5041 | PyUnicodeObject *v; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5042 | Py_UNICODE *p; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5043 | const char *end; | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5044 | char* message; | 
|  | 5045 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5046 | PyObject *errorHandler = NULL; | 
|  | 5047 | PyObject *exc = NULL; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5048 | Py_ssize_t ascii_length; | 
|  | 5049 | Py_ssize_t i; | 
|  | 5050 | int kind; | 
|  | 5051 | void *data; | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5052 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5053 | ascii_length = length_of_escaped_ascii_string(s, size); | 
|  | 5054 |  | 
|  | 5055 | /* After length_of_escaped_ascii_string() there are two alternatives, | 
|  | 5056 | either the string is pure ASCII with named escapes like \n, etc. | 
|  | 5057 | and we determined it's exact size (common case) | 
|  | 5058 | or it contains \x, \u, ... escape sequences.  then we create a | 
|  | 5059 | legacy wchar string and resize it at the end of this function. */ | 
|  | 5060 | if (ascii_length >= 0) { | 
|  | 5061 | v = (PyUnicodeObject *)PyUnicode_New(ascii_length, 127); | 
|  | 5062 | if (!v) | 
|  | 5063 | goto onError; | 
|  | 5064 | assert(PyUnicode_KIND(v) == PyUnicode_1BYTE_KIND); | 
|  | 5065 | kind = PyUnicode_1BYTE_KIND; | 
|  | 5066 | data = PyUnicode_DATA(v); | 
|  | 5067 | } | 
|  | 5068 | else { | 
|  | 5069 | /* Escaped strings will always be longer than the resulting | 
|  | 5070 | Unicode string, so we start with size here and then reduce the | 
|  | 5071 | length after conversion to the true value. | 
|  | 5072 | (but if the error callback returns a long replacement string | 
|  | 5073 | we'll have to allocate more space) */ | 
|  | 5074 | v = _PyUnicode_New(size); | 
|  | 5075 | if (!v) | 
|  | 5076 | goto onError; | 
|  | 5077 | kind = PyUnicode_WCHAR_KIND; | 
|  | 5078 | data = PyUnicode_AS_UNICODE(v); | 
|  | 5079 | } | 
|  | 5080 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5081 | if (size == 0) | 
|  | 5082 | return (PyObject *)v; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5083 | i = 0; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5084 | end = s + size; | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5085 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5086 | while (s < end) { | 
|  | 5087 | unsigned char c; | 
| Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 5088 | Py_UNICODE x; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5089 | int digits; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5090 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5091 | if (kind == PyUnicode_WCHAR_KIND) { | 
|  | 5092 | assert(i < _PyUnicode_WSTR_LENGTH(v)); | 
|  | 5093 | } | 
|  | 5094 | else { | 
|  | 5095 | /* The only case in which i == ascii_length is a backslash | 
|  | 5096 | followed by a newline. */ | 
|  | 5097 | assert(i <= ascii_length); | 
|  | 5098 | } | 
|  | 5099 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5100 | /* Non-escape characters are interpreted as Unicode ordinals */ | 
|  | 5101 | if (*s != '\\') { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5102 | WRITE_ASCII_OR_WSTR(kind, data, i++, (unsigned char) *s++); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5103 | continue; | 
|  | 5104 | } | 
|  | 5105 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5106 | startinpos = s-starts; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5107 | /* \ - Escapes */ | 
|  | 5108 | s++; | 
| Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5109 | c = *s++; | 
|  | 5110 | if (s > end) | 
|  | 5111 | c = '\0'; /* Invalid after \ */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5112 |  | 
|  | 5113 | if (kind == PyUnicode_WCHAR_KIND) { | 
|  | 5114 | assert(i < _PyUnicode_WSTR_LENGTH(v)); | 
|  | 5115 | } | 
|  | 5116 | else { | 
|  | 5117 | /* The only case in which i == ascii_length is a backslash | 
|  | 5118 | followed by a newline. */ | 
|  | 5119 | assert(i < ascii_length || (i == ascii_length && c == '\n')); | 
|  | 5120 | } | 
|  | 5121 |  | 
| Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5122 | switch (c) { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5123 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5124 | /* \x escapes */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5125 | case '\n': break; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5126 | case '\\': WRITE_ASCII_OR_WSTR(kind, data, i++, '\\'); break; | 
|  | 5127 | case '\'': WRITE_ASCII_OR_WSTR(kind, data, i++, '\''); break; | 
|  | 5128 | case '\"': WRITE_ASCII_OR_WSTR(kind, data, i++, '\"'); break; | 
|  | 5129 | case 'b': WRITE_ASCII_OR_WSTR(kind, data, i++, '\b'); break; | 
|  | 5130 | /* FF */ | 
|  | 5131 | case 'f': WRITE_ASCII_OR_WSTR(kind, data, i++, '\014'); break; | 
|  | 5132 | case 't': WRITE_ASCII_OR_WSTR(kind, data, i++, '\t'); break; | 
|  | 5133 | case 'n': WRITE_ASCII_OR_WSTR(kind, data, i++, '\n'); break; | 
|  | 5134 | case 'r': WRITE_ASCII_OR_WSTR(kind, data, i++, '\r'); break; | 
|  | 5135 | /* VT */ | 
|  | 5136 | case 'v': WRITE_ASCII_OR_WSTR(kind, data, i++, '\013'); break; | 
|  | 5137 | /* BEL, not classic C */ | 
|  | 5138 | case 'a': WRITE_ASCII_OR_WSTR(kind, data, i++, '\007'); break; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5139 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5140 | /* \OOO (octal) escapes */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5141 | case '0': case '1': case '2': case '3': | 
|  | 5142 | case '4': case '5': case '6': case '7': | 
| Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5143 | x = s[-1] - '0'; | 
| Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5144 | if (s < end && '0' <= *s && *s <= '7') { | 
| Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5145 | x = (x<<3) + *s++ - '0'; | 
| Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5146 | if (s < end && '0' <= *s && *s <= '7') | 
| Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5147 | x = (x<<3) + *s++ - '0'; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5148 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5149 | WRITE_WSTR(data, i++, x); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5150 | break; | 
|  | 5151 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5152 | /* hex escapes */ | 
|  | 5153 | /* \xXX */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5154 | case 'x': | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5155 | digits = 2; | 
|  | 5156 | message = "truncated \\xXX escape"; | 
|  | 5157 | goto hexescape; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5158 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5159 | /* \uXXXX */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5160 | case 'u': | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5161 | digits = 4; | 
|  | 5162 | message = "truncated \\uXXXX escape"; | 
|  | 5163 | goto hexescape; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5164 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5165 | /* \UXXXXXXXX */ | 
| Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5166 | case 'U': | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5167 | digits = 8; | 
|  | 5168 | message = "truncated \\UXXXXXXXX escape"; | 
|  | 5169 | hexescape: | 
|  | 5170 | chr = 0; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5171 | p = PyUnicode_AS_UNICODE(v) + i; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5172 | if (s+digits>end) { | 
|  | 5173 | endinpos = size; | 
|  | 5174 | if (unicode_decode_call_errorhandler( | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5175 | errors, &errorHandler, | 
|  | 5176 | "unicodeescape", "end of string in escape sequence", | 
|  | 5177 | &starts, &end, &startinpos, &endinpos, &exc, &s, | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5178 | &v, &i, &p)) | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5179 | goto onError; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5180 | data = PyUnicode_AS_UNICODE(v); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5181 | goto nextByte; | 
|  | 5182 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5183 | for (j = 0; j < digits; ++j) { | 
|  | 5184 | c = (unsigned char) s[j]; | 
| David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 5185 | if (!Py_ISXDIGIT(c)) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5186 | endinpos = (s+j+1)-starts; | 
|  | 5187 | p = PyUnicode_AS_UNICODE(v) + i; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5188 | if (unicode_decode_call_errorhandler( | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5189 | errors, &errorHandler, | 
|  | 5190 | "unicodeescape", message, | 
|  | 5191 | &starts, &end, &startinpos, &endinpos, &exc, &s, | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5192 | &v, &i, &p)) | 
| Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5193 | goto onError; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5194 | data = PyUnicode_AS_UNICODE(v); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5195 | goto nextByte; | 
| Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5196 | } | 
|  | 5197 | chr = (chr<<4) & ~0xF; | 
|  | 5198 | if (c >= '0' && c <= '9') | 
|  | 5199 | chr += c - '0'; | 
|  | 5200 | else if (c >= 'a' && c <= 'f') | 
|  | 5201 | chr += 10 + c - 'a'; | 
|  | 5202 | else | 
|  | 5203 | chr += 10 + c - 'A'; | 
|  | 5204 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5205 | s += j; | 
| Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 5206 | if (chr == 0xffffffff && PyErr_Occurred()) | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5207 | /* _decoding_error will have already written into the | 
|  | 5208 | target buffer. */ | 
|  | 5209 | break; | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5210 | store: | 
| Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5211 | /* when we get here, chr is a 32-bit unicode character */ | 
|  | 5212 | if (chr <= 0xffff) | 
|  | 5213 | /* UCS-2 character */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5214 | WRITE_WSTR(data, i++, chr); | 
| Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5215 | else if (chr <= 0x10ffff) { | 
| Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5216 | /* UCS-4 character. Either store directly, or as | 
| Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5217 | surrogate pair. */ | 
| Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 5218 | #ifdef Py_UNICODE_WIDE | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5219 | WRITE_WSTR(data, i++, chr); | 
| Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5220 | #else | 
| Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5221 | chr -= 0x10000L; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5222 | WRITE_WSTR(data, i++, 0xD800 + (Py_UNICODE) (chr >> 10)); | 
|  | 5223 | WRITE_WSTR(data, i++, 0xDC00 + (Py_UNICODE) (chr & 0x03FF)); | 
| Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5224 | #endif | 
| Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5225 | } else { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5226 | endinpos = s-starts; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5227 | p = PyUnicode_AS_UNICODE(v) + i; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5228 | if (unicode_decode_call_errorhandler( | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5229 | errors, &errorHandler, | 
|  | 5230 | "unicodeescape", "illegal Unicode character", | 
|  | 5231 | &starts, &end, &startinpos, &endinpos, &exc, &s, | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5232 | &v, &i, &p)) | 
| Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5233 | goto onError; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5234 | data = PyUnicode_AS_UNICODE(v); | 
| Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5235 | } | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5236 | break; | 
|  | 5237 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5238 | /* \N{name} */ | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5239 | case 'N': | 
|  | 5240 | message = "malformed \\N character escape"; | 
|  | 5241 | if (ucnhash_CAPI == NULL) { | 
|  | 5242 | /* load the unicode data module */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5243 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import( | 
|  | 5244 | PyUnicodeData_CAPSULE_NAME, 1); | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5245 | if (ucnhash_CAPI == NULL) | 
|  | 5246 | goto ucnhashError; | 
|  | 5247 | } | 
|  | 5248 | if (*s == '{') { | 
|  | 5249 | const char *start = s+1; | 
|  | 5250 | /* look for the closing brace */ | 
|  | 5251 | while (*s != '}' && s < end) | 
|  | 5252 | s++; | 
|  | 5253 | if (s > start && s < end && *s == '}') { | 
|  | 5254 | /* found a name.  look it up in the unicode database */ | 
|  | 5255 | message = "unknown Unicode character name"; | 
|  | 5256 | s++; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5257 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), | 
|  | 5258 | &chr)) | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5259 | goto store; | 
|  | 5260 | } | 
|  | 5261 | } | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5262 | endinpos = s-starts; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5263 | p = PyUnicode_AS_UNICODE(v) + i; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5264 | if (unicode_decode_call_errorhandler( | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5265 | errors, &errorHandler, | 
|  | 5266 | "unicodeescape", message, | 
|  | 5267 | &starts, &end, &startinpos, &endinpos, &exc, &s, | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5268 | &v, &i, &p)) | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5269 | goto onError; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5270 | data = PyUnicode_AS_UNICODE(v); | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5271 | break; | 
|  | 5272 |  | 
|  | 5273 | default: | 
| Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5274 | if (s > end) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5275 | assert(kind == PyUnicode_WCHAR_KIND); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5276 | message = "\\ at end of string"; | 
|  | 5277 | s--; | 
|  | 5278 | endinpos = s-starts; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5279 | p = PyUnicode_AS_UNICODE(v) + i; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5280 | if (unicode_decode_call_errorhandler( | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5281 | errors, &errorHandler, | 
|  | 5282 | "unicodeescape", message, | 
|  | 5283 | &starts, &end, &startinpos, &endinpos, &exc, &s, | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5284 | &v, &i, &p)) | 
| Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5285 | goto onError; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5286 | data = PyUnicode_AS_UNICODE(v); | 
| Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5287 | } | 
|  | 5288 | else { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5289 | WRITE_ASCII_OR_WSTR(kind, data, i++, '\\'); | 
|  | 5290 | WRITE_ASCII_OR_WSTR(kind, data, i++, (unsigned char)s[-1]); | 
| Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5291 | } | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5292 | break; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5293 | } | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5294 | nextByte: | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5295 | ; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5296 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5297 | /* Ensure the length prediction worked in case of ASCII strings */ | 
|  | 5298 | assert(kind == PyUnicode_WCHAR_KIND || i == ascii_length); | 
|  | 5299 |  | 
|  | 5300 | if (kind == PyUnicode_WCHAR_KIND && (_PyUnicode_Resize(&v, i) < 0 || | 
|  | 5301 | PyUnicode_READY(v) == -1)) | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5302 | goto onError; | 
| Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 5303 | Py_XDECREF(errorHandler); | 
|  | 5304 | Py_XDECREF(exc); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5305 | return (PyObject *)v; | 
| Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5306 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5307 | ucnhashError: | 
| Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5308 | PyErr_SetString( | 
|  | 5309 | PyExc_UnicodeError, | 
|  | 5310 | "\\N escapes not supported (can't load unicodedata module)" | 
|  | 5311 | ); | 
| Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 5312 | Py_XDECREF(v); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5313 | Py_XDECREF(errorHandler); | 
|  | 5314 | Py_XDECREF(exc); | 
| Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 5315 | return NULL; | 
|  | 5316 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5317 | onError: | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5318 | Py_XDECREF(v); | 
| 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 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5324 | #undef WRITE_ASCII_OR_WSTR | 
|  | 5325 | #undef WRITE_WSTR | 
|  | 5326 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5327 | /* Return a Unicode-Escape string version of the Unicode object. | 
|  | 5328 |  | 
|  | 5329 | If quotes is true, the string is enclosed in u"" or u'' quotes as | 
|  | 5330 | appropriate. | 
|  | 5331 |  | 
|  | 5332 | */ | 
|  | 5333 |  | 
| Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5334 | static const char *hexdigits = "0123456789abcdef"; | 
|  | 5335 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5336 | PyObject * | 
|  | 5337 | PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5338 | Py_ssize_t size) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5339 | { | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5340 | PyObject *repr; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5341 | char *p; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5342 |  | 
| Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5343 | #ifdef Py_UNICODE_WIDE | 
|  | 5344 | const Py_ssize_t expandsize = 10; | 
|  | 5345 | #else | 
|  | 5346 | const Py_ssize_t expandsize = 6; | 
|  | 5347 | #endif | 
|  | 5348 |  | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5349 | /* XXX(nnorwitz): rather than over-allocating, it would be | 
|  | 5350 | better to choose a different scheme.  Perhaps scan the | 
|  | 5351 | first N-chars of the string and allocate based on that size. | 
|  | 5352 | */ | 
|  | 5353 | /* Initial allocation is based on the longest-possible unichr | 
|  | 5354 | escape. | 
|  | 5355 |  | 
|  | 5356 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source | 
|  | 5357 | unichr, so in this case it's the longest unichr escape. In | 
|  | 5358 | narrow (UTF-16) builds this is five chars per source unichr | 
|  | 5359 | since there are two unichrs in the surrogate pair, so in narrow | 
|  | 5360 | (UTF-16) builds it's not the longest unichr escape. | 
|  | 5361 |  | 
|  | 5362 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, | 
|  | 5363 | so in the narrow (UTF-16) build case it's the longest unichr | 
|  | 5364 | escape. | 
|  | 5365 | */ | 
|  | 5366 |  | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5367 | if (size == 0) | 
|  | 5368 | return PyBytes_FromStringAndSize(NULL, 0); | 
|  | 5369 |  | 
| Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5370 | if (size > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5371 | return PyErr_NoMemory(); | 
| Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5372 |  | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5373 | repr = PyBytes_FromStringAndSize(NULL, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5374 | 2 | 
|  | 5375 | + expandsize*size | 
|  | 5376 | + 1); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5377 | if (repr == NULL) | 
|  | 5378 | return NULL; | 
|  | 5379 |  | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5380 | p = PyBytes_AS_STRING(repr); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5381 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5382 | while (size-- > 0) { | 
|  | 5383 | Py_UNICODE ch = *s++; | 
| Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5384 |  | 
| Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5385 | /* Escape backslashes */ | 
|  | 5386 | if (ch == '\\') { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5387 | *p++ = '\\'; | 
|  | 5388 | *p++ = (char) ch; | 
| Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5389 | continue; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5390 | } | 
| Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5391 |  | 
| Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 5392 | #ifdef Py_UNICODE_WIDE | 
| Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5393 | /* Map 21-bit characters to '\U00xxxxxx' */ | 
|  | 5394 | else if (ch >= 0x10000) { | 
|  | 5395 | *p++ = '\\'; | 
|  | 5396 | *p++ = 'U'; | 
| Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5397 | *p++ = hexdigits[(ch >> 28) & 0x0000000F]; | 
|  | 5398 | *p++ = hexdigits[(ch >> 24) & 0x0000000F]; | 
|  | 5399 | *p++ = hexdigits[(ch >> 20) & 0x0000000F]; | 
|  | 5400 | *p++ = hexdigits[(ch >> 16) & 0x0000000F]; | 
|  | 5401 | *p++ = hexdigits[(ch >> 12) & 0x0000000F]; | 
|  | 5402 | *p++ = hexdigits[(ch >> 8) & 0x0000000F]; | 
|  | 5403 | *p++ = hexdigits[(ch >> 4) & 0x0000000F]; | 
|  | 5404 | *p++ = hexdigits[ch & 0x0000000F]; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5405 | continue; | 
| Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5406 | } | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5407 | #else | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5408 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ | 
|  | 5409 | else if (ch >= 0xD800 && ch < 0xDC00) { | 
|  | 5410 | Py_UNICODE ch2; | 
|  | 5411 | Py_UCS4 ucs; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5412 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5413 | ch2 = *s++; | 
|  | 5414 | size--; | 
| Georg Brandl | 78eef3de | 2010-08-01 20:51:02 +0000 | [diff] [blame] | 5415 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5416 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; | 
|  | 5417 | *p++ = '\\'; | 
|  | 5418 | *p++ = 'U'; | 
|  | 5419 | *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; | 
|  | 5420 | *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; | 
|  | 5421 | *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; | 
|  | 5422 | *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; | 
|  | 5423 | *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; | 
|  | 5424 | *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; | 
|  | 5425 | *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; | 
|  | 5426 | *p++ = hexdigits[ucs & 0x0000000F]; | 
|  | 5427 | continue; | 
|  | 5428 | } | 
|  | 5429 | /* Fall through: isolated surrogates are copied as-is */ | 
|  | 5430 | s--; | 
|  | 5431 | size++; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5432 | } | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5433 | #endif | 
| Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5434 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5435 | /* Map 16-bit characters to '\uxxxx' */ | 
| Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5436 | if (ch >= 256) { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5437 | *p++ = '\\'; | 
|  | 5438 | *p++ = 'u'; | 
| Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5439 | *p++ = hexdigits[(ch >> 12) & 0x000F]; | 
|  | 5440 | *p++ = hexdigits[(ch >> 8) & 0x000F]; | 
|  | 5441 | *p++ = hexdigits[(ch >> 4) & 0x000F]; | 
|  | 5442 | *p++ = hexdigits[ch & 0x000F]; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5443 | } | 
| Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5444 |  | 
| Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5445 | /* Map special whitespace to '\t', \n', '\r' */ | 
|  | 5446 | else if (ch == '\t') { | 
|  | 5447 | *p++ = '\\'; | 
|  | 5448 | *p++ = 't'; | 
|  | 5449 | } | 
|  | 5450 | else if (ch == '\n') { | 
|  | 5451 | *p++ = '\\'; | 
|  | 5452 | *p++ = 'n'; | 
|  | 5453 | } | 
|  | 5454 | else if (ch == '\r') { | 
|  | 5455 | *p++ = '\\'; | 
|  | 5456 | *p++ = 'r'; | 
|  | 5457 | } | 
| Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5458 |  | 
| Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5459 | /* Map non-printable US ASCII to '\xhh' */ | 
| Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 5460 | else if (ch < ' ' || ch >= 0x7F) { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5461 | *p++ = '\\'; | 
| Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5462 | *p++ = 'x'; | 
| Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5463 | *p++ = hexdigits[(ch >> 4) & 0x000F]; | 
|  | 5464 | *p++ = hexdigits[ch & 0x000F]; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5465 | } | 
| Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5466 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5467 | /* Copy everything else as-is */ | 
|  | 5468 | else | 
|  | 5469 | *p++ = (char) ch; | 
|  | 5470 | } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5471 |  | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5472 | assert(p - PyBytes_AS_STRING(repr) > 0); | 
|  | 5473 | if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) | 
|  | 5474 | return NULL; | 
|  | 5475 | return repr; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5476 | } | 
|  | 5477 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5478 | PyObject * | 
|  | 5479 | PyUnicode_AsUnicodeEscapeString(PyObject *unicode) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5480 | { | 
| Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 5481 | PyObject *s; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5482 | if (!PyUnicode_Check(unicode)) { | 
|  | 5483 | PyErr_BadArgument(); | 
|  | 5484 | return NULL; | 
|  | 5485 | } | 
| Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5486 | s = PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode), | 
|  | 5487 | PyUnicode_GET_SIZE(unicode)); | 
| Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 5488 | return s; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5489 | } | 
|  | 5490 |  | 
|  | 5491 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ | 
|  | 5492 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5493 | PyObject * | 
|  | 5494 | PyUnicode_DecodeRawUnicodeEscape(const char *s, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5495 | Py_ssize_t size, | 
|  | 5496 | const char *errors) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5497 | { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5498 | const char *starts = s; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5499 | Py_ssize_t startinpos; | 
|  | 5500 | Py_ssize_t endinpos; | 
|  | 5501 | Py_ssize_t outpos; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5502 | PyUnicodeObject *v; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5503 | Py_UNICODE *p; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5504 | const char *end; | 
|  | 5505 | const char *bs; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5506 | PyObject *errorHandler = NULL; | 
|  | 5507 | PyObject *exc = NULL; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5508 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5509 | /* Escaped strings will always be longer than the resulting | 
|  | 5510 | 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] | 5511 | length after conversion to the true value. (But decoding error | 
|  | 5512 | handler might have to resize the string) */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5513 | v = _PyUnicode_New(size); | 
|  | 5514 | if (v == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5515 | goto onError; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5516 | if (size == 0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5517 | return (PyObject *)v; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5518 | p = PyUnicode_AS_UNICODE(v); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5519 | end = s + size; | 
|  | 5520 | while (s < end) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5521 | unsigned char c; | 
|  | 5522 | Py_UCS4 x; | 
|  | 5523 | int i; | 
| Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5524 | int count; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5525 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5526 | /* Non-escape characters are interpreted as Unicode ordinals */ | 
|  | 5527 | if (*s != '\\') { | 
|  | 5528 | *p++ = (unsigned char)*s++; | 
|  | 5529 | continue; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5530 | } | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5531 | startinpos = s-starts; | 
|  | 5532 |  | 
|  | 5533 | /* \u-escapes are only interpreted iff the number of leading | 
|  | 5534 | backslashes if odd */ | 
|  | 5535 | bs = s; | 
|  | 5536 | for (;s < end;) { | 
|  | 5537 | if (*s != '\\') | 
|  | 5538 | break; | 
|  | 5539 | *p++ = (unsigned char)*s++; | 
|  | 5540 | } | 
|  | 5541 | if (((s - bs) & 1) == 0 || | 
|  | 5542 | s >= end || | 
|  | 5543 | (*s != 'u' && *s != 'U')) { | 
|  | 5544 | continue; | 
|  | 5545 | } | 
|  | 5546 | p--; | 
|  | 5547 | count = *s=='u' ? 4 : 8; | 
|  | 5548 | s++; | 
|  | 5549 |  | 
|  | 5550 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ | 
|  | 5551 | outpos = p-PyUnicode_AS_UNICODE(v); | 
|  | 5552 | for (x = 0, i = 0; i < count; ++i, ++s) { | 
|  | 5553 | c = (unsigned char)*s; | 
| David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 5554 | if (!Py_ISXDIGIT(c)) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5555 | endinpos = s-starts; | 
|  | 5556 | if (unicode_decode_call_errorhandler( | 
|  | 5557 | errors, &errorHandler, | 
|  | 5558 | "rawunicodeescape", "truncated \\uXXXX", | 
|  | 5559 | &starts, &end, &startinpos, &endinpos, &exc, &s, | 
|  | 5560 | &v, &outpos, &p)) | 
|  | 5561 | goto onError; | 
|  | 5562 | goto nextByte; | 
|  | 5563 | } | 
|  | 5564 | x = (x<<4) & ~0xF; | 
|  | 5565 | if (c >= '0' && c <= '9') | 
|  | 5566 | x += c - '0'; | 
|  | 5567 | else if (c >= 'a' && c <= 'f') | 
|  | 5568 | x += 10 + c - 'a'; | 
|  | 5569 | else | 
|  | 5570 | x += 10 + c - 'A'; | 
|  | 5571 | } | 
| Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5572 | if (x <= 0xffff) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5573 | /* UCS-2 character */ | 
|  | 5574 | *p++ = (Py_UNICODE) x; | 
| Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5575 | else if (x <= 0x10ffff) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5576 | /* UCS-4 character. Either store directly, or as | 
|  | 5577 | surrogate pair. */ | 
| Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5578 | #ifdef Py_UNICODE_WIDE | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5579 | *p++ = (Py_UNICODE) x; | 
| Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5580 | #else | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5581 | x -= 0x10000L; | 
|  | 5582 | *p++ = 0xD800 + (Py_UNICODE) (x >> 10); | 
|  | 5583 | *p++ = 0xDC00 + (Py_UNICODE) (x & 0x03FF); | 
| Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5584 | #endif | 
|  | 5585 | } else { | 
|  | 5586 | endinpos = s-starts; | 
|  | 5587 | outpos = p-PyUnicode_AS_UNICODE(v); | 
| Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5588 | if (unicode_decode_call_errorhandler( | 
|  | 5589 | errors, &errorHandler, | 
|  | 5590 | "rawunicodeescape", "\\Uxxxxxxxx out of range", | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5591 | &starts, &end, &startinpos, &endinpos, &exc, &s, | 
|  | 5592 | &v, &outpos, &p)) | 
|  | 5593 | goto onError; | 
| Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5594 | } | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5595 | nextByte: | 
|  | 5596 | ; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5597 | } | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5598 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5599 | goto onError; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5600 | Py_XDECREF(errorHandler); | 
|  | 5601 | Py_XDECREF(exc); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5602 | if (PyUnicode_READY(v) == -1) { | 
|  | 5603 | Py_DECREF(v); | 
|  | 5604 | return NULL; | 
|  | 5605 | } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5606 | return (PyObject *)v; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5607 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5608 | onError: | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5609 | Py_XDECREF(v); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5610 | Py_XDECREF(errorHandler); | 
|  | 5611 | Py_XDECREF(exc); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5612 | return NULL; | 
|  | 5613 | } | 
|  | 5614 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5615 | PyObject * | 
|  | 5616 | PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5617 | Py_ssize_t size) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5618 | { | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5619 | PyObject *repr; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5620 | char *p; | 
|  | 5621 | char *q; | 
|  | 5622 |  | 
| Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5623 | #ifdef Py_UNICODE_WIDE | 
| Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5624 | const Py_ssize_t expandsize = 10; | 
| Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5625 | #else | 
| Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5626 | const Py_ssize_t expandsize = 6; | 
| Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5627 | #endif | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5628 |  | 
| Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5629 | if (size > PY_SSIZE_T_MAX / expandsize) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5630 | return PyErr_NoMemory(); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5631 |  | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5632 | repr = PyBytes_FromStringAndSize(NULL, expandsize * size); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5633 | if (repr == NULL) | 
|  | 5634 | return NULL; | 
| Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 5635 | if (size == 0) | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5636 | return repr; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5637 |  | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5638 | p = q = PyBytes_AS_STRING(repr); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5639 | while (size-- > 0) { | 
|  | 5640 | Py_UNICODE ch = *s++; | 
| Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5641 | #ifdef Py_UNICODE_WIDE | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5642 | /* Map 32-bit characters to '\Uxxxxxxxx' */ | 
|  | 5643 | if (ch >= 0x10000) { | 
| Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5644 | *p++ = '\\'; | 
|  | 5645 | *p++ = 'U'; | 
| Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 5646 | *p++ = hexdigits[(ch >> 28) & 0xf]; | 
|  | 5647 | *p++ = hexdigits[(ch >> 24) & 0xf]; | 
|  | 5648 | *p++ = hexdigits[(ch >> 20) & 0xf]; | 
|  | 5649 | *p++ = hexdigits[(ch >> 16) & 0xf]; | 
|  | 5650 | *p++ = hexdigits[(ch >> 12) & 0xf]; | 
|  | 5651 | *p++ = hexdigits[(ch >> 8) & 0xf]; | 
|  | 5652 | *p++ = hexdigits[(ch >> 4) & 0xf]; | 
|  | 5653 | *p++ = hexdigits[ch & 15]; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5654 | } | 
| Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5655 | else | 
| Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5656 | #else | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5657 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ | 
|  | 5658 | if (ch >= 0xD800 && ch < 0xDC00) { | 
|  | 5659 | Py_UNICODE ch2; | 
|  | 5660 | Py_UCS4 ucs; | 
| Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5661 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5662 | ch2 = *s++; | 
|  | 5663 | size--; | 
| Georg Brandl | 78eef3de | 2010-08-01 20:51:02 +0000 | [diff] [blame] | 5664 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5665 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; | 
|  | 5666 | *p++ = '\\'; | 
|  | 5667 | *p++ = 'U'; | 
|  | 5668 | *p++ = hexdigits[(ucs >> 28) & 0xf]; | 
|  | 5669 | *p++ = hexdigits[(ucs >> 24) & 0xf]; | 
|  | 5670 | *p++ = hexdigits[(ucs >> 20) & 0xf]; | 
|  | 5671 | *p++ = hexdigits[(ucs >> 16) & 0xf]; | 
|  | 5672 | *p++ = hexdigits[(ucs >> 12) & 0xf]; | 
|  | 5673 | *p++ = hexdigits[(ucs >> 8) & 0xf]; | 
|  | 5674 | *p++ = hexdigits[(ucs >> 4) & 0xf]; | 
|  | 5675 | *p++ = hexdigits[ucs & 0xf]; | 
|  | 5676 | continue; | 
|  | 5677 | } | 
|  | 5678 | /* Fall through: isolated surrogates are copied as-is */ | 
|  | 5679 | s--; | 
|  | 5680 | size++; | 
|  | 5681 | } | 
| Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5682 | #endif | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5683 | /* Map 16-bit characters to '\uxxxx' */ | 
|  | 5684 | if (ch >= 256) { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5685 | *p++ = '\\'; | 
|  | 5686 | *p++ = 'u'; | 
| Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 5687 | *p++ = hexdigits[(ch >> 12) & 0xf]; | 
|  | 5688 | *p++ = hexdigits[(ch >> 8) & 0xf]; | 
|  | 5689 | *p++ = hexdigits[(ch >> 4) & 0xf]; | 
|  | 5690 | *p++ = hexdigits[ch & 15]; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5691 | } | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5692 | /* Copy everything else as-is */ | 
|  | 5693 | else | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5694 | *p++ = (char) ch; | 
|  | 5695 | } | 
| Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5696 | size = p - q; | 
|  | 5697 |  | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5698 | assert(size > 0); | 
|  | 5699 | if (_PyBytes_Resize(&repr, size) < 0) | 
|  | 5700 | return NULL; | 
|  | 5701 | return repr; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5702 | } | 
|  | 5703 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5704 | PyObject * | 
|  | 5705 | PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5706 | { | 
| Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 5707 | PyObject *s; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5708 | if (!PyUnicode_Check(unicode)) { | 
| Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 5709 | PyErr_BadArgument(); | 
|  | 5710 | return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5711 | } | 
| Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 5712 | s = PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode), | 
|  | 5713 | PyUnicode_GET_SIZE(unicode)); | 
|  | 5714 |  | 
| Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 5715 | return s; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5716 | } | 
|  | 5717 |  | 
| Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5718 | /* --- Unicode Internal Codec ------------------------------------------- */ | 
|  | 5719 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5720 | PyObject * | 
|  | 5721 | _PyUnicode_DecodeUnicodeInternal(const char *s, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5722 | Py_ssize_t size, | 
|  | 5723 | const char *errors) | 
| Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5724 | { | 
|  | 5725 | const char *starts = s; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5726 | Py_ssize_t startinpos; | 
|  | 5727 | Py_ssize_t endinpos; | 
|  | 5728 | Py_ssize_t outpos; | 
| Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5729 | PyUnicodeObject *v; | 
|  | 5730 | Py_UNICODE *p; | 
|  | 5731 | const char *end; | 
|  | 5732 | const char *reason; | 
|  | 5733 | PyObject *errorHandler = NULL; | 
|  | 5734 | PyObject *exc = NULL; | 
|  | 5735 |  | 
| Neal Norwitz | d43069c | 2006-01-08 01:12:10 +0000 | [diff] [blame] | 5736 | #ifdef Py_UNICODE_WIDE | 
|  | 5737 | Py_UNICODE unimax = PyUnicode_GetMax(); | 
|  | 5738 | #endif | 
|  | 5739 |  | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5740 | /* XXX overflow detection missing */ | 
| Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5741 | v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE); | 
|  | 5742 | if (v == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5743 | goto onError; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5744 | /* Intentionally PyUnicode_GET_SIZE instead of PyUnicode_GET_LENGTH | 
|  | 5745 | as string was created with the old API. */ | 
|  | 5746 | if (PyUnicode_GET_SIZE(v) == 0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5747 | return (PyObject *)v; | 
| Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5748 | p = PyUnicode_AS_UNICODE(v); | 
|  | 5749 | end = s + size; | 
|  | 5750 |  | 
|  | 5751 | while (s < end) { | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5752 | memcpy(p, s, sizeof(Py_UNICODE)); | 
| Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5753 | /* We have to sanity check the raw data, otherwise doom looms for | 
|  | 5754 | some malformed UCS-4 data. */ | 
|  | 5755 | if ( | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5756 | #ifdef Py_UNICODE_WIDE | 
| Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5757 | *p > unimax || *p < 0 || | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5758 | #endif | 
| Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5759 | end-s < Py_UNICODE_SIZE | 
|  | 5760 | ) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5761 | { | 
| Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5762 | startinpos = s - starts; | 
|  | 5763 | if (end-s < Py_UNICODE_SIZE) { | 
|  | 5764 | endinpos = end-starts; | 
|  | 5765 | reason = "truncated input"; | 
|  | 5766 | } | 
|  | 5767 | else { | 
|  | 5768 | endinpos = s - starts + Py_UNICODE_SIZE; | 
|  | 5769 | reason = "illegal code point (> 0x10FFFF)"; | 
|  | 5770 | } | 
|  | 5771 | outpos = p - PyUnicode_AS_UNICODE(v); | 
|  | 5772 | if (unicode_decode_call_errorhandler( | 
|  | 5773 | errors, &errorHandler, | 
|  | 5774 | "unicode_internal", reason, | 
| Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 5775 | &starts, &end, &startinpos, &endinpos, &exc, &s, | 
| Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 5776 | &v, &outpos, &p)) { | 
| Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5777 | goto onError; | 
|  | 5778 | } | 
|  | 5779 | } | 
|  | 5780 | else { | 
|  | 5781 | p++; | 
|  | 5782 | s += Py_UNICODE_SIZE; | 
|  | 5783 | } | 
|  | 5784 | } | 
|  | 5785 |  | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5786 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) | 
| Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5787 | goto onError; | 
|  | 5788 | Py_XDECREF(errorHandler); | 
|  | 5789 | Py_XDECREF(exc); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5790 | if (PyUnicode_READY(v) == -1) { | 
|  | 5791 | Py_DECREF(v); | 
|  | 5792 | return NULL; | 
|  | 5793 | } | 
| Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5794 | return (PyObject *)v; | 
|  | 5795 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5796 | onError: | 
| Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5797 | Py_XDECREF(v); | 
|  | 5798 | Py_XDECREF(errorHandler); | 
|  | 5799 | Py_XDECREF(exc); | 
|  | 5800 | return NULL; | 
|  | 5801 | } | 
|  | 5802 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5803 | /* --- Latin-1 Codec ------------------------------------------------------ */ | 
|  | 5804 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5805 | PyObject * | 
|  | 5806 | PyUnicode_DecodeLatin1(const char *s, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5807 | Py_ssize_t size, | 
|  | 5808 | const char *errors) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5809 | { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5810 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ | 
| Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 5811 | return _PyUnicode_FromUCS1((unsigned char*)s, size); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5812 | } | 
|  | 5813 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5814 | /* create or adjust a UnicodeEncodeError */ | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5815 | static void | 
|  | 5816 | make_encode_exception(PyObject **exceptionObject, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5817 | const char *encoding, | 
|  | 5818 | const Py_UNICODE *unicode, Py_ssize_t size, | 
|  | 5819 | Py_ssize_t startpos, Py_ssize_t endpos, | 
|  | 5820 | const char *reason) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5821 | { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5822 | if (*exceptionObject == NULL) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5823 | *exceptionObject = PyUnicodeEncodeError_Create( | 
|  | 5824 | encoding, unicode, size, startpos, endpos, reason); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5825 | } | 
|  | 5826 | else { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5827 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) | 
|  | 5828 | goto onError; | 
|  | 5829 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) | 
|  | 5830 | goto onError; | 
|  | 5831 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) | 
|  | 5832 | goto onError; | 
|  | 5833 | return; | 
|  | 5834 | onError: | 
|  | 5835 | Py_DECREF(*exceptionObject); | 
|  | 5836 | *exceptionObject = NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5837 | } | 
|  | 5838 | } | 
|  | 5839 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5840 | /* raises a UnicodeEncodeError */ | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5841 | static void | 
|  | 5842 | raise_encode_exception(PyObject **exceptionObject, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5843 | const char *encoding, | 
|  | 5844 | const Py_UNICODE *unicode, Py_ssize_t size, | 
|  | 5845 | Py_ssize_t startpos, Py_ssize_t endpos, | 
|  | 5846 | const char *reason) | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5847 | { | 
|  | 5848 | make_encode_exception(exceptionObject, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5849 | encoding, unicode, size, startpos, endpos, reason); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5850 | if (*exceptionObject != NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5851 | PyCodec_StrictErrors(*exceptionObject); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5852 | } | 
|  | 5853 |  | 
|  | 5854 | /* error handling callback helper: | 
|  | 5855 | build arguments, call the callback and check the arguments, | 
|  | 5856 | put the result into newpos and return the replacement string, which | 
|  | 5857 | has to be freed by the caller */ | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5858 | static PyObject * | 
|  | 5859 | unicode_encode_call_errorhandler(const char *errors, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5860 | PyObject **errorHandler, | 
|  | 5861 | const char *encoding, const char *reason, | 
|  | 5862 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, | 
|  | 5863 | Py_ssize_t startpos, Py_ssize_t endpos, | 
|  | 5864 | Py_ssize_t *newpos) | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5865 | { | 
| Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5866 | static char *argparse = "On;encoding error handler must return (str/bytes, int) tuple"; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5867 |  | 
|  | 5868 | PyObject *restuple; | 
|  | 5869 | PyObject *resunicode; | 
|  | 5870 |  | 
|  | 5871 | if (*errorHandler == NULL) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5872 | *errorHandler = PyCodec_LookupError(errors); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5873 | if (*errorHandler == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5874 | return NULL; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5875 | } | 
|  | 5876 |  | 
|  | 5877 | make_encode_exception(exceptionObject, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5878 | encoding, unicode, size, startpos, endpos, reason); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5879 | if (*exceptionObject == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5880 | return NULL; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5881 |  | 
|  | 5882 | restuple = PyObject_CallFunctionObjArgs( | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5883 | *errorHandler, *exceptionObject, NULL); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5884 | if (restuple == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5885 | return NULL; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5886 | if (!PyTuple_Check(restuple)) { | 
| Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5887 | PyErr_SetString(PyExc_TypeError, &argparse[3]); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5888 | Py_DECREF(restuple); | 
|  | 5889 | return NULL; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5890 | } | 
| Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5891 | if (!PyArg_ParseTuple(restuple, argparse, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5892 | &resunicode, newpos)) { | 
|  | 5893 | Py_DECREF(restuple); | 
|  | 5894 | return NULL; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5895 | } | 
| Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5896 | if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) { | 
|  | 5897 | PyErr_SetString(PyExc_TypeError, &argparse[3]); | 
|  | 5898 | Py_DECREF(restuple); | 
|  | 5899 | return NULL; | 
|  | 5900 | } | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5901 | if (*newpos<0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5902 | *newpos = size+*newpos; | 
| Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 5903 | if (*newpos<0 || *newpos>size) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5904 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); | 
|  | 5905 | Py_DECREF(restuple); | 
|  | 5906 | return NULL; | 
| Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 5907 | } | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5908 | Py_INCREF(resunicode); | 
|  | 5909 | Py_DECREF(restuple); | 
|  | 5910 | return resunicode; | 
|  | 5911 | } | 
|  | 5912 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5913 | static PyObject * | 
|  | 5914 | unicode_encode_ucs1(const Py_UNICODE *p, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5915 | Py_ssize_t size, | 
|  | 5916 | const char *errors, | 
|  | 5917 | int limit) | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5918 | { | 
|  | 5919 | /* output object */ | 
|  | 5920 | PyObject *res; | 
|  | 5921 | /* pointers to the beginning and end+1 of input */ | 
|  | 5922 | const Py_UNICODE *startp = p; | 
|  | 5923 | const Py_UNICODE *endp = p + size; | 
|  | 5924 | /* pointer to the beginning of the unencodable characters */ | 
|  | 5925 | /* const Py_UNICODE *badp = NULL; */ | 
|  | 5926 | /* pointer into the output */ | 
|  | 5927 | char *str; | 
|  | 5928 | /* current output position */ | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5929 | Py_ssize_t ressize; | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5930 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; | 
|  | 5931 | 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] | 5932 | PyObject *errorHandler = NULL; | 
|  | 5933 | PyObject *exc = NULL; | 
|  | 5934 | /* the following variable is used for caching string comparisons | 
|  | 5935 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ | 
|  | 5936 | int known_errorHandler = -1; | 
|  | 5937 |  | 
|  | 5938 | /* allocate enough for a simple encoding without | 
|  | 5939 | replacements, if we need more, we'll resize */ | 
| Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5940 | if (size == 0) | 
| Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5941 | return PyBytes_FromStringAndSize(NULL, 0); | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5942 | res = PyBytes_FromStringAndSize(NULL, size); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5943 | if (res == NULL) | 
| Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5944 | return NULL; | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5945 | str = PyBytes_AS_STRING(res); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5946 | ressize = size; | 
|  | 5947 |  | 
|  | 5948 | while (p<endp) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5949 | Py_UNICODE c = *p; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5950 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5951 | /* can we encode this? */ | 
|  | 5952 | if (c<limit) { | 
|  | 5953 | /* no overflow check, because we know that the space is enough */ | 
|  | 5954 | *str++ = (char)c; | 
|  | 5955 | ++p; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5956 | } | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5957 | else { | 
|  | 5958 | Py_ssize_t unicodepos = p-startp; | 
|  | 5959 | Py_ssize_t requiredsize; | 
|  | 5960 | PyObject *repunicode; | 
|  | 5961 | Py_ssize_t repsize; | 
|  | 5962 | Py_ssize_t newpos; | 
|  | 5963 | Py_ssize_t respos; | 
|  | 5964 | Py_UNICODE *uni2; | 
|  | 5965 | /* startpos for collecting unencodable chars */ | 
|  | 5966 | const Py_UNICODE *collstart = p; | 
|  | 5967 | const Py_UNICODE *collend = p; | 
|  | 5968 | /* find all unecodable characters */ | 
|  | 5969 | while ((collend < endp) && ((*collend)>=limit)) | 
|  | 5970 | ++collend; | 
|  | 5971 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ | 
|  | 5972 | if (known_errorHandler==-1) { | 
|  | 5973 | if ((errors==NULL) || (!strcmp(errors, "strict"))) | 
|  | 5974 | known_errorHandler = 1; | 
|  | 5975 | else if (!strcmp(errors, "replace")) | 
|  | 5976 | known_errorHandler = 2; | 
|  | 5977 | else if (!strcmp(errors, "ignore")) | 
|  | 5978 | known_errorHandler = 3; | 
|  | 5979 | else if (!strcmp(errors, "xmlcharrefreplace")) | 
|  | 5980 | known_errorHandler = 4; | 
|  | 5981 | else | 
|  | 5982 | known_errorHandler = 0; | 
|  | 5983 | } | 
|  | 5984 | switch (known_errorHandler) { | 
|  | 5985 | case 1: /* strict */ | 
|  | 5986 | raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason); | 
|  | 5987 | goto onError; | 
|  | 5988 | case 2: /* replace */ | 
|  | 5989 | while (collstart++<collend) | 
|  | 5990 | *str++ = '?'; /* fall through */ | 
|  | 5991 | case 3: /* ignore */ | 
|  | 5992 | p = collend; | 
|  | 5993 | break; | 
|  | 5994 | case 4: /* xmlcharrefreplace */ | 
|  | 5995 | respos = str - PyBytes_AS_STRING(res); | 
|  | 5996 | /* determine replacement size (temporarily (mis)uses p) */ | 
|  | 5997 | for (p = collstart, repsize = 0; p < collend; ++p) { | 
|  | 5998 | if (*p<10) | 
|  | 5999 | repsize += 2+1+1; | 
|  | 6000 | else if (*p<100) | 
|  | 6001 | repsize += 2+2+1; | 
|  | 6002 | else if (*p<1000) | 
|  | 6003 | repsize += 2+3+1; | 
|  | 6004 | else if (*p<10000) | 
|  | 6005 | repsize += 2+4+1; | 
| Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 6006 | #ifndef Py_UNICODE_WIDE | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6007 | else | 
|  | 6008 | repsize += 2+5+1; | 
| Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 6009 | #else | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6010 | else if (*p<100000) | 
|  | 6011 | repsize += 2+5+1; | 
|  | 6012 | else if (*p<1000000) | 
|  | 6013 | repsize += 2+6+1; | 
|  | 6014 | else | 
|  | 6015 | repsize += 2+7+1; | 
| Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 6016 | #endif | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6017 | } | 
|  | 6018 | requiredsize = respos+repsize+(endp-collend); | 
|  | 6019 | if (requiredsize > ressize) { | 
|  | 6020 | if (requiredsize<2*ressize) | 
|  | 6021 | requiredsize = 2*ressize; | 
|  | 6022 | if (_PyBytes_Resize(&res, requiredsize)) | 
|  | 6023 | goto onError; | 
|  | 6024 | str = PyBytes_AS_STRING(res) + respos; | 
|  | 6025 | ressize = requiredsize; | 
|  | 6026 | } | 
|  | 6027 | /* generate replacement (temporarily (mis)uses p) */ | 
|  | 6028 | for (p = collstart; p < collend; ++p) { | 
|  | 6029 | str += sprintf(str, "&#%d;", (int)*p); | 
|  | 6030 | } | 
|  | 6031 | p = collend; | 
|  | 6032 | break; | 
|  | 6033 | default: | 
|  | 6034 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, | 
|  | 6035 | encoding, reason, startp, size, &exc, | 
|  | 6036 | collstart-startp, collend-startp, &newpos); | 
|  | 6037 | if (repunicode == NULL) | 
|  | 6038 | goto onError; | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6039 | if (PyBytes_Check(repunicode)) { | 
|  | 6040 | /* Directly copy bytes result to output. */ | 
|  | 6041 | repsize = PyBytes_Size(repunicode); | 
|  | 6042 | if (repsize > 1) { | 
|  | 6043 | /* Make room for all additional bytes. */ | 
| Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6044 | respos = str - PyBytes_AS_STRING(res); | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6045 | if (_PyBytes_Resize(&res, ressize+repsize-1)) { | 
|  | 6046 | Py_DECREF(repunicode); | 
|  | 6047 | goto onError; | 
|  | 6048 | } | 
| Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6049 | str = PyBytes_AS_STRING(res) + respos; | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6050 | ressize += repsize-1; | 
|  | 6051 | } | 
|  | 6052 | memcpy(str, PyBytes_AsString(repunicode), repsize); | 
|  | 6053 | str += repsize; | 
|  | 6054 | p = startp + newpos; | 
| Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6055 | Py_DECREF(repunicode); | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6056 | break; | 
| Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6057 | } | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6058 | /* need more space? (at least enough for what we | 
|  | 6059 | have+the replacement+the rest of the string, so | 
|  | 6060 | we won't have to check space for encodable characters) */ | 
|  | 6061 | respos = str - PyBytes_AS_STRING(res); | 
|  | 6062 | repsize = PyUnicode_GET_SIZE(repunicode); | 
|  | 6063 | requiredsize = respos+repsize+(endp-collend); | 
|  | 6064 | if (requiredsize > ressize) { | 
|  | 6065 | if (requiredsize<2*ressize) | 
|  | 6066 | requiredsize = 2*ressize; | 
|  | 6067 | if (_PyBytes_Resize(&res, requiredsize)) { | 
|  | 6068 | Py_DECREF(repunicode); | 
|  | 6069 | goto onError; | 
|  | 6070 | } | 
|  | 6071 | str = PyBytes_AS_STRING(res) + respos; | 
|  | 6072 | ressize = requiredsize; | 
|  | 6073 | } | 
|  | 6074 | /* check if there is anything unencodable in the replacement | 
|  | 6075 | and copy it to the output */ | 
|  | 6076 | for (uni2 = PyUnicode_AS_UNICODE(repunicode);repsize-->0; ++uni2, ++str) { | 
|  | 6077 | c = *uni2; | 
|  | 6078 | if (c >= limit) { | 
|  | 6079 | raise_encode_exception(&exc, encoding, startp, size, | 
|  | 6080 | unicodepos, unicodepos+1, reason); | 
|  | 6081 | Py_DECREF(repunicode); | 
|  | 6082 | goto onError; | 
|  | 6083 | } | 
|  | 6084 | *str = (char)c; | 
|  | 6085 | } | 
|  | 6086 | p = startp + newpos; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6087 | Py_DECREF(repunicode); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6088 | } | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6089 | } | 
|  | 6090 | } | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6091 | /* Resize if we allocated to much */ | 
|  | 6092 | size = str - PyBytes_AS_STRING(res); | 
|  | 6093 | if (size < ressize) { /* If this falls res will be NULL */ | 
| Alexandre Vassalotti | bad1b92 | 2008-12-27 09:49:09 +0000 | [diff] [blame] | 6094 | assert(size >= 0); | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6095 | if (_PyBytes_Resize(&res, size) < 0) | 
|  | 6096 | goto onError; | 
|  | 6097 | } | 
|  | 6098 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6099 | Py_XDECREF(errorHandler); | 
|  | 6100 | Py_XDECREF(exc); | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6101 | return res; | 
|  | 6102 |  | 
|  | 6103 | onError: | 
|  | 6104 | Py_XDECREF(res); | 
|  | 6105 | Py_XDECREF(errorHandler); | 
|  | 6106 | Py_XDECREF(exc); | 
|  | 6107 | return NULL; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6108 | } | 
|  | 6109 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6110 | PyObject * | 
|  | 6111 | PyUnicode_EncodeLatin1(const Py_UNICODE *p, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6112 | Py_ssize_t size, | 
|  | 6113 | const char *errors) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6114 | { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6115 | return unicode_encode_ucs1(p, size, errors, 256); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6116 | } | 
|  | 6117 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6118 | PyObject * | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6119 | _PyUnicode_AsLatin1String(PyObject *unicode, const char *errors) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6120 | { | 
|  | 6121 | if (!PyUnicode_Check(unicode)) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6122 | PyErr_BadArgument(); | 
|  | 6123 | return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6124 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6125 | if (PyUnicode_READY(unicode) == -1) | 
|  | 6126 | return NULL; | 
|  | 6127 | /* Fast path: if it is a one-byte string, construct | 
|  | 6128 | bytes object directly. */ | 
|  | 6129 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) | 
|  | 6130 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), | 
|  | 6131 | PyUnicode_GET_LENGTH(unicode)); | 
|  | 6132 | /* Non-Latin-1 characters present. Defer to above function to | 
|  | 6133 | raise the exception. */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6134 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6135 | PyUnicode_GET_SIZE(unicode), | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6136 | errors); | 
|  | 6137 | } | 
|  | 6138 |  | 
|  | 6139 | PyObject* | 
|  | 6140 | PyUnicode_AsLatin1String(PyObject *unicode) | 
|  | 6141 | { | 
|  | 6142 | return _PyUnicode_AsLatin1String(unicode, NULL); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6143 | } | 
|  | 6144 |  | 
|  | 6145 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ | 
|  | 6146 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6147 | PyObject * | 
|  | 6148 | PyUnicode_DecodeASCII(const char *s, | 
|  | 6149 | Py_ssize_t size, | 
|  | 6150 | const char *errors) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6151 | { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6152 | const char *starts = s; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6153 | PyUnicodeObject *v; | 
|  | 6154 | Py_UNICODE *p; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6155 | Py_ssize_t startinpos; | 
|  | 6156 | Py_ssize_t endinpos; | 
|  | 6157 | Py_ssize_t outpos; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6158 | const char *e; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6159 | unsigned char* d; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6160 | PyObject *errorHandler = NULL; | 
|  | 6161 | PyObject *exc = NULL; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6162 | Py_ssize_t i; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6163 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6164 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6165 | if (size == 1 && *(unsigned char*)s < 128) | 
|  | 6166 | return PyUnicode_FromOrdinal(*(unsigned char*)s); | 
|  | 6167 |  | 
|  | 6168 | /* Fast path. Assume the input actually *is* ASCII, and allocate | 
|  | 6169 | a single-block Unicode object with that assumption. If there is | 
|  | 6170 | an error, drop the object and start over. */ | 
|  | 6171 | v = (PyUnicodeObject*)PyUnicode_New(size, 127); | 
|  | 6172 | if (v == NULL) | 
|  | 6173 | goto onError; | 
|  | 6174 | d = PyUnicode_1BYTE_DATA(v); | 
|  | 6175 | for (i = 0; i < size; i++) { | 
|  | 6176 | unsigned char ch = ((unsigned char*)s)[i]; | 
|  | 6177 | if (ch < 128) | 
|  | 6178 | d[i] = ch; | 
|  | 6179 | else | 
|  | 6180 | break; | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 6181 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6182 | if (i == size) | 
|  | 6183 | return (PyObject*)v; | 
|  | 6184 | Py_DECREF(v); /* start over */ | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6185 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6186 | v = _PyUnicode_New(size); | 
|  | 6187 | if (v == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6188 | goto onError; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6189 | if (size == 0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6190 | return (PyObject *)v; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6191 | p = PyUnicode_AS_UNICODE(v); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6192 | e = s + size; | 
|  | 6193 | while (s < e) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6194 | register unsigned char c = (unsigned char)*s; | 
|  | 6195 | if (c < 128) { | 
|  | 6196 | *p++ = c; | 
|  | 6197 | ++s; | 
|  | 6198 | } | 
|  | 6199 | else { | 
|  | 6200 | startinpos = s-starts; | 
|  | 6201 | endinpos = startinpos + 1; | 
|  | 6202 | outpos = p - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); | 
|  | 6203 | if (unicode_decode_call_errorhandler( | 
|  | 6204 | errors, &errorHandler, | 
|  | 6205 | "ascii", "ordinal not in range(128)", | 
|  | 6206 | &starts, &e, &startinpos, &endinpos, &exc, &s, | 
|  | 6207 | &v, &outpos, &p)) | 
|  | 6208 | goto onError; | 
|  | 6209 | } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6210 | } | 
| Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 6211 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6212 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) | 
|  | 6213 | goto onError; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6214 | Py_XDECREF(errorHandler); | 
|  | 6215 | Py_XDECREF(exc); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6216 | if (PyUnicode_READY(v) == -1) { | 
|  | 6217 | Py_DECREF(v); | 
|  | 6218 | return NULL; | 
|  | 6219 | } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6220 | return (PyObject *)v; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6221 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6222 | onError: | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6223 | Py_XDECREF(v); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6224 | Py_XDECREF(errorHandler); | 
|  | 6225 | Py_XDECREF(exc); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6226 | return NULL; | 
|  | 6227 | } | 
|  | 6228 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6229 | PyObject * | 
|  | 6230 | PyUnicode_EncodeASCII(const Py_UNICODE *p, | 
|  | 6231 | Py_ssize_t size, | 
|  | 6232 | const char *errors) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6233 | { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6234 | return unicode_encode_ucs1(p, size, errors, 128); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6235 | } | 
|  | 6236 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6237 | PyObject * | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6238 | _PyUnicode_AsASCIIString(PyObject *unicode, const char *errors) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6239 | { | 
|  | 6240 | if (!PyUnicode_Check(unicode)) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6241 | PyErr_BadArgument(); | 
|  | 6242 | return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6243 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6244 | if (PyUnicode_READY(unicode) == -1) | 
|  | 6245 | return NULL; | 
|  | 6246 | /* Fast path: if it is an ASCII-only string, construct bytes object | 
|  | 6247 | directly. Else defer to above function to raise the exception. */ | 
|  | 6248 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) | 
|  | 6249 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), | 
|  | 6250 | PyUnicode_GET_LENGTH(unicode)); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6251 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6252 | PyUnicode_GET_SIZE(unicode), | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6253 | errors); | 
|  | 6254 | } | 
|  | 6255 |  | 
|  | 6256 | PyObject * | 
|  | 6257 | PyUnicode_AsASCIIString(PyObject *unicode) | 
|  | 6258 | { | 
|  | 6259 | return _PyUnicode_AsASCIIString(unicode, NULL); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6260 | } | 
|  | 6261 |  | 
| Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 6262 | #ifdef HAVE_MBCS | 
| Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6263 |  | 
| Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6264 | /* --- MBCS codecs for Windows -------------------------------------------- */ | 
| Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6265 |  | 
| Hirokazu Yamamoto | 3530246 | 2009-03-21 13:23:27 +0000 | [diff] [blame] | 6266 | #if SIZEOF_INT < SIZEOF_SIZE_T | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6267 | #define NEED_RETRY | 
|  | 6268 | #endif | 
|  | 6269 |  | 
|  | 6270 | /* XXX This code is limited to "true" double-byte encodings, as | 
|  | 6271 | a) it assumes an incomplete character consists of a single byte, and | 
|  | 6272 | b) IsDBCSLeadByte (probably) does not work for non-DBCS multi-byte | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6273 | encodings, see IsDBCSLeadByteEx documentation. */ | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6274 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6275 | static int | 
|  | 6276 | is_dbcs_lead_byte(const char *s, int offset) | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6277 | { | 
|  | 6278 | const char *curr = s + offset; | 
|  | 6279 |  | 
|  | 6280 | if (IsDBCSLeadByte(*curr)) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6281 | const char *prev = CharPrev(s, curr); | 
|  | 6282 | return (prev == curr) || !IsDBCSLeadByte(*prev) || (curr - prev == 2); | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6283 | } | 
|  | 6284 | return 0; | 
|  | 6285 | } | 
|  | 6286 |  | 
|  | 6287 | /* | 
|  | 6288 | * Decode MBCS string into unicode object. If 'final' is set, converts | 
|  | 6289 | * trailing lead-byte too. Returns consumed size if succeed, -1 otherwise. | 
|  | 6290 | */ | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6291 | static int | 
|  | 6292 | decode_mbcs(PyUnicodeObject **v, | 
|  | 6293 | const char *s, /* MBCS string */ | 
|  | 6294 | int size, /* sizeof MBCS string */ | 
|  | 6295 | int final, | 
|  | 6296 | const char *errors) | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6297 | { | 
|  | 6298 | Py_UNICODE *p; | 
| Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6299 | Py_ssize_t n; | 
|  | 6300 | DWORD usize; | 
|  | 6301 | DWORD flags; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6302 |  | 
|  | 6303 | assert(size >= 0); | 
|  | 6304 |  | 
| Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6305 | /* check and handle 'errors' arg */ | 
|  | 6306 | if (errors==NULL || strcmp(errors, "strict")==0) | 
|  | 6307 | flags = MB_ERR_INVALID_CHARS; | 
|  | 6308 | else if (strcmp(errors, "ignore")==0) | 
|  | 6309 | flags = 0; | 
|  | 6310 | else { | 
|  | 6311 | PyErr_Format(PyExc_ValueError, | 
|  | 6312 | "mbcs encoding does not support errors='%s'", | 
|  | 6313 | errors); | 
|  | 6314 | return -1; | 
|  | 6315 | } | 
|  | 6316 |  | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6317 | /* Skip trailing lead-byte unless 'final' is set */ | 
|  | 6318 | if (!final && size >= 1 && is_dbcs_lead_byte(s, size - 1)) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6319 | --size; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6320 |  | 
|  | 6321 | /* First get the size of the result */ | 
|  | 6322 | if (size > 0) { | 
| Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6323 | usize = MultiByteToWideChar(CP_ACP, flags, s, size, NULL, 0); | 
|  | 6324 | if (usize==0) | 
|  | 6325 | goto mbcs_decode_error; | 
|  | 6326 | } else | 
|  | 6327 | usize = 0; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6328 |  | 
|  | 6329 | if (*v == NULL) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6330 | /* Create unicode object */ | 
|  | 6331 | *v = _PyUnicode_New(usize); | 
|  | 6332 | if (*v == NULL) | 
|  | 6333 | return -1; | 
| Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6334 | n = 0; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6335 | } | 
|  | 6336 | else { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6337 | /* Extend unicode object */ | 
|  | 6338 | n = PyUnicode_GET_SIZE(*v); | 
|  | 6339 | if (_PyUnicode_Resize(v, n + usize) < 0) | 
|  | 6340 | return -1; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6341 | } | 
|  | 6342 |  | 
|  | 6343 | /* Do the conversion */ | 
| Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6344 | if (usize > 0) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6345 | p = PyUnicode_AS_UNICODE(*v) + n; | 
| Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6346 | if (0 == MultiByteToWideChar(CP_ACP, flags, s, size, p, usize)) { | 
|  | 6347 | goto mbcs_decode_error; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6348 | } | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6349 | } | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6350 | return size; | 
| Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6351 |  | 
|  | 6352 | mbcs_decode_error: | 
|  | 6353 | /* If the last error was ERROR_NO_UNICODE_TRANSLATION, then | 
|  | 6354 | we raise a UnicodeDecodeError - else it is a 'generic' | 
|  | 6355 | windows error | 
|  | 6356 | */ | 
|  | 6357 | if (GetLastError()==ERROR_NO_UNICODE_TRANSLATION) { | 
|  | 6358 | /* Ideally, we should get reason from FormatMessage - this | 
|  | 6359 | is the Windows 2000 English version of the message | 
|  | 6360 | */ | 
|  | 6361 | PyObject *exc = NULL; | 
|  | 6362 | const char *reason = "No mapping for the Unicode character exists " | 
|  | 6363 | "in the target multi-byte code page."; | 
|  | 6364 | make_decode_exception(&exc, "mbcs", s, size, 0, 0, reason); | 
|  | 6365 | if (exc != NULL) { | 
|  | 6366 | PyCodec_StrictErrors(exc); | 
|  | 6367 | Py_DECREF(exc); | 
|  | 6368 | } | 
|  | 6369 | } else { | 
|  | 6370 | PyErr_SetFromWindowsErrWithFilename(0, NULL); | 
|  | 6371 | } | 
|  | 6372 | return -1; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6373 | } | 
|  | 6374 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6375 | PyObject * | 
|  | 6376 | PyUnicode_DecodeMBCSStateful(const char *s, | 
|  | 6377 | Py_ssize_t size, | 
|  | 6378 | const char *errors, | 
|  | 6379 | Py_ssize_t *consumed) | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6380 | { | 
|  | 6381 | PyUnicodeObject *v = NULL; | 
|  | 6382 | int done; | 
|  | 6383 |  | 
|  | 6384 | if (consumed) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6385 | *consumed = 0; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6386 |  | 
|  | 6387 | #ifdef NEED_RETRY | 
|  | 6388 | retry: | 
|  | 6389 | if (size > INT_MAX) | 
| Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6390 | done = decode_mbcs(&v, s, INT_MAX, 0, errors); | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6391 | else | 
|  | 6392 | #endif | 
| Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6393 | done = decode_mbcs(&v, s, (int)size, !consumed, errors); | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6394 |  | 
|  | 6395 | if (done < 0) { | 
|  | 6396 | Py_XDECREF(v); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6397 | return NULL; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6398 | } | 
|  | 6399 |  | 
|  | 6400 | if (consumed) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6401 | *consumed += done; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6402 |  | 
|  | 6403 | #ifdef NEED_RETRY | 
|  | 6404 | if (size > INT_MAX) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6405 | s += done; | 
|  | 6406 | size -= done; | 
|  | 6407 | goto retry; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6408 | } | 
|  | 6409 | #endif | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6410 | if (PyUnicode_READY(v) == -1) { | 
|  | 6411 | Py_DECREF(v); | 
|  | 6412 | return NULL; | 
|  | 6413 | } | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6414 | return (PyObject *)v; | 
|  | 6415 | } | 
|  | 6416 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6417 | PyObject * | 
|  | 6418 | PyUnicode_DecodeMBCS(const char *s, | 
|  | 6419 | Py_ssize_t size, | 
|  | 6420 | const char *errors) | 
| Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6421 | { | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6422 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); | 
|  | 6423 | } | 
|  | 6424 |  | 
|  | 6425 | /* | 
|  | 6426 | * Convert unicode into string object (MBCS). | 
|  | 6427 | * Returns 0 if succeed, -1 otherwise. | 
|  | 6428 | */ | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6429 | static int | 
|  | 6430 | encode_mbcs(PyObject **repr, | 
|  | 6431 | const Py_UNICODE *p, /* unicode */ | 
|  | 6432 | int size, /* size of unicode */ | 
|  | 6433 | const char* errors) | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6434 | { | 
| Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6435 | BOOL usedDefaultChar = FALSE; | 
|  | 6436 | BOOL *pusedDefaultChar; | 
|  | 6437 | int mbcssize; | 
|  | 6438 | Py_ssize_t n; | 
|  | 6439 | PyObject *exc = NULL; | 
|  | 6440 | DWORD flags; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6441 |  | 
|  | 6442 | assert(size >= 0); | 
| Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6443 |  | 
| Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6444 | /* check and handle 'errors' arg */ | 
|  | 6445 | if (errors==NULL || strcmp(errors, "strict")==0) { | 
|  | 6446 | flags = WC_NO_BEST_FIT_CHARS; | 
|  | 6447 | pusedDefaultChar = &usedDefaultChar; | 
|  | 6448 | } else if (strcmp(errors, "replace")==0) { | 
|  | 6449 | flags = 0; | 
|  | 6450 | pusedDefaultChar = NULL; | 
|  | 6451 | } else { | 
|  | 6452 | PyErr_Format(PyExc_ValueError, | 
|  | 6453 | "mbcs encoding does not support errors='%s'", | 
|  | 6454 | errors); | 
|  | 6455 | return -1; | 
|  | 6456 | } | 
|  | 6457 |  | 
| Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6458 | /* First get the size of the result */ | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6459 | if (size > 0) { | 
| Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6460 | mbcssize = WideCharToMultiByte(CP_ACP, flags, p, size, NULL, 0, | 
|  | 6461 | NULL, pusedDefaultChar); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6462 | if (mbcssize == 0) { | 
|  | 6463 | PyErr_SetFromWindowsErrWithFilename(0, NULL); | 
|  | 6464 | return -1; | 
|  | 6465 | } | 
| Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6466 | /* If we used a default char, then we failed! */ | 
|  | 6467 | if (pusedDefaultChar && *pusedDefaultChar) | 
|  | 6468 | goto mbcs_encode_error; | 
|  | 6469 | } else { | 
|  | 6470 | mbcssize = 0; | 
| Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6471 | } | 
|  | 6472 |  | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6473 | if (*repr == NULL) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6474 | /* Create string object */ | 
|  | 6475 | *repr = PyBytes_FromStringAndSize(NULL, mbcssize); | 
|  | 6476 | if (*repr == NULL) | 
|  | 6477 | return -1; | 
| Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6478 | n = 0; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6479 | } | 
|  | 6480 | else { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6481 | /* Extend string object */ | 
|  | 6482 | n = PyBytes_Size(*repr); | 
|  | 6483 | if (_PyBytes_Resize(repr, n + mbcssize) < 0) | 
|  | 6484 | return -1; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6485 | } | 
|  | 6486 |  | 
|  | 6487 | /* Do the conversion */ | 
|  | 6488 | if (size > 0) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6489 | char *s = PyBytes_AS_STRING(*repr) + n; | 
| Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6490 | if (0 == WideCharToMultiByte(CP_ACP, flags, p, size, s, mbcssize, | 
|  | 6491 | NULL, pusedDefaultChar)) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6492 | PyErr_SetFromWindowsErrWithFilename(0, NULL); | 
|  | 6493 | return -1; | 
|  | 6494 | } | 
| Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6495 | if (pusedDefaultChar && *pusedDefaultChar) | 
|  | 6496 | goto mbcs_encode_error; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6497 | } | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6498 | return 0; | 
| Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6499 |  | 
|  | 6500 | mbcs_encode_error: | 
|  | 6501 | raise_encode_exception(&exc, "mbcs", p, size, 0, 0, "invalid character"); | 
|  | 6502 | Py_XDECREF(exc); | 
|  | 6503 | return -1; | 
| Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6504 | } | 
|  | 6505 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6506 | PyObject * | 
|  | 6507 | PyUnicode_EncodeMBCS(const Py_UNICODE *p, | 
|  | 6508 | Py_ssize_t size, | 
|  | 6509 | const char *errors) | 
| Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6510 | { | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6511 | PyObject *repr = NULL; | 
|  | 6512 | int ret; | 
| Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 6513 |  | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6514 | #ifdef NEED_RETRY | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6515 | retry: | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6516 | if (size > INT_MAX) | 
| Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6517 | ret = encode_mbcs(&repr, p, INT_MAX, errors); | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6518 | else | 
|  | 6519 | #endif | 
| Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6520 | ret = encode_mbcs(&repr, p, (int)size, errors); | 
| Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6521 |  | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6522 | if (ret < 0) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6523 | Py_XDECREF(repr); | 
|  | 6524 | return NULL; | 
| Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6525 | } | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6526 |  | 
|  | 6527 | #ifdef NEED_RETRY | 
|  | 6528 | if (size > INT_MAX) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6529 | p += INT_MAX; | 
|  | 6530 | size -= INT_MAX; | 
|  | 6531 | goto retry; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6532 | } | 
|  | 6533 | #endif | 
|  | 6534 |  | 
| Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6535 | return repr; | 
|  | 6536 | } | 
| Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6537 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6538 | PyObject * | 
|  | 6539 | PyUnicode_AsMBCSString(PyObject *unicode) | 
| Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 6540 | { | 
|  | 6541 | if (!PyUnicode_Check(unicode)) { | 
|  | 6542 | PyErr_BadArgument(); | 
|  | 6543 | return NULL; | 
|  | 6544 | } | 
|  | 6545 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6546 | PyUnicode_GET_SIZE(unicode), | 
|  | 6547 | NULL); | 
| Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 6548 | } | 
|  | 6549 |  | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6550 | #undef NEED_RETRY | 
|  | 6551 |  | 
| Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 6552 | #endif /* HAVE_MBCS */ | 
| Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6553 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6554 | /* --- Character Mapping Codec -------------------------------------------- */ | 
|  | 6555 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6556 | PyObject * | 
|  | 6557 | PyUnicode_DecodeCharmap(const char *s, | 
|  | 6558 | Py_ssize_t size, | 
|  | 6559 | PyObject *mapping, | 
|  | 6560 | const char *errors) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6561 | { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6562 | const char *starts = s; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6563 | Py_ssize_t startinpos; | 
|  | 6564 | Py_ssize_t endinpos; | 
|  | 6565 | Py_ssize_t outpos; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6566 | const char *e; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6567 | PyUnicodeObject *v; | 
|  | 6568 | Py_UNICODE *p; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6569 | Py_ssize_t extrachars = 0; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6570 | PyObject *errorHandler = NULL; | 
|  | 6571 | PyObject *exc = NULL; | 
| Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 6572 | Py_UNICODE *mapstring = NULL; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6573 | Py_ssize_t maplen = 0; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6574 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6575 | /* Default to Latin-1 */ | 
|  | 6576 | if (mapping == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6577 | return PyUnicode_DecodeLatin1(s, size, errors); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6578 |  | 
|  | 6579 | v = _PyUnicode_New(size); | 
|  | 6580 | if (v == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6581 | goto onError; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6582 | if (size == 0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6583 | return (PyObject *)v; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6584 | p = PyUnicode_AS_UNICODE(v); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6585 | e = s + size; | 
| Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 6586 | if (PyUnicode_CheckExact(mapping)) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6587 | mapstring = PyUnicode_AS_UNICODE(mapping); | 
|  | 6588 | maplen = PyUnicode_GET_SIZE(mapping); | 
|  | 6589 | while (s < e) { | 
|  | 6590 | unsigned char ch = *s; | 
|  | 6591 | Py_UNICODE x = 0xfffe; /* illegal value */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6592 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6593 | if (ch < maplen) | 
|  | 6594 | x = mapstring[ch]; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6595 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6596 | if (x == 0xfffe) { | 
|  | 6597 | /* undefined mapping */ | 
|  | 6598 | outpos = p-PyUnicode_AS_UNICODE(v); | 
|  | 6599 | startinpos = s-starts; | 
|  | 6600 | endinpos = startinpos+1; | 
|  | 6601 | if (unicode_decode_call_errorhandler( | 
|  | 6602 | errors, &errorHandler, | 
|  | 6603 | "charmap", "character maps to <undefined>", | 
|  | 6604 | &starts, &e, &startinpos, &endinpos, &exc, &s, | 
|  | 6605 | &v, &outpos, &p)) { | 
|  | 6606 | goto onError; | 
|  | 6607 | } | 
|  | 6608 | continue; | 
|  | 6609 | } | 
|  | 6610 | *p++ = x; | 
|  | 6611 | ++s; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6612 | } | 
| Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 6613 | } | 
|  | 6614 | else { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6615 | while (s < e) { | 
|  | 6616 | unsigned char ch = *s; | 
|  | 6617 | PyObject *w, *x; | 
| Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 6618 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6619 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ | 
|  | 6620 | w = PyLong_FromLong((long)ch); | 
|  | 6621 | if (w == NULL) | 
|  | 6622 | goto onError; | 
|  | 6623 | x = PyObject_GetItem(mapping, w); | 
|  | 6624 | Py_DECREF(w); | 
|  | 6625 | if (x == NULL) { | 
|  | 6626 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { | 
|  | 6627 | /* No mapping found means: mapping is undefined. */ | 
|  | 6628 | PyErr_Clear(); | 
|  | 6629 | x = Py_None; | 
|  | 6630 | Py_INCREF(x); | 
|  | 6631 | } else | 
|  | 6632 | goto onError; | 
|  | 6633 | } | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6634 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6635 | /* Apply mapping */ | 
|  | 6636 | if (PyLong_Check(x)) { | 
|  | 6637 | long value = PyLong_AS_LONG(x); | 
|  | 6638 | if (value < 0 || value > 65535) { | 
|  | 6639 | PyErr_SetString(PyExc_TypeError, | 
|  | 6640 | "character mapping must be in range(65536)"); | 
|  | 6641 | Py_DECREF(x); | 
|  | 6642 | goto onError; | 
|  | 6643 | } | 
|  | 6644 | *p++ = (Py_UNICODE)value; | 
|  | 6645 | } | 
|  | 6646 | else if (x == Py_None) { | 
|  | 6647 | /* undefined mapping */ | 
|  | 6648 | outpos = p-PyUnicode_AS_UNICODE(v); | 
|  | 6649 | startinpos = s-starts; | 
|  | 6650 | endinpos = startinpos+1; | 
|  | 6651 | if (unicode_decode_call_errorhandler( | 
|  | 6652 | errors, &errorHandler, | 
|  | 6653 | "charmap", "character maps to <undefined>", | 
|  | 6654 | &starts, &e, &startinpos, &endinpos, &exc, &s, | 
|  | 6655 | &v, &outpos, &p)) { | 
|  | 6656 | Py_DECREF(x); | 
|  | 6657 | goto onError; | 
|  | 6658 | } | 
|  | 6659 | Py_DECREF(x); | 
|  | 6660 | continue; | 
|  | 6661 | } | 
|  | 6662 | else if (PyUnicode_Check(x)) { | 
|  | 6663 | Py_ssize_t targetsize = PyUnicode_GET_SIZE(x); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6664 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6665 | if (targetsize == 1) | 
|  | 6666 | /* 1-1 mapping */ | 
|  | 6667 | *p++ = *PyUnicode_AS_UNICODE(x); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6668 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6669 | else if (targetsize > 1) { | 
|  | 6670 | /* 1-n mapping */ | 
|  | 6671 | if (targetsize > extrachars) { | 
|  | 6672 | /* resize first */ | 
|  | 6673 | Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v); | 
|  | 6674 | Py_ssize_t needed = (targetsize - extrachars) + \ | 
|  | 6675 | (targetsize << 2); | 
|  | 6676 | extrachars += needed; | 
|  | 6677 | /* XXX overflow detection missing */ | 
|  | 6678 | if (_PyUnicode_Resize(&v, | 
|  | 6679 | PyUnicode_GET_SIZE(v) + needed) < 0) { | 
|  | 6680 | Py_DECREF(x); | 
|  | 6681 | goto onError; | 
|  | 6682 | } | 
|  | 6683 | p = PyUnicode_AS_UNICODE(v) + oldpos; | 
|  | 6684 | } | 
|  | 6685 | Py_UNICODE_COPY(p, | 
|  | 6686 | PyUnicode_AS_UNICODE(x), | 
|  | 6687 | targetsize); | 
|  | 6688 | p += targetsize; | 
|  | 6689 | extrachars -= targetsize; | 
|  | 6690 | } | 
|  | 6691 | /* 1-0 mapping: skip the character */ | 
|  | 6692 | } | 
|  | 6693 | else { | 
|  | 6694 | /* wrong return value */ | 
|  | 6695 | PyErr_SetString(PyExc_TypeError, | 
|  | 6696 | "character mapping must return integer, None or str"); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6697 | Py_DECREF(x); | 
|  | 6698 | goto onError; | 
|  | 6699 | } | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6700 | Py_DECREF(x); | 
|  | 6701 | ++s; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6702 | } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6703 | } | 
|  | 6704 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6705 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) | 
|  | 6706 | goto onError; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6707 | Py_XDECREF(errorHandler); | 
|  | 6708 | Py_XDECREF(exc); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6709 | if (PyUnicode_READY(v) == -1) { | 
|  | 6710 | Py_DECREF(v); | 
|  | 6711 | return NULL; | 
|  | 6712 | } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6713 | return (PyObject *)v; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6714 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6715 | onError: | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6716 | Py_XDECREF(errorHandler); | 
|  | 6717 | Py_XDECREF(exc); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6718 | Py_XDECREF(v); | 
|  | 6719 | return NULL; | 
|  | 6720 | } | 
|  | 6721 |  | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6722 | /* Charmap encoding: the lookup table */ | 
|  | 6723 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6724 | struct encoding_map { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6725 | PyObject_HEAD | 
|  | 6726 | unsigned char level1[32]; | 
|  | 6727 | int count2, count3; | 
|  | 6728 | unsigned char level23[1]; | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6729 | }; | 
|  | 6730 |  | 
|  | 6731 | static PyObject* | 
|  | 6732 | encoding_map_size(PyObject *obj, PyObject* args) | 
|  | 6733 | { | 
|  | 6734 | struct encoding_map *map = (struct encoding_map*)obj; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6735 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6736 | 128*map->count3); | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6737 | } | 
|  | 6738 |  | 
|  | 6739 | static PyMethodDef encoding_map_methods[] = { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6740 | {"size", encoding_map_size, METH_NOARGS, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6741 | PyDoc_STR("Return the size (in bytes) of this object") }, | 
|  | 6742 | { 0 } | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6743 | }; | 
|  | 6744 |  | 
|  | 6745 | static void | 
|  | 6746 | encoding_map_dealloc(PyObject* o) | 
|  | 6747 | { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6748 | PyObject_FREE(o); | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6749 | } | 
|  | 6750 |  | 
|  | 6751 | static PyTypeObject EncodingMapType = { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6752 | PyVarObject_HEAD_INIT(NULL, 0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6753 | "EncodingMap",          /*tp_name*/ | 
|  | 6754 | sizeof(struct encoding_map),   /*tp_basicsize*/ | 
|  | 6755 | 0,                      /*tp_itemsize*/ | 
|  | 6756 | /* methods */ | 
|  | 6757 | encoding_map_dealloc,   /*tp_dealloc*/ | 
|  | 6758 | 0,                      /*tp_print*/ | 
|  | 6759 | 0,                      /*tp_getattr*/ | 
|  | 6760 | 0,                      /*tp_setattr*/ | 
| Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 6761 | 0,                      /*tp_reserved*/ | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6762 | 0,                      /*tp_repr*/ | 
|  | 6763 | 0,                      /*tp_as_number*/ | 
|  | 6764 | 0,                      /*tp_as_sequence*/ | 
|  | 6765 | 0,                      /*tp_as_mapping*/ | 
|  | 6766 | 0,                      /*tp_hash*/ | 
|  | 6767 | 0,                      /*tp_call*/ | 
|  | 6768 | 0,                      /*tp_str*/ | 
|  | 6769 | 0,                      /*tp_getattro*/ | 
|  | 6770 | 0,                      /*tp_setattro*/ | 
|  | 6771 | 0,                      /*tp_as_buffer*/ | 
|  | 6772 | Py_TPFLAGS_DEFAULT,     /*tp_flags*/ | 
|  | 6773 | 0,                      /*tp_doc*/ | 
|  | 6774 | 0,                      /*tp_traverse*/ | 
|  | 6775 | 0,                      /*tp_clear*/ | 
|  | 6776 | 0,                      /*tp_richcompare*/ | 
|  | 6777 | 0,                      /*tp_weaklistoffset*/ | 
|  | 6778 | 0,                      /*tp_iter*/ | 
|  | 6779 | 0,                      /*tp_iternext*/ | 
|  | 6780 | encoding_map_methods,   /*tp_methods*/ | 
|  | 6781 | 0,                      /*tp_members*/ | 
|  | 6782 | 0,                      /*tp_getset*/ | 
|  | 6783 | 0,                      /*tp_base*/ | 
|  | 6784 | 0,                      /*tp_dict*/ | 
|  | 6785 | 0,                      /*tp_descr_get*/ | 
|  | 6786 | 0,                      /*tp_descr_set*/ | 
|  | 6787 | 0,                      /*tp_dictoffset*/ | 
|  | 6788 | 0,                      /*tp_init*/ | 
|  | 6789 | 0,                      /*tp_alloc*/ | 
|  | 6790 | 0,                      /*tp_new*/ | 
|  | 6791 | 0,                      /*tp_free*/ | 
|  | 6792 | 0,                      /*tp_is_gc*/ | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6793 | }; | 
|  | 6794 |  | 
|  | 6795 | PyObject* | 
|  | 6796 | PyUnicode_BuildEncodingMap(PyObject* string) | 
|  | 6797 | { | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6798 | PyObject *result; | 
|  | 6799 | struct encoding_map *mresult; | 
|  | 6800 | int i; | 
|  | 6801 | int need_dict = 0; | 
|  | 6802 | unsigned char level1[32]; | 
|  | 6803 | unsigned char level2[512]; | 
|  | 6804 | unsigned char *mlevel1, *mlevel2, *mlevel3; | 
|  | 6805 | int count2 = 0, count3 = 0; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6806 | int kind; | 
|  | 6807 | void *data; | 
|  | 6808 | Py_UCS4 ch; | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6809 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6810 | if (!PyUnicode_Check(string) || PyUnicode_GET_LENGTH(string) != 256) { | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6811 | PyErr_BadArgument(); | 
|  | 6812 | return NULL; | 
|  | 6813 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6814 | kind = PyUnicode_KIND(string); | 
|  | 6815 | data = PyUnicode_DATA(string); | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6816 | memset(level1, 0xFF, sizeof level1); | 
|  | 6817 | memset(level2, 0xFF, sizeof level2); | 
|  | 6818 |  | 
|  | 6819 | /* If there isn't a one-to-one mapping of NULL to \0, | 
|  | 6820 | or if there are non-BMP characters, we need to use | 
|  | 6821 | a mapping dictionary. */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6822 | if (PyUnicode_READ(kind, data, 0) != 0) | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6823 | need_dict = 1; | 
|  | 6824 | for (i = 1; i < 256; i++) { | 
|  | 6825 | int l1, l2; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6826 | ch = PyUnicode_READ(kind, data, i); | 
|  | 6827 | if (ch == 0 || ch > 0xFFFF) { | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6828 | need_dict = 1; | 
|  | 6829 | break; | 
|  | 6830 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6831 | if (ch == 0xFFFE) | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6832 | /* unmapped character */ | 
|  | 6833 | continue; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6834 | l1 = ch >> 11; | 
|  | 6835 | l2 = ch >> 7; | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6836 | if (level1[l1] == 0xFF) | 
|  | 6837 | level1[l1] = count2++; | 
|  | 6838 | if (level2[l2] == 0xFF) | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6839 | level2[l2] = count3++; | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6840 | } | 
|  | 6841 |  | 
|  | 6842 | if (count2 >= 0xFF || count3 >= 0xFF) | 
|  | 6843 | need_dict = 1; | 
|  | 6844 |  | 
|  | 6845 | if (need_dict) { | 
|  | 6846 | PyObject *result = PyDict_New(); | 
|  | 6847 | PyObject *key, *value; | 
|  | 6848 | if (!result) | 
|  | 6849 | return NULL; | 
|  | 6850 | for (i = 0; i < 256; i++) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6851 | key = PyLong_FromLong(PyUnicode_READ(kind, data, i)); | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 6852 | value = PyLong_FromLong(i); | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6853 | if (!key || !value) | 
|  | 6854 | goto failed1; | 
|  | 6855 | if (PyDict_SetItem(result, key, value) == -1) | 
|  | 6856 | goto failed1; | 
|  | 6857 | Py_DECREF(key); | 
|  | 6858 | Py_DECREF(value); | 
|  | 6859 | } | 
|  | 6860 | return result; | 
|  | 6861 | failed1: | 
|  | 6862 | Py_XDECREF(key); | 
|  | 6863 | Py_XDECREF(value); | 
|  | 6864 | Py_DECREF(result); | 
|  | 6865 | return NULL; | 
|  | 6866 | } | 
|  | 6867 |  | 
|  | 6868 | /* Create a three-level trie */ | 
|  | 6869 | result = PyObject_MALLOC(sizeof(struct encoding_map) + | 
|  | 6870 | 16*count2 + 128*count3 - 1); | 
|  | 6871 | if (!result) | 
|  | 6872 | return PyErr_NoMemory(); | 
|  | 6873 | PyObject_Init(result, &EncodingMapType); | 
|  | 6874 | mresult = (struct encoding_map*)result; | 
|  | 6875 | mresult->count2 = count2; | 
|  | 6876 | mresult->count3 = count3; | 
|  | 6877 | mlevel1 = mresult->level1; | 
|  | 6878 | mlevel2 = mresult->level23; | 
|  | 6879 | mlevel3 = mresult->level23 + 16*count2; | 
|  | 6880 | memcpy(mlevel1, level1, 32); | 
|  | 6881 | memset(mlevel2, 0xFF, 16*count2); | 
|  | 6882 | memset(mlevel3, 0, 128*count3); | 
|  | 6883 | count3 = 0; | 
|  | 6884 | for (i = 1; i < 256; i++) { | 
|  | 6885 | int o1, o2, o3, i2, i3; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6886 | if (PyUnicode_READ(kind, data, i) == 0xFFFE) | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6887 | /* unmapped character */ | 
|  | 6888 | continue; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6889 | o1 = PyUnicode_READ(kind, data, i)>>11; | 
|  | 6890 | o2 = (PyUnicode_READ(kind, data, i)>>7) & 0xF; | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6891 | i2 = 16*mlevel1[o1] + o2; | 
|  | 6892 | if (mlevel2[i2] == 0xFF) | 
|  | 6893 | mlevel2[i2] = count3++; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6894 | o3 = PyUnicode_READ(kind, data, i) & 0x7F; | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6895 | i3 = 128*mlevel2[i2] + o3; | 
|  | 6896 | mlevel3[i3] = i; | 
|  | 6897 | } | 
|  | 6898 | return result; | 
|  | 6899 | } | 
|  | 6900 |  | 
|  | 6901 | static int | 
|  | 6902 | encoding_map_lookup(Py_UNICODE c, PyObject *mapping) | 
|  | 6903 | { | 
|  | 6904 | struct encoding_map *map = (struct encoding_map*)mapping; | 
|  | 6905 | int l1 = c>>11; | 
|  | 6906 | int l2 = (c>>7) & 0xF; | 
|  | 6907 | int l3 = c & 0x7F; | 
|  | 6908 | int i; | 
|  | 6909 |  | 
|  | 6910 | #ifdef Py_UNICODE_WIDE | 
|  | 6911 | if (c > 0xFFFF) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6912 | return -1; | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6913 | } | 
|  | 6914 | #endif | 
|  | 6915 | if (c == 0) | 
|  | 6916 | return 0; | 
|  | 6917 | /* level 1*/ | 
|  | 6918 | i = map->level1[l1]; | 
|  | 6919 | if (i == 0xFF) { | 
|  | 6920 | return -1; | 
|  | 6921 | } | 
|  | 6922 | /* level 2*/ | 
|  | 6923 | i = map->level23[16*i+l2]; | 
|  | 6924 | if (i == 0xFF) { | 
|  | 6925 | return -1; | 
|  | 6926 | } | 
|  | 6927 | /* level 3 */ | 
|  | 6928 | i = map->level23[16*map->count2 + 128*i + l3]; | 
|  | 6929 | if (i == 0) { | 
|  | 6930 | return -1; | 
|  | 6931 | } | 
|  | 6932 | return i; | 
|  | 6933 | } | 
|  | 6934 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6935 | /* Lookup the character ch in the mapping. If the character | 
|  | 6936 | can't be found, Py_None is returned (or NULL, if another | 
| Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 6937 | error occurred). */ | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6938 | static PyObject * | 
|  | 6939 | charmapencode_lookup(Py_UNICODE c, PyObject *mapping) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6940 | { | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 6941 | PyObject *w = PyLong_FromLong((long)c); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6942 | PyObject *x; | 
|  | 6943 |  | 
|  | 6944 | if (w == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6945 | return NULL; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6946 | x = PyObject_GetItem(mapping, w); | 
|  | 6947 | Py_DECREF(w); | 
|  | 6948 | if (x == NULL) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6949 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { | 
|  | 6950 | /* No mapping found means: mapping is undefined. */ | 
|  | 6951 | PyErr_Clear(); | 
|  | 6952 | x = Py_None; | 
|  | 6953 | Py_INCREF(x); | 
|  | 6954 | return x; | 
|  | 6955 | } else | 
|  | 6956 | return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6957 | } | 
| Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 6958 | else if (x == Py_None) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6959 | return x; | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 6960 | else if (PyLong_Check(x)) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6961 | long value = PyLong_AS_LONG(x); | 
|  | 6962 | if (value < 0 || value > 255) { | 
|  | 6963 | PyErr_SetString(PyExc_TypeError, | 
|  | 6964 | "character mapping must be in range(256)"); | 
|  | 6965 | Py_DECREF(x); | 
|  | 6966 | return NULL; | 
|  | 6967 | } | 
|  | 6968 | return x; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6969 | } | 
| Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6970 | else if (PyBytes_Check(x)) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6971 | return x; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6972 | else { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6973 | /* wrong return value */ | 
|  | 6974 | PyErr_Format(PyExc_TypeError, | 
|  | 6975 | "character mapping must return integer, bytes or None, not %.400s", | 
|  | 6976 | x->ob_type->tp_name); | 
|  | 6977 | Py_DECREF(x); | 
|  | 6978 | return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6979 | } | 
|  | 6980 | } | 
|  | 6981 |  | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6982 | static int | 
| Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6983 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6984 | { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6985 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); | 
|  | 6986 | /* exponentially overallocate to minimize reallocations */ | 
|  | 6987 | if (requiredsize < 2*outsize) | 
|  | 6988 | requiredsize = 2*outsize; | 
|  | 6989 | if (_PyBytes_Resize(outobj, requiredsize)) | 
|  | 6990 | return -1; | 
|  | 6991 | return 0; | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6992 | } | 
|  | 6993 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6994 | typedef enum charmapencode_result { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6995 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6996 | } charmapencode_result; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6997 | /* 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] | 6998 | various state variables. Resize the output bytes object if not enough | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6999 | space is available. Return a new reference to the object that | 
|  | 7000 | was put in the output buffer, or Py_None, if the mapping was undefined | 
|  | 7001 | (in which case no character was written) or NULL, if a | 
| Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 7002 | reallocation error occurred. The caller must decref the result */ | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7003 | static charmapencode_result | 
|  | 7004 | charmapencode_output(Py_UNICODE c, PyObject *mapping, | 
|  | 7005 | PyObject **outobj, Py_ssize_t *outpos) | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7006 | { | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7007 | PyObject *rep; | 
|  | 7008 | char *outstart; | 
| Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7009 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7010 |  | 
| Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7011 | if (Py_TYPE(mapping) == &EncodingMapType) { | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7012 | int res = encoding_map_lookup(c, mapping); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7013 | Py_ssize_t requiredsize = *outpos+1; | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7014 | if (res == -1) | 
|  | 7015 | return enc_FAILED; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7016 | if (outsize<requiredsize) | 
|  | 7017 | if (charmapencode_resize(outobj, outpos, requiredsize)) | 
|  | 7018 | return enc_EXCEPTION; | 
| Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7019 | outstart = PyBytes_AS_STRING(*outobj); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7020 | outstart[(*outpos)++] = (char)res; | 
|  | 7021 | return enc_SUCCESS; | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7022 | } | 
|  | 7023 |  | 
|  | 7024 | rep = charmapencode_lookup(c, mapping); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7025 | if (rep==NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7026 | return enc_EXCEPTION; | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7027 | else if (rep==Py_None) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7028 | Py_DECREF(rep); | 
|  | 7029 | return enc_FAILED; | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7030 | } else { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7031 | if (PyLong_Check(rep)) { | 
|  | 7032 | Py_ssize_t requiredsize = *outpos+1; | 
|  | 7033 | if (outsize<requiredsize) | 
|  | 7034 | if (charmapencode_resize(outobj, outpos, requiredsize)) { | 
|  | 7035 | Py_DECREF(rep); | 
|  | 7036 | return enc_EXCEPTION; | 
|  | 7037 | } | 
| Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7038 | outstart = PyBytes_AS_STRING(*outobj); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7039 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7040 | } | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7041 | else { | 
|  | 7042 | const char *repchars = PyBytes_AS_STRING(rep); | 
|  | 7043 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); | 
|  | 7044 | Py_ssize_t requiredsize = *outpos+repsize; | 
|  | 7045 | if (outsize<requiredsize) | 
|  | 7046 | if (charmapencode_resize(outobj, outpos, requiredsize)) { | 
|  | 7047 | Py_DECREF(rep); | 
|  | 7048 | return enc_EXCEPTION; | 
|  | 7049 | } | 
| Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7050 | outstart = PyBytes_AS_STRING(*outobj); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7051 | memcpy(outstart + *outpos, repchars, repsize); | 
|  | 7052 | *outpos += repsize; | 
|  | 7053 | } | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7054 | } | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7055 | Py_DECREF(rep); | 
|  | 7056 | return enc_SUCCESS; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7057 | } | 
|  | 7058 |  | 
|  | 7059 | /* handle an error in PyUnicode_EncodeCharmap | 
|  | 7060 | Return 0 on success, -1 on error */ | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7061 | static int | 
|  | 7062 | charmap_encoding_error( | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7063 | const Py_UNICODE *p, Py_ssize_t size, Py_ssize_t *inpos, PyObject *mapping, | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7064 | PyObject **exceptionObject, | 
| Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 7065 | int *known_errorHandler, PyObject **errorHandler, const char *errors, | 
| Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 7066 | PyObject **res, Py_ssize_t *respos) | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7067 | { | 
|  | 7068 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7069 | Py_ssize_t repsize; | 
|  | 7070 | Py_ssize_t newpos; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7071 | Py_UNICODE *uni2; | 
|  | 7072 | /* startpos for collecting unencodable chars */ | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7073 | Py_ssize_t collstartpos = *inpos; | 
|  | 7074 | Py_ssize_t collendpos = *inpos+1; | 
|  | 7075 | Py_ssize_t collpos; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7076 | char *encoding = "charmap"; | 
|  | 7077 | char *reason = "character maps to <undefined>"; | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7078 | charmapencode_result x; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7079 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7080 | /* find all unencodable characters */ | 
|  | 7081 | while (collendpos < size) { | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7082 | PyObject *rep; | 
| Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7083 | if (Py_TYPE(mapping) == &EncodingMapType) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7084 | int res = encoding_map_lookup(p[collendpos], mapping); | 
|  | 7085 | if (res != -1) | 
|  | 7086 | break; | 
|  | 7087 | ++collendpos; | 
|  | 7088 | continue; | 
|  | 7089 | } | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7090 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7091 | rep = charmapencode_lookup(p[collendpos], mapping); | 
|  | 7092 | if (rep==NULL) | 
|  | 7093 | return -1; | 
|  | 7094 | else if (rep!=Py_None) { | 
|  | 7095 | Py_DECREF(rep); | 
|  | 7096 | break; | 
|  | 7097 | } | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7098 | Py_DECREF(rep); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7099 | ++collendpos; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7100 | } | 
|  | 7101 | /* cache callback name lookup | 
|  | 7102 | * (if not done yet, i.e. it's the first error) */ | 
|  | 7103 | if (*known_errorHandler==-1) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7104 | if ((errors==NULL) || (!strcmp(errors, "strict"))) | 
|  | 7105 | *known_errorHandler = 1; | 
|  | 7106 | else if (!strcmp(errors, "replace")) | 
|  | 7107 | *known_errorHandler = 2; | 
|  | 7108 | else if (!strcmp(errors, "ignore")) | 
|  | 7109 | *known_errorHandler = 3; | 
|  | 7110 | else if (!strcmp(errors, "xmlcharrefreplace")) | 
|  | 7111 | *known_errorHandler = 4; | 
|  | 7112 | else | 
|  | 7113 | *known_errorHandler = 0; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7114 | } | 
|  | 7115 | switch (*known_errorHandler) { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7116 | case 1: /* strict */ | 
|  | 7117 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); | 
|  | 7118 | return -1; | 
|  | 7119 | case 2: /* replace */ | 
|  | 7120 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7121 | x = charmapencode_output('?', mapping, res, respos); | 
|  | 7122 | if (x==enc_EXCEPTION) { | 
|  | 7123 | return -1; | 
|  | 7124 | } | 
|  | 7125 | else if (x==enc_FAILED) { | 
|  | 7126 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); | 
|  | 7127 | return -1; | 
|  | 7128 | } | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7129 | } | 
|  | 7130 | /* fall through */ | 
|  | 7131 | case 3: /* ignore */ | 
|  | 7132 | *inpos = collendpos; | 
|  | 7133 | break; | 
|  | 7134 | case 4: /* xmlcharrefreplace */ | 
|  | 7135 | /* generate replacement (temporarily (mis)uses p) */ | 
|  | 7136 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7137 | char buffer[2+29+1+1]; | 
|  | 7138 | char *cp; | 
|  | 7139 | sprintf(buffer, "&#%d;", (int)p[collpos]); | 
|  | 7140 | for (cp = buffer; *cp; ++cp) { | 
|  | 7141 | x = charmapencode_output(*cp, mapping, res, respos); | 
|  | 7142 | if (x==enc_EXCEPTION) | 
|  | 7143 | return -1; | 
|  | 7144 | else if (x==enc_FAILED) { | 
|  | 7145 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); | 
|  | 7146 | return -1; | 
|  | 7147 | } | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7148 | } | 
|  | 7149 | } | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7150 | *inpos = collendpos; | 
|  | 7151 | break; | 
|  | 7152 | default: | 
|  | 7153 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7154 | encoding, reason, p, size, exceptionObject, | 
|  | 7155 | collstartpos, collendpos, &newpos); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7156 | if (repunicode == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7157 | return -1; | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7158 | if (PyBytes_Check(repunicode)) { | 
|  | 7159 | /* Directly copy bytes result to output. */ | 
|  | 7160 | Py_ssize_t outsize = PyBytes_Size(*res); | 
|  | 7161 | Py_ssize_t requiredsize; | 
|  | 7162 | repsize = PyBytes_Size(repunicode); | 
|  | 7163 | requiredsize = *respos + repsize; | 
|  | 7164 | if (requiredsize > outsize) | 
|  | 7165 | /* Make room for all additional bytes. */ | 
|  | 7166 | if (charmapencode_resize(res, respos, requiredsize)) { | 
|  | 7167 | Py_DECREF(repunicode); | 
|  | 7168 | return -1; | 
|  | 7169 | } | 
|  | 7170 | memcpy(PyBytes_AsString(*res) + *respos, | 
|  | 7171 | PyBytes_AsString(repunicode),  repsize); | 
|  | 7172 | *respos += repsize; | 
|  | 7173 | *inpos = newpos; | 
| Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 7174 | Py_DECREF(repunicode); | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7175 | break; | 
| Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 7176 | } | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7177 | /* generate replacement  */ | 
|  | 7178 | repsize = PyUnicode_GET_SIZE(repunicode); | 
|  | 7179 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7180 | x = charmapencode_output(*uni2, mapping, res, respos); | 
|  | 7181 | if (x==enc_EXCEPTION) { | 
|  | 7182 | return -1; | 
|  | 7183 | } | 
|  | 7184 | else if (x==enc_FAILED) { | 
|  | 7185 | Py_DECREF(repunicode); | 
|  | 7186 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); | 
|  | 7187 | return -1; | 
|  | 7188 | } | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7189 | } | 
|  | 7190 | *inpos = newpos; | 
|  | 7191 | Py_DECREF(repunicode); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7192 | } | 
|  | 7193 | return 0; | 
|  | 7194 | } | 
|  | 7195 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7196 | PyObject * | 
|  | 7197 | PyUnicode_EncodeCharmap(const Py_UNICODE *p, | 
|  | 7198 | Py_ssize_t size, | 
|  | 7199 | PyObject *mapping, | 
|  | 7200 | const char *errors) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7201 | { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7202 | /* output object */ | 
|  | 7203 | PyObject *res = NULL; | 
|  | 7204 | /* current input position */ | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7205 | Py_ssize_t inpos = 0; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7206 | /* current output position */ | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7207 | Py_ssize_t respos = 0; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7208 | PyObject *errorHandler = NULL; | 
|  | 7209 | PyObject *exc = NULL; | 
|  | 7210 | /* the following variable is used for caching string comparisons | 
|  | 7211 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, | 
|  | 7212 | * 3=ignore, 4=xmlcharrefreplace */ | 
|  | 7213 | int known_errorHandler = -1; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7214 |  | 
|  | 7215 | /* Default to Latin-1 */ | 
|  | 7216 | if (mapping == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7217 | return PyUnicode_EncodeLatin1(p, size, errors); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7218 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7219 | /* allocate enough for a simple encoding without | 
|  | 7220 | replacements, if we need more, we'll resize */ | 
| Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7221 | res = PyBytes_FromStringAndSize(NULL, size); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7222 | if (res == NULL) | 
|  | 7223 | goto onError; | 
| Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 7224 | if (size == 0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7225 | return res; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7226 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7227 | while (inpos<size) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7228 | /* try to encode it */ | 
|  | 7229 | charmapencode_result x = charmapencode_output(p[inpos], mapping, &res, &respos); | 
|  | 7230 | if (x==enc_EXCEPTION) /* error */ | 
|  | 7231 | goto onError; | 
|  | 7232 | if (x==enc_FAILED) { /* unencodable character */ | 
|  | 7233 | if (charmap_encoding_error(p, size, &inpos, mapping, | 
|  | 7234 | &exc, | 
|  | 7235 | &known_errorHandler, &errorHandler, errors, | 
|  | 7236 | &res, &respos)) { | 
|  | 7237 | goto onError; | 
|  | 7238 | } | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7239 | } | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7240 | else | 
|  | 7241 | /* done with this character => adjust input position */ | 
|  | 7242 | ++inpos; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7243 | } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7244 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7245 | /* Resize if we allocated to much */ | 
| Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7246 | if (respos<PyBytes_GET_SIZE(res)) | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 7247 | if (_PyBytes_Resize(&res, respos) < 0) | 
|  | 7248 | goto onError; | 
| Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 7249 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7250 | Py_XDECREF(exc); | 
|  | 7251 | Py_XDECREF(errorHandler); | 
|  | 7252 | return res; | 
|  | 7253 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7254 | onError: | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7255 | Py_XDECREF(res); | 
|  | 7256 | Py_XDECREF(exc); | 
|  | 7257 | Py_XDECREF(errorHandler); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7258 | return NULL; | 
|  | 7259 | } | 
|  | 7260 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7261 | PyObject * | 
|  | 7262 | PyUnicode_AsCharmapString(PyObject *unicode, | 
|  | 7263 | PyObject *mapping) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7264 | { | 
|  | 7265 | if (!PyUnicode_Check(unicode) || mapping == NULL) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7266 | PyErr_BadArgument(); | 
|  | 7267 | return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7268 | } | 
|  | 7269 | return PyUnicode_EncodeCharmap(PyUnicode_AS_UNICODE(unicode), | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7270 | PyUnicode_GET_SIZE(unicode), | 
|  | 7271 | mapping, | 
|  | 7272 | NULL); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7273 | } | 
|  | 7274 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7275 | /* create or adjust a UnicodeTranslateError */ | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7276 | static void | 
|  | 7277 | make_translate_exception(PyObject **exceptionObject, | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7278 | PyObject *unicode, | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7279 | Py_ssize_t startpos, Py_ssize_t endpos, | 
|  | 7280 | const char *reason) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7281 | { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7282 | if (*exceptionObject == NULL) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7283 | *exceptionObject = _PyUnicodeTranslateError_Create( | 
|  | 7284 | unicode, startpos, endpos, reason); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7285 | } | 
|  | 7286 | else { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7287 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) | 
|  | 7288 | goto onError; | 
|  | 7289 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) | 
|  | 7290 | goto onError; | 
|  | 7291 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) | 
|  | 7292 | goto onError; | 
|  | 7293 | return; | 
|  | 7294 | onError: | 
|  | 7295 | Py_DECREF(*exceptionObject); | 
|  | 7296 | *exceptionObject = NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7297 | } | 
|  | 7298 | } | 
|  | 7299 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7300 | /* raises a UnicodeTranslateError */ | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7301 | static void | 
|  | 7302 | raise_translate_exception(PyObject **exceptionObject, | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7303 | PyObject *unicode, | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7304 | Py_ssize_t startpos, Py_ssize_t endpos, | 
|  | 7305 | const char *reason) | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7306 | { | 
|  | 7307 | make_translate_exception(exceptionObject, | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7308 | unicode, startpos, endpos, reason); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7309 | if (*exceptionObject != NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7310 | PyCodec_StrictErrors(*exceptionObject); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7311 | } | 
|  | 7312 |  | 
|  | 7313 | /* error handling callback helper: | 
|  | 7314 | build arguments, call the callback and check the arguments, | 
|  | 7315 | put the result into newpos and return the replacement string, which | 
|  | 7316 | has to be freed by the caller */ | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7317 | static PyObject * | 
|  | 7318 | unicode_translate_call_errorhandler(const char *errors, | 
|  | 7319 | PyObject **errorHandler, | 
|  | 7320 | const char *reason, | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7321 | PyObject *unicode, PyObject **exceptionObject, | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7322 | Py_ssize_t startpos, Py_ssize_t endpos, | 
|  | 7323 | Py_ssize_t *newpos) | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7324 | { | 
| Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7325 | 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] | 7326 |  | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7327 | Py_ssize_t i_newpos; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7328 | PyObject *restuple; | 
|  | 7329 | PyObject *resunicode; | 
|  | 7330 |  | 
|  | 7331 | if (*errorHandler == NULL) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7332 | *errorHandler = PyCodec_LookupError(errors); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7333 | if (*errorHandler == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7334 | return NULL; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7335 | } | 
|  | 7336 |  | 
|  | 7337 | make_translate_exception(exceptionObject, | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7338 | unicode, startpos, endpos, reason); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7339 | if (*exceptionObject == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7340 | return NULL; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7341 |  | 
|  | 7342 | restuple = PyObject_CallFunctionObjArgs( | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7343 | *errorHandler, *exceptionObject, NULL); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7344 | if (restuple == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7345 | return NULL; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7346 | if (!PyTuple_Check(restuple)) { | 
| Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 7347 | PyErr_SetString(PyExc_TypeError, &argparse[4]); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7348 | Py_DECREF(restuple); | 
|  | 7349 | return NULL; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7350 | } | 
|  | 7351 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7352 | &resunicode, &i_newpos)) { | 
|  | 7353 | Py_DECREF(restuple); | 
|  | 7354 | return NULL; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7355 | } | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7356 | if (i_newpos<0) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7357 | *newpos = PyUnicode_GET_LENGTH(unicode)+i_newpos; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7358 | else | 
|  | 7359 | *newpos = i_newpos; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7360 | if (*newpos<0 || *newpos>PyUnicode_GET_LENGTH(unicode)) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7361 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); | 
|  | 7362 | Py_DECREF(restuple); | 
|  | 7363 | return NULL; | 
| Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 7364 | } | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7365 | Py_INCREF(resunicode); | 
|  | 7366 | Py_DECREF(restuple); | 
|  | 7367 | return resunicode; | 
|  | 7368 | } | 
|  | 7369 |  | 
|  | 7370 | /* Lookup the character ch in the mapping and put the result in result, | 
|  | 7371 | which must be decrefed by the caller. | 
|  | 7372 | Return 0 on success, -1 on error */ | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7373 | static int | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7374 | charmaptranslate_lookup(Py_UCS4 c, PyObject *mapping, PyObject **result) | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7375 | { | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7376 | PyObject *w = PyLong_FromLong((long)c); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7377 | PyObject *x; | 
|  | 7378 |  | 
|  | 7379 | if (w == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7380 | return -1; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7381 | x = PyObject_GetItem(mapping, w); | 
|  | 7382 | Py_DECREF(w); | 
|  | 7383 | if (x == NULL) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7384 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { | 
|  | 7385 | /* No mapping found means: use 1:1 mapping. */ | 
|  | 7386 | PyErr_Clear(); | 
|  | 7387 | *result = NULL; | 
|  | 7388 | return 0; | 
|  | 7389 | } else | 
|  | 7390 | return -1; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7391 | } | 
|  | 7392 | else if (x == Py_None) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7393 | *result = x; | 
|  | 7394 | return 0; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7395 | } | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7396 | else if (PyLong_Check(x)) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7397 | long value = PyLong_AS_LONG(x); | 
|  | 7398 | long max = PyUnicode_GetMax(); | 
|  | 7399 | if (value < 0 || value > max) { | 
|  | 7400 | PyErr_Format(PyExc_TypeError, | 
| Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 7401 | "character mapping must be in range(0x%x)", max+1); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7402 | Py_DECREF(x); | 
|  | 7403 | return -1; | 
|  | 7404 | } | 
|  | 7405 | *result = x; | 
|  | 7406 | return 0; | 
|  | 7407 | } | 
|  | 7408 | else if (PyUnicode_Check(x)) { | 
|  | 7409 | *result = x; | 
|  | 7410 | return 0; | 
|  | 7411 | } | 
|  | 7412 | else { | 
|  | 7413 | /* wrong return value */ | 
|  | 7414 | PyErr_SetString(PyExc_TypeError, | 
|  | 7415 | "character mapping must return integer, None or str"); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7416 | Py_DECREF(x); | 
|  | 7417 | return -1; | 
|  | 7418 | } | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7419 | } | 
|  | 7420 | /* ensure that *outobj is at least requiredsize characters long, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7421 | if not reallocate and adjust various state variables. | 
|  | 7422 | Return 0 on success, -1 on error */ | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7423 | static int | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7424 | charmaptranslate_makespace(Py_UCS4 **outobj, Py_ssize_t *psize, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7425 | Py_ssize_t requiredsize) | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7426 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7427 | Py_ssize_t oldsize = *psize; | 
| Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 7428 | if (requiredsize > oldsize) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7429 | /* exponentially overallocate to minimize reallocations */ | 
|  | 7430 | if (requiredsize < 2 * oldsize) | 
|  | 7431 | requiredsize = 2 * oldsize; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7432 | *outobj = PyMem_Realloc(*outobj, requiredsize * sizeof(Py_UCS4)); | 
|  | 7433 | if (*outobj == 0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7434 | return -1; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7435 | *psize = requiredsize; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7436 | } | 
|  | 7437 | return 0; | 
|  | 7438 | } | 
|  | 7439 | /* lookup the character, put the result in the output string and adjust | 
|  | 7440 | various state variables. Return a new reference to the object that | 
|  | 7441 | was put in the output buffer in *result, or Py_None, if the mapping was | 
|  | 7442 | undefined (in which case no character was written). | 
|  | 7443 | The called must decref result. | 
|  | 7444 | Return 0 on success, -1 on error. */ | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7445 | static int | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7446 | charmaptranslate_output(PyObject *input, Py_ssize_t ipos, | 
|  | 7447 | PyObject *mapping, Py_UCS4 **output, | 
|  | 7448 | Py_ssize_t *osize, Py_ssize_t *opos, | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7449 | PyObject **res) | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7450 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7451 | Py_UCS4 curinp = PyUnicode_READ_CHAR(input, ipos); | 
|  | 7452 | if (charmaptranslate_lookup(curinp, mapping, res)) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7453 | return -1; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7454 | if (*res==NULL) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7455 | /* not found => default to 1:1 mapping */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7456 | (*output)[(*opos)++] = curinp; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7457 | } | 
|  | 7458 | else if (*res==Py_None) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7459 | ; | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7460 | else if (PyLong_Check(*res)) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7461 | /* 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] | 7462 | (*output)[(*opos)++] = (Py_UCS4)PyLong_AS_LONG(*res); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7463 | } | 
|  | 7464 | else if (PyUnicode_Check(*res)) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7465 | Py_ssize_t repsize; | 
|  | 7466 | if (PyUnicode_READY(*res) == -1) | 
|  | 7467 | return -1; | 
|  | 7468 | repsize = PyUnicode_GET_LENGTH(*res); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7469 | if (repsize==1) { | 
|  | 7470 | /* 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] | 7471 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, 0); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7472 | } | 
|  | 7473 | else if (repsize!=0) { | 
|  | 7474 | /* more than one character */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7475 | Py_ssize_t requiredsize = *opos + | 
|  | 7476 | (PyUnicode_GET_LENGTH(input) - ipos) + | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7477 | repsize - 1; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7478 | Py_ssize_t i; | 
|  | 7479 | if (charmaptranslate_makespace(output, osize, requiredsize)) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7480 | return -1; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7481 | for(i = 0; i < repsize; i++) | 
|  | 7482 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, i); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7483 | } | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7484 | } | 
|  | 7485 | else | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7486 | return -1; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7487 | return 0; | 
|  | 7488 | } | 
|  | 7489 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7490 | PyObject * | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7491 | _PyUnicode_TranslateCharmap(PyObject *input, | 
|  | 7492 | PyObject *mapping, | 
|  | 7493 | const char *errors) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7494 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7495 | /* input object */ | 
|  | 7496 | char *idata; | 
|  | 7497 | Py_ssize_t size, i; | 
|  | 7498 | int kind; | 
|  | 7499 | /* output buffer */ | 
|  | 7500 | Py_UCS4 *output = NULL; | 
|  | 7501 | Py_ssize_t osize; | 
|  | 7502 | PyObject *res; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7503 | /* current output position */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7504 | Py_ssize_t opos; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7505 | char *reason = "character maps to <undefined>"; | 
|  | 7506 | PyObject *errorHandler = NULL; | 
|  | 7507 | PyObject *exc = NULL; | 
|  | 7508 | /* the following variable is used for caching string comparisons | 
|  | 7509 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, | 
|  | 7510 | * 3=ignore, 4=xmlcharrefreplace */ | 
|  | 7511 | int known_errorHandler = -1; | 
|  | 7512 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7513 | if (mapping == NULL) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7514 | PyErr_BadArgument(); | 
|  | 7515 | return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7516 | } | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7517 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7518 | if (PyUnicode_READY(input) == -1) | 
|  | 7519 | return NULL; | 
|  | 7520 | idata = (char*)PyUnicode_DATA(input); | 
|  | 7521 | kind = PyUnicode_KIND(input); | 
|  | 7522 | size = PyUnicode_GET_LENGTH(input); | 
|  | 7523 | i = 0; | 
|  | 7524 |  | 
|  | 7525 | if (size == 0) { | 
|  | 7526 | Py_INCREF(input); | 
|  | 7527 | return input; | 
|  | 7528 | } | 
|  | 7529 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7530 | /* allocate enough for a simple 1:1 translation without | 
|  | 7531 | replacements, if we need more, we'll resize */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7532 | osize = size; | 
|  | 7533 | output = PyMem_Malloc(osize * sizeof(Py_UCS4)); | 
|  | 7534 | opos = 0; | 
|  | 7535 | if (output == NULL) { | 
|  | 7536 | PyErr_NoMemory(); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7537 | goto onError; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7538 | } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7539 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7540 | while (i<size) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7541 | /* try to encode it */ | 
|  | 7542 | PyObject *x = NULL; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7543 | if (charmaptranslate_output(input, i, mapping, | 
|  | 7544 | &output, &osize, &opos, &x)) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7545 | Py_XDECREF(x); | 
|  | 7546 | goto onError; | 
|  | 7547 | } | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7548 | Py_XDECREF(x); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7549 | if (x!=Py_None) /* it worked => adjust input pointer */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7550 | ++i; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7551 | else { /* untranslatable character */ | 
|  | 7552 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ | 
|  | 7553 | Py_ssize_t repsize; | 
|  | 7554 | Py_ssize_t newpos; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7555 | Py_ssize_t uni2; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7556 | /* startpos for collecting untranslatable chars */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7557 | Py_ssize_t collstart = i; | 
|  | 7558 | Py_ssize_t collend = i+1; | 
|  | 7559 | Py_ssize_t coll; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7560 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7561 | /* find all untranslatable characters */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7562 | while (collend < size) { | 
|  | 7563 | if (charmaptranslate_lookup(PyUnicode_READ(kind,idata, collend), mapping, &x)) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7564 | goto onError; | 
|  | 7565 | Py_XDECREF(x); | 
|  | 7566 | if (x!=Py_None) | 
|  | 7567 | break; | 
|  | 7568 | ++collend; | 
|  | 7569 | } | 
|  | 7570 | /* cache callback name lookup | 
|  | 7571 | * (if not done yet, i.e. it's the first error) */ | 
|  | 7572 | if (known_errorHandler==-1) { | 
|  | 7573 | if ((errors==NULL) || (!strcmp(errors, "strict"))) | 
|  | 7574 | known_errorHandler = 1; | 
|  | 7575 | else if (!strcmp(errors, "replace")) | 
|  | 7576 | known_errorHandler = 2; | 
|  | 7577 | else if (!strcmp(errors, "ignore")) | 
|  | 7578 | known_errorHandler = 3; | 
|  | 7579 | else if (!strcmp(errors, "xmlcharrefreplace")) | 
|  | 7580 | known_errorHandler = 4; | 
|  | 7581 | else | 
|  | 7582 | known_errorHandler = 0; | 
|  | 7583 | } | 
|  | 7584 | switch (known_errorHandler) { | 
|  | 7585 | case 1: /* strict */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7586 | raise_translate_exception(&exc, input, collstart, | 
|  | 7587 | collend, reason); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7588 | goto onError; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7589 | case 2: /* replace */ | 
|  | 7590 | /* 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] | 7591 | for (coll = collstart; coll<collend; coll++) | 
|  | 7592 | output[opos++] = '?'; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7593 | /* fall through */ | 
|  | 7594 | case 3: /* ignore */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7595 | i = collend; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7596 | break; | 
|  | 7597 | case 4: /* xmlcharrefreplace */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7598 | /* generate replacement (temporarily (mis)uses i) */ | 
|  | 7599 | for (i = collstart; i < collend; ++i) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7600 | char buffer[2+29+1+1]; | 
|  | 7601 | char *cp; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7602 | sprintf(buffer, "&#%d;", PyUnicode_READ(kind, idata, i)); | 
|  | 7603 | if (charmaptranslate_makespace(&output, &osize, | 
|  | 7604 | opos+strlen(buffer)+(size-collend))) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7605 | goto onError; | 
|  | 7606 | for (cp = buffer; *cp; ++cp) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7607 | output[opos++] = *cp; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7608 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7609 | i = collend; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7610 | break; | 
|  | 7611 | default: | 
|  | 7612 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7613 | reason, input, &exc, | 
|  | 7614 | collstart, collend, &newpos); | 
|  | 7615 | if (repunicode == NULL || PyUnicode_READY(repunicode) == -1) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7616 | goto onError; | 
|  | 7617 | /* generate replacement  */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7618 | repsize = PyUnicode_GET_LENGTH(repunicode); | 
|  | 7619 | if (charmaptranslate_makespace(&output, &osize, | 
|  | 7620 | opos+repsize+(size-collend))) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7621 | Py_DECREF(repunicode); | 
|  | 7622 | goto onError; | 
|  | 7623 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7624 | for (uni2 = 0; repsize-->0; ++uni2) | 
|  | 7625 | output[opos++] = PyUnicode_READ_CHAR(repunicode, uni2); | 
|  | 7626 | i = newpos; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7627 | Py_DECREF(repunicode); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7628 | } | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7629 | } | 
|  | 7630 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7631 | res = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, output, opos); | 
|  | 7632 | if (!res) | 
|  | 7633 | goto onError; | 
|  | 7634 | PyMem_Free(output); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7635 | Py_XDECREF(exc); | 
|  | 7636 | Py_XDECREF(errorHandler); | 
|  | 7637 | return res; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7638 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7639 | onError: | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7640 | PyMem_Free(output); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7641 | Py_XDECREF(exc); | 
|  | 7642 | Py_XDECREF(errorHandler); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7643 | return NULL; | 
|  | 7644 | } | 
|  | 7645 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7646 | /* Deprecated. Use PyUnicode_Translate instead. */ | 
|  | 7647 | PyObject * | 
|  | 7648 | PyUnicode_TranslateCharmap(const Py_UNICODE *p, | 
|  | 7649 | Py_ssize_t size, | 
|  | 7650 | PyObject *mapping, | 
|  | 7651 | const char *errors) | 
|  | 7652 | { | 
|  | 7653 | PyObject *unicode = PyUnicode_FromUnicode(p, size); | 
|  | 7654 | if (!unicode) | 
|  | 7655 | return NULL; | 
|  | 7656 | return _PyUnicode_TranslateCharmap(unicode, mapping, errors); | 
|  | 7657 | } | 
|  | 7658 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7659 | PyObject * | 
|  | 7660 | PyUnicode_Translate(PyObject *str, | 
|  | 7661 | PyObject *mapping, | 
|  | 7662 | const char *errors) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7663 | { | 
|  | 7664 | PyObject *result; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7665 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7666 | str = PyUnicode_FromObject(str); | 
|  | 7667 | if (str == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7668 | goto onError; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7669 | result = _PyUnicode_TranslateCharmap(str, mapping, errors); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7670 | Py_DECREF(str); | 
|  | 7671 | return result; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7672 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7673 | onError: | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7674 | Py_XDECREF(str); | 
|  | 7675 | return NULL; | 
|  | 7676 | } | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7677 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7678 | static Py_UCS4 | 
|  | 7679 | fix_decimal_and_space_to_ascii(PyUnicodeObject *self) | 
|  | 7680 | { | 
|  | 7681 | /* No need to call PyUnicode_READY(self) because this function is only | 
|  | 7682 | called as a callback from fixup() which does it already. */ | 
|  | 7683 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); | 
|  | 7684 | const int kind = PyUnicode_KIND(self); | 
|  | 7685 | void *data = PyUnicode_DATA(self); | 
|  | 7686 | Py_UCS4 maxchar = 0, ch, fixed; | 
|  | 7687 | Py_ssize_t i; | 
|  | 7688 |  | 
|  | 7689 | for (i = 0; i < len; ++i) { | 
|  | 7690 | ch = PyUnicode_READ(kind, data, i); | 
|  | 7691 | fixed = 0; | 
|  | 7692 | if (ch > 127) { | 
|  | 7693 | if (Py_UNICODE_ISSPACE(ch)) | 
|  | 7694 | fixed = ' '; | 
|  | 7695 | else { | 
|  | 7696 | const int decimal = Py_UNICODE_TODECIMAL(ch); | 
|  | 7697 | if (decimal >= 0) | 
|  | 7698 | fixed = '0' + decimal; | 
|  | 7699 | } | 
|  | 7700 | if (fixed != 0) { | 
|  | 7701 | if (fixed > maxchar) | 
|  | 7702 | maxchar = fixed; | 
|  | 7703 | PyUnicode_WRITE(kind, data, i, fixed); | 
|  | 7704 | } | 
|  | 7705 | else if (ch > maxchar) | 
|  | 7706 | maxchar = ch; | 
|  | 7707 | } | 
|  | 7708 | else if (ch > maxchar) | 
|  | 7709 | maxchar = ch; | 
|  | 7710 | } | 
|  | 7711 |  | 
|  | 7712 | return maxchar; | 
|  | 7713 | } | 
|  | 7714 |  | 
|  | 7715 | PyObject * | 
|  | 7716 | _PyUnicode_TransformDecimalAndSpaceToASCII(PyObject *unicode) | 
|  | 7717 | { | 
|  | 7718 | if (!PyUnicode_Check(unicode)) { | 
|  | 7719 | PyErr_BadInternalCall(); | 
|  | 7720 | return NULL; | 
|  | 7721 | } | 
|  | 7722 | if (PyUnicode_READY(unicode) == -1) | 
|  | 7723 | return NULL; | 
|  | 7724 | if (PyUnicode_MAX_CHAR_VALUE(unicode) <= 127) { | 
|  | 7725 | /* If the string is already ASCII, just return the same string */ | 
|  | 7726 | Py_INCREF(unicode); | 
|  | 7727 | return unicode; | 
|  | 7728 | } | 
|  | 7729 | return fixup((PyUnicodeObject *)unicode, fix_decimal_and_space_to_ascii); | 
|  | 7730 | } | 
|  | 7731 |  | 
| Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 7732 | PyObject * | 
|  | 7733 | PyUnicode_TransformDecimalToASCII(Py_UNICODE *s, | 
|  | 7734 | Py_ssize_t length) | 
|  | 7735 | { | 
|  | 7736 | PyObject *result; | 
|  | 7737 | Py_UNICODE *p; /* write pointer into result */ | 
|  | 7738 | Py_ssize_t i; | 
|  | 7739 | /* Copy to a new string */ | 
|  | 7740 | result = (PyObject *)_PyUnicode_New(length); | 
|  | 7741 | Py_UNICODE_COPY(PyUnicode_AS_UNICODE(result), s, length); | 
|  | 7742 | if (result == NULL) | 
|  | 7743 | return result; | 
|  | 7744 | p = PyUnicode_AS_UNICODE(result); | 
|  | 7745 | /* Iterate over code points */ | 
|  | 7746 | for (i = 0; i < length; i++) { | 
|  | 7747 | Py_UNICODE ch =s[i]; | 
|  | 7748 | if (ch > 127) { | 
|  | 7749 | int decimal = Py_UNICODE_TODECIMAL(ch); | 
|  | 7750 | if (decimal >= 0) | 
|  | 7751 | p[i] = '0' + decimal; | 
|  | 7752 | } | 
|  | 7753 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7754 | if (PyUnicode_READY((PyUnicodeObject*)result) == -1) { | 
|  | 7755 | Py_DECREF(result); | 
|  | 7756 | return NULL; | 
|  | 7757 | } | 
| Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 7758 | return result; | 
|  | 7759 | } | 
| Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 7760 | /* --- Decimal Encoder ---------------------------------------------------- */ | 
|  | 7761 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7762 | int | 
|  | 7763 | PyUnicode_EncodeDecimal(Py_UNICODE *s, | 
|  | 7764 | Py_ssize_t length, | 
|  | 7765 | char *output, | 
|  | 7766 | const char *errors) | 
| Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 7767 | { | 
|  | 7768 | Py_UNICODE *p, *end; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7769 | PyObject *errorHandler = NULL; | 
|  | 7770 | PyObject *exc = NULL; | 
|  | 7771 | const char *encoding = "decimal"; | 
|  | 7772 | const char *reason = "invalid decimal Unicode string"; | 
|  | 7773 | /* the following variable is used for caching string comparisons | 
|  | 7774 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ | 
|  | 7775 | int known_errorHandler = -1; | 
| Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 7776 |  | 
|  | 7777 | if (output == NULL) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7778 | PyErr_BadArgument(); | 
|  | 7779 | return -1; | 
| Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 7780 | } | 
|  | 7781 |  | 
|  | 7782 | p = s; | 
|  | 7783 | end = s + length; | 
|  | 7784 | while (p < end) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7785 | register Py_UNICODE ch = *p; | 
|  | 7786 | int decimal; | 
|  | 7787 | PyObject *repunicode; | 
|  | 7788 | Py_ssize_t repsize; | 
|  | 7789 | Py_ssize_t newpos; | 
|  | 7790 | Py_UNICODE *uni2; | 
|  | 7791 | Py_UNICODE *collstart; | 
|  | 7792 | Py_UNICODE *collend; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7793 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7794 | if (Py_UNICODE_ISSPACE(ch)) { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7795 | *output++ = ' '; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7796 | ++p; | 
|  | 7797 | continue; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7798 | } | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7799 | decimal = Py_UNICODE_TODECIMAL(ch); | 
|  | 7800 | if (decimal >= 0) { | 
|  | 7801 | *output++ = '0' + decimal; | 
|  | 7802 | ++p; | 
|  | 7803 | continue; | 
|  | 7804 | } | 
|  | 7805 | if (0 < ch && ch < 256) { | 
|  | 7806 | *output++ = (char)ch; | 
|  | 7807 | ++p; | 
|  | 7808 | continue; | 
|  | 7809 | } | 
|  | 7810 | /* All other characters are considered unencodable */ | 
|  | 7811 | collstart = p; | 
|  | 7812 | collend = p+1; | 
|  | 7813 | while (collend < end) { | 
|  | 7814 | if ((0 < *collend && *collend < 256) || | 
|  | 7815 | !Py_UNICODE_ISSPACE(*collend) || | 
|  | 7816 | Py_UNICODE_TODECIMAL(*collend)) | 
|  | 7817 | break; | 
|  | 7818 | } | 
|  | 7819 | /* cache callback name lookup | 
|  | 7820 | * (if not done yet, i.e. it's the first error) */ | 
|  | 7821 | if (known_errorHandler==-1) { | 
|  | 7822 | if ((errors==NULL) || (!strcmp(errors, "strict"))) | 
|  | 7823 | known_errorHandler = 1; | 
|  | 7824 | else if (!strcmp(errors, "replace")) | 
|  | 7825 | known_errorHandler = 2; | 
|  | 7826 | else if (!strcmp(errors, "ignore")) | 
|  | 7827 | known_errorHandler = 3; | 
|  | 7828 | else if (!strcmp(errors, "xmlcharrefreplace")) | 
|  | 7829 | known_errorHandler = 4; | 
|  | 7830 | else | 
|  | 7831 | known_errorHandler = 0; | 
|  | 7832 | } | 
|  | 7833 | switch (known_errorHandler) { | 
|  | 7834 | case 1: /* strict */ | 
|  | 7835 | raise_encode_exception(&exc, encoding, s, length, collstart-s, collend-s, reason); | 
|  | 7836 | goto onError; | 
|  | 7837 | case 2: /* replace */ | 
|  | 7838 | for (p = collstart; p < collend; ++p) | 
|  | 7839 | *output++ = '?'; | 
|  | 7840 | /* fall through */ | 
|  | 7841 | case 3: /* ignore */ | 
|  | 7842 | p = collend; | 
|  | 7843 | break; | 
|  | 7844 | case 4: /* xmlcharrefreplace */ | 
|  | 7845 | /* generate replacement (temporarily (mis)uses p) */ | 
|  | 7846 | for (p = collstart; p < collend; ++p) | 
|  | 7847 | output += sprintf(output, "&#%d;", (int)*p); | 
|  | 7848 | p = collend; | 
|  | 7849 | break; | 
|  | 7850 | default: | 
|  | 7851 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, | 
|  | 7852 | encoding, reason, s, length, &exc, | 
|  | 7853 | collstart-s, collend-s, &newpos); | 
|  | 7854 | if (repunicode == NULL) | 
|  | 7855 | goto onError; | 
| Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 7856 | if (!PyUnicode_Check(repunicode)) { | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7857 | /* Byte results not supported, since they have no decimal property. */ | 
| Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 7858 | PyErr_SetString(PyExc_TypeError, "error handler should return unicode"); | 
|  | 7859 | Py_DECREF(repunicode); | 
|  | 7860 | goto onError; | 
|  | 7861 | } | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7862 | /* generate replacement  */ | 
|  | 7863 | repsize = PyUnicode_GET_SIZE(repunicode); | 
|  | 7864 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { | 
|  | 7865 | Py_UNICODE ch = *uni2; | 
|  | 7866 | if (Py_UNICODE_ISSPACE(ch)) | 
|  | 7867 | *output++ = ' '; | 
|  | 7868 | else { | 
|  | 7869 | decimal = Py_UNICODE_TODECIMAL(ch); | 
|  | 7870 | if (decimal >= 0) | 
|  | 7871 | *output++ = '0' + decimal; | 
|  | 7872 | else if (0 < ch && ch < 256) | 
|  | 7873 | *output++ = (char)ch; | 
|  | 7874 | else { | 
|  | 7875 | Py_DECREF(repunicode); | 
|  | 7876 | raise_encode_exception(&exc, encoding, | 
|  | 7877 | s, length, collstart-s, collend-s, reason); | 
|  | 7878 | goto onError; | 
|  | 7879 | } | 
|  | 7880 | } | 
|  | 7881 | } | 
|  | 7882 | p = s + newpos; | 
|  | 7883 | Py_DECREF(repunicode); | 
|  | 7884 | } | 
| Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 7885 | } | 
|  | 7886 | /* 0-terminate the output string */ | 
|  | 7887 | *output++ = '\0'; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7888 | Py_XDECREF(exc); | 
|  | 7889 | Py_XDECREF(errorHandler); | 
| Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 7890 | return 0; | 
|  | 7891 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7892 | onError: | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7893 | Py_XDECREF(exc); | 
|  | 7894 | Py_XDECREF(errorHandler); | 
| Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 7895 | return -1; | 
|  | 7896 | } | 
|  | 7897 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7898 | /* --- Helpers ------------------------------------------------------------ */ | 
|  | 7899 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7900 | #include "stringlib/ucs1lib.h" | 
|  | 7901 | #include "stringlib/fastsearch.h" | 
|  | 7902 | #include "stringlib/partition.h" | 
|  | 7903 | #include "stringlib/split.h" | 
|  | 7904 | #include "stringlib/count.h" | 
|  | 7905 | #include "stringlib/find.h" | 
|  | 7906 | #include "stringlib/localeutil.h" | 
|  | 7907 | #include "stringlib/undef.h" | 
|  | 7908 |  | 
|  | 7909 | #include "stringlib/ucs2lib.h" | 
|  | 7910 | #include "stringlib/fastsearch.h" | 
|  | 7911 | #include "stringlib/partition.h" | 
|  | 7912 | #include "stringlib/split.h" | 
|  | 7913 | #include "stringlib/count.h" | 
|  | 7914 | #include "stringlib/find.h" | 
|  | 7915 | #include "stringlib/localeutil.h" | 
|  | 7916 | #include "stringlib/undef.h" | 
|  | 7917 |  | 
|  | 7918 | #include "stringlib/ucs4lib.h" | 
|  | 7919 | #include "stringlib/fastsearch.h" | 
|  | 7920 | #include "stringlib/partition.h" | 
|  | 7921 | #include "stringlib/split.h" | 
|  | 7922 | #include "stringlib/count.h" | 
|  | 7923 | #include "stringlib/find.h" | 
|  | 7924 | #include "stringlib/localeutil.h" | 
|  | 7925 | #include "stringlib/undef.h" | 
|  | 7926 |  | 
|  | 7927 | static Py_ssize_t | 
|  | 7928 | any_find_slice(Py_ssize_t Py_LOCAL_CALLBACK(ucs1)(const Py_UCS1*, Py_ssize_t, | 
|  | 7929 | const Py_UCS1*, Py_ssize_t, | 
|  | 7930 | Py_ssize_t, Py_ssize_t), | 
|  | 7931 | Py_ssize_t Py_LOCAL_CALLBACK(ucs2)(const Py_UCS2*, Py_ssize_t, | 
|  | 7932 | const Py_UCS2*, Py_ssize_t, | 
|  | 7933 | Py_ssize_t, Py_ssize_t), | 
|  | 7934 | Py_ssize_t Py_LOCAL_CALLBACK(ucs4)(const Py_UCS4*, Py_ssize_t, | 
|  | 7935 | const Py_UCS4*, Py_ssize_t, | 
|  | 7936 | Py_ssize_t, Py_ssize_t), | 
|  | 7937 | PyObject* s1, PyObject* s2, | 
|  | 7938 | Py_ssize_t start, | 
|  | 7939 | Py_ssize_t end) | 
|  | 7940 | { | 
|  | 7941 | int kind1, kind2, kind; | 
|  | 7942 | void *buf1, *buf2; | 
|  | 7943 | Py_ssize_t len1, len2, result; | 
|  | 7944 |  | 
|  | 7945 | kind1 = PyUnicode_KIND(s1); | 
|  | 7946 | kind2 = PyUnicode_KIND(s2); | 
|  | 7947 | kind = kind1 > kind2 ? kind1 : kind2; | 
|  | 7948 | buf1 = PyUnicode_DATA(s1); | 
|  | 7949 | buf2 = PyUnicode_DATA(s2); | 
|  | 7950 | if (kind1 != kind) | 
|  | 7951 | buf1 = _PyUnicode_AsKind(s1, kind); | 
|  | 7952 | if (!buf1) | 
|  | 7953 | return -2; | 
|  | 7954 | if (kind2 != kind) | 
|  | 7955 | buf2 = _PyUnicode_AsKind(s2, kind); | 
|  | 7956 | if (!buf2) { | 
|  | 7957 | if (kind1 != kind) PyMem_Free(buf1); | 
|  | 7958 | return -2; | 
|  | 7959 | } | 
|  | 7960 | len1 = PyUnicode_GET_LENGTH(s1); | 
|  | 7961 | len2 = PyUnicode_GET_LENGTH(s2); | 
|  | 7962 |  | 
|  | 7963 | switch(kind) { | 
|  | 7964 | case PyUnicode_1BYTE_KIND: | 
|  | 7965 | result = ucs1(buf1, len1, buf2, len2, start, end); | 
|  | 7966 | break; | 
|  | 7967 | case PyUnicode_2BYTE_KIND: | 
|  | 7968 | result = ucs2(buf1, len1, buf2, len2, start, end); | 
|  | 7969 | break; | 
|  | 7970 | case PyUnicode_4BYTE_KIND: | 
|  | 7971 | result = ucs4(buf1, len1, buf2, len2, start, end); | 
|  | 7972 | break; | 
|  | 7973 | default: | 
|  | 7974 | assert(0); result = -2; | 
|  | 7975 | } | 
|  | 7976 |  | 
|  | 7977 | if (kind1 != kind) | 
|  | 7978 | PyMem_Free(buf1); | 
|  | 7979 | if (kind2 != kind) | 
|  | 7980 | PyMem_Free(buf2); | 
|  | 7981 |  | 
|  | 7982 | return result; | 
|  | 7983 | } | 
|  | 7984 |  | 
|  | 7985 | Py_ssize_t | 
|  | 7986 | _PyUnicode_InsertThousandsGrouping(int kind, void *data, | 
|  | 7987 | Py_ssize_t n_buffer, | 
|  | 7988 | void *digits, Py_ssize_t n_digits, | 
|  | 7989 | Py_ssize_t min_width, | 
|  | 7990 | const char *grouping, | 
|  | 7991 | const char *thousands_sep) | 
|  | 7992 | { | 
|  | 7993 | switch(kind) { | 
|  | 7994 | case PyUnicode_1BYTE_KIND: | 
|  | 7995 | return _PyUnicode_ucs1_InsertThousandsGrouping( | 
|  | 7996 | (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, | 
|  | 7997 | min_width, grouping, thousands_sep); | 
|  | 7998 | case PyUnicode_2BYTE_KIND: | 
|  | 7999 | return _PyUnicode_ucs2_InsertThousandsGrouping( | 
|  | 8000 | (Py_UCS2*)data, n_buffer, (Py_UCS2*)digits, n_digits, | 
|  | 8001 | min_width, grouping, thousands_sep); | 
|  | 8002 | case PyUnicode_4BYTE_KIND: | 
|  | 8003 | return _PyUnicode_ucs4_InsertThousandsGrouping( | 
|  | 8004 | (Py_UCS4*)data, n_buffer, (Py_UCS4*)digits, n_digits, | 
|  | 8005 | min_width, grouping, thousands_sep); | 
|  | 8006 | } | 
|  | 8007 | assert(0); | 
|  | 8008 | return -1; | 
|  | 8009 | } | 
|  | 8010 |  | 
|  | 8011 |  | 
| Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8012 | #include "stringlib/unicodedefs.h" | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8013 | #include "stringlib/fastsearch.h" | 
| Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 8014 |  | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8015 | #include "stringlib/count.h" | 
|  | 8016 | #include "stringlib/find.h" | 
| Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 8017 |  | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8018 | /* helper macro to fixup start/end slice values */ | 
| Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 8019 | #define ADJUST_INDICES(start, end, len)         \ | 
|  | 8020 | if (end > len)                              \ | 
|  | 8021 | end = len;                              \ | 
|  | 8022 | else if (end < 0) {                         \ | 
|  | 8023 | end += len;                             \ | 
|  | 8024 | if (end < 0)                            \ | 
|  | 8025 | end = 0;                            \ | 
|  | 8026 | }                                           \ | 
|  | 8027 | if (start < 0) {                            \ | 
|  | 8028 | start += len;                           \ | 
|  | 8029 | if (start < 0)                          \ | 
|  | 8030 | start = 0;                          \ | 
|  | 8031 | } | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8032 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8033 | Py_ssize_t | 
|  | 8034 | PyUnicode_Count(PyObject *str, | 
|  | 8035 | PyObject *substr, | 
|  | 8036 | Py_ssize_t start, | 
|  | 8037 | Py_ssize_t end) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8038 | { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8039 | Py_ssize_t result; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8040 | PyUnicodeObject* str_obj; | 
|  | 8041 | PyUnicodeObject* sub_obj; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8042 | int kind1, kind2, kind; | 
|  | 8043 | void *buf1 = NULL, *buf2 = NULL; | 
|  | 8044 | Py_ssize_t len1, len2; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8045 |  | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8046 | str_obj = (PyUnicodeObject*) PyUnicode_FromObject(str); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8047 | if (!str_obj || PyUnicode_READY(str_obj) == -1) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8048 | return -1; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8049 | sub_obj = (PyUnicodeObject*) PyUnicode_FromObject(substr); | 
| Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 8050 | if (!sub_obj || PyUnicode_READY(sub_obj) == -1) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8051 | Py_DECREF(str_obj); | 
|  | 8052 | return -1; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8053 | } | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8054 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8055 | kind1 = PyUnicode_KIND(str_obj); | 
|  | 8056 | kind2 = PyUnicode_KIND(sub_obj); | 
|  | 8057 | kind = kind1 > kind2 ? kind1 : kind2; | 
|  | 8058 | buf1 = PyUnicode_DATA(str_obj); | 
|  | 8059 | if (kind1 != kind) | 
|  | 8060 | buf1 = _PyUnicode_AsKind((PyObject*)str_obj, kind); | 
|  | 8061 | if (!buf1) | 
|  | 8062 | goto onError; | 
|  | 8063 | buf2 = PyUnicode_DATA(sub_obj); | 
|  | 8064 | if (kind2 != kind) | 
|  | 8065 | buf2 = _PyUnicode_AsKind((PyObject*)sub_obj, kind); | 
|  | 8066 | if (!buf2) | 
|  | 8067 | goto onError; | 
|  | 8068 | len1 = PyUnicode_GET_LENGTH(str_obj); | 
|  | 8069 | len2 = PyUnicode_GET_LENGTH(sub_obj); | 
|  | 8070 |  | 
|  | 8071 | ADJUST_INDICES(start, end, len1); | 
|  | 8072 | switch(kind) { | 
|  | 8073 | case PyUnicode_1BYTE_KIND: | 
|  | 8074 | result = ucs1lib_count( | 
|  | 8075 | ((Py_UCS1*)buf1) + start, end - start, | 
|  | 8076 | buf2, len2, PY_SSIZE_T_MAX | 
|  | 8077 | ); | 
|  | 8078 | break; | 
|  | 8079 | case PyUnicode_2BYTE_KIND: | 
|  | 8080 | result = ucs2lib_count( | 
|  | 8081 | ((Py_UCS2*)buf1) + start, end - start, | 
|  | 8082 | buf2, len2, PY_SSIZE_T_MAX | 
|  | 8083 | ); | 
|  | 8084 | break; | 
|  | 8085 | case PyUnicode_4BYTE_KIND: | 
|  | 8086 | result = ucs4lib_count( | 
|  | 8087 | ((Py_UCS4*)buf1) + start, end - start, | 
|  | 8088 | buf2, len2, PY_SSIZE_T_MAX | 
|  | 8089 | ); | 
|  | 8090 | break; | 
|  | 8091 | default: | 
|  | 8092 | assert(0); result = 0; | 
|  | 8093 | } | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8094 |  | 
|  | 8095 | Py_DECREF(sub_obj); | 
|  | 8096 | Py_DECREF(str_obj); | 
|  | 8097 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8098 | if (kind1 != kind) | 
|  | 8099 | PyMem_Free(buf1); | 
|  | 8100 | if (kind2 != kind) | 
|  | 8101 | PyMem_Free(buf2); | 
|  | 8102 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8103 | return result; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8104 | onError: | 
|  | 8105 | Py_DECREF(sub_obj); | 
|  | 8106 | Py_DECREF(str_obj); | 
|  | 8107 | if (kind1 != kind && buf1) | 
|  | 8108 | PyMem_Free(buf1); | 
|  | 8109 | if (kind2 != kind && buf2) | 
|  | 8110 | PyMem_Free(buf2); | 
|  | 8111 | return -1; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8112 | } | 
|  | 8113 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8114 | Py_ssize_t | 
|  | 8115 | PyUnicode_Find(PyObject *str, | 
|  | 8116 | PyObject *sub, | 
|  | 8117 | Py_ssize_t start, | 
|  | 8118 | Py_ssize_t end, | 
|  | 8119 | int direction) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8120 | { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8121 | Py_ssize_t result; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8122 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8123 | str = PyUnicode_FromObject(str); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8124 | if (!str || PyUnicode_READY(str) == -1) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8125 | return -2; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8126 | sub = PyUnicode_FromObject(sub); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8127 | if (!sub || PyUnicode_READY(sub) == -1) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8128 | Py_DECREF(str); | 
|  | 8129 | return -2; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8130 | } | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8131 |  | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8132 | if (direction > 0) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8133 | result = any_find_slice( | 
|  | 8134 | ucs1lib_find_slice, ucs2lib_find_slice, ucs4lib_find_slice, | 
|  | 8135 | str, sub, start, end | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8136 | ); | 
|  | 8137 | else | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8138 | result = any_find_slice( | 
|  | 8139 | ucs1lib_rfind_slice, ucs2lib_rfind_slice, ucs4lib_rfind_slice, | 
|  | 8140 | str, sub, start, end | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8141 | ); | 
|  | 8142 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8143 | Py_DECREF(str); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8144 | Py_DECREF(sub); | 
|  | 8145 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8146 | return result; | 
|  | 8147 | } | 
|  | 8148 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8149 | Py_ssize_t | 
|  | 8150 | PyUnicode_FindChar(PyObject *str, Py_UCS4 ch, | 
|  | 8151 | Py_ssize_t start, Py_ssize_t end, | 
|  | 8152 | int direction) | 
|  | 8153 | { | 
|  | 8154 | char *result; | 
|  | 8155 | int kind; | 
|  | 8156 | if (PyUnicode_READY(str) == -1) | 
|  | 8157 | return -2; | 
| Victor Stinner | 267aa24 | 2011-10-02 01:08:37 +0200 | [diff] [blame] | 8158 | if (start < 0 || end < 0) { | 
|  | 8159 | PyErr_SetString(PyExc_IndexError, "string index out of range"); | 
|  | 8160 | return -2; | 
|  | 8161 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8162 | if (end > PyUnicode_GET_LENGTH(str)) | 
|  | 8163 | end = PyUnicode_GET_LENGTH(str); | 
|  | 8164 | kind = PyUnicode_KIND(str); | 
|  | 8165 | result = findchar(PyUnicode_1BYTE_DATA(str) | 
|  | 8166 | + PyUnicode_KIND_SIZE(kind, start), | 
|  | 8167 | kind, | 
|  | 8168 | end-start, ch, direction); | 
|  | 8169 | if (!result) | 
|  | 8170 | return -1; | 
|  | 8171 | return (result-(char*)PyUnicode_DATA(str)) >> (kind-1); | 
|  | 8172 | } | 
|  | 8173 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8174 | static int | 
|  | 8175 | tailmatch(PyUnicodeObject *self, | 
|  | 8176 | PyUnicodeObject *substring, | 
|  | 8177 | Py_ssize_t start, | 
|  | 8178 | Py_ssize_t end, | 
|  | 8179 | int direction) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8180 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8181 | int kind_self; | 
|  | 8182 | int kind_sub; | 
|  | 8183 | void *data_self; | 
|  | 8184 | void *data_sub; | 
|  | 8185 | Py_ssize_t offset; | 
|  | 8186 | Py_ssize_t i; | 
|  | 8187 | Py_ssize_t end_sub; | 
|  | 8188 |  | 
|  | 8189 | if (PyUnicode_READY(self) == -1 || | 
|  | 8190 | PyUnicode_READY(substring) == -1) | 
|  | 8191 | return 0; | 
|  | 8192 |  | 
|  | 8193 | if (PyUnicode_GET_LENGTH(substring) == 0) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8194 | return 1; | 
|  | 8195 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8196 | ADJUST_INDICES(start, end, PyUnicode_GET_LENGTH(self)); | 
|  | 8197 | end -= PyUnicode_GET_LENGTH(substring); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8198 | if (end < start) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8199 | return 0; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8200 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8201 | kind_self = PyUnicode_KIND(self); | 
|  | 8202 | data_self = PyUnicode_DATA(self); | 
|  | 8203 | kind_sub = PyUnicode_KIND(substring); | 
|  | 8204 | data_sub = PyUnicode_DATA(substring); | 
|  | 8205 | end_sub = PyUnicode_GET_LENGTH(substring) - 1; | 
|  | 8206 |  | 
|  | 8207 | if (direction > 0) | 
|  | 8208 | offset = end; | 
|  | 8209 | else | 
|  | 8210 | offset = start; | 
|  | 8211 |  | 
|  | 8212 | if (PyUnicode_READ(kind_self, data_self, offset) == | 
|  | 8213 | PyUnicode_READ(kind_sub, data_sub, 0) && | 
|  | 8214 | PyUnicode_READ(kind_self, data_self, offset + end_sub) == | 
|  | 8215 | PyUnicode_READ(kind_sub, data_sub, end_sub)) { | 
|  | 8216 | /* If both are of the same kind, memcmp is sufficient */ | 
|  | 8217 | if (kind_self == kind_sub) { | 
|  | 8218 | return ! memcmp((char *)data_self + | 
|  | 8219 | (offset * PyUnicode_CHARACTER_SIZE(substring)), | 
|  | 8220 | data_sub, | 
|  | 8221 | PyUnicode_GET_LENGTH(substring) * | 
|  | 8222 | PyUnicode_CHARACTER_SIZE(substring)); | 
|  | 8223 | } | 
|  | 8224 | /* otherwise we have to compare each character by first accesing it */ | 
|  | 8225 | else { | 
|  | 8226 | /* We do not need to compare 0 and len(substring)-1 because | 
|  | 8227 | the if statement above ensured already that they are equal | 
|  | 8228 | when we end up here. */ | 
|  | 8229 | // TODO: honor direction and do a forward or backwards search | 
|  | 8230 | for (i = 1; i < end_sub; ++i) { | 
|  | 8231 | if (PyUnicode_READ(kind_self, data_self, offset + i) != | 
|  | 8232 | PyUnicode_READ(kind_sub, data_sub, i)) | 
|  | 8233 | return 0; | 
|  | 8234 | } | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8235 | return 1; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8236 | } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8237 | } | 
|  | 8238 |  | 
|  | 8239 | return 0; | 
|  | 8240 | } | 
|  | 8241 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8242 | Py_ssize_t | 
|  | 8243 | PyUnicode_Tailmatch(PyObject *str, | 
|  | 8244 | PyObject *substr, | 
|  | 8245 | Py_ssize_t start, | 
|  | 8246 | Py_ssize_t end, | 
|  | 8247 | int direction) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8248 | { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8249 | Py_ssize_t result; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8250 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8251 | str = PyUnicode_FromObject(str); | 
|  | 8252 | if (str == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8253 | return -1; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8254 | substr = PyUnicode_FromObject(substr); | 
|  | 8255 | if (substr == NULL) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8256 | Py_DECREF(str); | 
|  | 8257 | return -1; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8258 | } | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8259 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8260 | result = tailmatch((PyUnicodeObject *)str, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8261 | (PyUnicodeObject *)substr, | 
|  | 8262 | start, end, direction); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8263 | Py_DECREF(str); | 
|  | 8264 | Py_DECREF(substr); | 
|  | 8265 | return result; | 
|  | 8266 | } | 
|  | 8267 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8268 | /* Apply fixfct filter to the Unicode object self and return a | 
|  | 8269 | reference to the modified object */ | 
|  | 8270 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8271 | static PyObject * | 
|  | 8272 | fixup(PyUnicodeObject *self, | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8273 | Py_UCS4 (*fixfct)(PyUnicodeObject *s)) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8274 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8275 | PyObject *u; | 
|  | 8276 | Py_UCS4 maxchar_old, maxchar_new = 0; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8277 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8278 | if (PyUnicode_READY(self) == -1) | 
|  | 8279 | return NULL; | 
|  | 8280 | maxchar_old = PyUnicode_MAX_CHAR_VALUE(self); | 
|  | 8281 | u = PyUnicode_New(PyUnicode_GET_LENGTH(self), | 
|  | 8282 | maxchar_old); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8283 | if (u == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8284 | return NULL; | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8285 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8286 | Py_MEMCPY(PyUnicode_1BYTE_DATA(u), PyUnicode_1BYTE_DATA(self), | 
|  | 8287 | PyUnicode_GET_LENGTH(u) * PyUnicode_CHARACTER_SIZE(u)); | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8288 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8289 | /* fix functions return the new maximum character in a string, | 
|  | 8290 | if the kind of the resulting unicode object does not change, | 
|  | 8291 | everything is fine.  Otherwise we need to change the string kind | 
|  | 8292 | and re-run the fix function. */ | 
|  | 8293 | maxchar_new = fixfct((PyUnicodeObject*)u); | 
|  | 8294 | if (maxchar_new == 0) | 
|  | 8295 | /* do nothing, keep maxchar_new at 0 which means no changes. */; | 
|  | 8296 | else if (maxchar_new <= 127) | 
|  | 8297 | maxchar_new = 127; | 
|  | 8298 | else if (maxchar_new <= 255) | 
|  | 8299 | maxchar_new = 255; | 
|  | 8300 | else if (maxchar_new <= 65535) | 
|  | 8301 | maxchar_new = 65535; | 
|  | 8302 | else | 
|  | 8303 | maxchar_new = 1114111; /* 0x10ffff */ | 
|  | 8304 |  | 
|  | 8305 | if (!maxchar_new && PyUnicode_CheckExact(self)) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8306 | /* fixfct should return TRUE if it modified the buffer. If | 
|  | 8307 | FALSE, return a reference to the original buffer instead | 
|  | 8308 | (to save space, not time) */ | 
|  | 8309 | Py_INCREF(self); | 
|  | 8310 | Py_DECREF(u); | 
|  | 8311 | return (PyObject*) self; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8312 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8313 | else if (maxchar_new == maxchar_old) { | 
|  | 8314 | return u; | 
|  | 8315 | } | 
|  | 8316 | else { | 
|  | 8317 | /* In case the maximum character changed, we need to | 
|  | 8318 | convert the string to the new category. */ | 
| Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8319 | PyObject *v = PyUnicode_New(PyUnicode_GET_LENGTH(self), maxchar_new); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8320 | if (v == NULL) { | 
|  | 8321 | Py_DECREF(u); | 
|  | 8322 | return NULL; | 
|  | 8323 | } | 
|  | 8324 | if (maxchar_new > maxchar_old) { | 
|  | 8325 | /* If the maxchar increased so that the kind changed, not all | 
|  | 8326 | characters are representable anymore and we need to fix the | 
|  | 8327 | string again. This only happens in very few cases. */ | 
| Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 8328 | if (PyUnicode_CopyCharacters(v, 0, | 
|  | 8329 | (PyObject*)self, 0, | 
| Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8330 | PyUnicode_GET_LENGTH(self)) < 0) | 
|  | 8331 | { | 
|  | 8332 | Py_DECREF(u); | 
|  | 8333 | return NULL; | 
|  | 8334 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8335 | maxchar_old = fixfct((PyUnicodeObject*)v); | 
|  | 8336 | assert(maxchar_old > 0 && maxchar_old <= maxchar_new); | 
|  | 8337 | } | 
| Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8338 | else { | 
| Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 8339 | if (PyUnicode_CopyCharacters(v, 0, | 
|  | 8340 | u, 0, | 
| Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8341 | PyUnicode_GET_LENGTH(self)) < 0) | 
|  | 8342 | { | 
|  | 8343 | Py_DECREF(u); | 
|  | 8344 | return NULL; | 
|  | 8345 | } | 
|  | 8346 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8347 |  | 
|  | 8348 | Py_DECREF(u); | 
|  | 8349 | return v; | 
|  | 8350 | } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8351 | } | 
|  | 8352 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8353 | static Py_UCS4 | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8354 | fixupper(PyUnicodeObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8355 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8356 | /* No need to call PyUnicode_READY(self) because this function is only | 
|  | 8357 | called as a callback from fixup() which does it already. */ | 
|  | 8358 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); | 
|  | 8359 | const int kind = PyUnicode_KIND(self); | 
|  | 8360 | void *data = PyUnicode_DATA(self); | 
|  | 8361 | int touched = 0; | 
|  | 8362 | Py_UCS4 maxchar = 0; | 
|  | 8363 | Py_ssize_t i; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8364 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8365 | for (i = 0; i < len; ++i) { | 
|  | 8366 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); | 
|  | 8367 | const Py_UCS4 up = Py_UNICODE_TOUPPER(ch); | 
|  | 8368 | if (up != ch) { | 
|  | 8369 | if (up > maxchar) | 
|  | 8370 | maxchar = up; | 
|  | 8371 | PyUnicode_WRITE(kind, data, i, up); | 
|  | 8372 | touched = 1; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8373 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8374 | else if (ch > maxchar) | 
|  | 8375 | maxchar = ch; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8376 | } | 
|  | 8377 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8378 | if (touched) | 
|  | 8379 | return maxchar; | 
|  | 8380 | else | 
|  | 8381 | return 0; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8382 | } | 
|  | 8383 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8384 | static Py_UCS4 | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8385 | fixlower(PyUnicodeObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8386 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8387 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ | 
|  | 8388 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); | 
|  | 8389 | const int kind = PyUnicode_KIND(self); | 
|  | 8390 | void *data = PyUnicode_DATA(self); | 
|  | 8391 | int touched = 0; | 
|  | 8392 | Py_UCS4 maxchar = 0; | 
|  | 8393 | Py_ssize_t i; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8394 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8395 | for(i = 0; i < len; ++i) { | 
|  | 8396 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); | 
|  | 8397 | const Py_UCS4 lo = Py_UNICODE_TOLOWER(ch); | 
|  | 8398 | if (lo != ch) { | 
|  | 8399 | if (lo > maxchar) | 
|  | 8400 | maxchar = lo; | 
|  | 8401 | PyUnicode_WRITE(kind, data, i, lo); | 
|  | 8402 | touched = 1; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8403 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8404 | else if (ch > maxchar) | 
|  | 8405 | maxchar = ch; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8406 | } | 
|  | 8407 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8408 | if (touched) | 
|  | 8409 | return maxchar; | 
|  | 8410 | else | 
|  | 8411 | return 0; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8412 | } | 
|  | 8413 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8414 | static Py_UCS4 | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8415 | fixswapcase(PyUnicodeObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8416 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8417 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ | 
|  | 8418 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); | 
|  | 8419 | const int kind = PyUnicode_KIND(self); | 
|  | 8420 | void *data = PyUnicode_DATA(self); | 
|  | 8421 | int touched = 0; | 
|  | 8422 | Py_UCS4 maxchar = 0; | 
|  | 8423 | Py_ssize_t i; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8424 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8425 | for(i = 0; i < len; ++i) { | 
|  | 8426 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); | 
|  | 8427 | Py_UCS4 nu = 0; | 
|  | 8428 |  | 
|  | 8429 | if (Py_UNICODE_ISUPPER(ch)) | 
|  | 8430 | nu = Py_UNICODE_TOLOWER(ch); | 
|  | 8431 | else if (Py_UNICODE_ISLOWER(ch)) | 
|  | 8432 | nu = Py_UNICODE_TOUPPER(ch); | 
|  | 8433 |  | 
|  | 8434 | if (nu != 0) { | 
|  | 8435 | if (nu > maxchar) | 
|  | 8436 | maxchar = nu; | 
|  | 8437 | PyUnicode_WRITE(kind, data, i, nu); | 
|  | 8438 | touched = 1; | 
| 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 | else if (ch > maxchar) | 
|  | 8441 | maxchar = ch; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8442 | } | 
|  | 8443 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8444 | if (touched) | 
|  | 8445 | return maxchar; | 
|  | 8446 | else | 
|  | 8447 | return 0; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8448 | } | 
|  | 8449 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8450 | static Py_UCS4 | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8451 | fixcapitalize(PyUnicodeObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8452 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8453 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ | 
|  | 8454 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); | 
|  | 8455 | const int kind = PyUnicode_KIND(self); | 
|  | 8456 | void *data = PyUnicode_DATA(self); | 
|  | 8457 | int touched = 0; | 
|  | 8458 | Py_UCS4 maxchar = 0; | 
|  | 8459 | Py_ssize_t i = 0; | 
|  | 8460 | Py_UCS4 ch; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8461 |  | 
| Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 8462 | if (len == 0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8463 | return 0; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8464 |  | 
|  | 8465 | ch = PyUnicode_READ(kind, data, i); | 
|  | 8466 | if (!Py_UNICODE_ISUPPER(ch)) { | 
|  | 8467 | maxchar = Py_UNICODE_TOUPPER(ch); | 
|  | 8468 | PyUnicode_WRITE(kind, data, i, maxchar); | 
|  | 8469 | touched = 1; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8470 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8471 | ++i; | 
|  | 8472 | for(; i < len; ++i) { | 
|  | 8473 | ch = PyUnicode_READ(kind, data, i); | 
|  | 8474 | if (!Py_UNICODE_ISLOWER(ch)) { | 
|  | 8475 | const Py_UCS4 lo = Py_UNICODE_TOLOWER(ch); | 
|  | 8476 | if (lo > maxchar) | 
|  | 8477 | maxchar = lo; | 
|  | 8478 | PyUnicode_WRITE(kind, data, i, lo); | 
|  | 8479 | touched = 1; | 
| Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 8480 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8481 | else if (ch > maxchar) | 
|  | 8482 | maxchar = ch; | 
| Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 8483 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8484 |  | 
|  | 8485 | if (touched) | 
|  | 8486 | return maxchar; | 
|  | 8487 | else | 
|  | 8488 | return 0; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8489 | } | 
|  | 8490 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8491 | static Py_UCS4 | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8492 | fixtitle(PyUnicodeObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8493 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8494 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ | 
|  | 8495 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); | 
|  | 8496 | const int kind = PyUnicode_KIND(self); | 
|  | 8497 | void *data = PyUnicode_DATA(self); | 
|  | 8498 | Py_UCS4 maxchar = 0; | 
|  | 8499 | Py_ssize_t i = 0; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8500 | int previous_is_cased; | 
|  | 8501 |  | 
|  | 8502 | /* Shortcut for single character strings */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8503 | if (len == 1) { | 
|  | 8504 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); | 
|  | 8505 | const Py_UCS4 ti = Py_UNICODE_TOTITLE(ch); | 
|  | 8506 | if (ti != ch) { | 
|  | 8507 | PyUnicode_WRITE(kind, data, i, ti); | 
|  | 8508 | return ti; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8509 | } | 
|  | 8510 | else | 
|  | 8511 | return 0; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8512 | } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8513 | previous_is_cased = 0; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8514 | for(; i < len; ++i) { | 
|  | 8515 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); | 
|  | 8516 | Py_UCS4 nu; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8517 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8518 | if (previous_is_cased) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8519 | nu = Py_UNICODE_TOLOWER(ch); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8520 | else | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8521 | nu = Py_UNICODE_TOTITLE(ch); | 
|  | 8522 |  | 
|  | 8523 | if (nu > maxchar) | 
|  | 8524 | maxchar = nu; | 
|  | 8525 | PyUnicode_WRITE(kind, data, i, nu); | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8526 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8527 | if (Py_UNICODE_ISLOWER(ch) || | 
|  | 8528 | Py_UNICODE_ISUPPER(ch) || | 
|  | 8529 | Py_UNICODE_ISTITLE(ch)) | 
|  | 8530 | previous_is_cased = 1; | 
|  | 8531 | else | 
|  | 8532 | previous_is_cased = 0; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8533 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8534 | return maxchar; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8535 | } | 
|  | 8536 |  | 
| Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 8537 | PyObject * | 
|  | 8538 | PyUnicode_Join(PyObject *separator, PyObject *seq) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8539 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8540 | PyObject *sep = NULL; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8541 | Py_ssize_t seplen = 1; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8542 | PyObject *res = NULL; /* the result */ | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8543 | PyObject *fseq;          /* PySequence_Fast(seq) */ | 
| Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8544 | Py_ssize_t seqlen;       /* len(fseq) -- number of items in sequence */ | 
|  | 8545 | PyObject **items; | 
| Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 8546 | PyObject *item; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8547 | Py_ssize_t sz, i, res_offset; | 
|  | 8548 | Py_UCS4 maxchar = 0; | 
|  | 8549 | Py_UCS4 item_maxchar; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8550 |  | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8551 | fseq = PySequence_Fast(seq, ""); | 
|  | 8552 | if (fseq == NULL) { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8553 | return NULL; | 
| Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 8554 | } | 
|  | 8555 |  | 
| Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8556 | /* NOTE: the following code can't call back into Python code, | 
|  | 8557 | * so we are sure that fseq won't be mutated. | 
| Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 8558 | */ | 
| Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8559 |  | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8560 | seqlen = PySequence_Fast_GET_SIZE(fseq); | 
|  | 8561 | /* If empty sequence, return u"". */ | 
|  | 8562 | if (seqlen == 0) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8563 | res = PyUnicode_New(0, 0); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8564 | goto Done; | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8565 | } | 
| Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8566 | items = PySequence_Fast_ITEMS(fseq); | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8567 | /* If singleton sequence with an exact Unicode, return that. */ | 
|  | 8568 | if (seqlen == 1) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8569 | item = items[0]; | 
|  | 8570 | if (PyUnicode_CheckExact(item)) { | 
|  | 8571 | Py_INCREF(item); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8572 | res = item; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8573 | goto Done; | 
|  | 8574 | } | 
| Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 8575 | } | 
| Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8576 | else { | 
|  | 8577 | /* Set up sep and seplen */ | 
|  | 8578 | if (separator == NULL) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8579 | /* fall back to a blank space separator */ | 
|  | 8580 | sep = PyUnicode_FromOrdinal(' '); | 
| Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 8581 | if (!sep) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8582 | goto onError; | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8583 | } | 
| Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8584 | else { | 
|  | 8585 | if (!PyUnicode_Check(separator)) { | 
|  | 8586 | PyErr_Format(PyExc_TypeError, | 
|  | 8587 | "separator: expected str instance," | 
|  | 8588 | " %.80s found", | 
|  | 8589 | Py_TYPE(separator)->tp_name); | 
|  | 8590 | goto onError; | 
|  | 8591 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8592 | if (PyUnicode_READY(separator) == -1) | 
|  | 8593 | goto onError; | 
|  | 8594 | sep = separator; | 
|  | 8595 | seplen = PyUnicode_GET_LENGTH(separator); | 
|  | 8596 | maxchar = PyUnicode_MAX_CHAR_VALUE(separator); | 
|  | 8597 | /* inc refcount to keep this code path symetric with the | 
|  | 8598 | above case of a blank separator */ | 
|  | 8599 | Py_INCREF(sep); | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8600 | } | 
|  | 8601 | } | 
|  | 8602 |  | 
| Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8603 | /* There are at least two things to join, or else we have a subclass | 
|  | 8604 | * of str in the sequence. | 
|  | 8605 | * Do a pre-pass to figure out the total amount of space we'll | 
|  | 8606 | * need (sz), and see whether all argument are strings. | 
|  | 8607 | */ | 
|  | 8608 | sz = 0; | 
|  | 8609 | for (i = 0; i < seqlen; i++) { | 
|  | 8610 | const Py_ssize_t old_sz = sz; | 
|  | 8611 | item = items[i]; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8612 | if (!PyUnicode_Check(item)) { | 
|  | 8613 | PyErr_Format(PyExc_TypeError, | 
|  | 8614 | "sequence item %zd: expected str instance," | 
|  | 8615 | " %.80s found", | 
|  | 8616 | i, Py_TYPE(item)->tp_name); | 
|  | 8617 | goto onError; | 
|  | 8618 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8619 | if (PyUnicode_READY(item) == -1) | 
|  | 8620 | goto onError; | 
|  | 8621 | sz += PyUnicode_GET_LENGTH(item); | 
|  | 8622 | item_maxchar = PyUnicode_MAX_CHAR_VALUE(item); | 
|  | 8623 | if (item_maxchar > maxchar) | 
|  | 8624 | maxchar = item_maxchar; | 
| Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8625 | if (i != 0) | 
|  | 8626 | sz += seplen; | 
|  | 8627 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { | 
|  | 8628 | PyErr_SetString(PyExc_OverflowError, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8629 | "join() result is too long for a Python string"); | 
| Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8630 | goto onError; | 
|  | 8631 | } | 
|  | 8632 | } | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8633 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8634 | res = PyUnicode_New(sz, maxchar); | 
| Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8635 | if (res == NULL) | 
|  | 8636 | goto onError; | 
| Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 8637 |  | 
| Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8638 | /* Catenate everything. */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8639 | for (i = 0, res_offset = 0; i < seqlen; ++i) { | 
| Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8640 | Py_ssize_t itemlen; | 
|  | 8641 | item = items[i]; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8642 | itemlen = PyUnicode_GET_LENGTH(item); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8643 | /* Copy item, and maybe the separator. */ | 
|  | 8644 | if (i) { | 
| Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8645 | if (PyUnicode_CopyCharacters(res, res_offset, | 
|  | 8646 | sep, 0, seplen) < 0) | 
|  | 8647 | goto onError; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8648 | res_offset += seplen; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8649 | } | 
| Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8650 | if (PyUnicode_CopyCharacters(res, res_offset, | 
|  | 8651 | item, 0, itemlen) < 0) | 
|  | 8652 | goto onError; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8653 | res_offset += itemlen; | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8654 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8655 | assert(res_offset == PyUnicode_GET_LENGTH(res)); | 
| Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 8656 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8657 | Done: | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8658 | Py_DECREF(fseq); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8659 | Py_XDECREF(sep); | 
|  | 8660 | return res; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8661 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8662 | onError: | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8663 | Py_DECREF(fseq); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8664 | Py_XDECREF(sep); | 
| Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 8665 | Py_XDECREF(res); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8666 | return NULL; | 
|  | 8667 | } | 
|  | 8668 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8669 | #define FILL(kind, data, value, start, length) \ | 
|  | 8670 | do { \ | 
|  | 8671 | Py_ssize_t i_ = 0; \ | 
|  | 8672 | assert(kind != PyUnicode_WCHAR_KIND); \ | 
|  | 8673 | switch ((kind)) { \ | 
|  | 8674 | case PyUnicode_1BYTE_KIND: { \ | 
|  | 8675 | unsigned char * to_ = (unsigned char *)((data)) + (start); \ | 
|  | 8676 | memset(to_, (unsigned char)value, length); \ | 
|  | 8677 | break; \ | 
|  | 8678 | } \ | 
|  | 8679 | case PyUnicode_2BYTE_KIND: { \ | 
|  | 8680 | Py_UCS2 * to_ = (Py_UCS2 *)((data)) + (start); \ | 
|  | 8681 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ | 
|  | 8682 | break; \ | 
|  | 8683 | } \ | 
|  | 8684 | default: { \ | 
|  | 8685 | Py_UCS4 * to_ = (Py_UCS4 *)((data)) + (start); \ | 
|  | 8686 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ | 
|  | 8687 | break; \ | 
|  | 8688 | } \ | 
|  | 8689 | } \ | 
|  | 8690 | } while (0) | 
|  | 8691 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8692 | static PyUnicodeObject * | 
|  | 8693 | pad(PyUnicodeObject *self, | 
|  | 8694 | Py_ssize_t left, | 
|  | 8695 | Py_ssize_t right, | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8696 | Py_UCS4 fill) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8697 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8698 | PyObject *u; | 
|  | 8699 | Py_UCS4 maxchar; | 
| Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8700 | int kind; | 
|  | 8701 | void *data; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8702 |  | 
|  | 8703 | if (left < 0) | 
|  | 8704 | left = 0; | 
|  | 8705 | if (right < 0) | 
|  | 8706 | right = 0; | 
|  | 8707 |  | 
| Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 8708 | if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8709 | Py_INCREF(self); | 
|  | 8710 | return self; | 
|  | 8711 | } | 
|  | 8712 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8713 | if (left > PY_SSIZE_T_MAX - _PyUnicode_LENGTH(self) || | 
|  | 8714 | right > PY_SSIZE_T_MAX - (left + _PyUnicode_LENGTH(self))) { | 
| Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 8715 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); | 
|  | 8716 | return NULL; | 
|  | 8717 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8718 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); | 
|  | 8719 | if (fill > maxchar) | 
|  | 8720 | maxchar = fill; | 
|  | 8721 | u = PyUnicode_New(left + _PyUnicode_LENGTH(self) + right, maxchar); | 
| Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8722 | if (!u) | 
|  | 8723 | return NULL; | 
|  | 8724 |  | 
|  | 8725 | kind = PyUnicode_KIND(u); | 
|  | 8726 | data = PyUnicode_DATA(u); | 
|  | 8727 | if (left) | 
|  | 8728 | FILL(kind, data, fill, 0, left); | 
|  | 8729 | if (right) | 
|  | 8730 | FILL(kind, data, fill, left + _PyUnicode_LENGTH(self), right); | 
| Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 8731 | if (PyUnicode_CopyCharacters(u, left, | 
|  | 8732 | (PyObject*)self, 0, | 
| Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8733 | _PyUnicode_LENGTH(self)) < 0) | 
|  | 8734 | { | 
|  | 8735 | Py_DECREF(u); | 
|  | 8736 | return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8737 | } | 
|  | 8738 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8739 | return (PyUnicodeObject*)u; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8740 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8741 | #undef FILL | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8742 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8743 | PyObject * | 
|  | 8744 | PyUnicode_Splitlines(PyObject *string, int keepends) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8745 | { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8746 | PyObject *list; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8747 |  | 
|  | 8748 | string = PyUnicode_FromObject(string); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8749 | if (string == NULL || PyUnicode_READY(string) == -1) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8750 | return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8751 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8752 | switch(PyUnicode_KIND(string)) { | 
|  | 8753 | case PyUnicode_1BYTE_KIND: | 
|  | 8754 | list = ucs1lib_splitlines( | 
|  | 8755 | (PyObject*) string, PyUnicode_1BYTE_DATA(string), | 
|  | 8756 | PyUnicode_GET_LENGTH(string), keepends); | 
|  | 8757 | break; | 
|  | 8758 | case PyUnicode_2BYTE_KIND: | 
|  | 8759 | list = ucs2lib_splitlines( | 
|  | 8760 | (PyObject*) string, PyUnicode_2BYTE_DATA(string), | 
|  | 8761 | PyUnicode_GET_LENGTH(string), keepends); | 
|  | 8762 | break; | 
|  | 8763 | case PyUnicode_4BYTE_KIND: | 
|  | 8764 | list = ucs4lib_splitlines( | 
|  | 8765 | (PyObject*) string, PyUnicode_4BYTE_DATA(string), | 
|  | 8766 | PyUnicode_GET_LENGTH(string), keepends); | 
|  | 8767 | break; | 
|  | 8768 | default: | 
|  | 8769 | assert(0); | 
|  | 8770 | list = 0; | 
|  | 8771 | } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8772 | Py_DECREF(string); | 
|  | 8773 | return list; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8774 | } | 
|  | 8775 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8776 | static PyObject * | 
|  | 8777 | split(PyUnicodeObject *self, | 
|  | 8778 | PyUnicodeObject *substring, | 
|  | 8779 | Py_ssize_t maxcount) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8780 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8781 | int kind1, kind2, kind; | 
|  | 8782 | void *buf1, *buf2; | 
|  | 8783 | Py_ssize_t len1, len2; | 
|  | 8784 | PyObject* out; | 
|  | 8785 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8786 | if (maxcount < 0) | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8787 | maxcount = PY_SSIZE_T_MAX; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8788 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8789 | if (PyUnicode_READY(self) == -1) | 
|  | 8790 | return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8791 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8792 | if (substring == NULL) | 
|  | 8793 | switch(PyUnicode_KIND(self)) { | 
|  | 8794 | case PyUnicode_1BYTE_KIND: | 
|  | 8795 | return ucs1lib_split_whitespace( | 
|  | 8796 | (PyObject*) self,  PyUnicode_1BYTE_DATA(self), | 
|  | 8797 | PyUnicode_GET_LENGTH(self), maxcount | 
|  | 8798 | ); | 
|  | 8799 | case PyUnicode_2BYTE_KIND: | 
|  | 8800 | return ucs2lib_split_whitespace( | 
|  | 8801 | (PyObject*) self,  PyUnicode_2BYTE_DATA(self), | 
|  | 8802 | PyUnicode_GET_LENGTH(self), maxcount | 
|  | 8803 | ); | 
|  | 8804 | case PyUnicode_4BYTE_KIND: | 
|  | 8805 | return ucs4lib_split_whitespace( | 
|  | 8806 | (PyObject*) self,  PyUnicode_4BYTE_DATA(self), | 
|  | 8807 | PyUnicode_GET_LENGTH(self), maxcount | 
|  | 8808 | ); | 
|  | 8809 | default: | 
|  | 8810 | assert(0); | 
|  | 8811 | return NULL; | 
|  | 8812 | } | 
|  | 8813 |  | 
|  | 8814 | if (PyUnicode_READY(substring) == -1) | 
|  | 8815 | return NULL; | 
|  | 8816 |  | 
|  | 8817 | kind1 = PyUnicode_KIND(self); | 
|  | 8818 | kind2 = PyUnicode_KIND(substring); | 
|  | 8819 | kind = kind1 > kind2 ? kind1 : kind2; | 
|  | 8820 | buf1 = PyUnicode_DATA(self); | 
|  | 8821 | buf2 = PyUnicode_DATA(substring); | 
|  | 8822 | if (kind1 != kind) | 
|  | 8823 | buf1 = _PyUnicode_AsKind((PyObject*)self, kind); | 
|  | 8824 | if (!buf1) | 
|  | 8825 | return NULL; | 
|  | 8826 | if (kind2 != kind) | 
|  | 8827 | buf2 = _PyUnicode_AsKind((PyObject*)substring, kind); | 
|  | 8828 | if (!buf2) { | 
|  | 8829 | if (kind1 != kind) PyMem_Free(buf1); | 
|  | 8830 | return NULL; | 
|  | 8831 | } | 
|  | 8832 | len1 = PyUnicode_GET_LENGTH(self); | 
|  | 8833 | len2 = PyUnicode_GET_LENGTH(substring); | 
|  | 8834 |  | 
|  | 8835 | switch(kind) { | 
|  | 8836 | case PyUnicode_1BYTE_KIND: | 
|  | 8837 | out = ucs1lib_split( | 
|  | 8838 | (PyObject*) self,  buf1, len1, buf2, len2, maxcount); | 
|  | 8839 | break; | 
|  | 8840 | case PyUnicode_2BYTE_KIND: | 
|  | 8841 | out = ucs2lib_split( | 
|  | 8842 | (PyObject*) self,  buf1, len1, buf2, len2, maxcount); | 
|  | 8843 | break; | 
|  | 8844 | case PyUnicode_4BYTE_KIND: | 
|  | 8845 | out = ucs4lib_split( | 
|  | 8846 | (PyObject*) self,  buf1, len1, buf2, len2, maxcount); | 
|  | 8847 | break; | 
|  | 8848 | default: | 
|  | 8849 | out = NULL; | 
|  | 8850 | } | 
|  | 8851 | if (kind1 != kind) | 
|  | 8852 | PyMem_Free(buf1); | 
|  | 8853 | if (kind2 != kind) | 
|  | 8854 | PyMem_Free(buf2); | 
|  | 8855 | return out; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8856 | } | 
|  | 8857 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8858 | static PyObject * | 
|  | 8859 | rsplit(PyUnicodeObject *self, | 
|  | 8860 | PyUnicodeObject *substring, | 
|  | 8861 | Py_ssize_t maxcount) | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8862 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8863 | int kind1, kind2, kind; | 
|  | 8864 | void *buf1, *buf2; | 
|  | 8865 | Py_ssize_t len1, len2; | 
|  | 8866 | PyObject* out; | 
|  | 8867 |  | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8868 | if (maxcount < 0) | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8869 | maxcount = PY_SSIZE_T_MAX; | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8870 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8871 | if (PyUnicode_READY(self) == -1) | 
|  | 8872 | return NULL; | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8873 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8874 | if (substring == NULL) | 
|  | 8875 | switch(PyUnicode_KIND(self)) { | 
|  | 8876 | case PyUnicode_1BYTE_KIND: | 
|  | 8877 | return ucs1lib_rsplit_whitespace( | 
|  | 8878 | (PyObject*) self,  PyUnicode_1BYTE_DATA(self), | 
|  | 8879 | PyUnicode_GET_LENGTH(self), maxcount | 
|  | 8880 | ); | 
|  | 8881 | case PyUnicode_2BYTE_KIND: | 
|  | 8882 | return ucs2lib_rsplit_whitespace( | 
|  | 8883 | (PyObject*) self,  PyUnicode_2BYTE_DATA(self), | 
|  | 8884 | PyUnicode_GET_LENGTH(self), maxcount | 
|  | 8885 | ); | 
|  | 8886 | case PyUnicode_4BYTE_KIND: | 
|  | 8887 | return ucs4lib_rsplit_whitespace( | 
|  | 8888 | (PyObject*) self,  PyUnicode_4BYTE_DATA(self), | 
|  | 8889 | PyUnicode_GET_LENGTH(self), maxcount | 
|  | 8890 | ); | 
|  | 8891 | default: | 
|  | 8892 | assert(0); | 
|  | 8893 | return NULL; | 
|  | 8894 | } | 
|  | 8895 |  | 
|  | 8896 | if (PyUnicode_READY(substring) == -1) | 
|  | 8897 | return NULL; | 
|  | 8898 |  | 
|  | 8899 | kind1 = PyUnicode_KIND(self); | 
|  | 8900 | kind2 = PyUnicode_KIND(substring); | 
|  | 8901 | kind = kind1 > kind2 ? kind1 : kind2; | 
|  | 8902 | buf1 = PyUnicode_DATA(self); | 
|  | 8903 | buf2 = PyUnicode_DATA(substring); | 
|  | 8904 | if (kind1 != kind) | 
|  | 8905 | buf1 = _PyUnicode_AsKind((PyObject*)self, kind); | 
|  | 8906 | if (!buf1) | 
|  | 8907 | return NULL; | 
|  | 8908 | if (kind2 != kind) | 
|  | 8909 | buf2 = _PyUnicode_AsKind((PyObject*)substring, kind); | 
|  | 8910 | if (!buf2) { | 
|  | 8911 | if (kind1 != kind) PyMem_Free(buf1); | 
|  | 8912 | return NULL; | 
|  | 8913 | } | 
|  | 8914 | len1 = PyUnicode_GET_LENGTH(self); | 
|  | 8915 | len2 = PyUnicode_GET_LENGTH(substring); | 
|  | 8916 |  | 
|  | 8917 | switch(kind) { | 
|  | 8918 | case PyUnicode_1BYTE_KIND: | 
|  | 8919 | out = ucs1lib_rsplit( | 
|  | 8920 | (PyObject*) self,  buf1, len1, buf2, len2, maxcount); | 
|  | 8921 | break; | 
|  | 8922 | case PyUnicode_2BYTE_KIND: | 
|  | 8923 | out = ucs2lib_rsplit( | 
|  | 8924 | (PyObject*) self,  buf1, len1, buf2, len2, maxcount); | 
|  | 8925 | break; | 
|  | 8926 | case PyUnicode_4BYTE_KIND: | 
|  | 8927 | out = ucs4lib_rsplit( | 
|  | 8928 | (PyObject*) self,  buf1, len1, buf2, len2, maxcount); | 
|  | 8929 | break; | 
|  | 8930 | default: | 
|  | 8931 | out = NULL; | 
|  | 8932 | } | 
|  | 8933 | if (kind1 != kind) | 
|  | 8934 | PyMem_Free(buf1); | 
|  | 8935 | if (kind2 != kind) | 
|  | 8936 | PyMem_Free(buf2); | 
|  | 8937 | return out; | 
|  | 8938 | } | 
|  | 8939 |  | 
|  | 8940 | static Py_ssize_t | 
|  | 8941 | anylib_find(int kind, void *buf1, Py_ssize_t len1, | 
|  | 8942 | void *buf2, Py_ssize_t len2, Py_ssize_t offset) | 
|  | 8943 | { | 
|  | 8944 | switch(kind) { | 
|  | 8945 | case PyUnicode_1BYTE_KIND: | 
|  | 8946 | return ucs1lib_find(buf1, len1, buf2, len2, offset); | 
|  | 8947 | case PyUnicode_2BYTE_KIND: | 
|  | 8948 | return ucs2lib_find(buf1, len1, buf2, len2, offset); | 
|  | 8949 | case PyUnicode_4BYTE_KIND: | 
|  | 8950 | return ucs4lib_find(buf1, len1, buf2, len2, offset); | 
|  | 8951 | } | 
|  | 8952 | assert(0); | 
|  | 8953 | return -1; | 
|  | 8954 | } | 
|  | 8955 |  | 
|  | 8956 | static Py_ssize_t | 
|  | 8957 | anylib_count(int kind, void* sbuf, Py_ssize_t slen, | 
|  | 8958 | void *buf1, Py_ssize_t len1, Py_ssize_t maxcount) | 
|  | 8959 | { | 
|  | 8960 | switch(kind) { | 
|  | 8961 | case PyUnicode_1BYTE_KIND: | 
|  | 8962 | return ucs1lib_count(sbuf, slen, buf1, len1, maxcount); | 
|  | 8963 | case PyUnicode_2BYTE_KIND: | 
|  | 8964 | return ucs2lib_count(sbuf, slen, buf1, len1, maxcount); | 
|  | 8965 | case PyUnicode_4BYTE_KIND: | 
|  | 8966 | return ucs4lib_count(sbuf, slen, buf1, len1, maxcount); | 
|  | 8967 | } | 
|  | 8968 | assert(0); | 
|  | 8969 | return 0; | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8970 | } | 
|  | 8971 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8972 | static PyObject * | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8973 | replace(PyObject *self, PyObject *str1, | 
|  | 8974 | PyObject *str2, Py_ssize_t maxcount) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8975 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8976 | PyObject *u; | 
|  | 8977 | char *sbuf = PyUnicode_DATA(self); | 
|  | 8978 | char *buf1 = PyUnicode_DATA(str1); | 
|  | 8979 | char *buf2 = PyUnicode_DATA(str2); | 
|  | 8980 | int srelease = 0, release1 = 0, release2 = 0; | 
|  | 8981 | int skind = PyUnicode_KIND(self); | 
|  | 8982 | int kind1 = PyUnicode_KIND(str1); | 
|  | 8983 | int kind2 = PyUnicode_KIND(str2); | 
|  | 8984 | Py_ssize_t slen = PyUnicode_GET_LENGTH(self); | 
|  | 8985 | Py_ssize_t len1 = PyUnicode_GET_LENGTH(str1); | 
|  | 8986 | Py_ssize_t len2 = PyUnicode_GET_LENGTH(str2); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8987 |  | 
|  | 8988 | if (maxcount < 0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8989 | maxcount = PY_SSIZE_T_MAX; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8990 | else if (maxcount == 0 || slen == 0) | 
| Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 8991 | goto nothing; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8992 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8993 | if (skind < kind1) | 
|  | 8994 | /* substring too wide to be present */ | 
|  | 8995 | goto nothing; | 
|  | 8996 |  | 
|  | 8997 | if (len1 == len2) { | 
| Antoine Pitrou | cbfdee3 | 2010-01-13 08:58:08 +0000 | [diff] [blame] | 8998 | Py_ssize_t i; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8999 | /* same length */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9000 | if (len1 == 0) | 
| Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9001 | goto nothing; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9002 | if (len1 == 1) { | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9003 | /* replace characters */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9004 | Py_UCS4 u1, u2, maxchar; | 
|  | 9005 | int mayshrink, rkind; | 
|  | 9006 | u1 = PyUnicode_READ_CHAR(str1, 0); | 
|  | 9007 | if (!findchar(sbuf, PyUnicode_KIND(self), | 
|  | 9008 | slen, u1, 1)) | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9009 | goto nothing; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9010 | u2 = PyUnicode_READ_CHAR(str2, 0); | 
|  | 9011 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); | 
|  | 9012 | /* Replacing u1 with u2 may cause a maxchar reduction in the | 
|  | 9013 | result string. */ | 
|  | 9014 | mayshrink = maxchar > 127; | 
|  | 9015 | if (u2 > maxchar) { | 
|  | 9016 | maxchar = u2; | 
|  | 9017 | mayshrink = 0; | 
|  | 9018 | } | 
|  | 9019 | u = PyUnicode_New(slen, maxchar); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9020 | if (!u) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9021 | goto error; | 
| Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9022 | if (PyUnicode_CopyCharacters(u, 0, | 
|  | 9023 | (PyObject*)self, 0, slen) < 0) | 
|  | 9024 | { | 
|  | 9025 | Py_DECREF(u); | 
|  | 9026 | return NULL; | 
|  | 9027 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9028 | rkind = PyUnicode_KIND(u); | 
|  | 9029 | for (i = 0; i < PyUnicode_GET_LENGTH(u); i++) | 
|  | 9030 | if (PyUnicode_READ(rkind, PyUnicode_DATA(u), i) == u1) { | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9031 | if (--maxcount < 0) | 
|  | 9032 | break; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9033 | PyUnicode_WRITE(rkind, PyUnicode_DATA(u), i, u2); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9034 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9035 | if (mayshrink) { | 
|  | 9036 | PyObject *tmp = u; | 
|  | 9037 | u = PyUnicode_FromKindAndData(rkind, PyUnicode_DATA(tmp), | 
|  | 9038 | PyUnicode_GET_LENGTH(tmp)); | 
|  | 9039 | Py_DECREF(tmp); | 
|  | 9040 | } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9041 | } else { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9042 | int rkind = skind; | 
|  | 9043 | char *res; | 
|  | 9044 | if (kind1 < rkind) { | 
|  | 9045 | /* widen substring */ | 
|  | 9046 | buf1 = _PyUnicode_AsKind(str1, rkind); | 
|  | 9047 | if (!buf1) goto error; | 
|  | 9048 | release1 = 1; | 
|  | 9049 | } | 
|  | 9050 | i = anylib_find(rkind, sbuf, slen, buf1, len1, 0); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9051 | if (i < 0) | 
|  | 9052 | goto nothing; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9053 | if (rkind > kind2) { | 
|  | 9054 | /* widen replacement */ | 
|  | 9055 | buf2 = _PyUnicode_AsKind(str2, rkind); | 
|  | 9056 | if (!buf2) goto error; | 
|  | 9057 | release2 = 1; | 
|  | 9058 | } | 
|  | 9059 | else if (rkind < kind2) { | 
|  | 9060 | /* widen self and buf1 */ | 
|  | 9061 | rkind = kind2; | 
|  | 9062 | if (release1) PyMem_Free(buf1); | 
|  | 9063 | sbuf = _PyUnicode_AsKind(self, rkind); | 
|  | 9064 | if (!sbuf) goto error; | 
|  | 9065 | srelease = 1; | 
|  | 9066 | buf1 = _PyUnicode_AsKind(str1, rkind); | 
|  | 9067 | if (!buf1) goto error; | 
|  | 9068 | release1 = 1; | 
|  | 9069 | } | 
|  | 9070 | res = PyMem_Malloc(PyUnicode_KIND_SIZE(rkind, slen)); | 
|  | 9071 | if (!res) { | 
|  | 9072 | PyErr_NoMemory(); | 
|  | 9073 | goto error; | 
|  | 9074 | } | 
|  | 9075 | memcpy(res, sbuf, PyUnicode_KIND_SIZE(rkind, slen)); | 
| Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9076 | /* change everything in-place, starting with this one */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9077 | memcpy(res + PyUnicode_KIND_SIZE(rkind, i), | 
|  | 9078 | buf2, | 
|  | 9079 | PyUnicode_KIND_SIZE(rkind, len2)); | 
|  | 9080 | i += len1; | 
| Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9081 |  | 
|  | 9082 | while ( --maxcount > 0) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9083 | i = anylib_find(rkind, sbuf+PyUnicode_KIND_SIZE(rkind, i), | 
|  | 9084 | slen-i, | 
|  | 9085 | buf1, len1, i); | 
| Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9086 | if (i == -1) | 
|  | 9087 | break; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9088 | memcpy(res + PyUnicode_KIND_SIZE(rkind, i), | 
|  | 9089 | buf2, | 
|  | 9090 | PyUnicode_KIND_SIZE(rkind, len2)); | 
|  | 9091 | i += len1; | 
| Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9092 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9093 |  | 
|  | 9094 | u = PyUnicode_FromKindAndData(rkind, res, slen); | 
|  | 9095 | PyMem_Free(res); | 
|  | 9096 | if (!u) goto error; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9097 | } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9098 | } else { | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9099 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9100 | Py_ssize_t n, i, j, ires; | 
|  | 9101 | Py_ssize_t product, new_size; | 
|  | 9102 | int rkind = skind; | 
|  | 9103 | char *res; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9104 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9105 | if (kind1 < rkind) { | 
|  | 9106 | buf1 = _PyUnicode_AsKind(str1, rkind); | 
|  | 9107 | if (!buf1) goto error; | 
|  | 9108 | release1 = 1; | 
|  | 9109 | } | 
|  | 9110 | n = anylib_count(rkind, sbuf, slen, buf1, len1, maxcount); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9111 | if (n == 0) | 
|  | 9112 | goto nothing; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9113 | if (kind2 < rkind) { | 
|  | 9114 | buf2 = _PyUnicode_AsKind(str2, rkind); | 
|  | 9115 | if (!buf2) goto error; | 
|  | 9116 | release2 = 1; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9117 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9118 | else if (kind2 > rkind) { | 
|  | 9119 | rkind = kind2; | 
|  | 9120 | sbuf = _PyUnicode_AsKind(self, rkind); | 
|  | 9121 | if (!sbuf) goto error; | 
|  | 9122 | srelease = 1; | 
|  | 9123 | if (release1) PyMem_Free(buf1); | 
|  | 9124 | buf1 = _PyUnicode_AsKind(str1, rkind); | 
|  | 9125 | if (!buf1) goto error; | 
|  | 9126 | release1 = 1; | 
|  | 9127 | } | 
|  | 9128 | /* new_size = PyUnicode_GET_LENGTH(self) + n * (PyUnicode_GET_LENGTH(str2) - | 
|  | 9129 | PyUnicode_GET_LENGTH(str1))); */ | 
|  | 9130 | product = n * (len2-len1); | 
|  | 9131 | if ((product / (len2-len1)) != n) { | 
|  | 9132 | PyErr_SetString(PyExc_OverflowError, | 
|  | 9133 | "replace string is too long"); | 
|  | 9134 | goto error; | 
|  | 9135 | } | 
|  | 9136 | new_size = slen + product; | 
|  | 9137 | if (new_size < 0 || new_size > (PY_SSIZE_T_MAX >> (rkind-1))) { | 
|  | 9138 | PyErr_SetString(PyExc_OverflowError, | 
|  | 9139 | "replace string is too long"); | 
|  | 9140 | goto error; | 
|  | 9141 | } | 
|  | 9142 | res = PyMem_Malloc(PyUnicode_KIND_SIZE(rkind, new_size)); | 
|  | 9143 | if (!res) | 
|  | 9144 | goto error; | 
|  | 9145 | ires = i = 0; | 
|  | 9146 | if (len1 > 0) { | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9147 | while (n-- > 0) { | 
|  | 9148 | /* look for next match */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9149 | j = anylib_find(rkind, | 
|  | 9150 | sbuf + PyUnicode_KIND_SIZE(rkind, i), | 
|  | 9151 | slen-i, buf1, len1, i); | 
| Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9152 | if (j == -1) | 
|  | 9153 | break; | 
|  | 9154 | else if (j > i) { | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9155 | /* copy unchanged part [i:j] */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9156 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), | 
|  | 9157 | sbuf + PyUnicode_KIND_SIZE(rkind, i), | 
|  | 9158 | PyUnicode_KIND_SIZE(rkind, j-i)); | 
|  | 9159 | ires += j - i; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9160 | } | 
|  | 9161 | /* copy substitution string */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9162 | if (len2 > 0) { | 
|  | 9163 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), | 
|  | 9164 | buf2, | 
|  | 9165 | PyUnicode_KIND_SIZE(rkind, len2)); | 
|  | 9166 | ires += len2; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9167 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9168 | i = j + len1; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9169 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9170 | if (i < slen) | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9171 | /* copy tail [i:] */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9172 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), | 
|  | 9173 | sbuf + PyUnicode_KIND_SIZE(rkind, i), | 
|  | 9174 | PyUnicode_KIND_SIZE(rkind, slen-i)); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9175 | } else { | 
|  | 9176 | /* interleave */ | 
|  | 9177 | while (n > 0) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9178 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), | 
|  | 9179 | buf2, | 
|  | 9180 | PyUnicode_KIND_SIZE(rkind, len2)); | 
|  | 9181 | ires += len2; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9182 | if (--n <= 0) | 
|  | 9183 | break; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9184 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), | 
|  | 9185 | sbuf + PyUnicode_KIND_SIZE(rkind, i), | 
|  | 9186 | PyUnicode_KIND_SIZE(rkind, 1)); | 
|  | 9187 | ires++; | 
|  | 9188 | i++; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9189 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9190 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), | 
|  | 9191 | sbuf + PyUnicode_KIND_SIZE(rkind, i), | 
|  | 9192 | PyUnicode_KIND_SIZE(rkind, slen-i)); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9193 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9194 | u = PyUnicode_FromKindAndData(rkind, res, new_size); | 
| Martin v. Löwis | 0b1d348 | 2011-10-01 16:35:40 +0200 | [diff] [blame] | 9195 | PyMem_Free(res); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9196 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9197 | if (srelease) | 
|  | 9198 | PyMem_FREE(sbuf); | 
|  | 9199 | if (release1) | 
|  | 9200 | PyMem_FREE(buf1); | 
|  | 9201 | if (release2) | 
|  | 9202 | PyMem_FREE(buf2); | 
|  | 9203 | return u; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9204 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9205 | nothing: | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9206 | /* nothing to replace; return original string (when possible) */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9207 | if (srelease) | 
|  | 9208 | PyMem_FREE(sbuf); | 
|  | 9209 | if (release1) | 
|  | 9210 | PyMem_FREE(buf1); | 
|  | 9211 | if (release2) | 
|  | 9212 | PyMem_FREE(buf2); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9213 | if (PyUnicode_CheckExact(self)) { | 
|  | 9214 | Py_INCREF(self); | 
|  | 9215 | return (PyObject *) self; | 
|  | 9216 | } | 
| Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 9217 | return PyUnicode_Copy(self); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9218 | error: | 
|  | 9219 | if (srelease && sbuf) | 
|  | 9220 | PyMem_FREE(sbuf); | 
|  | 9221 | if (release1 && buf1) | 
|  | 9222 | PyMem_FREE(buf1); | 
|  | 9223 | if (release2 && buf2) | 
|  | 9224 | PyMem_FREE(buf2); | 
|  | 9225 | return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9226 | } | 
|  | 9227 |  | 
|  | 9228 | /* --- Unicode Object Methods --------------------------------------------- */ | 
|  | 9229 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9230 | PyDoc_STRVAR(title__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9231 | "S.title() -> str\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9232 | \n\ | 
|  | 9233 | 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] | 9234 | characters, all remaining cased characters have lower case."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9235 |  | 
|  | 9236 | static PyObject* | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9237 | unicode_title(PyUnicodeObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9238 | { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9239 | return fixup(self, fixtitle); | 
|  | 9240 | } | 
|  | 9241 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9242 | PyDoc_STRVAR(capitalize__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9243 | "S.capitalize() -> str\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9244 | \n\ | 
|  | 9245 | 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] | 9246 | have upper case and the rest lower case."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9247 |  | 
|  | 9248 | static PyObject* | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9249 | unicode_capitalize(PyUnicodeObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9250 | { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9251 | return fixup(self, fixcapitalize); | 
|  | 9252 | } | 
|  | 9253 |  | 
|  | 9254 | #if 0 | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9255 | PyDoc_STRVAR(capwords__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9256 | "S.capwords() -> str\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9257 | \n\ | 
|  | 9258 | Apply .capitalize() to all words in S and return the result with\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9259 | normalized whitespace (all whitespace strings are replaced by ' ')."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9260 |  | 
|  | 9261 | static PyObject* | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9262 | unicode_capwords(PyUnicodeObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9263 | { | 
|  | 9264 | PyObject *list; | 
|  | 9265 | PyObject *item; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9266 | Py_ssize_t i; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9267 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9268 | /* Split into words */ | 
|  | 9269 | list = split(self, NULL, -1); | 
|  | 9270 | if (!list) | 
|  | 9271 | return NULL; | 
|  | 9272 |  | 
|  | 9273 | /* Capitalize each word */ | 
|  | 9274 | for (i = 0; i < PyList_GET_SIZE(list); i++) { | 
|  | 9275 | item = fixup((PyUnicodeObject *)PyList_GET_ITEM(list, i), | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9276 | fixcapitalize); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9277 | if (item == NULL) | 
|  | 9278 | goto onError; | 
|  | 9279 | Py_DECREF(PyList_GET_ITEM(list, i)); | 
|  | 9280 | PyList_SET_ITEM(list, i, item); | 
|  | 9281 | } | 
|  | 9282 |  | 
|  | 9283 | /* Join the words to form a new string */ | 
|  | 9284 | item = PyUnicode_Join(NULL, list); | 
|  | 9285 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9286 | onError: | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9287 | Py_DECREF(list); | 
|  | 9288 | return (PyObject *)item; | 
|  | 9289 | } | 
|  | 9290 | #endif | 
|  | 9291 |  | 
| Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 9292 | /* Argument converter.  Coerces to a single unicode character */ | 
|  | 9293 |  | 
|  | 9294 | static int | 
|  | 9295 | convert_uc(PyObject *obj, void *addr) | 
|  | 9296 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9297 | Py_UCS4 *fillcharloc = (Py_UCS4 *)addr; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9298 | PyObject *uniobj; | 
| Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 9299 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9300 | uniobj = PyUnicode_FromObject(obj); | 
|  | 9301 | if (uniobj == NULL) { | 
|  | 9302 | PyErr_SetString(PyExc_TypeError, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9303 | "The fill character cannot be converted to Unicode"); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9304 | return 0; | 
|  | 9305 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9306 | if (PyUnicode_GET_LENGTH(uniobj) != 1) { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9307 | PyErr_SetString(PyExc_TypeError, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9308 | "The fill character must be exactly one character long"); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9309 | Py_DECREF(uniobj); | 
|  | 9310 | return 0; | 
|  | 9311 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9312 | *fillcharloc = PyUnicode_READ_CHAR(uniobj, 0); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9313 | Py_DECREF(uniobj); | 
|  | 9314 | return 1; | 
| Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 9315 | } | 
|  | 9316 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9317 | PyDoc_STRVAR(center__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9318 | "S.center(width[, fillchar]) -> str\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9319 | \n\ | 
| Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 9320 | Return S centered in a string of length width. Padding is\n\ | 
| Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 9321 | done using the specified fill character (default is a space)"); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9322 |  | 
|  | 9323 | static PyObject * | 
|  | 9324 | unicode_center(PyUnicodeObject *self, PyObject *args) | 
|  | 9325 | { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9326 | Py_ssize_t marg, left; | 
|  | 9327 | Py_ssize_t width; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9328 | Py_UCS4 fillchar = ' '; | 
|  | 9329 |  | 
| Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 9330 | 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] | 9331 | return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9332 |  | 
| Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 9333 | if (PyUnicode_READY(self) == -1) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9334 | return NULL; | 
|  | 9335 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9336 | if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9337 | Py_INCREF(self); | 
|  | 9338 | return (PyObject*) self; | 
|  | 9339 | } | 
|  | 9340 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9341 | marg = width - _PyUnicode_LENGTH(self); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9342 | left = marg / 2 + (marg & width & 1); | 
|  | 9343 |  | 
| Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 9344 | return (PyObject*) pad(self, left, marg - left, fillchar); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9345 | } | 
|  | 9346 |  | 
| Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9347 | #if 0 | 
|  | 9348 |  | 
|  | 9349 | /* This code should go into some future Unicode collation support | 
|  | 9350 | module. The basic comparison should compare ordinals on a naive | 
| Georg Brandl | c6c3178 | 2009-06-08 13:41:29 +0000 | [diff] [blame] | 9351 | basis (this is what Java does and thus Jython too). */ | 
| Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9352 |  | 
| Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9353 | /* speedy UTF-16 code point order comparison */ | 
|  | 9354 | /* gleaned from: */ | 
|  | 9355 | /* http://www-4.ibm.com/software/developer/library/utf16.html?dwzone=unicode */ | 
|  | 9356 |  | 
| Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 9357 | static short utf16Fixup[32] = | 
| Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9358 | { | 
| Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9359 | 0, 0, 0, 0, 0, 0, 0, 0, | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9360 | 0, 0, 0, 0, 0, 0, 0, 0, | 
|  | 9361 | 0, 0, 0, 0, 0, 0, 0, 0, | 
| Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 9362 | 0, 0, 0, 0x2000, -0x800, -0x800, -0x800, -0x800 | 
| Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9363 | }; | 
|  | 9364 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9365 | static int | 
|  | 9366 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) | 
|  | 9367 | { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9368 | Py_ssize_t len1, len2; | 
| Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9369 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9370 | Py_UNICODE *s1 = str1->str; | 
|  | 9371 | Py_UNICODE *s2 = str2->str; | 
|  | 9372 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9373 | len1 = str1->_base._base.length; | 
|  | 9374 | len2 = str2->_base._base.length; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9375 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9376 | while (len1 > 0 && len2 > 0) { | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9377 | Py_UNICODE c1, c2; | 
| Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9378 |  | 
|  | 9379 | c1 = *s1++; | 
|  | 9380 | c2 = *s2++; | 
| Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 9381 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9382 | if (c1 > (1<<11) * 26) | 
|  | 9383 | c1 += utf16Fixup[c1>>11]; | 
|  | 9384 | if (c2 > (1<<11) * 26) | 
| Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9385 | c2 += utf16Fixup[c2>>11]; | 
| Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9386 | /* now c1 and c2 are in UTF-32-compatible order */ | 
| Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 9387 |  | 
|  | 9388 | if (c1 != c2) | 
|  | 9389 | return (c1 < c2) ? -1 : 1; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9390 |  | 
| Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9391 | len1--; len2--; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9392 | } | 
|  | 9393 |  | 
|  | 9394 | return (len1 < len2) ? -1 : (len1 != len2); | 
|  | 9395 | } | 
|  | 9396 |  | 
| Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9397 | #else | 
|  | 9398 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9399 | /* This function assumes that str1 and str2 are readied by the caller. */ | 
|  | 9400 |  | 
| Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9401 | static int | 
|  | 9402 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) | 
|  | 9403 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9404 | int kind1, kind2; | 
|  | 9405 | void *data1, *data2; | 
|  | 9406 | Py_ssize_t len1, len2, i; | 
| Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9407 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9408 | kind1 = PyUnicode_KIND(str1); | 
|  | 9409 | kind2 = PyUnicode_KIND(str2); | 
|  | 9410 | data1 = PyUnicode_DATA(str1); | 
|  | 9411 | data2 = PyUnicode_DATA(str2); | 
|  | 9412 | len1 = PyUnicode_GET_LENGTH(str1); | 
|  | 9413 | len2 = PyUnicode_GET_LENGTH(str2); | 
| Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9414 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9415 | for (i = 0; i < len1 && i < len2; ++i) { | 
|  | 9416 | Py_UCS4 c1, c2; | 
|  | 9417 | c1 = PyUnicode_READ(kind1, data1, i); | 
|  | 9418 | c2 = PyUnicode_READ(kind2, data2, i); | 
| Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 9419 |  | 
|  | 9420 | if (c1 != c2) | 
|  | 9421 | return (c1 < c2) ? -1 : 1; | 
| Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9422 | } | 
|  | 9423 |  | 
|  | 9424 | return (len1 < len2) ? -1 : (len1 != len2); | 
|  | 9425 | } | 
|  | 9426 |  | 
|  | 9427 | #endif | 
|  | 9428 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9429 | int | 
|  | 9430 | PyUnicode_Compare(PyObject *left, PyObject *right) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9431 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9432 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { | 
|  | 9433 | if (PyUnicode_READY(left) == -1 || | 
|  | 9434 | PyUnicode_READY(right) == -1) | 
|  | 9435 | return -1; | 
| Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 9436 | return unicode_compare((PyUnicodeObject *)left, | 
|  | 9437 | (PyUnicodeObject *)right); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9438 | } | 
| Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 9439 | PyErr_Format(PyExc_TypeError, | 
|  | 9440 | "Can't compare %.100s and %.100s", | 
|  | 9441 | left->ob_type->tp_name, | 
|  | 9442 | right->ob_type->tp_name); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9443 | return -1; | 
|  | 9444 | } | 
|  | 9445 |  | 
| Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9446 | int | 
|  | 9447 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) | 
|  | 9448 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9449 | Py_ssize_t i; | 
|  | 9450 | int kind; | 
|  | 9451 | void *data; | 
|  | 9452 | Py_UCS4 chr; | 
|  | 9453 |  | 
| Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9454 | assert(PyUnicode_Check(uni)); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9455 | if (PyUnicode_READY(uni) == -1) | 
|  | 9456 | return -1; | 
|  | 9457 | kind = PyUnicode_KIND(uni); | 
|  | 9458 | data = PyUnicode_DATA(uni); | 
| Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9459 | /* Compare Unicode string and source character set string */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9460 | for (i = 0; (chr = PyUnicode_READ(kind, data, i)) && str[i]; i++) | 
|  | 9461 | if (chr != str[i]) | 
|  | 9462 | return (chr < (unsigned char)(str[i])) ? -1 : 1; | 
| Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 9463 | /* This check keeps Python strings that end in '\0' from comparing equal | 
|  | 9464 | to C strings identical up to that point. */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9465 | if (PyUnicode_GET_LENGTH(uni) != i || chr) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9466 | return 1; /* uni is longer */ | 
| Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9467 | if (str[i]) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9468 | return -1; /* str is longer */ | 
| Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9469 | return 0; | 
|  | 9470 | } | 
|  | 9471 |  | 
| Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 9472 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9473 | #define TEST_COND(cond)                         \ | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9474 | ((cond) ? Py_True : Py_False) | 
| Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 9475 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9476 | PyObject * | 
|  | 9477 | PyUnicode_RichCompare(PyObject *left, PyObject *right, int op) | 
| Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 9478 | { | 
|  | 9479 | int result; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9480 |  | 
| Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 9481 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { | 
|  | 9482 | PyObject *v; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9483 | if (PyUnicode_READY(left) == -1 || | 
|  | 9484 | PyUnicode_READY(right) == -1) | 
|  | 9485 | return NULL; | 
|  | 9486 | if (PyUnicode_GET_LENGTH(left) != PyUnicode_GET_LENGTH(right) || | 
|  | 9487 | PyUnicode_KIND(left) != PyUnicode_KIND(right)) { | 
| Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 9488 | if (op == Py_EQ) { | 
|  | 9489 | Py_INCREF(Py_False); | 
|  | 9490 | return Py_False; | 
|  | 9491 | } | 
|  | 9492 | if (op == Py_NE) { | 
|  | 9493 | Py_INCREF(Py_True); | 
|  | 9494 | return Py_True; | 
|  | 9495 | } | 
|  | 9496 | } | 
|  | 9497 | if (left == right) | 
|  | 9498 | result = 0; | 
|  | 9499 | else | 
|  | 9500 | result = unicode_compare((PyUnicodeObject *)left, | 
|  | 9501 | (PyUnicodeObject *)right); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9502 |  | 
| Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 9503 | /* Convert the return value to a Boolean */ | 
|  | 9504 | switch (op) { | 
|  | 9505 | case Py_EQ: | 
|  | 9506 | v = TEST_COND(result == 0); | 
|  | 9507 | break; | 
|  | 9508 | case Py_NE: | 
|  | 9509 | v = TEST_COND(result != 0); | 
|  | 9510 | break; | 
|  | 9511 | case Py_LE: | 
|  | 9512 | v = TEST_COND(result <= 0); | 
|  | 9513 | break; | 
|  | 9514 | case Py_GE: | 
|  | 9515 | v = TEST_COND(result >= 0); | 
|  | 9516 | break; | 
|  | 9517 | case Py_LT: | 
|  | 9518 | v = TEST_COND(result == -1); | 
|  | 9519 | break; | 
|  | 9520 | case Py_GT: | 
|  | 9521 | v = TEST_COND(result == 1); | 
|  | 9522 | break; | 
|  | 9523 | default: | 
|  | 9524 | PyErr_BadArgument(); | 
|  | 9525 | return NULL; | 
|  | 9526 | } | 
|  | 9527 | Py_INCREF(v); | 
|  | 9528 | return v; | 
| Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 9529 | } | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9530 |  | 
| Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 9531 | Py_RETURN_NOTIMPLEMENTED; | 
| Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 9532 | } | 
|  | 9533 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9534 | int | 
|  | 9535 | PyUnicode_Contains(PyObject *container, PyObject *element) | 
| Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 9536 | { | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9537 | PyObject *str, *sub; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9538 | int kind1, kind2, kind; | 
|  | 9539 | void *buf1, *buf2; | 
|  | 9540 | Py_ssize_t len1, len2; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9541 | int result; | 
| Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 9542 |  | 
|  | 9543 | /* Coerce the two arguments */ | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9544 | sub = PyUnicode_FromObject(element); | 
|  | 9545 | if (!sub) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9546 | PyErr_Format(PyExc_TypeError, | 
|  | 9547 | "'in <string>' requires string as left operand, not %s", | 
|  | 9548 | element->ob_type->tp_name); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9549 | return -1; | 
| Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 9550 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9551 | if (PyUnicode_READY(sub) == -1) | 
|  | 9552 | return -1; | 
| Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 9553 |  | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9554 | str = PyUnicode_FromObject(container); | 
| Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 9555 | if (!str || PyUnicode_READY(str) == -1) { | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9556 | Py_DECREF(sub); | 
|  | 9557 | return -1; | 
|  | 9558 | } | 
|  | 9559 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9560 | kind1 = PyUnicode_KIND(str); | 
|  | 9561 | kind2 = PyUnicode_KIND(sub); | 
|  | 9562 | kind = kind1 > kind2 ? kind1 : kind2; | 
|  | 9563 | buf1 = PyUnicode_DATA(str); | 
|  | 9564 | buf2 = PyUnicode_DATA(sub); | 
|  | 9565 | if (kind1 != kind) | 
|  | 9566 | buf1 = _PyUnicode_AsKind((PyObject*)str, kind); | 
|  | 9567 | if (!buf1) { | 
|  | 9568 | Py_DECREF(sub); | 
|  | 9569 | return -1; | 
|  | 9570 | } | 
|  | 9571 | if (kind2 != kind) | 
|  | 9572 | buf2 = _PyUnicode_AsKind((PyObject*)sub, kind); | 
|  | 9573 | if (!buf2) { | 
|  | 9574 | Py_DECREF(sub); | 
|  | 9575 | if (kind1 != kind) PyMem_Free(buf1); | 
|  | 9576 | return -1; | 
|  | 9577 | } | 
|  | 9578 | len1 = PyUnicode_GET_LENGTH(str); | 
|  | 9579 | len2 = PyUnicode_GET_LENGTH(sub); | 
|  | 9580 |  | 
|  | 9581 | switch(kind) { | 
|  | 9582 | case PyUnicode_1BYTE_KIND: | 
|  | 9583 | result = ucs1lib_find(buf1, len1, buf2, len2, 0) != -1; | 
|  | 9584 | break; | 
|  | 9585 | case PyUnicode_2BYTE_KIND: | 
|  | 9586 | result = ucs2lib_find(buf1, len1, buf2, len2, 0) != -1; | 
|  | 9587 | break; | 
|  | 9588 | case PyUnicode_4BYTE_KIND: | 
|  | 9589 | result = ucs4lib_find(buf1, len1, buf2, len2, 0) != -1; | 
|  | 9590 | break; | 
|  | 9591 | default: | 
|  | 9592 | result = -1; | 
|  | 9593 | assert(0); | 
|  | 9594 | } | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9595 |  | 
|  | 9596 | Py_DECREF(str); | 
|  | 9597 | Py_DECREF(sub); | 
|  | 9598 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9599 | if (kind1 != kind) | 
|  | 9600 | PyMem_Free(buf1); | 
|  | 9601 | if (kind2 != kind) | 
|  | 9602 | PyMem_Free(buf2); | 
|  | 9603 |  | 
| Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 9604 | return result; | 
| Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 9605 | } | 
|  | 9606 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9607 | /* Concat to string or Unicode object giving a new Unicode object. */ | 
|  | 9608 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9609 | PyObject * | 
|  | 9610 | PyUnicode_Concat(PyObject *left, PyObject *right) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9611 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9612 | PyObject *u = NULL, *v = NULL, *w; | 
|  | 9613 | Py_UCS4 maxchar; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9614 |  | 
|  | 9615 | /* Coerce the two arguments */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9616 | u = PyUnicode_FromObject(left); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9617 | if (u == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9618 | goto onError; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9619 | v = PyUnicode_FromObject(right); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9620 | if (v == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9621 | goto onError; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9622 |  | 
|  | 9623 | /* Shortcuts */ | 
| Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 9624 | if (v == unicode_empty) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9625 | Py_DECREF(v); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9626 | return u; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9627 | } | 
| Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 9628 | if (u == unicode_empty) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9629 | Py_DECREF(u); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9630 | return v; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9631 | } | 
|  | 9632 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9633 | maxchar = PyUnicode_MAX_CHAR_VALUE(u); | 
| Victor Stinner | ff9e50f | 2011-09-28 22:17:19 +0200 | [diff] [blame] | 9634 | maxchar = Py_MAX(maxchar, PyUnicode_MAX_CHAR_VALUE(v)); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9635 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9636 | /* Concat the two Unicode strings */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9637 | w = PyUnicode_New( | 
|  | 9638 | PyUnicode_GET_LENGTH(u) + PyUnicode_GET_LENGTH(v), | 
|  | 9639 | maxchar); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9640 | if (w == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9641 | goto onError; | 
| Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9642 | if (PyUnicode_CopyCharacters(w, 0, u, 0, PyUnicode_GET_LENGTH(u)) < 0) | 
|  | 9643 | goto onError; | 
| Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 9644 | if (PyUnicode_CopyCharacters(w, PyUnicode_GET_LENGTH(u), | 
| Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9645 | v, 0, | 
|  | 9646 | PyUnicode_GET_LENGTH(v)) < 0) | 
|  | 9647 | goto onError; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9648 | Py_DECREF(u); | 
|  | 9649 | Py_DECREF(v); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9650 | return w; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9651 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9652 | onError: | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9653 | Py_XDECREF(u); | 
|  | 9654 | Py_XDECREF(v); | 
|  | 9655 | return NULL; | 
|  | 9656 | } | 
|  | 9657 |  | 
| Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 9658 | void | 
|  | 9659 | PyUnicode_Append(PyObject **pleft, PyObject *right) | 
|  | 9660 | { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9661 | PyObject *new; | 
|  | 9662 | if (*pleft == NULL) | 
|  | 9663 | return; | 
|  | 9664 | if (right == NULL || !PyUnicode_Check(*pleft)) { | 
|  | 9665 | Py_DECREF(*pleft); | 
|  | 9666 | *pleft = NULL; | 
|  | 9667 | return; | 
|  | 9668 | } | 
|  | 9669 | new = PyUnicode_Concat(*pleft, right); | 
|  | 9670 | Py_DECREF(*pleft); | 
|  | 9671 | *pleft = new; | 
| Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 9672 | } | 
|  | 9673 |  | 
|  | 9674 | void | 
|  | 9675 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) | 
|  | 9676 | { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9677 | PyUnicode_Append(pleft, right); | 
|  | 9678 | Py_XDECREF(right); | 
| Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 9679 | } | 
|  | 9680 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9681 | PyDoc_STRVAR(count__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9682 | "S.count(sub[, start[, end]]) -> int\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9683 | \n\ | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9684 | Return the number of non-overlapping occurrences of substring sub in\n\ | 
| Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 9685 | 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] | 9686 | interpreted as in slice notation."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9687 |  | 
|  | 9688 | static PyObject * | 
|  | 9689 | unicode_count(PyUnicodeObject *self, PyObject *args) | 
|  | 9690 | { | 
|  | 9691 | PyUnicodeObject *substring; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9692 | Py_ssize_t start = 0; | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9693 | Py_ssize_t end = PY_SSIZE_T_MAX; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9694 | PyObject *result; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9695 | int kind1, kind2, kind; | 
|  | 9696 | void *buf1, *buf2; | 
|  | 9697 | Py_ssize_t len1, len2, iresult; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9698 |  | 
| Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 9699 | if (!stringlib_parse_args_finds_unicode("count", args, &substring, | 
|  | 9700 | &start, &end)) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9701 | return NULL; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9702 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9703 | kind1 = PyUnicode_KIND(self); | 
|  | 9704 | kind2 = PyUnicode_KIND(substring); | 
|  | 9705 | kind = kind1 > kind2 ? kind1 : kind2; | 
|  | 9706 | buf1 = PyUnicode_DATA(self); | 
|  | 9707 | buf2 = PyUnicode_DATA(substring); | 
|  | 9708 | if (kind1 != kind) | 
|  | 9709 | buf1 = _PyUnicode_AsKind((PyObject*)self, kind); | 
|  | 9710 | if (!buf1) { | 
|  | 9711 | Py_DECREF(substring); | 
|  | 9712 | return NULL; | 
|  | 9713 | } | 
|  | 9714 | if (kind2 != kind) | 
|  | 9715 | buf2 = _PyUnicode_AsKind((PyObject*)substring, kind); | 
|  | 9716 | if (!buf2) { | 
|  | 9717 | Py_DECREF(substring); | 
|  | 9718 | if (kind1 != kind) PyMem_Free(buf1); | 
|  | 9719 | return NULL; | 
|  | 9720 | } | 
|  | 9721 | len1 = PyUnicode_GET_LENGTH(self); | 
|  | 9722 | len2 = PyUnicode_GET_LENGTH(substring); | 
|  | 9723 |  | 
|  | 9724 | ADJUST_INDICES(start, end, len1); | 
|  | 9725 | switch(kind) { | 
|  | 9726 | case PyUnicode_1BYTE_KIND: | 
|  | 9727 | iresult = ucs1lib_count( | 
|  | 9728 | ((Py_UCS1*)buf1) + start, end - start, | 
|  | 9729 | buf2, len2, PY_SSIZE_T_MAX | 
|  | 9730 | ); | 
|  | 9731 | break; | 
|  | 9732 | case PyUnicode_2BYTE_KIND: | 
|  | 9733 | iresult = ucs2lib_count( | 
|  | 9734 | ((Py_UCS2*)buf1) + start, end - start, | 
|  | 9735 | buf2, len2, PY_SSIZE_T_MAX | 
|  | 9736 | ); | 
|  | 9737 | break; | 
|  | 9738 | case PyUnicode_4BYTE_KIND: | 
|  | 9739 | iresult = ucs4lib_count( | 
|  | 9740 | ((Py_UCS4*)buf1) + start, end - start, | 
|  | 9741 | buf2, len2, PY_SSIZE_T_MAX | 
|  | 9742 | ); | 
|  | 9743 | break; | 
|  | 9744 | default: | 
|  | 9745 | assert(0); iresult = 0; | 
|  | 9746 | } | 
|  | 9747 |  | 
|  | 9748 | result = PyLong_FromSsize_t(iresult); | 
|  | 9749 |  | 
|  | 9750 | if (kind1 != kind) | 
|  | 9751 | PyMem_Free(buf1); | 
|  | 9752 | if (kind2 != kind) | 
|  | 9753 | PyMem_Free(buf2); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9754 |  | 
|  | 9755 | Py_DECREF(substring); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9756 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9757 | return result; | 
|  | 9758 | } | 
|  | 9759 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9760 | PyDoc_STRVAR(encode__doc__, | 
| Victor Stinner | c911bbf | 2010-11-07 19:04:46 +0000 | [diff] [blame] | 9761 | "S.encode(encoding='utf-8', errors='strict') -> bytes\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9762 | \n\ | 
| Victor Stinner | e14e212 | 2010-11-07 18:41:46 +0000 | [diff] [blame] | 9763 | Encode S using the codec registered for encoding. Default encoding\n\ | 
|  | 9764 | 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] | 9765 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 9766 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ | 
|  | 9767 | 'xmlcharrefreplace' as well as any other name registered with\n\ | 
|  | 9768 | codecs.register_error that can handle UnicodeEncodeErrors."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9769 |  | 
|  | 9770 | static PyObject * | 
| Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 9771 | unicode_encode(PyUnicodeObject *self, PyObject *args, PyObject *kwargs) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9772 | { | 
| Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 9773 | static char *kwlist[] = {"encoding", "errors", 0}; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9774 | char *encoding = NULL; | 
|  | 9775 | char *errors = NULL; | 
| Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 9776 |  | 
| Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 9777 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", | 
|  | 9778 | kwlist, &encoding, &errors)) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9779 | return NULL; | 
| Georg Brandl | 3b9406b | 2010-12-03 07:54:09 +0000 | [diff] [blame] | 9780 | return PyUnicode_AsEncodedString((PyObject *)self, encoding, errors); | 
| Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 9781 | } | 
|  | 9782 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9783 | PyDoc_STRVAR(expandtabs__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9784 | "S.expandtabs([tabsize]) -> str\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9785 | \n\ | 
|  | 9786 | 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] | 9787 | 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] | 9788 |  | 
|  | 9789 | static PyObject* | 
|  | 9790 | unicode_expandtabs(PyUnicodeObject *self, PyObject *args) | 
|  | 9791 | { | 
|  | 9792 | Py_UNICODE *e; | 
|  | 9793 | Py_UNICODE *p; | 
|  | 9794 | Py_UNICODE *q; | 
| Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 9795 | Py_UNICODE *qe; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9796 | Py_ssize_t i, j, incr, wstr_length; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9797 | PyUnicodeObject *u; | 
|  | 9798 | int tabsize = 8; | 
|  | 9799 |  | 
|  | 9800 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9801 | return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9802 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9803 | if (PyUnicode_AsUnicodeAndSize((PyObject *)self, &wstr_length) == NULL) | 
|  | 9804 | return NULL; | 
|  | 9805 |  | 
| Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 9806 | /* First pass: determine size of output string */ | 
| Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 9807 | i = 0; /* chars up to and including most recent \n or \r */ | 
|  | 9808 | j = 0; /* chars since most recent \n or \r (use in tab calculations) */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9809 | e = _PyUnicode_WSTR(self) + wstr_length; /* end of input */ | 
|  | 9810 | for (p = _PyUnicode_WSTR(self); p < e; p++) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9811 | if (*p == '\t') { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9812 | if (tabsize > 0) { | 
|  | 9813 | incr = tabsize - (j % tabsize); /* cannot overflow */ | 
|  | 9814 | if (j > PY_SSIZE_T_MAX - incr) | 
|  | 9815 | goto overflow1; | 
|  | 9816 | j += incr; | 
| Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 9817 | } | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9818 | } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9819 | else { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9820 | if (j > PY_SSIZE_T_MAX - 1) | 
|  | 9821 | goto overflow1; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9822 | j++; | 
|  | 9823 | if (*p == '\n' || *p == '\r') { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9824 | if (i > PY_SSIZE_T_MAX - j) | 
|  | 9825 | goto overflow1; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9826 | i += j; | 
| Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 9827 | j = 0; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9828 | } | 
|  | 9829 | } | 
|  | 9830 |  | 
| Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 9831 | if (i > PY_SSIZE_T_MAX - j) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9832 | goto overflow1; | 
| Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 9833 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9834 | /* Second pass: create output string and fill it */ | 
|  | 9835 | u = _PyUnicode_New(i + j); | 
|  | 9836 | if (!u) | 
|  | 9837 | return NULL; | 
|  | 9838 |  | 
| Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 9839 | j = 0; /* same as in first pass */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9840 | q = _PyUnicode_WSTR(u); /* next output char */ | 
|  | 9841 | qe = _PyUnicode_WSTR(u) + PyUnicode_GET_SIZE(u); /* end of output */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9842 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9843 | for (p = _PyUnicode_WSTR(self); p < e; p++) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9844 | if (*p == '\t') { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9845 | if (tabsize > 0) { | 
|  | 9846 | i = tabsize - (j % tabsize); | 
|  | 9847 | j += i; | 
|  | 9848 | while (i--) { | 
|  | 9849 | if (q >= qe) | 
|  | 9850 | goto overflow2; | 
|  | 9851 | *q++ = ' '; | 
| Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 9852 | } | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9853 | } | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9854 | } | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9855 | else { | 
|  | 9856 | if (q >= qe) | 
|  | 9857 | goto overflow2; | 
|  | 9858 | *q++ = *p; | 
| Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 9859 | j++; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9860 | if (*p == '\n' || *p == '\r') | 
|  | 9861 | j = 0; | 
|  | 9862 | } | 
|  | 9863 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9864 | if (PyUnicode_READY(u) == -1) { | 
|  | 9865 | Py_DECREF(u); | 
|  | 9866 | return NULL; | 
|  | 9867 | } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9868 | return (PyObject*) u; | 
| Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 9869 |  | 
|  | 9870 | overflow2: | 
|  | 9871 | Py_DECREF(u); | 
|  | 9872 | overflow1: | 
|  | 9873 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); | 
|  | 9874 | return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9875 | } | 
|  | 9876 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9877 | PyDoc_STRVAR(find__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9878 | "S.find(sub[, start[, end]]) -> int\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9879 | \n\ | 
|  | 9880 | Return the lowest index in S where substring sub is found,\n\ | 
| Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 9881 | such that sub is contained within S[start:end].  Optional\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9882 | arguments start and end are interpreted as in slice notation.\n\ | 
|  | 9883 | \n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9884 | Return -1 on failure."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9885 |  | 
|  | 9886 | static PyObject * | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9887 | unicode_find(PyObject *self, PyObject *args) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9888 | { | 
| Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 9889 | PyUnicodeObject *substring; | 
| Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 9890 | Py_ssize_t start; | 
|  | 9891 | Py_ssize_t end; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9892 | Py_ssize_t result; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9893 |  | 
| Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 9894 | if (!stringlib_parse_args_finds_unicode("find", args, &substring, | 
|  | 9895 | &start, &end)) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9896 | return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9897 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9898 | if (PyUnicode_READY(self) == -1) | 
|  | 9899 | return NULL; | 
|  | 9900 | if (PyUnicode_READY(substring) == -1) | 
|  | 9901 | return NULL; | 
|  | 9902 |  | 
|  | 9903 | result = any_find_slice( | 
|  | 9904 | ucs1lib_find_slice, ucs2lib_find_slice, ucs4lib_find_slice, | 
|  | 9905 | self, (PyObject*)substring, start, end | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9906 | ); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9907 |  | 
|  | 9908 | Py_DECREF(substring); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9909 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9910 | if (result == -2) | 
|  | 9911 | return NULL; | 
|  | 9912 |  | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 9913 | return PyLong_FromSsize_t(result); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9914 | } | 
|  | 9915 |  | 
|  | 9916 | static PyObject * | 
| Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 9917 | unicode_getitem(PyObject *self, Py_ssize_t index) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9918 | { | 
| Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 9919 | Py_UCS4 ch = PyUnicode_ReadChar(self, index); | 
|  | 9920 | if (ch == (Py_UCS4)-1) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9921 | return NULL; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9922 | return PyUnicode_FromOrdinal(ch); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9923 | } | 
|  | 9924 |  | 
| Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 9925 | /* Believe it or not, this produces the same value for ASCII strings | 
| Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 9926 | as bytes_hash(). */ | 
| Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 9927 | static Py_hash_t | 
| Neil Schemenauer | f8c37d1 | 2007-09-07 20:49:04 +0000 | [diff] [blame] | 9928 | unicode_hash(PyUnicodeObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9929 | { | 
| Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 9930 | Py_ssize_t len; | 
| Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 9931 | Py_uhash_t x; | 
| Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 9932 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9933 | if (_PyUnicode_HASH(self) != -1) | 
|  | 9934 | return _PyUnicode_HASH(self); | 
|  | 9935 | if (PyUnicode_READY(self) == -1) | 
|  | 9936 | return -1; | 
|  | 9937 | len = PyUnicode_GET_LENGTH(self); | 
|  | 9938 |  | 
|  | 9939 | /* The hash function as a macro, gets expanded three times below. */ | 
|  | 9940 | #define HASH(P) \ | 
|  | 9941 | x = (Py_uhash_t)*P << 7; \ | 
|  | 9942 | while (--len >= 0) \ | 
|  | 9943 | x = (1000003*x) ^ (Py_uhash_t)*P++; | 
|  | 9944 |  | 
|  | 9945 | switch (PyUnicode_KIND(self)) { | 
|  | 9946 | case PyUnicode_1BYTE_KIND: { | 
|  | 9947 | const unsigned char *c = PyUnicode_1BYTE_DATA(self); | 
|  | 9948 | HASH(c); | 
|  | 9949 | break; | 
|  | 9950 | } | 
|  | 9951 | case PyUnicode_2BYTE_KIND: { | 
|  | 9952 | const Py_UCS2 *s = PyUnicode_2BYTE_DATA(self); | 
|  | 9953 | HASH(s); | 
|  | 9954 | break; | 
|  | 9955 | } | 
|  | 9956 | default: { | 
|  | 9957 | Py_UCS4 *l; | 
|  | 9958 | assert(PyUnicode_KIND(self) == PyUnicode_4BYTE_KIND && | 
|  | 9959 | "Impossible switch case in unicode_hash"); | 
|  | 9960 | l = PyUnicode_4BYTE_DATA(self); | 
|  | 9961 | HASH(l); | 
|  | 9962 | break; | 
|  | 9963 | } | 
|  | 9964 | } | 
|  | 9965 | x ^= (Py_uhash_t)PyUnicode_GET_LENGTH(self); | 
|  | 9966 |  | 
| Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 9967 | if (x == -1) | 
|  | 9968 | x = -2; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9969 | _PyUnicode_HASH(self) = x; | 
| Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 9970 | return x; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9971 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9972 | #undef HASH | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9973 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9974 | PyDoc_STRVAR(index__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9975 | "S.index(sub[, start[, end]]) -> int\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9976 | \n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9977 | 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] | 9978 |  | 
|  | 9979 | static PyObject * | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9980 | unicode_index(PyObject *self, PyObject *args) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9981 | { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9982 | Py_ssize_t result; | 
| Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 9983 | PyUnicodeObject *substring; | 
| Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 9984 | Py_ssize_t start; | 
|  | 9985 | Py_ssize_t end; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9986 |  | 
| Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 9987 | if (!stringlib_parse_args_finds_unicode("index", args, &substring, | 
|  | 9988 | &start, &end)) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9989 | return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9990 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9991 | if (PyUnicode_READY(self) == -1) | 
|  | 9992 | return NULL; | 
|  | 9993 | if (PyUnicode_READY(substring) == -1) | 
|  | 9994 | return NULL; | 
|  | 9995 |  | 
|  | 9996 | result = any_find_slice( | 
|  | 9997 | ucs1lib_find_slice, ucs2lib_find_slice, ucs4lib_find_slice, | 
|  | 9998 | self, (PyObject*)substring, start, end | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9999 | ); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10000 |  | 
|  | 10001 | Py_DECREF(substring); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10002 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10003 | if (result == -2) | 
|  | 10004 | return NULL; | 
|  | 10005 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10006 | if (result < 0) { | 
|  | 10007 | PyErr_SetString(PyExc_ValueError, "substring not found"); | 
|  | 10008 | return NULL; | 
|  | 10009 | } | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10010 |  | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 10011 | return PyLong_FromSsize_t(result); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10012 | } | 
|  | 10013 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10014 | PyDoc_STRVAR(islower__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10015 | "S.islower() -> bool\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10016 | \n\ | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10017 | 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] | 10018 | at least one cased character in S, False otherwise."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10019 |  | 
|  | 10020 | static PyObject* | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10021 | unicode_islower(PyUnicodeObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10022 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10023 | Py_ssize_t i, length; | 
|  | 10024 | int kind; | 
|  | 10025 | void *data; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10026 | int cased; | 
|  | 10027 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10028 | if (PyUnicode_READY(self) == -1) | 
|  | 10029 | return NULL; | 
|  | 10030 | length = PyUnicode_GET_LENGTH(self); | 
|  | 10031 | kind = PyUnicode_KIND(self); | 
|  | 10032 | data = PyUnicode_DATA(self); | 
|  | 10033 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10034 | /* Shortcut for single character strings */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10035 | if (length == 1) | 
|  | 10036 | return PyBool_FromLong( | 
|  | 10037 | Py_UNICODE_ISLOWER(PyUnicode_READ(kind, data, 0))); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10038 |  | 
| Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10039 | /* Special case for empty strings */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10040 | if (length == 0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10041 | return PyBool_FromLong(0); | 
| Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10042 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10043 | cased = 0; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10044 | for (i = 0; i < length; i++) { | 
|  | 10045 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10046 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10047 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) | 
|  | 10048 | return PyBool_FromLong(0); | 
|  | 10049 | else if (!cased && Py_UNICODE_ISLOWER(ch)) | 
|  | 10050 | cased = 1; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10051 | } | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10052 | return PyBool_FromLong(cased); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10053 | } | 
|  | 10054 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10055 | PyDoc_STRVAR(isupper__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10056 | "S.isupper() -> bool\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10057 | \n\ | 
| Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10058 | 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] | 10059 | at least one cased character in S, False otherwise."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10060 |  | 
|  | 10061 | static PyObject* | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10062 | unicode_isupper(PyUnicodeObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10063 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10064 | Py_ssize_t i, length; | 
|  | 10065 | int kind; | 
|  | 10066 | void *data; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10067 | int cased; | 
|  | 10068 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10069 | if (PyUnicode_READY(self) == -1) | 
|  | 10070 | return NULL; | 
|  | 10071 | length = PyUnicode_GET_LENGTH(self); | 
|  | 10072 | kind = PyUnicode_KIND(self); | 
|  | 10073 | data = PyUnicode_DATA(self); | 
|  | 10074 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10075 | /* Shortcut for single character strings */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10076 | if (length == 1) | 
|  | 10077 | return PyBool_FromLong( | 
|  | 10078 | Py_UNICODE_ISUPPER(PyUnicode_READ(kind, data, 0)) != 0); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10079 |  | 
| Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10080 | /* Special case for empty strings */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10081 | if (length == 0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10082 | return PyBool_FromLong(0); | 
| Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10083 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10084 | cased = 0; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10085 | for (i = 0; i < length; i++) { | 
|  | 10086 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10087 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10088 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) | 
|  | 10089 | return PyBool_FromLong(0); | 
|  | 10090 | else if (!cased && Py_UNICODE_ISUPPER(ch)) | 
|  | 10091 | cased = 1; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10092 | } | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10093 | return PyBool_FromLong(cased); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10094 | } | 
|  | 10095 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10096 | PyDoc_STRVAR(istitle__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10097 | "S.istitle() -> bool\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10098 | \n\ | 
| Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10099 | Return True if S is a titlecased string and there is at least one\n\ | 
|  | 10100 | character in S, i.e. upper- and titlecase characters may only\n\ | 
|  | 10101 | follow uncased characters and lowercase characters only cased ones.\n\ | 
|  | 10102 | Return False otherwise."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10103 |  | 
|  | 10104 | static PyObject* | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10105 | unicode_istitle(PyUnicodeObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10106 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10107 | Py_ssize_t i, length; | 
|  | 10108 | int kind; | 
|  | 10109 | void *data; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10110 | int cased, previous_is_cased; | 
|  | 10111 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10112 | if (PyUnicode_READY(self) == -1) | 
|  | 10113 | return NULL; | 
|  | 10114 | length = PyUnicode_GET_LENGTH(self); | 
|  | 10115 | kind = PyUnicode_KIND(self); | 
|  | 10116 | data = PyUnicode_DATA(self); | 
|  | 10117 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10118 | /* Shortcut for single character strings */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10119 | if (length == 1) { | 
|  | 10120 | Py_UCS4 ch = PyUnicode_READ(kind, data, 0); | 
|  | 10121 | return PyBool_FromLong((Py_UNICODE_ISTITLE(ch) != 0) || | 
|  | 10122 | (Py_UNICODE_ISUPPER(ch) != 0)); | 
|  | 10123 | } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10124 |  | 
| Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10125 | /* Special case for empty strings */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10126 | if (length == 0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10127 | return PyBool_FromLong(0); | 
| Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10128 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10129 | cased = 0; | 
|  | 10130 | previous_is_cased = 0; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10131 | for (i = 0; i < length; i++) { | 
|  | 10132 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10133 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10134 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { | 
|  | 10135 | if (previous_is_cased) | 
|  | 10136 | return PyBool_FromLong(0); | 
|  | 10137 | previous_is_cased = 1; | 
|  | 10138 | cased = 1; | 
|  | 10139 | } | 
|  | 10140 | else if (Py_UNICODE_ISLOWER(ch)) { | 
|  | 10141 | if (!previous_is_cased) | 
|  | 10142 | return PyBool_FromLong(0); | 
|  | 10143 | previous_is_cased = 1; | 
|  | 10144 | cased = 1; | 
|  | 10145 | } | 
|  | 10146 | else | 
|  | 10147 | previous_is_cased = 0; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10148 | } | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10149 | return PyBool_FromLong(cased); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10150 | } | 
|  | 10151 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10152 | PyDoc_STRVAR(isspace__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10153 | "S.isspace() -> bool\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10154 | \n\ | 
| Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10155 | Return True if all characters in S are whitespace\n\ | 
|  | 10156 | and there is at least one character in S, False otherwise."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10157 |  | 
|  | 10158 | static PyObject* | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10159 | unicode_isspace(PyUnicodeObject *self) | 
| 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 | Py_ssize_t i, length; | 
|  | 10162 | int kind; | 
|  | 10163 | void *data; | 
|  | 10164 |  | 
|  | 10165 | if (PyUnicode_READY(self) == -1) | 
|  | 10166 | return NULL; | 
|  | 10167 | length = PyUnicode_GET_LENGTH(self); | 
|  | 10168 | kind = PyUnicode_KIND(self); | 
|  | 10169 | data = PyUnicode_DATA(self); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10170 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10171 | /* Shortcut for single character strings */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10172 | if (length == 1) | 
|  | 10173 | return PyBool_FromLong( | 
|  | 10174 | Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, 0))); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10175 |  | 
| Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10176 | /* Special case for empty strings */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10177 | if (length == 0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10178 | return PyBool_FromLong(0); | 
| Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10179 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10180 | for (i = 0; i < length; i++) { | 
|  | 10181 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); | 
| Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 10182 | if (!Py_UNICODE_ISSPACE(ch)) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10183 | return PyBool_FromLong(0); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10184 | } | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10185 | return PyBool_FromLong(1); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10186 | } | 
|  | 10187 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10188 | PyDoc_STRVAR(isalpha__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10189 | "S.isalpha() -> bool\n\ | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10190 | \n\ | 
| Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10191 | Return True if all characters in S are alphabetic\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10192 | and there is at least one character in S, False otherwise."); | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10193 |  | 
|  | 10194 | static PyObject* | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10195 | unicode_isalpha(PyUnicodeObject *self) | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10196 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10197 | Py_ssize_t i, length; | 
|  | 10198 | int kind; | 
|  | 10199 | void *data; | 
|  | 10200 |  | 
|  | 10201 | if (PyUnicode_READY(self) == -1) | 
|  | 10202 | return NULL; | 
|  | 10203 | length = PyUnicode_GET_LENGTH(self); | 
|  | 10204 | kind = PyUnicode_KIND(self); | 
|  | 10205 | data = PyUnicode_DATA(self); | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10206 |  | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10207 | /* Shortcut for single character strings */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10208 | if (length == 1) | 
|  | 10209 | return PyBool_FromLong( | 
|  | 10210 | Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, 0))); | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10211 |  | 
|  | 10212 | /* Special case for empty strings */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10213 | if (length == 0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10214 | return PyBool_FromLong(0); | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10215 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10216 | for (i = 0; i < length; i++) { | 
|  | 10217 | if (!Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, i))) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10218 | return PyBool_FromLong(0); | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10219 | } | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10220 | return PyBool_FromLong(1); | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10221 | } | 
|  | 10222 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10223 | PyDoc_STRVAR(isalnum__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10224 | "S.isalnum() -> bool\n\ | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10225 | \n\ | 
| Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10226 | Return True if all characters in S are alphanumeric\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10227 | and there is at least one character in S, False otherwise."); | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10228 |  | 
|  | 10229 | static PyObject* | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10230 | unicode_isalnum(PyUnicodeObject *self) | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10231 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10232 | int kind; | 
|  | 10233 | void *data; | 
|  | 10234 | Py_ssize_t len, i; | 
|  | 10235 |  | 
|  | 10236 | if (PyUnicode_READY(self) == -1) | 
|  | 10237 | return NULL; | 
|  | 10238 |  | 
|  | 10239 | kind = PyUnicode_KIND(self); | 
|  | 10240 | data = PyUnicode_DATA(self); | 
|  | 10241 | len = PyUnicode_GET_LENGTH(self); | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10242 |  | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10243 | /* Shortcut for single character strings */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10244 | if (len == 1) { | 
|  | 10245 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); | 
|  | 10246 | return PyBool_FromLong(Py_UNICODE_ISALNUM(ch)); | 
|  | 10247 | } | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10248 |  | 
|  | 10249 | /* Special case for empty strings */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10250 | if (len == 0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10251 | return PyBool_FromLong(0); | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10252 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10253 | for (i = 0; i < len; i++) { | 
|  | 10254 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); | 
| Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 10255 | if (!Py_UNICODE_ISALNUM(ch)) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10256 | return PyBool_FromLong(0); | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10257 | } | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10258 | return PyBool_FromLong(1); | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10259 | } | 
|  | 10260 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10261 | PyDoc_STRVAR(isdecimal__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10262 | "S.isdecimal() -> bool\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10263 | \n\ | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10264 | 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] | 10265 | False otherwise."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10266 |  | 
|  | 10267 | static PyObject* | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10268 | unicode_isdecimal(PyUnicodeObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10269 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10270 | Py_ssize_t i, length; | 
|  | 10271 | int kind; | 
|  | 10272 | void *data; | 
|  | 10273 |  | 
|  | 10274 | if (PyUnicode_READY(self) == -1) | 
|  | 10275 | return NULL; | 
|  | 10276 | length = PyUnicode_GET_LENGTH(self); | 
|  | 10277 | kind = PyUnicode_KIND(self); | 
|  | 10278 | data = PyUnicode_DATA(self); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10279 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10280 | /* Shortcut for single character strings */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10281 | if (length == 1) | 
|  | 10282 | return PyBool_FromLong( | 
|  | 10283 | Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, 0))); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10284 |  | 
| Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10285 | /* Special case for empty strings */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10286 | if (length == 0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10287 | return PyBool_FromLong(0); | 
| Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10288 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10289 | for (i = 0; i < length; i++) { | 
|  | 10290 | if (!Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, i))) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10291 | return PyBool_FromLong(0); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10292 | } | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10293 | return PyBool_FromLong(1); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10294 | } | 
|  | 10295 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10296 | PyDoc_STRVAR(isdigit__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10297 | "S.isdigit() -> bool\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10298 | \n\ | 
| Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10299 | Return True if all characters in S are digits\n\ | 
|  | 10300 | and there is at least one character in S, False otherwise."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10301 |  | 
|  | 10302 | static PyObject* | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10303 | unicode_isdigit(PyUnicodeObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10304 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10305 | Py_ssize_t i, length; | 
|  | 10306 | int kind; | 
|  | 10307 | void *data; | 
|  | 10308 |  | 
|  | 10309 | if (PyUnicode_READY(self) == -1) | 
|  | 10310 | return NULL; | 
|  | 10311 | length = PyUnicode_GET_LENGTH(self); | 
|  | 10312 | kind = PyUnicode_KIND(self); | 
|  | 10313 | data = PyUnicode_DATA(self); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10314 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10315 | /* Shortcut for single character strings */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10316 | if (length == 1) { | 
|  | 10317 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); | 
|  | 10318 | return PyBool_FromLong(Py_UNICODE_ISDIGIT(ch)); | 
|  | 10319 | } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10320 |  | 
| Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10321 | /* Special case for empty strings */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10322 | if (length == 0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10323 | return PyBool_FromLong(0); | 
| Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10324 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10325 | for (i = 0; i < length; i++) { | 
|  | 10326 | if (!Py_UNICODE_ISDIGIT(PyUnicode_READ(kind, data, i))) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10327 | return PyBool_FromLong(0); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10328 | } | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10329 | return PyBool_FromLong(1); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10330 | } | 
|  | 10331 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10332 | PyDoc_STRVAR(isnumeric__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10333 | "S.isnumeric() -> bool\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10334 | \n\ | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10335 | 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] | 10336 | False otherwise."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10337 |  | 
|  | 10338 | static PyObject* | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10339 | unicode_isnumeric(PyUnicodeObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10340 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10341 | Py_ssize_t i, length; | 
|  | 10342 | int kind; | 
|  | 10343 | void *data; | 
|  | 10344 |  | 
|  | 10345 | if (PyUnicode_READY(self) == -1) | 
|  | 10346 | return NULL; | 
|  | 10347 | length = PyUnicode_GET_LENGTH(self); | 
|  | 10348 | kind = PyUnicode_KIND(self); | 
|  | 10349 | data = PyUnicode_DATA(self); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10350 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10351 | /* Shortcut for single character strings */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10352 | if (length == 1) | 
|  | 10353 | return PyBool_FromLong( | 
|  | 10354 | Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, 0))); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10355 |  | 
| Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10356 | /* Special case for empty strings */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10357 | if (length == 0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10358 | return PyBool_FromLong(0); | 
| Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10359 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10360 | for (i = 0; i < length; i++) { | 
|  | 10361 | if (!Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, i))) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10362 | return PyBool_FromLong(0); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10363 | } | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10364 | return PyBool_FromLong(1); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10365 | } | 
|  | 10366 |  | 
| Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 10367 | int | 
|  | 10368 | PyUnicode_IsIdentifier(PyObject *self) | 
|  | 10369 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10370 | int kind; | 
|  | 10371 | void *data; | 
|  | 10372 | Py_ssize_t i; | 
| Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 10373 | Py_UCS4 first; | 
| Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 10374 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10375 | if (PyUnicode_READY(self) == -1) { | 
|  | 10376 | Py_FatalError("identifier not ready"); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10377 | return 0; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10378 | } | 
|  | 10379 |  | 
|  | 10380 | /* Special case for empty strings */ | 
|  | 10381 | if (PyUnicode_GET_LENGTH(self) == 0) | 
|  | 10382 | return 0; | 
|  | 10383 | kind = PyUnicode_KIND(self); | 
|  | 10384 | data = PyUnicode_DATA(self); | 
| Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 10385 |  | 
|  | 10386 | /* PEP 3131 says that the first character must be in | 
|  | 10387 | XID_Start and subsequent characters in XID_Continue, | 
|  | 10388 | and for the ASCII range, the 2.x rules apply (i.e | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10389 | start with letters and underscore, continue with | 
| Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 10390 | letters, digits, underscore). However, given the current | 
|  | 10391 | definition of XID_Start and XID_Continue, it is sufficient | 
|  | 10392 | to check just for these, except that _ must be allowed | 
|  | 10393 | as starting an identifier.  */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10394 | first = PyUnicode_READ(kind, data, 0); | 
| Benjamin Peterson | f413b80 | 2011-08-12 22:17:18 -0500 | [diff] [blame] | 10395 | if (!_PyUnicode_IsXidStart(first) && first != 0x5F /* LOW LINE */) | 
| Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 10396 | return 0; | 
|  | 10397 |  | 
| Benjamin Peterson | 9c6e6a0 | 2011-09-28 08:09:05 -0400 | [diff] [blame] | 10398 | for (i = 1; i < PyUnicode_GET_LENGTH(self); i++) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10399 | if (!_PyUnicode_IsXidContinue(PyUnicode_READ(kind, data, i))) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10400 | return 0; | 
| Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 10401 | return 1; | 
|  | 10402 | } | 
|  | 10403 |  | 
|  | 10404 | PyDoc_STRVAR(isidentifier__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10405 | "S.isidentifier() -> bool\n\ | 
| Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 10406 | \n\ | 
|  | 10407 | Return True if S is a valid identifier according\n\ | 
|  | 10408 | to the language definition."); | 
|  | 10409 |  | 
|  | 10410 | static PyObject* | 
|  | 10411 | unicode_isidentifier(PyObject *self) | 
|  | 10412 | { | 
|  | 10413 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); | 
|  | 10414 | } | 
|  | 10415 |  | 
| Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10416 | PyDoc_STRVAR(isprintable__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10417 | "S.isprintable() -> bool\n\ | 
| Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10418 | \n\ | 
|  | 10419 | Return True if all characters in S are considered\n\ | 
|  | 10420 | printable in repr() or S is empty, False otherwise."); | 
|  | 10421 |  | 
|  | 10422 | static PyObject* | 
|  | 10423 | unicode_isprintable(PyObject *self) | 
|  | 10424 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10425 | Py_ssize_t i, length; | 
|  | 10426 | int kind; | 
|  | 10427 | void *data; | 
|  | 10428 |  | 
|  | 10429 | if (PyUnicode_READY(self) == -1) | 
|  | 10430 | return NULL; | 
|  | 10431 | length = PyUnicode_GET_LENGTH(self); | 
|  | 10432 | kind = PyUnicode_KIND(self); | 
|  | 10433 | data = PyUnicode_DATA(self); | 
| Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10434 |  | 
|  | 10435 | /* Shortcut for single character strings */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10436 | if (length == 1) | 
|  | 10437 | return PyBool_FromLong( | 
|  | 10438 | Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, 0))); | 
| Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10439 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10440 | for (i = 0; i < length; i++) { | 
|  | 10441 | if (!Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, i))) { | 
| Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10442 | Py_RETURN_FALSE; | 
|  | 10443 | } | 
|  | 10444 | } | 
|  | 10445 | Py_RETURN_TRUE; | 
|  | 10446 | } | 
|  | 10447 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10448 | PyDoc_STRVAR(join__doc__, | 
| Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 10449 | "S.join(iterable) -> str\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10450 | \n\ | 
|  | 10451 | 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] | 10452 | iterable.  The separator between elements is S."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10453 |  | 
|  | 10454 | static PyObject* | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10455 | unicode_join(PyObject *self, PyObject *data) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10456 | { | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10457 | return PyUnicode_Join(self, data); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10458 | } | 
|  | 10459 |  | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10460 | static Py_ssize_t | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10461 | unicode_length(PyUnicodeObject *self) | 
|  | 10462 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10463 | if (PyUnicode_READY(self) == -1) | 
|  | 10464 | return -1; | 
|  | 10465 | return PyUnicode_GET_LENGTH(self); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10466 | } | 
|  | 10467 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10468 | PyDoc_STRVAR(ljust__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10469 | "S.ljust(width[, fillchar]) -> str\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10470 | \n\ | 
| Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 10471 | 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] | 10472 | done using the specified fill character (default is a space)."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10473 |  | 
|  | 10474 | static PyObject * | 
|  | 10475 | unicode_ljust(PyUnicodeObject *self, PyObject *args) | 
|  | 10476 | { | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10477 | Py_ssize_t width; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10478 | Py_UCS4 fillchar = ' '; | 
|  | 10479 |  | 
|  | 10480 | if (PyUnicode_READY(self) == -1) | 
|  | 10481 | return NULL; | 
| Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10482 |  | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10483 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10484 | return NULL; | 
|  | 10485 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10486 | if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10487 | Py_INCREF(self); | 
|  | 10488 | return (PyObject*) self; | 
|  | 10489 | } | 
|  | 10490 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10491 | return (PyObject*) pad(self, 0, width - _PyUnicode_LENGTH(self), fillchar); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10492 | } | 
|  | 10493 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10494 | PyDoc_STRVAR(lower__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10495 | "S.lower() -> str\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10496 | \n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10497 | Return a copy of the string S converted to lowercase."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10498 |  | 
|  | 10499 | static PyObject* | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10500 | unicode_lower(PyUnicodeObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10501 | { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10502 | return fixup(self, fixlower); | 
|  | 10503 | } | 
|  | 10504 |  | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10505 | #define LEFTSTRIP 0 | 
|  | 10506 | #define RIGHTSTRIP 1 | 
|  | 10507 | #define BOTHSTRIP 2 | 
|  | 10508 |  | 
|  | 10509 | /* Arrays indexed by above */ | 
|  | 10510 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; | 
|  | 10511 |  | 
|  | 10512 | #define STRIPNAME(i) (stripformat[i]+3) | 
|  | 10513 |  | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10514 | /* externally visible for str.strip(unicode) */ | 
|  | 10515 | PyObject * | 
|  | 10516 | _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj) | 
|  | 10517 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10518 | void *data; | 
|  | 10519 | int kind; | 
|  | 10520 | Py_ssize_t i, j, len; | 
|  | 10521 | BLOOM_MASK sepmask; | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10522 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10523 | if (PyUnicode_READY(self) == -1 || PyUnicode_READY(sepobj) == -1) | 
|  | 10524 | return NULL; | 
|  | 10525 |  | 
|  | 10526 | kind = PyUnicode_KIND(self); | 
|  | 10527 | data = PyUnicode_DATA(self); | 
|  | 10528 | len = PyUnicode_GET_LENGTH(self); | 
|  | 10529 | sepmask = make_bloom_mask(PyUnicode_KIND(sepobj), | 
|  | 10530 | PyUnicode_DATA(sepobj), | 
|  | 10531 | PyUnicode_GET_LENGTH(sepobj)); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10532 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10533 | i = 0; | 
|  | 10534 | if (striptype != RIGHTSTRIP) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10535 | while (i < len && | 
|  | 10536 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, i), sepobj)) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10537 | i++; | 
|  | 10538 | } | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10539 | } | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10540 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10541 | j = len; | 
|  | 10542 | if (striptype != LEFTSTRIP) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10543 | do { | 
|  | 10544 | j--; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10545 | } while (j >= i && | 
|  | 10546 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, j), sepobj)); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10547 | j++; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10548 | } | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10549 |  | 
| Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 10550 | return PyUnicode_Substring((PyObject*)self, i, j); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10551 | } | 
|  | 10552 |  | 
|  | 10553 | PyObject* | 
|  | 10554 | PyUnicode_Substring(PyObject *self, Py_ssize_t start, Py_ssize_t end) | 
|  | 10555 | { | 
|  | 10556 | unsigned char *data; | 
|  | 10557 | int kind; | 
| Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 10558 | Py_ssize_t length; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10559 |  | 
| Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 10560 | if (PyUnicode_READY(self) == -1) | 
|  | 10561 | return NULL; | 
|  | 10562 |  | 
|  | 10563 | end = Py_MIN(end, PyUnicode_GET_LENGTH(self)); | 
|  | 10564 |  | 
| Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 10565 | if (start == 0 && end == PyUnicode_GET_LENGTH(self)) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10566 | { | 
| Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 10567 | if (PyUnicode_CheckExact(self)) { | 
|  | 10568 | Py_INCREF(self); | 
|  | 10569 | return self; | 
|  | 10570 | } | 
|  | 10571 | else | 
|  | 10572 | return PyUnicode_Copy(self); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10573 | } | 
|  | 10574 |  | 
| Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 10575 | length = end - start; | 
|  | 10576 | if (length == 1) | 
| Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 10577 | return unicode_getitem(self, start); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10578 |  | 
| Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 10579 | if (start < 0 || end < 0) { | 
| Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 10580 | PyErr_SetString(PyExc_IndexError, "string index out of range"); | 
|  | 10581 | return NULL; | 
|  | 10582 | } | 
|  | 10583 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10584 | kind = PyUnicode_KIND(self); | 
|  | 10585 | data = PyUnicode_1BYTE_DATA(self); | 
| Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 10586 | return PyUnicode_FromKindAndData(kind, | 
|  | 10587 | data + PyUnicode_KIND_SIZE(kind, start), | 
| Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 10588 | length); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10589 | } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10590 |  | 
|  | 10591 | static PyObject * | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10592 | do_strip(PyUnicodeObject *self, int striptype) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10593 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10594 | int kind; | 
|  | 10595 | void *data; | 
|  | 10596 | Py_ssize_t len, i, j; | 
|  | 10597 |  | 
|  | 10598 | if (PyUnicode_READY(self) == -1) | 
|  | 10599 | return NULL; | 
|  | 10600 |  | 
|  | 10601 | kind = PyUnicode_KIND(self); | 
|  | 10602 | data = PyUnicode_DATA(self); | 
|  | 10603 | len = PyUnicode_GET_LENGTH(self); | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10604 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10605 | i = 0; | 
|  | 10606 | if (striptype != RIGHTSTRIP) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10607 | while (i < len && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, i))) { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10608 | i++; | 
|  | 10609 | } | 
|  | 10610 | } | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10611 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10612 | j = len; | 
|  | 10613 | if (striptype != LEFTSTRIP) { | 
|  | 10614 | do { | 
|  | 10615 | j--; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10616 | } while (j >= i && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, j))); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10617 | j++; | 
|  | 10618 | } | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10619 |  | 
| Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 10620 | return PyUnicode_Substring((PyObject*)self, i, j); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10621 | } | 
|  | 10622 |  | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10623 |  | 
|  | 10624 | static PyObject * | 
|  | 10625 | do_argstrip(PyUnicodeObject *self, int striptype, PyObject *args) | 
|  | 10626 | { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10627 | PyObject *sep = NULL; | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10628 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10629 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) | 
|  | 10630 | return NULL; | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10631 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10632 | if (sep != NULL && sep != Py_None) { | 
|  | 10633 | if (PyUnicode_Check(sep)) | 
|  | 10634 | return _PyUnicode_XStrip(self, striptype, sep); | 
|  | 10635 | else { | 
|  | 10636 | PyErr_Format(PyExc_TypeError, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10637 | "%s arg must be None or str", | 
|  | 10638 | STRIPNAME(striptype)); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10639 | return NULL; | 
|  | 10640 | } | 
|  | 10641 | } | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10642 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10643 | return do_strip(self, striptype); | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10644 | } | 
|  | 10645 |  | 
|  | 10646 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10647 | PyDoc_STRVAR(strip__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10648 | "S.strip([chars]) -> str\n\ | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10649 | \n\ | 
|  | 10650 | Return a copy of the string S with leading and trailing\n\ | 
|  | 10651 | whitespace removed.\n\ | 
| Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10652 | 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] | 10653 |  | 
|  | 10654 | static PyObject * | 
|  | 10655 | unicode_strip(PyUnicodeObject *self, PyObject *args) | 
|  | 10656 | { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10657 | if (PyTuple_GET_SIZE(args) == 0) | 
|  | 10658 | return do_strip(self, BOTHSTRIP); /* Common case */ | 
|  | 10659 | else | 
|  | 10660 | return do_argstrip(self, BOTHSTRIP, args); | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10661 | } | 
|  | 10662 |  | 
|  | 10663 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10664 | PyDoc_STRVAR(lstrip__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10665 | "S.lstrip([chars]) -> str\n\ | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10666 | \n\ | 
|  | 10667 | Return a copy of the string S with leading whitespace removed.\n\ | 
| Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10668 | 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] | 10669 |  | 
|  | 10670 | static PyObject * | 
|  | 10671 | unicode_lstrip(PyUnicodeObject *self, PyObject *args) | 
|  | 10672 | { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10673 | if (PyTuple_GET_SIZE(args) == 0) | 
|  | 10674 | return do_strip(self, LEFTSTRIP); /* Common case */ | 
|  | 10675 | else | 
|  | 10676 | return do_argstrip(self, LEFTSTRIP, args); | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10677 | } | 
|  | 10678 |  | 
|  | 10679 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10680 | PyDoc_STRVAR(rstrip__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10681 | "S.rstrip([chars]) -> str\n\ | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10682 | \n\ | 
|  | 10683 | Return a copy of the string S with trailing whitespace removed.\n\ | 
| Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10684 | 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] | 10685 |  | 
|  | 10686 | static PyObject * | 
|  | 10687 | unicode_rstrip(PyUnicodeObject *self, PyObject *args) | 
|  | 10688 | { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10689 | if (PyTuple_GET_SIZE(args) == 0) | 
|  | 10690 | return do_strip(self, RIGHTSTRIP); /* Common case */ | 
|  | 10691 | else | 
|  | 10692 | return do_argstrip(self, RIGHTSTRIP, args); | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10693 | } | 
|  | 10694 |  | 
|  | 10695 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10696 | static PyObject* | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10697 | unicode_repeat(PyUnicodeObject *str, Py_ssize_t len) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10698 | { | 
|  | 10699 | PyUnicodeObject *u; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10700 | Py_ssize_t nchars, n; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10701 |  | 
| Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 10702 | if (len < 1) { | 
|  | 10703 | Py_INCREF(unicode_empty); | 
| Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10704 | return unicode_empty; | 
| Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 10705 | } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10706 |  | 
| Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 10707 | if (len == 1 && PyUnicode_CheckExact(str)) { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10708 | /* no repeat, return original string */ | 
|  | 10709 | Py_INCREF(str); | 
|  | 10710 | return (PyObject*) str; | 
|  | 10711 | } | 
| Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 10712 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10713 | if (PyUnicode_READY(str) == -1) | 
|  | 10714 | return NULL; | 
|  | 10715 |  | 
| Victor Stinner | c759f3e | 2011-10-01 03:09:58 +0200 | [diff] [blame] | 10716 | if (PyUnicode_GET_LENGTH(str) > PY_SSIZE_T_MAX / len) { | 
| Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 10717 | PyErr_SetString(PyExc_OverflowError, | 
|  | 10718 | "repeated string is too long"); | 
|  | 10719 | return NULL; | 
|  | 10720 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10721 | nchars = len * PyUnicode_GET_LENGTH(str); | 
| Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 10722 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10723 | u = (PyUnicodeObject *)PyUnicode_New(nchars, PyUnicode_MAX_CHAR_VALUE(str)); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10724 | if (!u) | 
|  | 10725 | return NULL; | 
| Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 10726 | assert(PyUnicode_KIND(u) == PyUnicode_KIND(str)); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10727 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10728 | if (PyUnicode_GET_LENGTH(str) == 1) { | 
|  | 10729 | const int kind = PyUnicode_KIND(str); | 
|  | 10730 | const Py_UCS4 fill_char = PyUnicode_READ(kind, PyUnicode_DATA(str), 0); | 
|  | 10731 | void *to = PyUnicode_DATA(u); | 
| Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 10732 | if (kind == PyUnicode_1BYTE_KIND) | 
|  | 10733 | memset(to, (unsigned char)fill_char, len); | 
|  | 10734 | else { | 
|  | 10735 | for (n = 0; n < len; ++n) | 
|  | 10736 | PyUnicode_WRITE(kind, to, n, fill_char); | 
|  | 10737 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10738 | } | 
|  | 10739 | else { | 
|  | 10740 | /* number of characters copied this far */ | 
|  | 10741 | Py_ssize_t done = PyUnicode_GET_LENGTH(str); | 
|  | 10742 | const Py_ssize_t char_size = PyUnicode_CHARACTER_SIZE(str); | 
|  | 10743 | char *to = (char *) PyUnicode_DATA(u); | 
|  | 10744 | Py_MEMCPY(to, PyUnicode_DATA(str), | 
|  | 10745 | PyUnicode_GET_LENGTH(str) * char_size); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10746 | while (done < nchars) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10747 | n = (done <= nchars-done) ? done : nchars-done; | 
|  | 10748 | Py_MEMCPY(to + (done * char_size), to, n * char_size); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10749 | done += n; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10750 | } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10751 | } | 
|  | 10752 |  | 
|  | 10753 | return (PyObject*) u; | 
|  | 10754 | } | 
|  | 10755 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10756 | PyObject * | 
|  | 10757 | PyUnicode_Replace(PyObject *obj, | 
|  | 10758 | PyObject *subobj, | 
|  | 10759 | PyObject *replobj, | 
|  | 10760 | Py_ssize_t maxcount) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10761 | { | 
|  | 10762 | PyObject *self; | 
|  | 10763 | PyObject *str1; | 
|  | 10764 | PyObject *str2; | 
|  | 10765 | PyObject *result; | 
|  | 10766 |  | 
|  | 10767 | self = PyUnicode_FromObject(obj); | 
| Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10768 | if (self == NULL || PyUnicode_READY(self) == -1) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10769 | return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10770 | str1 = PyUnicode_FromObject(subobj); | 
| Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10771 | if (str1 == NULL || PyUnicode_READY(str1) == -1) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10772 | Py_DECREF(self); | 
|  | 10773 | return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10774 | } | 
|  | 10775 | str2 = PyUnicode_FromObject(replobj); | 
| Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10776 | if (str2 == NULL || PyUnicode_READY(str2)) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10777 | Py_DECREF(self); | 
|  | 10778 | Py_DECREF(str1); | 
|  | 10779 | return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10780 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10781 | result = replace(self, str1, str2, maxcount); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10782 | Py_DECREF(self); | 
|  | 10783 | Py_DECREF(str1); | 
|  | 10784 | Py_DECREF(str2); | 
|  | 10785 | return result; | 
|  | 10786 | } | 
|  | 10787 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10788 | PyDoc_STRVAR(replace__doc__, | 
| Ezio Melotti | c1897e7 | 2010-06-26 18:50:39 +0000 | [diff] [blame] | 10789 | "S.replace(old, new[, count]) -> str\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10790 | \n\ | 
|  | 10791 | Return a copy of S with all occurrences of substring\n\ | 
| Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 10792 | old replaced by new.  If the optional argument count is\n\ | 
|  | 10793 | given, only the first count occurrences are replaced."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10794 |  | 
|  | 10795 | static PyObject* | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10796 | unicode_replace(PyObject *self, PyObject *args) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10797 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10798 | PyObject *str1; | 
|  | 10799 | PyObject *str2; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10800 | Py_ssize_t maxcount = -1; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10801 | PyObject *result; | 
|  | 10802 |  | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10803 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10804 | return NULL; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10805 | if (!PyUnicode_READY(self) == -1) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10806 | return NULL; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10807 | str1 = PyUnicode_FromObject(str1); | 
|  | 10808 | if (str1 == NULL || PyUnicode_READY(str1) == -1) | 
|  | 10809 | return NULL; | 
|  | 10810 | str2 = PyUnicode_FromObject(str2); | 
| Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10811 | if (str2 == NULL || PyUnicode_READY(str2) == -1) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10812 | Py_DECREF(str1); | 
|  | 10813 | return NULL; | 
| Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 10814 | } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10815 |  | 
|  | 10816 | result = replace(self, str1, str2, maxcount); | 
|  | 10817 |  | 
|  | 10818 | Py_DECREF(str1); | 
|  | 10819 | Py_DECREF(str2); | 
|  | 10820 | return result; | 
|  | 10821 | } | 
|  | 10822 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10823 | static PyObject * | 
|  | 10824 | unicode_repr(PyObject *unicode) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10825 | { | 
| Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 10826 | PyObject *repr; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10827 | Py_ssize_t isize; | 
|  | 10828 | Py_ssize_t osize, squote, dquote, i, o; | 
|  | 10829 | Py_UCS4 max, quote; | 
|  | 10830 | int ikind, okind; | 
|  | 10831 | void *idata, *odata; | 
| Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 10832 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10833 | if (PyUnicode_READY(unicode) == -1) | 
| Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 10834 | return NULL; | 
|  | 10835 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10836 | isize = PyUnicode_GET_LENGTH(unicode); | 
|  | 10837 | idata = PyUnicode_DATA(unicode); | 
| Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 10838 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10839 | /* Compute length of output, quote characters, and | 
|  | 10840 | maximum character */ | 
|  | 10841 | osize = 2; /* quotes */ | 
|  | 10842 | max = 127; | 
|  | 10843 | squote = dquote = 0; | 
|  | 10844 | ikind = PyUnicode_KIND(unicode); | 
|  | 10845 | for (i = 0; i < isize; i++) { | 
|  | 10846 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); | 
|  | 10847 | switch (ch) { | 
|  | 10848 | case '\'': squote++; osize++; break; | 
|  | 10849 | case '"':  dquote++; osize++; break; | 
|  | 10850 | case '\\': case '\t': case '\r': case '\n': | 
|  | 10851 | osize += 2; break; | 
|  | 10852 | default: | 
|  | 10853 | /* Fast-path ASCII */ | 
|  | 10854 | if (ch < ' ' || ch == 0x7f) | 
|  | 10855 | osize += 4; /* \xHH */ | 
|  | 10856 | else if (ch < 0x7f) | 
|  | 10857 | osize++; | 
|  | 10858 | else if (Py_UNICODE_ISPRINTABLE(ch)) { | 
|  | 10859 | osize++; | 
|  | 10860 | max = ch > max ? ch : max; | 
|  | 10861 | } | 
|  | 10862 | else if (ch < 0x100) | 
|  | 10863 | osize += 4; /* \xHH */ | 
|  | 10864 | else if (ch < 0x10000) | 
|  | 10865 | osize += 6; /* \uHHHH */ | 
|  | 10866 | else | 
|  | 10867 | osize += 10; /* \uHHHHHHHH */ | 
|  | 10868 | } | 
|  | 10869 | } | 
|  | 10870 |  | 
|  | 10871 | quote = '\''; | 
|  | 10872 | if (squote) { | 
|  | 10873 | if (dquote) | 
|  | 10874 | /* Both squote and dquote present. Use squote, | 
|  | 10875 | and escape them */ | 
|  | 10876 | osize += squote; | 
|  | 10877 | else | 
|  | 10878 | quote = '"'; | 
|  | 10879 | } | 
|  | 10880 |  | 
|  | 10881 | repr = PyUnicode_New(osize, max); | 
|  | 10882 | if (repr == NULL) | 
|  | 10883 | return NULL; | 
|  | 10884 | okind = PyUnicode_KIND(repr); | 
|  | 10885 | odata = PyUnicode_DATA(repr); | 
|  | 10886 |  | 
|  | 10887 | PyUnicode_WRITE(okind, odata, 0, quote); | 
|  | 10888 | PyUnicode_WRITE(okind, odata, osize-1, quote); | 
|  | 10889 |  | 
|  | 10890 | for (i = 0, o = 1; i < isize; i++) { | 
|  | 10891 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); | 
| Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 10892 |  | 
|  | 10893 | /* Escape quotes and backslashes */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10894 | if ((ch == quote) || (ch == '\\')) { | 
|  | 10895 | PyUnicode_WRITE(okind, odata, o++, '\\'); | 
|  | 10896 | PyUnicode_WRITE(okind, odata, o++, ch); | 
| Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 10897 | continue; | 
|  | 10898 | } | 
|  | 10899 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10900 | /* Map special whitespace to '\t', \n', '\r' */ | 
| Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10901 | if (ch == '\t') { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10902 | PyUnicode_WRITE(okind, odata, o++, '\\'); | 
|  | 10903 | PyUnicode_WRITE(okind, odata, o++, 't'); | 
| Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 10904 | } | 
|  | 10905 | else if (ch == '\n') { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10906 | PyUnicode_WRITE(okind, odata, o++, '\\'); | 
|  | 10907 | PyUnicode_WRITE(okind, odata, o++, 'n'); | 
| Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 10908 | } | 
|  | 10909 | else if (ch == '\r') { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10910 | PyUnicode_WRITE(okind, odata, o++, '\\'); | 
|  | 10911 | PyUnicode_WRITE(okind, odata, o++, 'r'); | 
| Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 10912 | } | 
|  | 10913 |  | 
|  | 10914 | /* Map non-printable US ASCII to '\xhh' */ | 
| Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10915 | else if (ch < ' ' || ch == 0x7F) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10916 | PyUnicode_WRITE(okind, odata, o++, '\\'); | 
|  | 10917 | PyUnicode_WRITE(okind, odata, o++, 'x'); | 
|  | 10918 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0x000F]); | 
|  | 10919 | PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0x000F]); | 
| Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 10920 | } | 
|  | 10921 |  | 
| Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10922 | /* Copy ASCII characters as-is */ | 
|  | 10923 | else if (ch < 0x7F) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10924 | PyUnicode_WRITE(okind, odata, o++, ch); | 
| Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10925 | } | 
|  | 10926 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10927 | /* Non-ASCII characters */ | 
| Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10928 | else { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10929 | /* Map Unicode whitespace and control characters | 
| Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10930 | (categories Z* and C* except ASCII space) | 
|  | 10931 | */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10932 | if (!Py_UNICODE_ISPRINTABLE(ch)) { | 
| Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10933 | /* Map 8-bit characters to '\xhh' */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10934 | if (ch <= 0xff) { | 
|  | 10935 | PyUnicode_WRITE(okind, odata, o++, '\\'); | 
|  | 10936 | PyUnicode_WRITE(okind, odata, o++, 'x'); | 
|  | 10937 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0x000F]); | 
|  | 10938 | PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0x000F]); | 
| Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10939 | } | 
|  | 10940 | /* Map 21-bit characters to '\U00xxxxxx' */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10941 | else if (ch >= 0x10000) { | 
|  | 10942 | PyUnicode_WRITE(okind, odata, o++, '\\'); | 
|  | 10943 | PyUnicode_WRITE(okind, odata, o++, 'U'); | 
|  | 10944 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 28) & 0xF]); | 
|  | 10945 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 24) & 0xF]); | 
|  | 10946 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 20) & 0xF]); | 
|  | 10947 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 16) & 0xF]); | 
|  | 10948 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 12) & 0xF]); | 
|  | 10949 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 8) & 0xF]); | 
|  | 10950 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0xF]); | 
|  | 10951 | PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0xF]); | 
| Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10952 | } | 
|  | 10953 | /* Map 16-bit characters to '\uxxxx' */ | 
|  | 10954 | else { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10955 | PyUnicode_WRITE(okind, odata, o++, '\\'); | 
|  | 10956 | PyUnicode_WRITE(okind, odata, o++, 'u'); | 
|  | 10957 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 12) & 0xF]); | 
|  | 10958 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 8) & 0xF]); | 
|  | 10959 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0xF]); | 
|  | 10960 | PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0xF]); | 
| Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10961 | } | 
|  | 10962 | } | 
|  | 10963 | /* Copy characters as-is */ | 
|  | 10964 | else { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10965 | PyUnicode_WRITE(okind, odata, o++, ch); | 
| Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10966 | } | 
|  | 10967 | } | 
| Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 10968 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10969 | /* Closing quote already added at the beginning */ | 
| Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 10970 | return repr; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10971 | } | 
|  | 10972 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10973 | PyDoc_STRVAR(rfind__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10974 | "S.rfind(sub[, start[, end]]) -> int\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10975 | \n\ | 
|  | 10976 | Return the highest index in S where substring sub is found,\n\ | 
| Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 10977 | such that sub is contained within S[start:end].  Optional\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10978 | arguments start and end are interpreted as in slice notation.\n\ | 
|  | 10979 | \n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10980 | Return -1 on failure."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10981 |  | 
|  | 10982 | static PyObject * | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10983 | unicode_rfind(PyObject *self, PyObject *args) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10984 | { | 
| Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10985 | PyUnicodeObject *substring; | 
| Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 10986 | Py_ssize_t start; | 
|  | 10987 | Py_ssize_t end; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10988 | Py_ssize_t result; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10989 |  | 
| Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10990 | if (!stringlib_parse_args_finds_unicode("rfind", args, &substring, | 
|  | 10991 | &start, &end)) | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10992 | return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10993 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10994 | if (PyUnicode_READY(self) == -1) | 
|  | 10995 | return NULL; | 
|  | 10996 | if (PyUnicode_READY(substring) == -1) | 
|  | 10997 | return NULL; | 
|  | 10998 |  | 
|  | 10999 | result = any_find_slice( | 
|  | 11000 | ucs1lib_rfind_slice, ucs2lib_rfind_slice, ucs4lib_rfind_slice, | 
|  | 11001 | self, (PyObject*)substring, start, end | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11002 | ); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11003 |  | 
|  | 11004 | Py_DECREF(substring); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11005 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11006 | if (result == -2) | 
|  | 11007 | return NULL; | 
|  | 11008 |  | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11009 | return PyLong_FromSsize_t(result); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11010 | } | 
|  | 11011 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11012 | PyDoc_STRVAR(rindex__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11013 | "S.rindex(sub[, start[, end]]) -> int\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11014 | \n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11015 | 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] | 11016 |  | 
|  | 11017 | static PyObject * | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11018 | unicode_rindex(PyObject *self, PyObject *args) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11019 | { | 
| Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11020 | PyUnicodeObject *substring; | 
| Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11021 | Py_ssize_t start; | 
|  | 11022 | Py_ssize_t end; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11023 | Py_ssize_t result; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11024 |  | 
| Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11025 | if (!stringlib_parse_args_finds_unicode("rindex", args, &substring, | 
|  | 11026 | &start, &end)) | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11027 | return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11028 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11029 | if (PyUnicode_READY(self) == -1) | 
|  | 11030 | return NULL; | 
|  | 11031 | if (PyUnicode_READY(substring) == -1) | 
|  | 11032 | return NULL; | 
|  | 11033 |  | 
|  | 11034 | result = any_find_slice( | 
|  | 11035 | ucs1lib_rfind_slice, ucs2lib_rfind_slice, ucs4lib_rfind_slice, | 
|  | 11036 | self, (PyObject*)substring, start, end | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11037 | ); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11038 |  | 
|  | 11039 | Py_DECREF(substring); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11040 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11041 | if (result == -2) | 
|  | 11042 | return NULL; | 
|  | 11043 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11044 | if (result < 0) { | 
|  | 11045 | PyErr_SetString(PyExc_ValueError, "substring not found"); | 
|  | 11046 | return NULL; | 
|  | 11047 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11048 |  | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11049 | return PyLong_FromSsize_t(result); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11050 | } | 
|  | 11051 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11052 | PyDoc_STRVAR(rjust__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11053 | "S.rjust(width[, fillchar]) -> str\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11054 | \n\ | 
| Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 11055 | 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] | 11056 | done using the specified fill character (default is a space)."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11057 |  | 
|  | 11058 | static PyObject * | 
|  | 11059 | unicode_rjust(PyUnicodeObject *self, PyObject *args) | 
|  | 11060 | { | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11061 | Py_ssize_t width; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11062 | Py_UCS4 fillchar = ' '; | 
|  | 11063 |  | 
| Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11064 | 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] | 11065 | return NULL; | 
| Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 11066 |  | 
| Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11067 | if (PyUnicode_READY(self) == -1) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11068 | return NULL; | 
|  | 11069 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11070 | if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11071 | Py_INCREF(self); | 
|  | 11072 | return (PyObject*) self; | 
|  | 11073 | } | 
|  | 11074 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11075 | return (PyObject*) pad(self, width - _PyUnicode_LENGTH(self), 0, fillchar); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11076 | } | 
|  | 11077 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11078 | PyObject * | 
|  | 11079 | PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11080 | { | 
|  | 11081 | PyObject *result; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11082 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11083 | s = PyUnicode_FromObject(s); | 
|  | 11084 | if (s == NULL) | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11085 | return NULL; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11086 | if (sep != NULL) { | 
|  | 11087 | sep = PyUnicode_FromObject(sep); | 
|  | 11088 | if (sep == NULL) { | 
|  | 11089 | Py_DECREF(s); | 
|  | 11090 | return NULL; | 
|  | 11091 | } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11092 | } | 
|  | 11093 |  | 
|  | 11094 | result = split((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); | 
|  | 11095 |  | 
|  | 11096 | Py_DECREF(s); | 
|  | 11097 | Py_XDECREF(sep); | 
|  | 11098 | return result; | 
|  | 11099 | } | 
|  | 11100 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11101 | PyDoc_STRVAR(split__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11102 | "S.split([sep[, maxsplit]]) -> list of strings\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11103 | \n\ | 
|  | 11104 | Return a list of the words in S, using sep as the\n\ | 
|  | 11105 | delimiter string.  If maxsplit is given, at most maxsplit\n\ | 
| Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 11106 | 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] | 11107 | whitespace string is a separator and empty strings are\n\ | 
|  | 11108 | removed from the result."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11109 |  | 
|  | 11110 | static PyObject* | 
|  | 11111 | unicode_split(PyUnicodeObject *self, PyObject *args) | 
|  | 11112 | { | 
|  | 11113 | PyObject *substring = Py_None; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11114 | Py_ssize_t maxcount = -1; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11115 |  | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11116 | if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11117 | return NULL; | 
|  | 11118 |  | 
|  | 11119 | if (substring == Py_None) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11120 | return split(self, NULL, maxcount); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11121 | else if (PyUnicode_Check(substring)) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11122 | return split(self, (PyUnicodeObject *)substring, maxcount); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11123 | else | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11124 | return PyUnicode_Split((PyObject *)self, substring, maxcount); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11125 | } | 
|  | 11126 |  | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11127 | PyObject * | 
|  | 11128 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) | 
|  | 11129 | { | 
|  | 11130 | PyObject* str_obj; | 
|  | 11131 | PyObject* sep_obj; | 
|  | 11132 | PyObject* out; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11133 | int kind1, kind2, kind; | 
|  | 11134 | void *buf1 = NULL, *buf2 = NULL; | 
|  | 11135 | Py_ssize_t len1, len2; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11136 |  | 
|  | 11137 | str_obj = PyUnicode_FromObject(str_in); | 
| Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11138 | if (!str_obj || PyUnicode_READY(str_obj) == -1) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11139 | return NULL; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11140 | sep_obj = PyUnicode_FromObject(sep_in); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11141 | if (!sep_obj || PyUnicode_READY(sep_obj) == -1) { | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11142 | Py_DECREF(str_obj); | 
|  | 11143 | return NULL; | 
|  | 11144 | } | 
|  | 11145 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11146 | kind1 = PyUnicode_KIND(str_in); | 
|  | 11147 | kind2 = PyUnicode_KIND(sep_obj); | 
|  | 11148 | kind = kind1 > kind2 ? kind1 : kind2; | 
|  | 11149 | buf1 = PyUnicode_DATA(str_in); | 
|  | 11150 | if (kind1 != kind) | 
|  | 11151 | buf1 = _PyUnicode_AsKind(str_in, kind); | 
|  | 11152 | if (!buf1) | 
|  | 11153 | goto onError; | 
|  | 11154 | buf2 = PyUnicode_DATA(sep_obj); | 
|  | 11155 | if (kind2 != kind) | 
|  | 11156 | buf2 = _PyUnicode_AsKind(sep_obj, kind); | 
|  | 11157 | if (!buf2) | 
|  | 11158 | goto onError; | 
|  | 11159 | len1 = PyUnicode_GET_LENGTH(str_obj); | 
|  | 11160 | len2 = PyUnicode_GET_LENGTH(sep_obj); | 
|  | 11161 |  | 
|  | 11162 | switch(PyUnicode_KIND(str_in)) { | 
|  | 11163 | case PyUnicode_1BYTE_KIND: | 
|  | 11164 | out = ucs1lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); | 
|  | 11165 | break; | 
|  | 11166 | case PyUnicode_2BYTE_KIND: | 
|  | 11167 | out = ucs2lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); | 
|  | 11168 | break; | 
|  | 11169 | case PyUnicode_4BYTE_KIND: | 
|  | 11170 | out = ucs4lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); | 
|  | 11171 | break; | 
|  | 11172 | default: | 
|  | 11173 | assert(0); | 
|  | 11174 | out = 0; | 
|  | 11175 | } | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11176 |  | 
|  | 11177 | Py_DECREF(sep_obj); | 
|  | 11178 | Py_DECREF(str_obj); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11179 | if (kind1 != kind) | 
|  | 11180 | PyMem_Free(buf1); | 
|  | 11181 | if (kind2 != kind) | 
|  | 11182 | PyMem_Free(buf2); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11183 |  | 
|  | 11184 | return out; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11185 | onError: | 
|  | 11186 | Py_DECREF(sep_obj); | 
|  | 11187 | Py_DECREF(str_obj); | 
|  | 11188 | if (kind1 != kind && buf1) | 
|  | 11189 | PyMem_Free(buf1); | 
|  | 11190 | if (kind2 != kind && buf2) | 
|  | 11191 | PyMem_Free(buf2); | 
|  | 11192 | return NULL; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11193 | } | 
|  | 11194 |  | 
|  | 11195 |  | 
|  | 11196 | PyObject * | 
|  | 11197 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) | 
|  | 11198 | { | 
|  | 11199 | PyObject* str_obj; | 
|  | 11200 | PyObject* sep_obj; | 
|  | 11201 | PyObject* out; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11202 | int kind1, kind2, kind; | 
|  | 11203 | void *buf1 = NULL, *buf2 = NULL; | 
|  | 11204 | Py_ssize_t len1, len2; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11205 |  | 
|  | 11206 | str_obj = PyUnicode_FromObject(str_in); | 
|  | 11207 | if (!str_obj) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11208 | return NULL; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11209 | sep_obj = PyUnicode_FromObject(sep_in); | 
|  | 11210 | if (!sep_obj) { | 
|  | 11211 | Py_DECREF(str_obj); | 
|  | 11212 | return NULL; | 
|  | 11213 | } | 
|  | 11214 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11215 | kind1 = PyUnicode_KIND(str_in); | 
|  | 11216 | kind2 = PyUnicode_KIND(sep_obj); | 
| Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 11217 | kind = Py_MAX(kind1, kind2); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11218 | buf1 = PyUnicode_DATA(str_in); | 
|  | 11219 | if (kind1 != kind) | 
|  | 11220 | buf1 = _PyUnicode_AsKind(str_in, kind); | 
|  | 11221 | if (!buf1) | 
|  | 11222 | goto onError; | 
|  | 11223 | buf2 = PyUnicode_DATA(sep_obj); | 
|  | 11224 | if (kind2 != kind) | 
|  | 11225 | buf2 = _PyUnicode_AsKind(sep_obj, kind); | 
|  | 11226 | if (!buf2) | 
|  | 11227 | goto onError; | 
|  | 11228 | len1 = PyUnicode_GET_LENGTH(str_obj); | 
|  | 11229 | len2 = PyUnicode_GET_LENGTH(sep_obj); | 
|  | 11230 |  | 
|  | 11231 | switch(PyUnicode_KIND(str_in)) { | 
|  | 11232 | case PyUnicode_1BYTE_KIND: | 
|  | 11233 | out = ucs1lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); | 
|  | 11234 | break; | 
|  | 11235 | case PyUnicode_2BYTE_KIND: | 
|  | 11236 | out = ucs2lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); | 
|  | 11237 | break; | 
|  | 11238 | case PyUnicode_4BYTE_KIND: | 
|  | 11239 | out = ucs4lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); | 
|  | 11240 | break; | 
|  | 11241 | default: | 
|  | 11242 | assert(0); | 
|  | 11243 | out = 0; | 
|  | 11244 | } | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11245 |  | 
|  | 11246 | Py_DECREF(sep_obj); | 
|  | 11247 | Py_DECREF(str_obj); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11248 | if (kind1 != kind) | 
|  | 11249 | PyMem_Free(buf1); | 
|  | 11250 | if (kind2 != kind) | 
|  | 11251 | PyMem_Free(buf2); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11252 |  | 
|  | 11253 | return out; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11254 | onError: | 
|  | 11255 | Py_DECREF(sep_obj); | 
|  | 11256 | Py_DECREF(str_obj); | 
|  | 11257 | if (kind1 != kind && buf1) | 
|  | 11258 | PyMem_Free(buf1); | 
|  | 11259 | if (kind2 != kind && buf2) | 
|  | 11260 | PyMem_Free(buf2); | 
|  | 11261 | return NULL; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11262 | } | 
|  | 11263 |  | 
|  | 11264 | PyDoc_STRVAR(partition__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11265 | "S.partition(sep) -> (head, sep, tail)\n\ | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11266 | \n\ | 
| Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 11267 | 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] | 11268 | 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] | 11269 | found, return S and two empty strings."); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11270 |  | 
|  | 11271 | static PyObject* | 
|  | 11272 | unicode_partition(PyUnicodeObject *self, PyObject *separator) | 
|  | 11273 | { | 
|  | 11274 | return PyUnicode_Partition((PyObject *)self, separator); | 
|  | 11275 | } | 
|  | 11276 |  | 
|  | 11277 | PyDoc_STRVAR(rpartition__doc__, | 
| Ezio Melotti | 5b2b242 | 2010-01-25 11:58:28 +0000 | [diff] [blame] | 11278 | "S.rpartition(sep) -> (head, sep, tail)\n\ | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11279 | \n\ | 
| Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 11280 | 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] | 11281 | 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] | 11282 | separator is not found, return two empty strings and S."); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11283 |  | 
|  | 11284 | static PyObject* | 
|  | 11285 | unicode_rpartition(PyUnicodeObject *self, PyObject *separator) | 
|  | 11286 | { | 
|  | 11287 | return PyUnicode_RPartition((PyObject *)self, separator); | 
|  | 11288 | } | 
|  | 11289 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11290 | PyObject * | 
|  | 11291 | PyUnicode_RSplit(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11292 | { | 
|  | 11293 | PyObject *result; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11294 |  | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11295 | s = PyUnicode_FromObject(s); | 
|  | 11296 | if (s == NULL) | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11297 | return NULL; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11298 | if (sep != NULL) { | 
|  | 11299 | sep = PyUnicode_FromObject(sep); | 
|  | 11300 | if (sep == NULL) { | 
|  | 11301 | Py_DECREF(s); | 
|  | 11302 | return NULL; | 
|  | 11303 | } | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11304 | } | 
|  | 11305 |  | 
|  | 11306 | result = rsplit((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); | 
|  | 11307 |  | 
|  | 11308 | Py_DECREF(s); | 
|  | 11309 | Py_XDECREF(sep); | 
|  | 11310 | return result; | 
|  | 11311 | } | 
|  | 11312 |  | 
|  | 11313 | PyDoc_STRVAR(rsplit__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11314 | "S.rsplit([sep[, maxsplit]]) -> list of strings\n\ | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11315 | \n\ | 
|  | 11316 | Return a list of the words in S, using sep as the\n\ | 
|  | 11317 | delimiter string, starting at the end of the string and\n\ | 
|  | 11318 | working to the front.  If maxsplit is given, at most maxsplit\n\ | 
|  | 11319 | splits are done. If sep is not specified, any whitespace string\n\ | 
|  | 11320 | is a separator."); | 
|  | 11321 |  | 
|  | 11322 | static PyObject* | 
|  | 11323 | unicode_rsplit(PyUnicodeObject *self, PyObject *args) | 
|  | 11324 | { | 
|  | 11325 | PyObject *substring = Py_None; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11326 | Py_ssize_t maxcount = -1; | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11327 |  | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11328 | if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11329 | return NULL; | 
|  | 11330 |  | 
|  | 11331 | if (substring == Py_None) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11332 | return rsplit(self, NULL, maxcount); | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11333 | else if (PyUnicode_Check(substring)) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11334 | return rsplit(self, (PyUnicodeObject *)substring, maxcount); | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11335 | else | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11336 | return PyUnicode_RSplit((PyObject *)self, substring, maxcount); | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11337 | } | 
|  | 11338 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11339 | PyDoc_STRVAR(splitlines__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11340 | "S.splitlines([keepends]) -> list of strings\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11341 | \n\ | 
|  | 11342 | 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] | 11343 | 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] | 11344 | is given and true."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11345 |  | 
|  | 11346 | static PyObject* | 
| Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 11347 | unicode_splitlines(PyUnicodeObject *self, PyObject *args, PyObject *kwds) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11348 | { | 
| Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 11349 | static char *kwlist[] = {"keepends", 0}; | 
| Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 11350 | int keepends = 0; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11351 |  | 
| Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 11352 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:splitlines", | 
|  | 11353 | kwlist, &keepends)) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11354 | return NULL; | 
|  | 11355 |  | 
| Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 11356 | return PyUnicode_Splitlines((PyObject *)self, keepends); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11357 | } | 
|  | 11358 |  | 
|  | 11359 | static | 
| Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 11360 | PyObject *unicode_str(PyObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11361 | { | 
| Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 11362 | if (PyUnicode_CheckExact(self)) { | 
|  | 11363 | Py_INCREF(self); | 
|  | 11364 | return self; | 
|  | 11365 | } else | 
|  | 11366 | /* Subtype -- return genuine unicode string with the same value. */ | 
| Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 11367 | return PyUnicode_Copy(self); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11368 | } | 
|  | 11369 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11370 | PyDoc_STRVAR(swapcase__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11371 | "S.swapcase() -> str\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11372 | \n\ | 
|  | 11373 | 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] | 11374 | and vice versa."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11375 |  | 
|  | 11376 | static PyObject* | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11377 | unicode_swapcase(PyUnicodeObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11378 | { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11379 | return fixup(self, fixswapcase); | 
|  | 11380 | } | 
|  | 11381 |  | 
| Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11382 | PyDoc_STRVAR(maketrans__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11383 | "str.maketrans(x[, y[, z]]) -> dict (static method)\n\ | 
| Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11384 | \n\ | 
|  | 11385 | Return a translation table usable for str.translate().\n\ | 
|  | 11386 | If there is only one argument, it must be a dictionary mapping Unicode\n\ | 
|  | 11387 | ordinals (integers) or characters to Unicode ordinals, strings or None.\n\ | 
| Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11388 | Character keys will be then converted to ordinals.\n\ | 
| Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11389 | If there are two arguments, they must be strings of equal length, and\n\ | 
|  | 11390 | in the resulting dictionary, each character in x will be mapped to the\n\ | 
|  | 11391 | character at the same position in y. If there is a third argument, it\n\ | 
|  | 11392 | must be a string, whose characters will be mapped to None in the result."); | 
|  | 11393 |  | 
|  | 11394 | static PyObject* | 
|  | 11395 | unicode_maketrans(PyUnicodeObject *null, PyObject *args) | 
|  | 11396 | { | 
|  | 11397 | PyObject *x, *y = NULL, *z = NULL; | 
|  | 11398 | PyObject *new = NULL, *key, *value; | 
|  | 11399 | Py_ssize_t i = 0; | 
|  | 11400 | int res; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11401 |  | 
| Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11402 | if (!PyArg_ParseTuple(args, "O|UU:maketrans", &x, &y, &z)) | 
|  | 11403 | return NULL; | 
|  | 11404 | new = PyDict_New(); | 
|  | 11405 | if (!new) | 
|  | 11406 | return NULL; | 
|  | 11407 | if (y != NULL) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11408 | int x_kind, y_kind, z_kind; | 
|  | 11409 | void *x_data, *y_data, *z_data; | 
|  | 11410 |  | 
| Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11411 | /* x must be a string too, of equal length */ | 
| Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11412 | if (!PyUnicode_Check(x)) { | 
|  | 11413 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " | 
|  | 11414 | "be a string if there is a second argument"); | 
|  | 11415 | goto err; | 
|  | 11416 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11417 | if (PyUnicode_GET_LENGTH(x) != PyUnicode_GET_LENGTH(y)) { | 
| Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11418 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " | 
|  | 11419 | "arguments must have equal length"); | 
|  | 11420 | goto err; | 
|  | 11421 | } | 
|  | 11422 | /* 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] | 11423 | x_kind = PyUnicode_KIND(x); | 
|  | 11424 | y_kind = PyUnicode_KIND(y); | 
|  | 11425 | x_data = PyUnicode_DATA(x); | 
|  | 11426 | y_data = PyUnicode_DATA(y); | 
|  | 11427 | for (i = 0; i < PyUnicode_GET_LENGTH(x); i++) { | 
|  | 11428 | key = PyLong_FromLong(PyUnicode_READ(x_kind, x_data, i)); | 
|  | 11429 | value = PyLong_FromLong(PyUnicode_READ(y_kind, y_data, i)); | 
| Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11430 | if (!key || !value) | 
|  | 11431 | goto err; | 
|  | 11432 | res = PyDict_SetItem(new, key, value); | 
|  | 11433 | Py_DECREF(key); | 
|  | 11434 | Py_DECREF(value); | 
|  | 11435 | if (res < 0) | 
|  | 11436 | goto err; | 
|  | 11437 | } | 
|  | 11438 | /* create entries for deleting chars in z */ | 
|  | 11439 | if (z != NULL) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11440 | z_kind = PyUnicode_KIND(z); | 
|  | 11441 | z_data = PyUnicode_DATA(z); | 
| Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11442 | for (i = 0; i < PyUnicode_GET_SIZE(z); i++) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11443 | key = PyLong_FromLong(PyUnicode_READ(z_kind, z_data, i)); | 
| Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11444 | if (!key) | 
|  | 11445 | goto err; | 
|  | 11446 | res = PyDict_SetItem(new, key, Py_None); | 
|  | 11447 | Py_DECREF(key); | 
|  | 11448 | if (res < 0) | 
|  | 11449 | goto err; | 
|  | 11450 | } | 
|  | 11451 | } | 
|  | 11452 | } else { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11453 | int kind; | 
|  | 11454 | void *data; | 
|  | 11455 |  | 
| Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11456 | /* x must be a dict */ | 
| Raymond Hettinger | 3ad0576 | 2009-05-29 22:11:22 +0000 | [diff] [blame] | 11457 | if (!PyDict_CheckExact(x)) { | 
| Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11458 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " | 
|  | 11459 | "to maketrans it must be a dict"); | 
|  | 11460 | goto err; | 
|  | 11461 | } | 
|  | 11462 | /* copy entries into the new dict, converting string keys to int keys */ | 
|  | 11463 | while (PyDict_Next(x, &i, &key, &value)) { | 
|  | 11464 | if (PyUnicode_Check(key)) { | 
|  | 11465 | /* convert string keys to integer keys */ | 
|  | 11466 | PyObject *newkey; | 
|  | 11467 | if (PyUnicode_GET_SIZE(key) != 1) { | 
|  | 11468 | PyErr_SetString(PyExc_ValueError, "string keys in translate " | 
|  | 11469 | "table must be of length 1"); | 
|  | 11470 | goto err; | 
|  | 11471 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11472 | kind = PyUnicode_KIND(key); | 
|  | 11473 | data = PyUnicode_DATA(key); | 
|  | 11474 | newkey = PyLong_FromLong(PyUnicode_READ(kind, data, 0)); | 
| Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11475 | if (!newkey) | 
|  | 11476 | goto err; | 
|  | 11477 | res = PyDict_SetItem(new, newkey, value); | 
|  | 11478 | Py_DECREF(newkey); | 
|  | 11479 | if (res < 0) | 
|  | 11480 | goto err; | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11481 | } else if (PyLong_Check(key)) { | 
| Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11482 | /* just keep integer keys */ | 
|  | 11483 | if (PyDict_SetItem(new, key, value) < 0) | 
|  | 11484 | goto err; | 
|  | 11485 | } else { | 
|  | 11486 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " | 
|  | 11487 | "be strings or integers"); | 
|  | 11488 | goto err; | 
|  | 11489 | } | 
|  | 11490 | } | 
|  | 11491 | } | 
|  | 11492 | return new; | 
|  | 11493 | err: | 
|  | 11494 | Py_DECREF(new); | 
|  | 11495 | return NULL; | 
|  | 11496 | } | 
|  | 11497 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11498 | PyDoc_STRVAR(translate__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11499 | "S.translate(table) -> str\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11500 | \n\ | 
|  | 11501 | Return a copy of the string S, where all characters have been mapped\n\ | 
|  | 11502 | through the given translation table, which must be a mapping of\n\ | 
| Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11503 | Unicode ordinals to Unicode ordinals, strings, or None.\n\ | 
| Walter Dörwald | 5c1ee17 | 2002-09-04 20:31:32 +0000 | [diff] [blame] | 11504 | Unmapped characters are left untouched. Characters mapped to None\n\ | 
|  | 11505 | are deleted."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11506 |  | 
|  | 11507 | static PyObject* | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11508 | unicode_translate(PyObject *self, PyObject *table) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11509 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11510 | return _PyUnicode_TranslateCharmap(self, table, "ignore"); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11511 | } | 
|  | 11512 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11513 | PyDoc_STRVAR(upper__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11514 | "S.upper() -> str\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11515 | \n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11516 | Return a copy of S converted to uppercase."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11517 |  | 
|  | 11518 | static PyObject* | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11519 | unicode_upper(PyUnicodeObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11520 | { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11521 | return fixup(self, fixupper); | 
|  | 11522 | } | 
|  | 11523 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11524 | PyDoc_STRVAR(zfill__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11525 | "S.zfill(width) -> str\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11526 | \n\ | 
| Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 11527 | Pad a numeric string S with zeros on the left, to fill a field\n\ | 
|  | 11528 | of the specified width. The string S is never truncated."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11529 |  | 
|  | 11530 | static PyObject * | 
|  | 11531 | unicode_zfill(PyUnicodeObject *self, PyObject *args) | 
|  | 11532 | { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11533 | Py_ssize_t fill; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11534 | PyUnicodeObject *u; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11535 | Py_ssize_t width; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11536 | int kind; | 
|  | 11537 | void *data; | 
|  | 11538 | Py_UCS4 chr; | 
|  | 11539 |  | 
|  | 11540 | if (PyUnicode_READY(self) == -1) | 
|  | 11541 | return NULL; | 
|  | 11542 |  | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11543 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11544 | return NULL; | 
|  | 11545 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11546 | if (PyUnicode_GET_LENGTH(self) >= width) { | 
| Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 11547 | if (PyUnicode_CheckExact(self)) { | 
|  | 11548 | Py_INCREF(self); | 
|  | 11549 | return (PyObject*) self; | 
|  | 11550 | } | 
|  | 11551 | else | 
| Victor Stinner | 2219e0a | 2011-10-01 01:16:59 +0200 | [diff] [blame] | 11552 | return PyUnicode_Copy((PyObject*)self); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11553 | } | 
|  | 11554 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11555 | fill = width - _PyUnicode_LENGTH(self); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11556 |  | 
|  | 11557 | u = pad(self, fill, 0, '0'); | 
|  | 11558 |  | 
| Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 11559 | if (u == NULL) | 
|  | 11560 | return NULL; | 
|  | 11561 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11562 | kind = PyUnicode_KIND(u); | 
|  | 11563 | data = PyUnicode_DATA(u); | 
|  | 11564 | chr = PyUnicode_READ(kind, data, fill); | 
|  | 11565 |  | 
|  | 11566 | if (chr == '+' || chr == '-') { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11567 | /* move sign to beginning of string */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11568 | PyUnicode_WRITE(kind, data, 0, chr); | 
|  | 11569 | PyUnicode_WRITE(kind, data, fill, '0'); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11570 | } | 
|  | 11571 |  | 
|  | 11572 | return (PyObject*) u; | 
|  | 11573 | } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11574 |  | 
|  | 11575 | #if 0 | 
| Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 11576 | static PyObject * | 
|  | 11577 | unicode__decimal2ascii(PyObject *self) | 
|  | 11578 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11579 | return PyUnicode_TransformDecimalAndSpaceToASCII(self); | 
| Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 11580 | } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11581 | #endif | 
|  | 11582 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11583 | PyDoc_STRVAR(startswith__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11584 | "S.startswith(prefix[, start[, end]]) -> bool\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11585 | \n\ | 
| Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 11586 | Return True if S starts with the specified prefix, False otherwise.\n\ | 
|  | 11587 | With optional start, test S beginning at that position.\n\ | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11588 | With optional end, stop comparing S at that position.\n\ | 
|  | 11589 | prefix can also be a tuple of strings to try."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11590 |  | 
|  | 11591 | static PyObject * | 
|  | 11592 | unicode_startswith(PyUnicodeObject *self, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11593 | PyObject *args) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11594 | { | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11595 | PyObject *subobj; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11596 | PyUnicodeObject *substring; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11597 | Py_ssize_t start = 0; | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11598 | Py_ssize_t end = PY_SSIZE_T_MAX; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11599 | int result; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11600 |  | 
| Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11601 | if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end)) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11602 | return NULL; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11603 | if (PyTuple_Check(subobj)) { | 
|  | 11604 | Py_ssize_t i; | 
|  | 11605 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { | 
|  | 11606 | substring = (PyUnicodeObject *)PyUnicode_FromObject( | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11607 | PyTuple_GET_ITEM(subobj, i)); | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11608 | if (substring == NULL) | 
|  | 11609 | return NULL; | 
|  | 11610 | result = tailmatch(self, substring, start, end, -1); | 
|  | 11611 | Py_DECREF(substring); | 
|  | 11612 | if (result) { | 
|  | 11613 | Py_RETURN_TRUE; | 
|  | 11614 | } | 
|  | 11615 | } | 
|  | 11616 | /* nothing matched */ | 
|  | 11617 | Py_RETURN_FALSE; | 
|  | 11618 | } | 
|  | 11619 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); | 
| Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 11620 | if (substring == NULL) { | 
|  | 11621 | if (PyErr_ExceptionMatches(PyExc_TypeError)) | 
|  | 11622 | PyErr_Format(PyExc_TypeError, "startswith first arg must be str or " | 
|  | 11623 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11624 | return NULL; | 
| Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 11625 | } | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11626 | result = tailmatch(self, substring, start, end, -1); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11627 | Py_DECREF(substring); | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11628 | return PyBool_FromLong(result); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11629 | } | 
|  | 11630 |  | 
|  | 11631 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11632 | PyDoc_STRVAR(endswith__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11633 | "S.endswith(suffix[, start[, end]]) -> bool\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11634 | \n\ | 
| Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 11635 | Return True if S ends with the specified suffix, False otherwise.\n\ | 
|  | 11636 | With optional start, test S beginning at that position.\n\ | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11637 | With optional end, stop comparing S at that position.\n\ | 
|  | 11638 | suffix can also be a tuple of strings to try."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11639 |  | 
|  | 11640 | static PyObject * | 
|  | 11641 | unicode_endswith(PyUnicodeObject *self, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11642 | PyObject *args) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11643 | { | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11644 | PyObject *subobj; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11645 | PyUnicodeObject *substring; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11646 | Py_ssize_t start = 0; | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11647 | Py_ssize_t end = PY_SSIZE_T_MAX; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11648 | int result; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11649 |  | 
| Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11650 | if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end)) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11651 | return NULL; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11652 | if (PyTuple_Check(subobj)) { | 
|  | 11653 | Py_ssize_t i; | 
|  | 11654 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { | 
|  | 11655 | substring = (PyUnicodeObject *)PyUnicode_FromObject( | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11656 | PyTuple_GET_ITEM(subobj, i)); | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11657 | if (substring == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11658 | return NULL; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11659 | result = tailmatch(self, substring, start, end, +1); | 
|  | 11660 | Py_DECREF(substring); | 
|  | 11661 | if (result) { | 
|  | 11662 | Py_RETURN_TRUE; | 
|  | 11663 | } | 
|  | 11664 | } | 
|  | 11665 | Py_RETURN_FALSE; | 
|  | 11666 | } | 
|  | 11667 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); | 
| Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 11668 | if (substring == NULL) { | 
|  | 11669 | if (PyErr_ExceptionMatches(PyExc_TypeError)) | 
|  | 11670 | PyErr_Format(PyExc_TypeError, "endswith first arg must be str or " | 
|  | 11671 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11672 | return NULL; | 
| Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 11673 | } | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11674 | result = tailmatch(self, substring, start, end, +1); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11675 | Py_DECREF(substring); | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11676 | return PyBool_FromLong(result); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11677 | } | 
|  | 11678 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11679 | #include "stringlib/unicode_format.h" | 
| Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 11680 |  | 
|  | 11681 | PyDoc_STRVAR(format__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11682 | "S.format(*args, **kwargs) -> str\n\ | 
| Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 11683 | \n\ | 
| Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 11684 | Return a formatted version of S, using substitutions from args and kwargs.\n\ | 
|  | 11685 | The substitutions are identified by braces ('{' and '}')."); | 
| Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 11686 |  | 
| Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 11687 | PyDoc_STRVAR(format_map__doc__, | 
|  | 11688 | "S.format_map(mapping) -> str\n\ | 
|  | 11689 | \n\ | 
| Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 11690 | Return a formatted version of S, using substitutions from mapping.\n\ | 
|  | 11691 | The substitutions are identified by braces ('{' and '}')."); | 
| Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 11692 |  | 
| Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 11693 | static PyObject * | 
|  | 11694 | unicode__format__(PyObject* self, PyObject* args) | 
|  | 11695 | { | 
|  | 11696 | PyObject *format_spec; | 
|  | 11697 |  | 
|  | 11698 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) | 
|  | 11699 | return NULL; | 
|  | 11700 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11701 | return _PyUnicode_FormatAdvanced(self, format_spec, 0, | 
|  | 11702 | PyUnicode_GET_LENGTH(format_spec)); | 
| Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 11703 | } | 
|  | 11704 |  | 
| Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 11705 | PyDoc_STRVAR(p_format__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11706 | "S.__format__(format_spec) -> str\n\ | 
| Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 11707 | \n\ | 
| Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 11708 | Return a formatted version of S as described by format_spec."); | 
| Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 11709 |  | 
|  | 11710 | static PyObject * | 
| Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 11711 | unicode__sizeof__(PyUnicodeObject *v) | 
|  | 11712 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11713 | Py_ssize_t size; | 
|  | 11714 |  | 
|  | 11715 | /* If it's a compact object, account for base structure + | 
|  | 11716 | character data. */ | 
|  | 11717 | if (PyUnicode_IS_COMPACT_ASCII(v)) | 
|  | 11718 | size = sizeof(PyASCIIObject) + PyUnicode_GET_LENGTH(v) + 1; | 
|  | 11719 | else if (PyUnicode_IS_COMPACT(v)) | 
|  | 11720 | size = sizeof(PyCompactUnicodeObject) + | 
|  | 11721 | (PyUnicode_GET_LENGTH(v) + 1) * PyUnicode_CHARACTER_SIZE(v); | 
|  | 11722 | else { | 
|  | 11723 | /* If it is a two-block object, account for base object, and | 
|  | 11724 | for character block if present. */ | 
|  | 11725 | size = sizeof(PyUnicodeObject); | 
| Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 11726 | if (_PyUnicode_DATA_ANY(v)) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11727 | size += (PyUnicode_GET_LENGTH(v) + 1) * | 
|  | 11728 | PyUnicode_CHARACTER_SIZE(v); | 
|  | 11729 | } | 
|  | 11730 | /* If the wstr pointer is present, account for it unless it is shared | 
|  | 11731 | with the data pointer. Since PyUnicode_DATA will crash if the object | 
|  | 11732 | is not ready, check whether it's either not ready (in which case the | 
|  | 11733 | data is entirely in wstr) or if the data is not shared. */ | 
|  | 11734 | if (_PyUnicode_WSTR(v) && | 
|  | 11735 | (!PyUnicode_IS_READY(v) || | 
|  | 11736 | (PyUnicode_DATA(v) != _PyUnicode_WSTR(v)))) | 
|  | 11737 | size += (PyUnicode_WSTR_LENGTH(v) + 1) * sizeof(wchar_t); | 
| Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 11738 | if (!PyUnicode_IS_COMPACT_ASCII(v) | 
|  | 11739 | && _PyUnicode_UTF8(v) | 
|  | 11740 | && _PyUnicode_UTF8(v) != PyUnicode_DATA(v)) | 
|  | 11741 | size += PyUnicode_UTF8_LENGTH(v) + 1; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11742 |  | 
|  | 11743 | return PyLong_FromSsize_t(size); | 
| Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 11744 | } | 
|  | 11745 |  | 
|  | 11746 | PyDoc_STRVAR(sizeof__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11747 | "S.__sizeof__() -> size of S in memory, in bytes"); | 
| Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 11748 |  | 
|  | 11749 | static PyObject * | 
| Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 11750 | unicode_getnewargs(PyObject *v) | 
| Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 11751 | { | 
| Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 11752 | PyObject *copy = PyUnicode_Copy(v); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11753 | if (!copy) | 
|  | 11754 | return NULL; | 
|  | 11755 | return Py_BuildValue("(N)", copy); | 
| Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 11756 | } | 
|  | 11757 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11758 | static PyMethodDef unicode_methods[] = { | 
|  | 11759 |  | 
|  | 11760 | /* Order is according to common usage: often used methods should | 
|  | 11761 | appear first, since lookup is done sequentially. */ | 
|  | 11762 |  | 
| Benjamin Peterson | 28a4dce | 2010-12-12 01:33:04 +0000 | [diff] [blame] | 11763 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11764 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, | 
|  | 11765 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11766 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11767 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, | 
|  | 11768 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, | 
|  | 11769 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, | 
|  | 11770 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, | 
|  | 11771 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, | 
|  | 11772 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, | 
|  | 11773 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11774 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11775 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, | 
|  | 11776 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, | 
|  | 11777 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11778 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11779 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, | 
|  | 11780 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, | 
|  | 11781 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11782 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11783 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, | 
| Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 11784 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS | METH_KEYWORDS, splitlines__doc__}, | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11785 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11786 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, | 
|  | 11787 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, | 
|  | 11788 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, | 
|  | 11789 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, | 
|  | 11790 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, | 
|  | 11791 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, | 
|  | 11792 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, | 
|  | 11793 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, | 
|  | 11794 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, | 
|  | 11795 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, | 
|  | 11796 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, | 
|  | 11797 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, | 
|  | 11798 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, | 
|  | 11799 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, | 
| Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11800 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, | 
| Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11801 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11802 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, | 
| Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 11803 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, | 
| Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 11804 | {"format_map", (PyCFunction) do_string_format_map, METH_O, format_map__doc__}, | 
| Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 11805 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, | 
| Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11806 | {"maketrans", (PyCFunction) unicode_maketrans, | 
|  | 11807 | METH_VARARGS | METH_STATIC, maketrans__doc__}, | 
| Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 11808 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, | 
| Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 11809 | #if 0 | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11810 | {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11811 | #endif | 
|  | 11812 |  | 
|  | 11813 | #if 0 | 
| Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 11814 | /* These methods are just used for debugging the implementation. */ | 
| Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 11815 | {"_decimal2ascii", (PyCFunction) unicode__decimal2ascii, METH_NOARGS}, | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11816 | #endif | 
|  | 11817 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11818 | {"__getnewargs__",  (PyCFunction)unicode_getnewargs, METH_NOARGS}, | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11819 | {NULL, NULL} | 
|  | 11820 | }; | 
|  | 11821 |  | 
| Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 11822 | static PyObject * | 
|  | 11823 | unicode_mod(PyObject *v, PyObject *w) | 
|  | 11824 | { | 
| Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 11825 | if (!PyUnicode_Check(v)) | 
|  | 11826 | Py_RETURN_NOTIMPLEMENTED; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11827 | return PyUnicode_Format(v, w); | 
| Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 11828 | } | 
|  | 11829 |  | 
|  | 11830 | static PyNumberMethods unicode_as_number = { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11831 | 0,              /*nb_add*/ | 
|  | 11832 | 0,              /*nb_subtract*/ | 
|  | 11833 | 0,              /*nb_multiply*/ | 
|  | 11834 | unicode_mod,            /*nb_remainder*/ | 
| Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 11835 | }; | 
|  | 11836 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11837 | static PySequenceMethods unicode_as_sequence = { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11838 | (lenfunc) unicode_length,       /* sq_length */ | 
|  | 11839 | PyUnicode_Concat,           /* sq_concat */ | 
|  | 11840 | (ssizeargfunc) unicode_repeat,  /* sq_repeat */ | 
|  | 11841 | (ssizeargfunc) unicode_getitem,     /* sq_item */ | 
|  | 11842 | 0,                  /* sq_slice */ | 
|  | 11843 | 0,                  /* sq_ass_item */ | 
|  | 11844 | 0,                  /* sq_ass_slice */ | 
|  | 11845 | PyUnicode_Contains,         /* sq_contains */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11846 | }; | 
|  | 11847 |  | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 11848 | static PyObject* | 
|  | 11849 | unicode_subscript(PyUnicodeObject* self, PyObject* item) | 
|  | 11850 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11851 | if (PyUnicode_READY(self) == -1) | 
|  | 11852 | return NULL; | 
|  | 11853 |  | 
| Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 11854 | if (PyIndex_Check(item)) { | 
|  | 11855 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 11856 | if (i == -1 && PyErr_Occurred()) | 
|  | 11857 | return NULL; | 
|  | 11858 | if (i < 0) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11859 | i += PyUnicode_GET_LENGTH(self); | 
| Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 11860 | return unicode_getitem((PyObject*)self, i); | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 11861 | } else if (PySlice_Check(item)) { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11862 | Py_ssize_t start, stop, step, slicelength, cur, i; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11863 | const Py_UNICODE* source_buf; | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 11864 | Py_UNICODE* result_buf; | 
|  | 11865 | PyObject* result; | 
|  | 11866 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11867 | if (PySlice_GetIndicesEx(item, PyUnicode_GET_LENGTH(self), | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11868 | &start, &stop, &step, &slicelength) < 0) { | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 11869 | return NULL; | 
|  | 11870 | } | 
|  | 11871 |  | 
|  | 11872 | if (slicelength <= 0) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11873 | return PyUnicode_New(0, 0); | 
|  | 11874 | } else if (start == 0 && step == 1 && | 
|  | 11875 | slicelength == PyUnicode_GET_LENGTH(self) && | 
| Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 11876 | PyUnicode_CheckExact(self)) { | 
|  | 11877 | Py_INCREF(self); | 
|  | 11878 | return (PyObject *)self; | 
|  | 11879 | } else if (step == 1) { | 
| Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11880 | return PyUnicode_Substring((PyObject*)self, | 
|  | 11881 | start, start + slicelength); | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 11882 | } else { | 
|  | 11883 | source_buf = PyUnicode_AS_UNICODE((PyObject*)self); | 
| Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 11884 | result_buf = (Py_UNICODE *)PyObject_MALLOC(slicelength* | 
|  | 11885 | sizeof(Py_UNICODE)); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11886 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11887 | if (result_buf == NULL) | 
|  | 11888 | return PyErr_NoMemory(); | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 11889 |  | 
|  | 11890 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { | 
|  | 11891 | result_buf[i] = source_buf[cur]; | 
|  | 11892 | } | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11893 |  | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 11894 | result = PyUnicode_FromUnicode(result_buf, slicelength); | 
| Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 11895 | PyObject_FREE(result_buf); | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 11896 | return result; | 
|  | 11897 | } | 
|  | 11898 | } else { | 
|  | 11899 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); | 
|  | 11900 | return NULL; | 
|  | 11901 | } | 
|  | 11902 | } | 
|  | 11903 |  | 
|  | 11904 | static PyMappingMethods unicode_as_mapping = { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11905 | (lenfunc)unicode_length,        /* mp_length */ | 
|  | 11906 | (binaryfunc)unicode_subscript,  /* mp_subscript */ | 
|  | 11907 | (objobjargproc)0,           /* mp_ass_subscript */ | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 11908 | }; | 
|  | 11909 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11910 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11911 | /* Helpers for PyUnicode_Format() */ | 
|  | 11912 |  | 
|  | 11913 | static PyObject * | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11914 | getnextarg(PyObject *args, Py_ssize_t arglen, Py_ssize_t *p_argidx) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11915 | { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11916 | Py_ssize_t argidx = *p_argidx; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11917 | if (argidx < arglen) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11918 | (*p_argidx)++; | 
|  | 11919 | if (arglen < 0) | 
|  | 11920 | return args; | 
|  | 11921 | else | 
|  | 11922 | return PyTuple_GetItem(args, argidx); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11923 | } | 
|  | 11924 | PyErr_SetString(PyExc_TypeError, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11925 | "not enough arguments for format string"); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11926 | return NULL; | 
|  | 11927 | } | 
|  | 11928 |  | 
| Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 11929 | /* 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] | 11930 |  | 
| Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 11931 | static PyObject * | 
|  | 11932 | formatfloat(PyObject *v, int flags, int prec, int type) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11933 | { | 
| Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 11934 | char *p; | 
|  | 11935 | PyObject *result; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11936 | double x; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11937 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11938 | x = PyFloat_AsDouble(v); | 
|  | 11939 | if (x == -1.0 && PyErr_Occurred()) | 
| Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 11940 | return NULL; | 
|  | 11941 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11942 | if (prec < 0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11943 | prec = 6; | 
| Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 11944 |  | 
| Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 11945 | p = PyOS_double_to_string(x, type, prec, | 
|  | 11946 | (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL); | 
| Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 11947 | if (p == NULL) | 
|  | 11948 | return NULL; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11949 | result = PyUnicode_DecodeASCII(p, strlen(p), NULL); | 
| Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 11950 | PyMem_Free(p); | 
|  | 11951 | return result; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11952 | } | 
|  | 11953 |  | 
| Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 11954 | static PyObject* | 
|  | 11955 | formatlong(PyObject *val, int flags, int prec, int type) | 
|  | 11956 | { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11957 | char *buf; | 
|  | 11958 | int len; | 
|  | 11959 | PyObject *str; /* temporary string object. */ | 
|  | 11960 | PyObject *result; | 
| Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 11961 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11962 | str = _PyBytes_FormatLong(val, flags, prec, type, &buf, &len); | 
|  | 11963 | if (!str) | 
|  | 11964 | return NULL; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11965 | result = PyUnicode_DecodeASCII(buf, len, NULL); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11966 | Py_DECREF(str); | 
|  | 11967 | return result; | 
| Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 11968 | } | 
|  | 11969 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11970 | static int | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11971 | formatchar(Py_UCS4 *buf, | 
| Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 11972 | size_t buflen, | 
|  | 11973 | PyObject *v) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11974 | { | 
| Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 11975 | /* presume that the buffer is at least 3 characters long */ | 
| Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 11976 | if (PyUnicode_Check(v)) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11977 | if (PyUnicode_GET_LENGTH(v) == 1) { | 
|  | 11978 | buf[0] = PyUnicode_READ_CHAR(v, 0); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11979 | buf[1] = '\0'; | 
|  | 11980 | return 1; | 
|  | 11981 | } | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11982 | goto onError; | 
|  | 11983 | } | 
|  | 11984 | else { | 
|  | 11985 | /* Integer input truncated to a character */ | 
|  | 11986 | long x; | 
|  | 11987 | x = PyLong_AsLong(v); | 
|  | 11988 | if (x == -1 && PyErr_Occurred()) | 
|  | 11989 | goto onError; | 
|  | 11990 |  | 
|  | 11991 | if (x < 0 || x > 0x10ffff) { | 
|  | 11992 | PyErr_SetString(PyExc_OverflowError, | 
|  | 11993 | "%c arg not in range(0x110000)"); | 
|  | 11994 | return -1; | 
|  | 11995 | } | 
|  | 11996 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11997 | buf[0] = (Py_UCS4) x; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11998 | buf[1] = '\0'; | 
|  | 11999 | return 1; | 
|  | 12000 | } | 
| Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 12001 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12002 | onError: | 
| Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 12003 | PyErr_SetString(PyExc_TypeError, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12004 | "%c requires int or char"); | 
| Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 12005 | return -1; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12006 | } | 
|  | 12007 |  | 
| Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 12008 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) | 
| Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12009 | FORMATBUFLEN is the length of the buffer in which chars are formatted. | 
| Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 12010 | */ | 
| Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12011 | #define FORMATBUFLEN (size_t)10 | 
| Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 12012 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12013 | PyObject * | 
|  | 12014 | PyUnicode_Format(PyObject *format, PyObject *args) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12015 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12016 | void *fmt; | 
|  | 12017 | int fmtkind; | 
|  | 12018 | PyObject *result; | 
|  | 12019 | Py_UCS4 *res, *res0; | 
|  | 12020 | Py_UCS4 max; | 
|  | 12021 | int kind; | 
|  | 12022 | Py_ssize_t fmtcnt, fmtpos, rescnt, reslen, arglen, argidx; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12023 | int args_owned = 0; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12024 | PyObject *dict = NULL; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12025 | PyUnicodeObject *uformat; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 12026 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12027 | if (format == NULL || args == NULL) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12028 | PyErr_BadInternalCall(); | 
|  | 12029 | return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12030 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12031 | uformat = (PyUnicodeObject*)PyUnicode_FromObject(format); | 
|  | 12032 | if (uformat == NULL || PyUnicode_READY(uformat) == -1) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12033 | return NULL; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12034 | fmt = PyUnicode_DATA(uformat); | 
|  | 12035 | fmtkind = PyUnicode_KIND(uformat); | 
|  | 12036 | fmtcnt = PyUnicode_GET_LENGTH(uformat); | 
|  | 12037 | fmtpos = 0; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12038 |  | 
|  | 12039 | reslen = rescnt = fmtcnt + 100; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12040 | res = res0 = PyMem_Malloc(reslen * sizeof(Py_UCS4)); | 
|  | 12041 | if (res0 == NULL) { | 
|  | 12042 | PyErr_NoMemory(); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12043 | goto onError; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12044 | } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12045 |  | 
|  | 12046 | if (PyTuple_Check(args)) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12047 | arglen = PyTuple_Size(args); | 
|  | 12048 | argidx = 0; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12049 | } | 
|  | 12050 | else { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12051 | arglen = -1; | 
|  | 12052 | argidx = -2; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12053 | } | 
| Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 12054 | if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) && | 
| Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 12055 | !PyUnicode_Check(args)) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12056 | dict = args; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12057 |  | 
|  | 12058 | while (--fmtcnt >= 0) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12059 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) != '%') { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12060 | if (--rescnt < 0) { | 
|  | 12061 | rescnt = fmtcnt + 100; | 
|  | 12062 | reslen += rescnt; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12063 | res0 = PyMem_Realloc(res0, reslen*sizeof(Py_UCS4)); | 
|  | 12064 | if (res0 == NULL){ | 
|  | 12065 | PyErr_NoMemory(); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12066 | goto onError; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12067 | } | 
|  | 12068 | res = res0 + reslen - rescnt; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12069 | --rescnt; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12070 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12071 | *res++ = PyUnicode_READ(fmtkind, fmt, fmtpos++); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12072 | } | 
|  | 12073 | else { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12074 | /* Got a format specifier */ | 
|  | 12075 | int flags = 0; | 
|  | 12076 | Py_ssize_t width = -1; | 
|  | 12077 | int prec = -1; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12078 | Py_UCS4 c = '\0'; | 
|  | 12079 | Py_UCS4 fill; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12080 | int isnumok; | 
|  | 12081 | PyObject *v = NULL; | 
|  | 12082 | PyObject *temp = NULL; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12083 | void *pbuf; | 
|  | 12084 | Py_ssize_t pindex; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12085 | Py_UNICODE sign; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12086 | Py_ssize_t len, len1; | 
|  | 12087 | Py_UCS4 formatbuf[FORMATBUFLEN]; /* For formatchar() */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12088 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12089 | fmtpos++; | 
|  | 12090 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) == '(') { | 
|  | 12091 | Py_ssize_t keystart; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12092 | Py_ssize_t keylen; | 
|  | 12093 | PyObject *key; | 
|  | 12094 | int pcount = 1; | 
| Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 12095 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12096 | if (dict == NULL) { | 
|  | 12097 | PyErr_SetString(PyExc_TypeError, | 
|  | 12098 | "format requires a mapping"); | 
|  | 12099 | goto onError; | 
|  | 12100 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12101 | ++fmtpos; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12102 | --fmtcnt; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12103 | keystart = fmtpos; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12104 | /* Skip over balanced parentheses */ | 
|  | 12105 | while (pcount > 0 && --fmtcnt >= 0) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12106 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) == ')') | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12107 | --pcount; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12108 | else if (PyUnicode_READ(fmtkind, fmt, fmtpos) == '(') | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12109 | ++pcount; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12110 | fmtpos++; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12111 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12112 | keylen = fmtpos - keystart - 1; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12113 | if (fmtcnt < 0 || pcount > 0) { | 
|  | 12114 | PyErr_SetString(PyExc_ValueError, | 
|  | 12115 | "incomplete format key"); | 
|  | 12116 | goto onError; | 
|  | 12117 | } | 
| Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 12118 | key = PyUnicode_Substring((PyObject*)uformat, | 
|  | 12119 | keystart, keystart + keylen); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12120 | if (key == NULL) | 
|  | 12121 | goto onError; | 
|  | 12122 | if (args_owned) { | 
|  | 12123 | Py_DECREF(args); | 
|  | 12124 | args_owned = 0; | 
|  | 12125 | } | 
|  | 12126 | args = PyObject_GetItem(dict, key); | 
|  | 12127 | Py_DECREF(key); | 
|  | 12128 | if (args == NULL) { | 
|  | 12129 | goto onError; | 
|  | 12130 | } | 
|  | 12131 | args_owned = 1; | 
|  | 12132 | arglen = -1; | 
|  | 12133 | argidx = -2; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12134 | } | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12135 | while (--fmtcnt >= 0) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12136 | switch (c = PyUnicode_READ(fmtkind, fmt, fmtpos++)) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12137 | case '-': flags |= F_LJUST; continue; | 
|  | 12138 | case '+': flags |= F_SIGN; continue; | 
|  | 12139 | case ' ': flags |= F_BLANK; continue; | 
|  | 12140 | case '#': flags |= F_ALT; continue; | 
|  | 12141 | case '0': flags |= F_ZERO; continue; | 
|  | 12142 | } | 
|  | 12143 | break; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12144 | } | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12145 | if (c == '*') { | 
|  | 12146 | v = getnextarg(args, arglen, &argidx); | 
|  | 12147 | if (v == NULL) | 
|  | 12148 | goto onError; | 
|  | 12149 | if (!PyLong_Check(v)) { | 
|  | 12150 | PyErr_SetString(PyExc_TypeError, | 
|  | 12151 | "* wants int"); | 
|  | 12152 | goto onError; | 
|  | 12153 | } | 
|  | 12154 | width = PyLong_AsLong(v); | 
|  | 12155 | if (width == -1 && PyErr_Occurred()) | 
|  | 12156 | goto onError; | 
|  | 12157 | if (width < 0) { | 
|  | 12158 | flags |= F_LJUST; | 
|  | 12159 | width = -width; | 
|  | 12160 | } | 
|  | 12161 | if (--fmtcnt >= 0) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12162 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12163 | } | 
|  | 12164 | else if (c >= '0' && c <= '9') { | 
|  | 12165 | width = c - '0'; | 
|  | 12166 | while (--fmtcnt >= 0) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12167 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12168 | if (c < '0' || c > '9') | 
|  | 12169 | break; | 
|  | 12170 | if ((width*10) / 10 != width) { | 
|  | 12171 | PyErr_SetString(PyExc_ValueError, | 
|  | 12172 | "width too big"); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12173 | goto onError; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12174 | } | 
|  | 12175 | width = width*10 + (c - '0'); | 
|  | 12176 | } | 
|  | 12177 | } | 
|  | 12178 | if (c == '.') { | 
|  | 12179 | prec = 0; | 
|  | 12180 | if (--fmtcnt >= 0) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12181 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12182 | if (c == '*') { | 
|  | 12183 | v = getnextarg(args, arglen, &argidx); | 
|  | 12184 | if (v == NULL) | 
|  | 12185 | goto onError; | 
|  | 12186 | if (!PyLong_Check(v)) { | 
|  | 12187 | PyErr_SetString(PyExc_TypeError, | 
|  | 12188 | "* wants int"); | 
|  | 12189 | goto onError; | 
|  | 12190 | } | 
|  | 12191 | prec = PyLong_AsLong(v); | 
|  | 12192 | if (prec == -1 && PyErr_Occurred()) | 
|  | 12193 | goto onError; | 
|  | 12194 | if (prec < 0) | 
|  | 12195 | prec = 0; | 
|  | 12196 | if (--fmtcnt >= 0) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12197 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12198 | } | 
|  | 12199 | else if (c >= '0' && c <= '9') { | 
|  | 12200 | prec = c - '0'; | 
|  | 12201 | while (--fmtcnt >= 0) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12202 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12203 | if (c < '0' || c > '9') | 
|  | 12204 | break; | 
|  | 12205 | if ((prec*10) / 10 != prec) { | 
|  | 12206 | PyErr_SetString(PyExc_ValueError, | 
|  | 12207 | "prec too big"); | 
|  | 12208 | goto onError; | 
|  | 12209 | } | 
|  | 12210 | prec = prec*10 + (c - '0'); | 
|  | 12211 | } | 
|  | 12212 | } | 
|  | 12213 | } /* prec */ | 
|  | 12214 | if (fmtcnt >= 0) { | 
|  | 12215 | if (c == 'h' || c == 'l' || c == 'L') { | 
|  | 12216 | if (--fmtcnt >= 0) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12217 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12218 | } | 
|  | 12219 | } | 
|  | 12220 | if (fmtcnt < 0) { | 
|  | 12221 | PyErr_SetString(PyExc_ValueError, | 
|  | 12222 | "incomplete format"); | 
|  | 12223 | goto onError; | 
|  | 12224 | } | 
|  | 12225 | if (c != '%') { | 
|  | 12226 | v = getnextarg(args, arglen, &argidx); | 
|  | 12227 | if (v == NULL) | 
|  | 12228 | goto onError; | 
|  | 12229 | } | 
|  | 12230 | sign = 0; | 
|  | 12231 | fill = ' '; | 
|  | 12232 | switch (c) { | 
|  | 12233 |  | 
|  | 12234 | case '%': | 
|  | 12235 | pbuf = formatbuf; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12236 | kind = PyUnicode_4BYTE_KIND; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12237 | /* presume that buffer length is at least 1 */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12238 | PyUnicode_WRITE(kind, pbuf, 0, '%'); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12239 | len = 1; | 
|  | 12240 | break; | 
|  | 12241 |  | 
|  | 12242 | case 's': | 
|  | 12243 | case 'r': | 
|  | 12244 | case 'a': | 
| Victor Stinner | 808fc0a | 2010-03-22 12:50:40 +0000 | [diff] [blame] | 12245 | if (PyUnicode_CheckExact(v) && c == 's') { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12246 | temp = v; | 
|  | 12247 | Py_INCREF(temp); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12248 | } | 
|  | 12249 | else { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12250 | if (c == 's') | 
|  | 12251 | temp = PyObject_Str(v); | 
|  | 12252 | else if (c == 'r') | 
|  | 12253 | temp = PyObject_Repr(v); | 
|  | 12254 | else | 
|  | 12255 | temp = PyObject_ASCII(v); | 
|  | 12256 | if (temp == NULL) | 
|  | 12257 | goto onError; | 
|  | 12258 | if (PyUnicode_Check(temp)) | 
|  | 12259 | /* nothing to do */; | 
|  | 12260 | else { | 
|  | 12261 | Py_DECREF(temp); | 
|  | 12262 | PyErr_SetString(PyExc_TypeError, | 
|  | 12263 | "%s argument has non-string str()"); | 
|  | 12264 | goto onError; | 
|  | 12265 | } | 
|  | 12266 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12267 | if (PyUnicode_READY(temp) == -1) { | 
|  | 12268 | Py_CLEAR(temp); | 
|  | 12269 | goto onError; | 
|  | 12270 | } | 
|  | 12271 | pbuf = PyUnicode_DATA(temp); | 
|  | 12272 | kind = PyUnicode_KIND(temp); | 
|  | 12273 | len = PyUnicode_GET_LENGTH(temp); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12274 | if (prec >= 0 && len > prec) | 
|  | 12275 | len = prec; | 
|  | 12276 | break; | 
|  | 12277 |  | 
|  | 12278 | case 'i': | 
|  | 12279 | case 'd': | 
|  | 12280 | case 'u': | 
|  | 12281 | case 'o': | 
|  | 12282 | case 'x': | 
|  | 12283 | case 'X': | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12284 | isnumok = 0; | 
|  | 12285 | if (PyNumber_Check(v)) { | 
|  | 12286 | PyObject *iobj=NULL; | 
|  | 12287 |  | 
|  | 12288 | if (PyLong_Check(v)) { | 
|  | 12289 | iobj = v; | 
|  | 12290 | Py_INCREF(iobj); | 
|  | 12291 | } | 
|  | 12292 | else { | 
|  | 12293 | iobj = PyNumber_Long(v); | 
|  | 12294 | } | 
|  | 12295 | if (iobj!=NULL) { | 
|  | 12296 | if (PyLong_Check(iobj)) { | 
|  | 12297 | isnumok = 1; | 
| Senthil Kumaran | 9ebe08d | 2011-07-03 21:03:16 -0700 | [diff] [blame] | 12298 | temp = formatlong(iobj, flags, prec, (c == 'i'? 'd': c)); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12299 | Py_DECREF(iobj); | 
|  | 12300 | if (!temp) | 
|  | 12301 | goto onError; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12302 | if (PyUnicode_READY(temp) == -1) { | 
|  | 12303 | Py_CLEAR(temp); | 
|  | 12304 | goto onError; | 
|  | 12305 | } | 
|  | 12306 | pbuf = PyUnicode_DATA(temp); | 
|  | 12307 | kind = PyUnicode_KIND(temp); | 
|  | 12308 | len = PyUnicode_GET_LENGTH(temp); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12309 | sign = 1; | 
|  | 12310 | } | 
|  | 12311 | else { | 
|  | 12312 | Py_DECREF(iobj); | 
|  | 12313 | } | 
|  | 12314 | } | 
|  | 12315 | } | 
|  | 12316 | if (!isnumok) { | 
|  | 12317 | PyErr_Format(PyExc_TypeError, | 
|  | 12318 | "%%%c format: a number is required, " | 
|  | 12319 | "not %.200s", (char)c, Py_TYPE(v)->tp_name); | 
|  | 12320 | goto onError; | 
|  | 12321 | } | 
|  | 12322 | if (flags & F_ZERO) | 
|  | 12323 | fill = '0'; | 
|  | 12324 | break; | 
|  | 12325 |  | 
|  | 12326 | case 'e': | 
|  | 12327 | case 'E': | 
|  | 12328 | case 'f': | 
|  | 12329 | case 'F': | 
|  | 12330 | case 'g': | 
|  | 12331 | case 'G': | 
| Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12332 | temp = formatfloat(v, flags, prec, c); | 
|  | 12333 | if (!temp) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12334 | goto onError; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12335 | if (PyUnicode_READY(temp) == -1) { | 
|  | 12336 | Py_CLEAR(temp); | 
|  | 12337 | goto onError; | 
|  | 12338 | } | 
|  | 12339 | pbuf = PyUnicode_DATA(temp); | 
|  | 12340 | kind = PyUnicode_KIND(temp); | 
|  | 12341 | len = PyUnicode_GET_LENGTH(temp); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12342 | sign = 1; | 
|  | 12343 | if (flags & F_ZERO) | 
|  | 12344 | fill = '0'; | 
|  | 12345 | break; | 
|  | 12346 |  | 
|  | 12347 | case 'c': | 
|  | 12348 | pbuf = formatbuf; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12349 | kind = PyUnicode_4BYTE_KIND; | 
| Victor Stinner | b9dcffb | 2011-09-29 00:39:24 +0200 | [diff] [blame] | 12350 | len = formatchar(pbuf, Py_ARRAY_LENGTH(formatbuf), v); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12351 | if (len < 0) | 
|  | 12352 | goto onError; | 
|  | 12353 | break; | 
|  | 12354 |  | 
|  | 12355 | default: | 
|  | 12356 | PyErr_Format(PyExc_ValueError, | 
|  | 12357 | "unsupported format character '%c' (0x%x) " | 
|  | 12358 | "at index %zd", | 
|  | 12359 | (31<=c && c<=126) ? (char)c : '?', | 
|  | 12360 | (int)c, | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12361 | fmtpos - 1); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12362 | goto onError; | 
|  | 12363 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12364 | /* pbuf is initialized here. */ | 
|  | 12365 | pindex = 0; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12366 | if (sign) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12367 | if (PyUnicode_READ(kind, pbuf, pindex) == '-' || | 
|  | 12368 | PyUnicode_READ(kind, pbuf, pindex) == '+') { | 
|  | 12369 | sign = PyUnicode_READ(kind, pbuf, pindex++); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12370 | len--; | 
|  | 12371 | } | 
|  | 12372 | else if (flags & F_SIGN) | 
|  | 12373 | sign = '+'; | 
|  | 12374 | else if (flags & F_BLANK) | 
|  | 12375 | sign = ' '; | 
|  | 12376 | else | 
|  | 12377 | sign = 0; | 
|  | 12378 | } | 
|  | 12379 | if (width < len) | 
|  | 12380 | width = len; | 
|  | 12381 | if (rescnt - (sign != 0) < width) { | 
|  | 12382 | reslen -= rescnt; | 
|  | 12383 | rescnt = width + fmtcnt + 100; | 
|  | 12384 | reslen += rescnt; | 
|  | 12385 | if (reslen < 0) { | 
|  | 12386 | Py_XDECREF(temp); | 
|  | 12387 | PyErr_NoMemory(); | 
|  | 12388 | goto onError; | 
|  | 12389 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12390 | res0 = PyMem_Realloc(res0, reslen*sizeof(Py_UCS4)); | 
|  | 12391 | if (res0 == 0) { | 
|  | 12392 | PyErr_NoMemory(); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12393 | Py_XDECREF(temp); | 
|  | 12394 | goto onError; | 
|  | 12395 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12396 | res = res0 + reslen - rescnt; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12397 | } | 
|  | 12398 | if (sign) { | 
|  | 12399 | if (fill != ' ') | 
|  | 12400 | *res++ = sign; | 
|  | 12401 | rescnt--; | 
|  | 12402 | if (width > len) | 
|  | 12403 | width--; | 
|  | 12404 | } | 
|  | 12405 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12406 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); | 
|  | 12407 | assert(PyUnicode_READ(kind, pbuf, pindex+1) == c); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12408 | if (fill != ' ') { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12409 | *res++ = PyUnicode_READ(kind, pbuf, pindex++); | 
|  | 12410 | *res++ = PyUnicode_READ(kind, pbuf, pindex++); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12411 | } | 
|  | 12412 | rescnt -= 2; | 
|  | 12413 | width -= 2; | 
|  | 12414 | if (width < 0) | 
|  | 12415 | width = 0; | 
|  | 12416 | len -= 2; | 
|  | 12417 | } | 
|  | 12418 | if (width > len && !(flags & F_LJUST)) { | 
|  | 12419 | do { | 
|  | 12420 | --rescnt; | 
|  | 12421 | *res++ = fill; | 
|  | 12422 | } while (--width > len); | 
|  | 12423 | } | 
|  | 12424 | if (fill == ' ') { | 
|  | 12425 | if (sign) | 
|  | 12426 | *res++ = sign; | 
|  | 12427 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12428 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); | 
|  | 12429 | assert(PyUnicode_READ(kind, pbuf, pindex+1) == c); | 
|  | 12430 | *res++ = PyUnicode_READ(kind, pbuf, pindex++); | 
|  | 12431 | *res++ = PyUnicode_READ(kind, pbuf, pindex++); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12432 | } | 
|  | 12433 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12434 | /* Copy all characters, preserving len */ | 
|  | 12435 | len1 = len; | 
|  | 12436 | while (len1--) { | 
|  | 12437 | *res++ = PyUnicode_READ(kind, pbuf, pindex++); | 
|  | 12438 | rescnt--; | 
|  | 12439 | } | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12440 | while (--width >= len) { | 
|  | 12441 | --rescnt; | 
|  | 12442 | *res++ = ' '; | 
|  | 12443 | } | 
|  | 12444 | if (dict && (argidx < arglen) && c != '%') { | 
|  | 12445 | PyErr_SetString(PyExc_TypeError, | 
|  | 12446 | "not all arguments converted during string formatting"); | 
| Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 12447 | Py_XDECREF(temp); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12448 | goto onError; | 
|  | 12449 | } | 
|  | 12450 | Py_XDECREF(temp); | 
|  | 12451 | } /* '%' */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12452 | } /* until end */ | 
|  | 12453 | if (argidx < arglen && !dict) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12454 | PyErr_SetString(PyExc_TypeError, | 
|  | 12455 | "not all arguments converted during string formatting"); | 
|  | 12456 | goto onError; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12457 | } | 
|  | 12458 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12459 |  | 
|  | 12460 | for (max=0, res = res0; res < res0+reslen-rescnt; res++) | 
|  | 12461 | if (*res > max) | 
|  | 12462 | max = *res; | 
|  | 12463 | result = PyUnicode_New(reslen - rescnt, max); | 
|  | 12464 | if (!result) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12465 | goto onError; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12466 | kind = PyUnicode_KIND(result); | 
|  | 12467 | for (res = res0; res < res0+reslen-rescnt; res++) | 
|  | 12468 | PyUnicode_WRITE(kind, PyUnicode_DATA(result), res-res0, *res); | 
|  | 12469 | PyMem_Free(res0); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12470 | if (args_owned) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12471 | Py_DECREF(args); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12472 | } | 
|  | 12473 | Py_DECREF(uformat); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12474 | return (PyObject *)result; | 
|  | 12475 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12476 | onError: | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12477 | PyMem_Free(res0); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12478 | Py_DECREF(uformat); | 
|  | 12479 | if (args_owned) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12480 | Py_DECREF(args); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12481 | } | 
|  | 12482 | return NULL; | 
|  | 12483 | } | 
|  | 12484 |  | 
| Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 12485 | static PyObject * | 
| Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 12486 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); | 
|  | 12487 |  | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 12488 | static PyObject * | 
|  | 12489 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) | 
|  | 12490 | { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12491 | PyObject *x = NULL; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12492 | static char *kwlist[] = {"object", "encoding", "errors", 0}; | 
|  | 12493 | char *encoding = NULL; | 
|  | 12494 | char *errors = NULL; | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 12495 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12496 | if (type != &PyUnicode_Type) | 
|  | 12497 | return unicode_subtype_new(type, args, kwds); | 
|  | 12498 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12499 | kwlist, &x, &encoding, &errors)) | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12500 | return NULL; | 
|  | 12501 | if (x == NULL) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12502 | return (PyObject *)PyUnicode_New(0, 0); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12503 | if (encoding == NULL && errors == NULL) | 
|  | 12504 | return PyObject_Str(x); | 
|  | 12505 | else | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12506 | return PyUnicode_FromEncodedObject(x, encoding, errors); | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 12507 | } | 
|  | 12508 |  | 
| Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 12509 | static PyObject * | 
|  | 12510 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) | 
|  | 12511 | { | 
| Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 12512 | PyUnicodeObject *unicode, *self; | 
|  | 12513 | Py_ssize_t length, char_size; | 
|  | 12514 | int share_wstr, share_utf8; | 
|  | 12515 | unsigned int kind; | 
|  | 12516 | void *data; | 
| Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 12517 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12518 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); | 
| Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 12519 |  | 
|  | 12520 | unicode = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); | 
|  | 12521 | if (unicode == NULL) | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12522 | return NULL; | 
| Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 12523 | assert(PyUnicode_Check(unicode)); | 
|  | 12524 | if (PyUnicode_READY(unicode)) | 
|  | 12525 | return NULL; | 
|  | 12526 |  | 
|  | 12527 | self = (PyUnicodeObject *) type->tp_alloc(type, 0); | 
|  | 12528 | if (self == NULL) { | 
|  | 12529 | Py_DECREF(unicode); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12530 | return NULL; | 
|  | 12531 | } | 
| Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 12532 | kind = PyUnicode_KIND(unicode); | 
|  | 12533 | length = PyUnicode_GET_LENGTH(unicode); | 
|  | 12534 |  | 
|  | 12535 | _PyUnicode_LENGTH(self) = length; | 
|  | 12536 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); | 
|  | 12537 | _PyUnicode_STATE(self).interned = 0; | 
|  | 12538 | _PyUnicode_STATE(self).kind = kind; | 
|  | 12539 | _PyUnicode_STATE(self).compact = 0; | 
|  | 12540 | _PyUnicode_STATE(self).ascii = 0; | 
|  | 12541 | _PyUnicode_STATE(self).ready = 1; | 
|  | 12542 | _PyUnicode_WSTR(self) = NULL; | 
|  | 12543 | _PyUnicode_UTF8_LENGTH(self) = 0; | 
|  | 12544 | _PyUnicode_UTF8(self) = NULL; | 
|  | 12545 | _PyUnicode_WSTR_LENGTH(self) = 0; | 
| Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 12546 | _PyUnicode_DATA_ANY(self) = NULL; | 
| Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 12547 |  | 
|  | 12548 | share_utf8 = 0; | 
|  | 12549 | share_wstr = 0; | 
|  | 12550 | if (kind == PyUnicode_1BYTE_KIND) { | 
|  | 12551 | char_size = 1; | 
|  | 12552 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) | 
|  | 12553 | share_utf8 = 1; | 
|  | 12554 | } | 
|  | 12555 | else if (kind == PyUnicode_2BYTE_KIND) { | 
|  | 12556 | char_size = 2; | 
|  | 12557 | if (sizeof(wchar_t) == 2) | 
|  | 12558 | share_wstr = 1; | 
|  | 12559 | } | 
|  | 12560 | else { | 
|  | 12561 | assert(kind == PyUnicode_4BYTE_KIND); | 
|  | 12562 | char_size = 4; | 
|  | 12563 | if (sizeof(wchar_t) == 4) | 
|  | 12564 | share_wstr = 1; | 
|  | 12565 | } | 
|  | 12566 |  | 
|  | 12567 | /* Ensure we won't overflow the length. */ | 
|  | 12568 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { | 
|  | 12569 | PyErr_NoMemory(); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12570 | goto onError; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12571 | } | 
| Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 12572 | data = PyObject_MALLOC((length + 1) * char_size); | 
|  | 12573 | if (data == NULL) { | 
|  | 12574 | PyErr_NoMemory(); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12575 | goto onError; | 
|  | 12576 | } | 
|  | 12577 |  | 
| Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 12578 | _PyUnicode_DATA_ANY(self) = data; | 
| Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 12579 | if (share_utf8) { | 
|  | 12580 | _PyUnicode_UTF8_LENGTH(self) = length; | 
|  | 12581 | _PyUnicode_UTF8(self) = data; | 
|  | 12582 | } | 
|  | 12583 | if (share_wstr) { | 
|  | 12584 | _PyUnicode_WSTR_LENGTH(self) = length; | 
|  | 12585 | _PyUnicode_WSTR(self) = (wchar_t *)data; | 
|  | 12586 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12587 |  | 
| Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 12588 | Py_MEMCPY(data, PyUnicode_DATA(unicode), | 
|  | 12589 | PyUnicode_KIND_SIZE(kind, length + 1)); | 
|  | 12590 | Py_DECREF(unicode); | 
|  | 12591 | return (PyObject *)self; | 
|  | 12592 |  | 
|  | 12593 | onError: | 
|  | 12594 | Py_DECREF(unicode); | 
|  | 12595 | Py_DECREF(self); | 
|  | 12596 | return NULL; | 
| Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 12597 | } | 
|  | 12598 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12599 | PyDoc_STRVAR(unicode_doc, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12600 | "str(string[, encoding[, errors]]) -> str\n\ | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 12601 | \n\ | 
| Collin Winter | d474ce8 | 2007-08-07 19:42:11 +0000 | [diff] [blame] | 12602 | Create a new string object from the given encoded string.\n\ | 
| Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 12603 | encoding defaults to the current default string encoding.\n\ | 
|  | 12604 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 12605 |  | 
| Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12606 | static PyObject *unicode_iter(PyObject *seq); | 
|  | 12607 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12608 | PyTypeObject PyUnicode_Type = { | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 12609 | PyVarObject_HEAD_INIT(&PyType_Type, 0) | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12610 | "str",              /* tp_name */ | 
|  | 12611 | sizeof(PyUnicodeObject),        /* tp_size */ | 
|  | 12612 | 0,                  /* tp_itemsize */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12613 | /* Slots */ | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12614 | (destructor)unicode_dealloc,    /* tp_dealloc */ | 
|  | 12615 | 0,                  /* tp_print */ | 
|  | 12616 | 0,                  /* tp_getattr */ | 
|  | 12617 | 0,                  /* tp_setattr */ | 
| Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 12618 | 0,                  /* tp_reserved */ | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12619 | unicode_repr,           /* tp_repr */ | 
|  | 12620 | &unicode_as_number,         /* tp_as_number */ | 
|  | 12621 | &unicode_as_sequence,       /* tp_as_sequence */ | 
|  | 12622 | &unicode_as_mapping,        /* tp_as_mapping */ | 
|  | 12623 | (hashfunc) unicode_hash,        /* tp_hash*/ | 
|  | 12624 | 0,                  /* tp_call*/ | 
|  | 12625 | (reprfunc) unicode_str,     /* tp_str */ | 
|  | 12626 | PyObject_GenericGetAttr,        /* tp_getattro */ | 
|  | 12627 | 0,                  /* tp_setattro */ | 
|  | 12628 | 0,                  /* tp_as_buffer */ | 
|  | 12629 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12630 | Py_TPFLAGS_UNICODE_SUBCLASS,    /* tp_flags */ | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12631 | unicode_doc,            /* tp_doc */ | 
|  | 12632 | 0,                  /* tp_traverse */ | 
|  | 12633 | 0,                  /* tp_clear */ | 
|  | 12634 | PyUnicode_RichCompare,      /* tp_richcompare */ | 
|  | 12635 | 0,                  /* tp_weaklistoffset */ | 
|  | 12636 | unicode_iter,           /* tp_iter */ | 
|  | 12637 | 0,                  /* tp_iternext */ | 
|  | 12638 | unicode_methods,            /* tp_methods */ | 
|  | 12639 | 0,                  /* tp_members */ | 
|  | 12640 | 0,                  /* tp_getset */ | 
|  | 12641 | &PyBaseObject_Type,         /* tp_base */ | 
|  | 12642 | 0,                  /* tp_dict */ | 
|  | 12643 | 0,                  /* tp_descr_get */ | 
|  | 12644 | 0,                  /* tp_descr_set */ | 
|  | 12645 | 0,                  /* tp_dictoffset */ | 
|  | 12646 | 0,                  /* tp_init */ | 
|  | 12647 | 0,                  /* tp_alloc */ | 
|  | 12648 | unicode_new,            /* tp_new */ | 
|  | 12649 | PyObject_Del,           /* tp_free */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12650 | }; | 
|  | 12651 |  | 
|  | 12652 | /* Initialize the Unicode implementation */ | 
|  | 12653 |  | 
| Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 12654 | void _PyUnicode_Init(void) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12655 | { | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 12656 | int i; | 
|  | 12657 |  | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12658 | /* XXX - move this array to unicodectype.c ? */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12659 | Py_UCS2 linebreak[] = { | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12660 | 0x000A, /* LINE FEED */ | 
|  | 12661 | 0x000D, /* CARRIAGE RETURN */ | 
|  | 12662 | 0x001C, /* FILE SEPARATOR */ | 
|  | 12663 | 0x001D, /* GROUP SEPARATOR */ | 
|  | 12664 | 0x001E, /* RECORD SEPARATOR */ | 
|  | 12665 | 0x0085, /* NEXT LINE */ | 
|  | 12666 | 0x2028, /* LINE SEPARATOR */ | 
|  | 12667 | 0x2029, /* PARAGRAPH SEPARATOR */ | 
|  | 12668 | }; | 
|  | 12669 |  | 
| Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 12670 | /* Init the implementation */ | 
| Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 12671 | unicode_empty = PyUnicode_New(0, 0); | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12672 | if (!unicode_empty) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12673 | Py_FatalError("Can't create empty string"); | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12674 |  | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 12675 | for (i = 0; i < 256; i++) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12676 | unicode_latin1[i] = NULL; | 
| Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 12677 | if (PyType_Ready(&PyUnicode_Type) < 0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12678 | Py_FatalError("Can't initialize 'unicode'"); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12679 |  | 
|  | 12680 | /* initialize the linebreak bloom filter */ | 
|  | 12681 | bloom_linebreak = make_bloom_mask( | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12682 | PyUnicode_2BYTE_KIND, linebreak, | 
| Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 12683 | Py_ARRAY_LENGTH(linebreak)); | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12684 |  | 
|  | 12685 | PyType_Ready(&EncodingMapType); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12686 | } | 
|  | 12687 |  | 
|  | 12688 | /* Finalize the Unicode implementation */ | 
|  | 12689 |  | 
| Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 12690 | int | 
|  | 12691 | PyUnicode_ClearFreeList(void) | 
|  | 12692 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12693 | return 0; | 
| Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 12694 | } | 
|  | 12695 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12696 | void | 
| Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 12697 | _PyUnicode_Fini(void) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12698 | { | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 12699 | int i; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12700 |  | 
| Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 12701 | Py_XDECREF(unicode_empty); | 
|  | 12702 | unicode_empty = NULL; | 
| Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 12703 |  | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 12704 | for (i = 0; i < 256; i++) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12705 | if (unicode_latin1[i]) { | 
|  | 12706 | Py_DECREF(unicode_latin1[i]); | 
|  | 12707 | unicode_latin1[i] = NULL; | 
|  | 12708 | } | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 12709 | } | 
| Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 12710 | (void)PyUnicode_ClearFreeList(); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12711 | } | 
| Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 12712 |  | 
| Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 12713 | void | 
|  | 12714 | PyUnicode_InternInPlace(PyObject **p) | 
|  | 12715 | { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12716 | register PyUnicodeObject *s = (PyUnicodeObject *)(*p); | 
|  | 12717 | PyObject *t; | 
|  | 12718 | if (s == NULL || !PyUnicode_Check(s)) | 
|  | 12719 | Py_FatalError( | 
|  | 12720 | "PyUnicode_InternInPlace: unicode strings only please!"); | 
|  | 12721 | /* If it's a subclass, we don't really know what putting | 
|  | 12722 | it in the interned dict might do. */ | 
|  | 12723 | if (!PyUnicode_CheckExact(s)) | 
|  | 12724 | return; | 
|  | 12725 | if (PyUnicode_CHECK_INTERNED(s)) | 
|  | 12726 | return; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12727 | if (PyUnicode_READY(s) == -1) { | 
|  | 12728 | assert(0 && "ready fail in intern..."); | 
|  | 12729 | return; | 
|  | 12730 | } | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12731 | if (interned == NULL) { | 
|  | 12732 | interned = PyDict_New(); | 
|  | 12733 | if (interned == NULL) { | 
|  | 12734 | PyErr_Clear(); /* Don't leave an exception */ | 
|  | 12735 | return; | 
|  | 12736 | } | 
|  | 12737 | } | 
|  | 12738 | /* It might be that the GetItem call fails even | 
|  | 12739 | though the key is present in the dictionary, | 
|  | 12740 | namely when this happens during a stack overflow. */ | 
|  | 12741 | Py_ALLOW_RECURSION | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12742 | t = PyDict_GetItem(interned, (PyObject *)s); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12743 | Py_END_ALLOW_RECURSION | 
| Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 12744 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12745 | if (t) { | 
|  | 12746 | Py_INCREF(t); | 
|  | 12747 | Py_DECREF(*p); | 
|  | 12748 | *p = t; | 
|  | 12749 | return; | 
|  | 12750 | } | 
| Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 12751 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12752 | PyThreadState_GET()->recursion_critical = 1; | 
|  | 12753 | if (PyDict_SetItem(interned, (PyObject *)s, (PyObject *)s) < 0) { | 
|  | 12754 | PyErr_Clear(); | 
|  | 12755 | PyThreadState_GET()->recursion_critical = 0; | 
|  | 12756 | return; | 
|  | 12757 | } | 
|  | 12758 | PyThreadState_GET()->recursion_critical = 0; | 
|  | 12759 | /* The two references in interned are not counted by refcnt. | 
|  | 12760 | The deallocator will take care of this */ | 
|  | 12761 | Py_REFCNT(s) -= 2; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12762 | _PyUnicode_STATE(s).interned = SSTATE_INTERNED_MORTAL; | 
| Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 12763 | } | 
|  | 12764 |  | 
|  | 12765 | void | 
|  | 12766 | PyUnicode_InternImmortal(PyObject **p) | 
|  | 12767 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12768 | PyUnicodeObject *u = (PyUnicodeObject *)*p; | 
|  | 12769 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12770 | PyUnicode_InternInPlace(p); | 
|  | 12771 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12772 | _PyUnicode_STATE(u).interned = SSTATE_INTERNED_IMMORTAL; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12773 | Py_INCREF(*p); | 
|  | 12774 | } | 
| Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 12775 | } | 
|  | 12776 |  | 
|  | 12777 | PyObject * | 
|  | 12778 | PyUnicode_InternFromString(const char *cp) | 
|  | 12779 | { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12780 | PyObject *s = PyUnicode_FromString(cp); | 
|  | 12781 | if (s == NULL) | 
|  | 12782 | return NULL; | 
|  | 12783 | PyUnicode_InternInPlace(&s); | 
|  | 12784 | return s; | 
| Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 12785 | } | 
|  | 12786 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12787 | void | 
|  | 12788 | _Py_ReleaseInternedUnicodeStrings(void) | 
| Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 12789 | { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12790 | PyObject *keys; | 
|  | 12791 | PyUnicodeObject *s; | 
|  | 12792 | Py_ssize_t i, n; | 
|  | 12793 | Py_ssize_t immortal_size = 0, mortal_size = 0; | 
| Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 12794 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12795 | if (interned == NULL || !PyDict_Check(interned)) | 
|  | 12796 | return; | 
|  | 12797 | keys = PyDict_Keys(interned); | 
|  | 12798 | if (keys == NULL || !PyList_Check(keys)) { | 
|  | 12799 | PyErr_Clear(); | 
|  | 12800 | return; | 
|  | 12801 | } | 
| Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 12802 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12803 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak | 
|  | 12804 | detector, interned unicode strings are not forcibly deallocated; | 
|  | 12805 | rather, we give them their stolen references back, and then clear | 
|  | 12806 | and DECREF the interned dict. */ | 
| Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 12807 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12808 | n = PyList_GET_SIZE(keys); | 
|  | 12809 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12810 | n); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12811 | for (i = 0; i < n; i++) { | 
|  | 12812 | s = (PyUnicodeObject *) PyList_GET_ITEM(keys, i); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12813 | if (PyUnicode_READY(s) == -1) | 
|  | 12814 | fprintf(stderr, "could not ready string\n"); | 
|  | 12815 | switch (PyUnicode_CHECK_INTERNED(s)) { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12816 | case SSTATE_NOT_INTERNED: | 
|  | 12817 | /* XXX Shouldn't happen */ | 
|  | 12818 | break; | 
|  | 12819 | case SSTATE_INTERNED_IMMORTAL: | 
|  | 12820 | Py_REFCNT(s) += 1; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12821 | immortal_size += PyUnicode_GET_LENGTH(s); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12822 | break; | 
|  | 12823 | case SSTATE_INTERNED_MORTAL: | 
|  | 12824 | Py_REFCNT(s) += 2; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12825 | mortal_size += PyUnicode_GET_LENGTH(s); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12826 | break; | 
|  | 12827 | default: | 
|  | 12828 | Py_FatalError("Inconsistent interned string state."); | 
|  | 12829 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12830 | _PyUnicode_STATE(s).interned = SSTATE_NOT_INTERNED; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12831 | } | 
|  | 12832 | fprintf(stderr, "total size of all interned strings: " | 
|  | 12833 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " | 
|  | 12834 | "mortal/immortal\n", mortal_size, immortal_size); | 
|  | 12835 | Py_DECREF(keys); | 
|  | 12836 | PyDict_Clear(interned); | 
|  | 12837 | Py_DECREF(interned); | 
|  | 12838 | interned = NULL; | 
| Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 12839 | } | 
| Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12840 |  | 
|  | 12841 |  | 
|  | 12842 | /********************* Unicode Iterator **************************/ | 
|  | 12843 |  | 
|  | 12844 | typedef struct { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12845 | PyObject_HEAD | 
|  | 12846 | Py_ssize_t it_index; | 
|  | 12847 | PyUnicodeObject *it_seq; /* Set to NULL when iterator is exhausted */ | 
| Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12848 | } unicodeiterobject; | 
|  | 12849 |  | 
|  | 12850 | static void | 
|  | 12851 | unicodeiter_dealloc(unicodeiterobject *it) | 
|  | 12852 | { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12853 | _PyObject_GC_UNTRACK(it); | 
|  | 12854 | Py_XDECREF(it->it_seq); | 
|  | 12855 | PyObject_GC_Del(it); | 
| Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12856 | } | 
|  | 12857 |  | 
|  | 12858 | static int | 
|  | 12859 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) | 
|  | 12860 | { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12861 | Py_VISIT(it->it_seq); | 
|  | 12862 | return 0; | 
| Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12863 | } | 
|  | 12864 |  | 
|  | 12865 | static PyObject * | 
|  | 12866 | unicodeiter_next(unicodeiterobject *it) | 
|  | 12867 | { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12868 | PyUnicodeObject *seq; | 
|  | 12869 | PyObject *item; | 
| Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12870 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12871 | assert(it != NULL); | 
|  | 12872 | seq = it->it_seq; | 
|  | 12873 | if (seq == NULL) | 
|  | 12874 | return NULL; | 
|  | 12875 | assert(PyUnicode_Check(seq)); | 
| Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12876 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12877 | if (it->it_index < PyUnicode_GET_LENGTH(seq)) { | 
|  | 12878 | int kind = PyUnicode_KIND(seq); | 
|  | 12879 | void *data = PyUnicode_DATA(seq); | 
|  | 12880 | Py_UCS4 chr = PyUnicode_READ(kind, data, it->it_index); | 
|  | 12881 | item = PyUnicode_FromOrdinal(chr); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12882 | if (item != NULL) | 
|  | 12883 | ++it->it_index; | 
|  | 12884 | return item; | 
|  | 12885 | } | 
| Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12886 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12887 | Py_DECREF(seq); | 
|  | 12888 | it->it_seq = NULL; | 
|  | 12889 | return NULL; | 
| Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12890 | } | 
|  | 12891 |  | 
|  | 12892 | static PyObject * | 
|  | 12893 | unicodeiter_len(unicodeiterobject *it) | 
|  | 12894 | { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12895 | Py_ssize_t len = 0; | 
|  | 12896 | if (it->it_seq) | 
|  | 12897 | len = PyUnicode_GET_SIZE(it->it_seq) - it->it_index; | 
|  | 12898 | return PyLong_FromSsize_t(len); | 
| Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12899 | } | 
|  | 12900 |  | 
|  | 12901 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); | 
|  | 12902 |  | 
|  | 12903 | static PyMethodDef unicodeiter_methods[] = { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12904 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12905 | length_hint_doc}, | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12906 | {NULL,      NULL}       /* sentinel */ | 
| Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12907 | }; | 
|  | 12908 |  | 
|  | 12909 | PyTypeObject PyUnicodeIter_Type = { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12910 | PyVarObject_HEAD_INIT(&PyType_Type, 0) | 
|  | 12911 | "str_iterator",         /* tp_name */ | 
|  | 12912 | sizeof(unicodeiterobject),      /* tp_basicsize */ | 
|  | 12913 | 0,                  /* tp_itemsize */ | 
|  | 12914 | /* methods */ | 
|  | 12915 | (destructor)unicodeiter_dealloc,    /* tp_dealloc */ | 
|  | 12916 | 0,                  /* tp_print */ | 
|  | 12917 | 0,                  /* tp_getattr */ | 
|  | 12918 | 0,                  /* tp_setattr */ | 
| Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 12919 | 0,                  /* tp_reserved */ | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12920 | 0,                  /* tp_repr */ | 
|  | 12921 | 0,                  /* tp_as_number */ | 
|  | 12922 | 0,                  /* tp_as_sequence */ | 
|  | 12923 | 0,                  /* tp_as_mapping */ | 
|  | 12924 | 0,                  /* tp_hash */ | 
|  | 12925 | 0,                  /* tp_call */ | 
|  | 12926 | 0,                  /* tp_str */ | 
|  | 12927 | PyObject_GenericGetAttr,        /* tp_getattro */ | 
|  | 12928 | 0,                  /* tp_setattro */ | 
|  | 12929 | 0,                  /* tp_as_buffer */ | 
|  | 12930 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ | 
|  | 12931 | 0,                  /* tp_doc */ | 
|  | 12932 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ | 
|  | 12933 | 0,                  /* tp_clear */ | 
|  | 12934 | 0,                  /* tp_richcompare */ | 
|  | 12935 | 0,                  /* tp_weaklistoffset */ | 
|  | 12936 | PyObject_SelfIter,          /* tp_iter */ | 
|  | 12937 | (iternextfunc)unicodeiter_next,     /* tp_iternext */ | 
|  | 12938 | unicodeiter_methods,            /* tp_methods */ | 
|  | 12939 | 0, | 
| Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12940 | }; | 
|  | 12941 |  | 
|  | 12942 | static PyObject * | 
|  | 12943 | unicode_iter(PyObject *seq) | 
|  | 12944 | { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12945 | unicodeiterobject *it; | 
| Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12946 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12947 | if (!PyUnicode_Check(seq)) { | 
|  | 12948 | PyErr_BadInternalCall(); | 
|  | 12949 | return NULL; | 
|  | 12950 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12951 | if (PyUnicode_READY(seq) == -1) | 
|  | 12952 | return NULL; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12953 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); | 
|  | 12954 | if (it == NULL) | 
|  | 12955 | return NULL; | 
|  | 12956 | it->it_index = 0; | 
|  | 12957 | Py_INCREF(seq); | 
|  | 12958 | it->it_seq = (PyUnicodeObject *)seq; | 
|  | 12959 | _PyObject_GC_TRACK(it); | 
|  | 12960 | return (PyObject *)it; | 
| Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12961 | } | 
|  | 12962 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12963 | #define UNIOP(x) Py_UNICODE_##x | 
|  | 12964 | #define UNIOP_t Py_UNICODE | 
|  | 12965 | #include "uniops.h" | 
|  | 12966 | #undef UNIOP | 
|  | 12967 | #undef UNIOP_t | 
|  | 12968 | #define UNIOP(x) Py_UCS4_##x | 
|  | 12969 | #define UNIOP_t Py_UCS4 | 
|  | 12970 | #include "uniops.h" | 
|  | 12971 | #undef UNIOP | 
|  | 12972 | #undef UNIOP_t | 
| Victor Stinner | 331ea92 | 2010-08-10 16:37:20 +0000 | [diff] [blame] | 12973 |  | 
| Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 12974 | Py_UNICODE* | 
| Victor Stinner | 4640860 | 2010-09-03 16:18:00 +0000 | [diff] [blame] | 12975 | PyUnicode_AsUnicodeCopy(PyObject *object) | 
| Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 12976 | { | 
|  | 12977 | PyUnicodeObject *unicode = (PyUnicodeObject *)object; | 
|  | 12978 | Py_UNICODE *copy; | 
|  | 12979 | Py_ssize_t size; | 
|  | 12980 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12981 | if (!PyUnicode_Check(unicode)) { | 
|  | 12982 | PyErr_BadArgument(); | 
|  | 12983 | return NULL; | 
|  | 12984 | } | 
| Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 12985 | /* Ensure we won't overflow the size. */ | 
|  | 12986 | if (PyUnicode_GET_SIZE(unicode) > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { | 
|  | 12987 | PyErr_NoMemory(); | 
|  | 12988 | return NULL; | 
|  | 12989 | } | 
|  | 12990 | size = PyUnicode_GET_SIZE(unicode) + 1; /* copy the nul character */ | 
|  | 12991 | size *= sizeof(Py_UNICODE); | 
|  | 12992 | copy = PyMem_Malloc(size); | 
|  | 12993 | if (copy == NULL) { | 
|  | 12994 | PyErr_NoMemory(); | 
|  | 12995 | return NULL; | 
|  | 12996 | } | 
|  | 12997 | memcpy(copy, PyUnicode_AS_UNICODE(unicode), size); | 
|  | 12998 | return copy; | 
|  | 12999 | } | 
| Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 13000 |  | 
| Georg Brandl | 66c221e | 2010-10-14 07:04:07 +0000 | [diff] [blame] | 13001 | /* A _string module, to export formatter_parser and formatter_field_name_split | 
|  | 13002 | to the string.Formatter class implemented in Python. */ | 
|  | 13003 |  | 
|  | 13004 | static PyMethodDef _string_methods[] = { | 
|  | 13005 | {"formatter_field_name_split", (PyCFunction) formatter_field_name_split, | 
|  | 13006 | METH_O, PyDoc_STR("split the argument as a field name")}, | 
|  | 13007 | {"formatter_parser", (PyCFunction) formatter_parser, | 
|  | 13008 | METH_O, PyDoc_STR("parse the argument as a format string")}, | 
|  | 13009 | {NULL, NULL} | 
|  | 13010 | }; | 
|  | 13011 |  | 
|  | 13012 | static struct PyModuleDef _string_module = { | 
|  | 13013 | PyModuleDef_HEAD_INIT, | 
|  | 13014 | "_string", | 
|  | 13015 | PyDoc_STR("string helper module"), | 
|  | 13016 | 0, | 
|  | 13017 | _string_methods, | 
|  | 13018 | NULL, | 
|  | 13019 | NULL, | 
|  | 13020 | NULL, | 
|  | 13021 | NULL | 
|  | 13022 | }; | 
|  | 13023 |  | 
|  | 13024 | PyMODINIT_FUNC | 
|  | 13025 | PyInit__string(void) | 
|  | 13026 | { | 
|  | 13027 | return PyModule_Create(&_string_module); | 
|  | 13028 | } | 
|  | 13029 |  | 
|  | 13030 |  | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 13031 | #ifdef __cplusplus | 
|  | 13032 | } | 
|  | 13033 | #endif |