Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1 | /* |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2 | |
| 3 | Unicode implementation based on original code by Fredrik Lundh, |
Fred Drake | 785d14f | 2000-05-09 19:54:43 +0000 | [diff] [blame] | 4 | modified by Marc-Andre Lemburg <mal@lemburg.com> according to the |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5 | Unicode Integration Proposal (see file Misc/unicode.txt). |
| 6 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7 | Major speed upgrades to the method implementations at the Reykjavik |
| 8 | NeedForSpeed sprint, by Fredrik Lundh and Andrew Dalke. |
| 9 | |
Guido van Rossum | 16b1ad9 | 2000-08-03 16:24:25 +0000 | [diff] [blame] | 10 | Copyright (c) Corporation for National Research Initiatives. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11 | |
Fredrik Lundh | 0fdb90c | 2001-01-19 09:45:02 +0000 | [diff] [blame] | 12 | -------------------------------------------------------------------- |
| 13 | The original string type implementation is: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14 | |
Fredrik Lundh | 0fdb90c | 2001-01-19 09:45:02 +0000 | [diff] [blame] | 15 | Copyright (c) 1999 by Secret Labs AB |
| 16 | Copyright (c) 1999 by Fredrik Lundh |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 17 | |
Fredrik Lundh | 0fdb90c | 2001-01-19 09:45:02 +0000 | [diff] [blame] | 18 | By obtaining, using, and/or copying this software and/or its |
| 19 | associated documentation, you agree that you have read, understood, |
| 20 | and will comply with the following terms and conditions: |
| 21 | |
| 22 | Permission to use, copy, modify, and distribute this software and its |
| 23 | associated documentation for any purpose and without fee is hereby |
| 24 | granted, provided that the above copyright notice appears in all |
| 25 | copies, and that both that copyright notice and this permission notice |
| 26 | appear in supporting documentation, and that the name of Secret Labs |
| 27 | AB or the author not be used in advertising or publicity pertaining to |
| 28 | distribution of the software without specific, written prior |
| 29 | permission. |
| 30 | |
| 31 | SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 32 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 33 | FITNESS. IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR BE LIABLE FOR |
| 34 | ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 35 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 36 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 37 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 38 | -------------------------------------------------------------------- |
| 39 | |
| 40 | */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 41 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 42 | #define PY_SSIZE_T_CLEAN |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 43 | #include "Python.h" |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 44 | #include "bytes_methods.h" |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 45 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 46 | #include "unicodeobject.h" |
Marc-André Lemburg | d49e5b4 | 2000-06-30 14:58:20 +0000 | [diff] [blame] | 47 | #include "ucnhash.h" |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 48 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 49 | #ifdef MS_WINDOWS |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 50 | #include <windows.h> |
| 51 | #endif |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 52 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 53 | /* Limit for the Unicode object free list */ |
| 54 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 55 | #define PyUnicode_MAXFREELIST 1024 |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 56 | |
| 57 | /* Limit for the Unicode object free list stay alive optimization. |
| 58 | |
| 59 | The implementation will keep allocated Unicode memory intact for |
| 60 | all objects on the free list having a size less than this |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 61 | limit. This reduces malloc() overhead for small Unicode objects. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 62 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 63 | At worst this will result in PyUnicode_MAXFREELIST * |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 64 | (sizeof(PyUnicodeObject) + KEEPALIVE_SIZE_LIMIT + |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 65 | malloc()-overhead) bytes of unused garbage. |
| 66 | |
| 67 | Setting the limit to 0 effectively turns the feature off. |
| 68 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 69 | Note: This is an experimental feature ! If you get core dumps when |
| 70 | using Unicode objects, turn this feature off. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 71 | |
| 72 | */ |
| 73 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 74 | #define KEEPALIVE_SIZE_LIMIT 9 |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 75 | |
| 76 | /* Endianness switches; defaults to little endian */ |
| 77 | |
| 78 | #ifdef WORDS_BIGENDIAN |
| 79 | # define BYTEORDER_IS_BIG_ENDIAN |
| 80 | #else |
| 81 | # define BYTEORDER_IS_LITTLE_ENDIAN |
| 82 | #endif |
| 83 | |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 84 | /* --- Globals ------------------------------------------------------------ |
| 85 | |
| 86 | The globals are initialized by the _PyUnicode_Init() API and should |
| 87 | not be used before calling that API. |
| 88 | |
| 89 | */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 90 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 91 | |
| 92 | #ifdef __cplusplus |
| 93 | extern "C" { |
| 94 | #endif |
| 95 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 96 | /* This dictionary holds all interned unicode strings. Note that references |
| 97 | to strings in this dictionary are *not* counted in the string's ob_refcnt. |
| 98 | When the interned string reaches a refcnt of 0 the string deallocation |
| 99 | function will delete the reference from this dictionary. |
| 100 | |
| 101 | Another way to look at this is that to say that the actual reference |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 102 | count of a string is: s->ob_refcnt + (s->state ? 2 : 0) |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 103 | */ |
| 104 | static PyObject *interned; |
| 105 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 106 | /* Free list for Unicode objects */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 107 | static PyUnicodeObject *free_list; |
| 108 | static int numfree; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 109 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 110 | /* The empty Unicode object is shared to improve performance. */ |
| 111 | static PyUnicodeObject *unicode_empty; |
| 112 | |
| 113 | /* Single character Unicode strings in the Latin-1 range are being |
| 114 | shared as well. */ |
| 115 | static PyUnicodeObject *unicode_latin1[256]; |
| 116 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 117 | /* Default encoding to use and assume when NULL is passed as encoding |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 118 | parameter; it is fixed to "utf-8". Always use the |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 119 | PyUnicode_GetDefaultEncoding() API to access this global. |
| 120 | |
Alexandre Vassalotti | 3d2fd7f | 2007-10-16 00:26:33 +0000 | [diff] [blame] | 121 | Don't forget to alter Py_FileSystemDefaultEncoding if you change the |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 122 | hard coded default! |
| 123 | */ |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 124 | static const char unicode_default_encoding[] = "utf-8"; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 125 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 126 | /* Fast detection of the most frequent whitespace characters */ |
| 127 | const unsigned char _Py_ascii_whitespace[] = { |
| 128 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 129 | /* case 0x0009: * HORIZONTAL TABULATION */ |
| 130 | /* case 0x000A: * LINE FEED */ |
| 131 | /* case 0x000B: * VERTICAL TABULATION */ |
| 132 | /* case 0x000C: * FORM FEED */ |
| 133 | /* case 0x000D: * CARRIAGE RETURN */ |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 134 | 0, 1, 1, 1, 1, 1, 0, 0, |
| 135 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 136 | /* case 0x001C: * FILE SEPARATOR */ |
| 137 | /* case 0x001D: * GROUP SEPARATOR */ |
| 138 | /* case 0x001E: * RECORD SEPARATOR */ |
| 139 | /* case 0x001F: * UNIT SEPARATOR */ |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 140 | 0, 0, 0, 0, 1, 1, 1, 1, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 141 | /* case 0x0020: * SPACE */ |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 142 | 1, 0, 0, 0, 0, 0, 0, 0, |
| 143 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 144 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 145 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 146 | |
| 147 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 148 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 149 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 150 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 151 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 152 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 153 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 154 | 0, 0, 0, 0, 0, 0, 0, 0 |
| 155 | }; |
| 156 | |
| 157 | /* Same for linebreaks */ |
| 158 | static unsigned char ascii_linebreak[] = { |
| 159 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 160 | /* 0x000A, * LINE FEED */ |
| 161 | /* 0x000D, * CARRIAGE RETURN */ |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 162 | 0, 0, 1, 0, 0, 1, 0, 0, |
| 163 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 164 | /* 0x001C, * FILE SEPARATOR */ |
| 165 | /* 0x001D, * GROUP SEPARATOR */ |
| 166 | /* 0x001E, * RECORD SEPARATOR */ |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 167 | 0, 0, 0, 0, 1, 1, 1, 0, |
| 168 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 169 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 170 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 171 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 172 | |
| 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, |
| 176 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 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 | }; |
| 182 | |
| 183 | |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 184 | Py_UNICODE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 185 | PyUnicode_GetMax(void) |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 186 | { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 187 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 188 | return 0x10FFFF; |
| 189 | #else |
| 190 | /* This is actually an illegal character, so it should |
| 191 | not be passed to unichr. */ |
| 192 | return 0xFFFF; |
| 193 | #endif |
| 194 | } |
| 195 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 196 | /* --- Bloom Filters ----------------------------------------------------- */ |
| 197 | |
| 198 | /* stuff to implement simple "bloom filters" for Unicode characters. |
| 199 | to keep things simple, we use a single bitmask, using the least 5 |
| 200 | bits from each unicode characters as the bit index. */ |
| 201 | |
| 202 | /* the linebreak mask is set up by Unicode_Init below */ |
| 203 | |
| 204 | #define BLOOM_MASK unsigned long |
| 205 | |
| 206 | static BLOOM_MASK bloom_linebreak; |
| 207 | |
| 208 | #define BLOOM(mask, ch) ((mask & (1 << ((ch) & 0x1F)))) |
| 209 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 210 | #define BLOOM_LINEBREAK(ch) \ |
| 211 | ((ch) < 128U ? ascii_linebreak[(ch)] : \ |
| 212 | (BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK(ch))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 213 | |
| 214 | Py_LOCAL_INLINE(BLOOM_MASK) make_bloom_mask(Py_UNICODE* ptr, Py_ssize_t len) |
| 215 | { |
| 216 | /* calculate simple bloom-style bitmask for a given unicode string */ |
| 217 | |
| 218 | long mask; |
| 219 | Py_ssize_t i; |
| 220 | |
| 221 | mask = 0; |
| 222 | for (i = 0; i < len; i++) |
| 223 | mask |= (1 << (ptr[i] & 0x1F)); |
| 224 | |
| 225 | return mask; |
| 226 | } |
| 227 | |
| 228 | Py_LOCAL_INLINE(int) unicode_member(Py_UNICODE chr, Py_UNICODE* set, Py_ssize_t setlen) |
| 229 | { |
| 230 | Py_ssize_t i; |
| 231 | |
| 232 | for (i = 0; i < setlen; i++) |
| 233 | if (set[i] == chr) |
| 234 | return 1; |
| 235 | |
| 236 | return 0; |
| 237 | } |
| 238 | |
| 239 | #define BLOOM_MEMBER(mask, chr, set, setlen)\ |
| 240 | BLOOM(mask, chr) && unicode_member(chr, set, setlen) |
| 241 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 242 | /* --- Unicode Object ----------------------------------------------------- */ |
| 243 | |
| 244 | static |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 245 | int unicode_resize(register PyUnicodeObject *unicode, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 246 | Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 247 | { |
| 248 | void *oldstr; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 249 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 250 | /* Shortcut if there's nothing much to do. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 251 | if (unicode->length == length) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 252 | goto reset; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 253 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 254 | /* Resizing shared object (unicode_empty or single character |
| 255 | objects) in-place is not allowed. Use PyUnicode_Resize() |
| 256 | instead ! */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 257 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 258 | if (unicode == unicode_empty || |
| 259 | (unicode->length == 1 && |
| 260 | unicode->str[0] < 256U && |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 261 | unicode_latin1[unicode->str[0]] == unicode)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 262 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 263 | "can't resize shared str objects"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 264 | return -1; |
| 265 | } |
| 266 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 267 | /* We allocate one more byte to make sure the string is Ux0000 terminated. |
| 268 | The overallocation is also used by fastsearch, which assumes that it's |
| 269 | safe to look at str[length] (without making any assumptions about what |
| 270 | it contains). */ |
| 271 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 272 | oldstr = unicode->str; |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 273 | unicode->str = PyObject_REALLOC(unicode->str, |
| 274 | sizeof(Py_UNICODE) * (length + 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 275 | if (!unicode->str) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 276 | unicode->str = (Py_UNICODE *)oldstr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 277 | PyErr_NoMemory(); |
| 278 | return -1; |
| 279 | } |
| 280 | unicode->str[length] = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 281 | unicode->length = length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 282 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 283 | reset: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 284 | /* Reset the object caches */ |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 285 | if (unicode->defenc) { |
| 286 | Py_DECREF(unicode->defenc); |
| 287 | unicode->defenc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 288 | } |
| 289 | unicode->hash = -1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 290 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | /* 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] | 295 | Ux0000 terminated; some code (e.g. new_identifier) |
| 296 | relies on that. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 297 | |
| 298 | XXX This allocator could further be enhanced by assuring that the |
| 299 | free list never reduces its size below 1. |
| 300 | |
| 301 | */ |
| 302 | |
| 303 | static |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 304 | PyUnicodeObject *_PyUnicode_New(Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 305 | { |
| 306 | register PyUnicodeObject *unicode; |
| 307 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 308 | /* Optimization for empty strings */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 309 | if (length == 0 && unicode_empty != NULL) { |
| 310 | Py_INCREF(unicode_empty); |
| 311 | return unicode_empty; |
| 312 | } |
| 313 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 314 | /* Ensure we won't overflow the size. */ |
| 315 | if (length > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
| 316 | return (PyUnicodeObject *)PyErr_NoMemory(); |
| 317 | } |
| 318 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 319 | /* Unicode freelist & memory allocation */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 320 | if (free_list) { |
| 321 | unicode = free_list; |
| 322 | free_list = *(PyUnicodeObject **)unicode; |
| 323 | numfree--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 324 | if (unicode->str) { |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 325 | /* Keep-Alive optimization: we only upsize the buffer, |
| 326 | never downsize it. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 327 | if ((unicode->length < length) && |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 328 | unicode_resize(unicode, length) < 0) { |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 329 | PyObject_DEL(unicode->str); |
Amaury Forgeot d'Arc | 7888d08 | 2008-08-01 01:06:32 +0000 | [diff] [blame] | 330 | unicode->str = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 331 | } |
| 332 | } |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 333 | else { |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 334 | size_t new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 335 | unicode->str = (Py_UNICODE*) PyObject_MALLOC(new_size); |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 336 | } |
| 337 | PyObject_INIT(unicode, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 338 | } |
| 339 | else { |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 340 | size_t new_size; |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 341 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 342 | if (unicode == NULL) |
| 343 | return NULL; |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 344 | new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 345 | unicode->str = (Py_UNICODE*) PyObject_MALLOC(new_size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 346 | } |
| 347 | |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 348 | if (!unicode->str) { |
| 349 | PyErr_NoMemory(); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 350 | goto onError; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 351 | } |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 352 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 353 | * the caller fails before initializing str -- unicode_resize() |
| 354 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 355 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 356 | * We don't want unicode_resize to read uninitialized memory in |
| 357 | * that case. |
| 358 | */ |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 359 | unicode->str[0] = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 360 | unicode->str[length] = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 361 | unicode->length = length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 362 | unicode->hash = -1; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 363 | unicode->state = 0; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 364 | unicode->defenc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 365 | return unicode; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 366 | |
| 367 | onError: |
Amaury Forgeot d'Arc | 7888d08 | 2008-08-01 01:06:32 +0000 | [diff] [blame] | 368 | /* XXX UNREF/NEWREF interface should be more symmetrical */ |
| 369 | _Py_DEC_REFTOTAL; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 370 | _Py_ForgetReference((PyObject *)unicode); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 371 | PyObject_Del(unicode); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 372 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | static |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 376 | void unicode_dealloc(register PyUnicodeObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 377 | { |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 378 | switch (PyUnicode_CHECK_INTERNED(unicode)) { |
| 379 | case SSTATE_NOT_INTERNED: |
| 380 | break; |
| 381 | |
| 382 | case SSTATE_INTERNED_MORTAL: |
| 383 | /* revive dead object temporarily for DelItem */ |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 384 | Py_REFCNT(unicode) = 3; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 385 | if (PyDict_DelItem(interned, (PyObject *)unicode) != 0) |
| 386 | Py_FatalError( |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 387 | "deletion of interned string failed"); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 388 | break; |
| 389 | |
| 390 | case SSTATE_INTERNED_IMMORTAL: |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 391 | Py_FatalError("Immortal interned string died."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 392 | |
| 393 | default: |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 394 | Py_FatalError("Inconsistent interned string state."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 395 | } |
| 396 | |
Guido van Rossum | 604ddf8 | 2001-12-06 20:03:56 +0000 | [diff] [blame] | 397 | if (PyUnicode_CheckExact(unicode) && |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 398 | numfree < PyUnicode_MAXFREELIST) { |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 399 | /* Keep-Alive optimization */ |
| 400 | if (unicode->length >= KEEPALIVE_SIZE_LIMIT) { |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 401 | PyObject_DEL(unicode->str); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 402 | unicode->str = NULL; |
| 403 | unicode->length = 0; |
| 404 | } |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 405 | if (unicode->defenc) { |
| 406 | Py_DECREF(unicode->defenc); |
| 407 | unicode->defenc = NULL; |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 408 | } |
| 409 | /* Add to free list */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 410 | *(PyUnicodeObject **)unicode = free_list; |
| 411 | free_list = unicode; |
| 412 | numfree++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 413 | } |
| 414 | else { |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 415 | PyObject_DEL(unicode->str); |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 416 | Py_XDECREF(unicode->defenc); |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 417 | Py_TYPE(unicode)->tp_free((PyObject *)unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 418 | } |
| 419 | } |
| 420 | |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 421 | static |
| 422 | int _PyUnicode_Resize(PyUnicodeObject **unicode, Py_ssize_t length) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 423 | { |
| 424 | register PyUnicodeObject *v; |
| 425 | |
| 426 | /* Argument checks */ |
| 427 | if (unicode == NULL) { |
| 428 | PyErr_BadInternalCall(); |
| 429 | return -1; |
| 430 | } |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 431 | v = *unicode; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 432 | if (v == NULL || !PyUnicode_Check(v) || Py_REFCNT(v) != 1 || length < 0) { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 433 | PyErr_BadInternalCall(); |
| 434 | return -1; |
| 435 | } |
| 436 | |
| 437 | /* Resizing unicode_empty and single character objects is not |
| 438 | possible since these are being shared. We simply return a fresh |
| 439 | copy with the same Unicode content. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 440 | if (v->length != length && |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 441 | (v == unicode_empty || v->length == 1)) { |
| 442 | PyUnicodeObject *w = _PyUnicode_New(length); |
| 443 | if (w == NULL) |
| 444 | return -1; |
| 445 | Py_UNICODE_COPY(w->str, v->str, |
| 446 | length < v->length ? length : v->length); |
Raymond Hettinger | c8df578 | 2003-03-09 07:30:43 +0000 | [diff] [blame] | 447 | Py_DECREF(*unicode); |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 448 | *unicode = w; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 449 | return 0; |
| 450 | } |
| 451 | |
| 452 | /* Note that we don't have to modify *unicode for unshared Unicode |
| 453 | objects, since we can modify them in-place. */ |
| 454 | return unicode_resize(v, length); |
| 455 | } |
| 456 | |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 457 | int PyUnicode_Resize(PyObject **unicode, Py_ssize_t length) |
| 458 | { |
| 459 | return _PyUnicode_Resize((PyUnicodeObject **)unicode, length); |
| 460 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 461 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 462 | PyObject *PyUnicode_FromUnicode(const Py_UNICODE *u, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 463 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 464 | { |
| 465 | PyUnicodeObject *unicode; |
| 466 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 467 | /* If the Unicode data is known at construction time, we can apply |
| 468 | some optimizations which share commonly used objects. */ |
| 469 | if (u != NULL) { |
| 470 | |
| 471 | /* Optimization for empty strings */ |
| 472 | if (size == 0 && unicode_empty != NULL) { |
| 473 | Py_INCREF(unicode_empty); |
| 474 | return (PyObject *)unicode_empty; |
| 475 | } |
| 476 | |
| 477 | /* Single character Unicode objects in the Latin-1 range are |
| 478 | shared when using this constructor */ |
| 479 | if (size == 1 && *u < 256) { |
| 480 | unicode = unicode_latin1[*u]; |
| 481 | if (!unicode) { |
| 482 | unicode = _PyUnicode_New(1); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 483 | if (!unicode) |
| 484 | return NULL; |
Marc-André Lemburg | 8879a33 | 2001-06-07 12:26:56 +0000 | [diff] [blame] | 485 | unicode->str[0] = *u; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 486 | unicode_latin1[*u] = unicode; |
| 487 | } |
| 488 | Py_INCREF(unicode); |
| 489 | return (PyObject *)unicode; |
| 490 | } |
| 491 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 492 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 493 | unicode = _PyUnicode_New(size); |
| 494 | if (!unicode) |
| 495 | return NULL; |
| 496 | |
| 497 | /* Copy the Unicode data into the new object */ |
| 498 | if (u != NULL) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 499 | Py_UNICODE_COPY(unicode->str, u, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 500 | |
| 501 | return (PyObject *)unicode; |
| 502 | } |
| 503 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 504 | PyObject *PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 505 | { |
| 506 | PyUnicodeObject *unicode; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 507 | |
| 508 | if (size < 0) { |
| 509 | PyErr_SetString(PyExc_SystemError, |
| 510 | "Negative size passed to PyUnicode_FromStringAndSize"); |
| 511 | return NULL; |
| 512 | } |
| 513 | |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 514 | /* 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] | 515 | some optimizations which share commonly used objects. |
| 516 | Also, this means the input must be UTF-8, so fall back to the |
| 517 | UTF-8 decoder at the end. */ |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 518 | if (u != NULL) { |
| 519 | |
| 520 | /* Optimization for empty strings */ |
| 521 | if (size == 0 && unicode_empty != NULL) { |
| 522 | Py_INCREF(unicode_empty); |
| 523 | return (PyObject *)unicode_empty; |
| 524 | } |
| 525 | |
Martin v. Löwis | 9c12106 | 2007-08-05 20:26:11 +0000 | [diff] [blame] | 526 | /* Single characters are shared when using this constructor. |
| 527 | Restrict to ASCII, since the input must be UTF-8. */ |
| 528 | if (size == 1 && Py_CHARMASK(*u) < 128) { |
Christian Heimes | bbe741d | 2008-03-28 10:53:29 +0000 | [diff] [blame] | 529 | unicode = unicode_latin1[Py_CHARMASK(*u)]; |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 530 | if (!unicode) { |
| 531 | unicode = _PyUnicode_New(1); |
| 532 | if (!unicode) |
| 533 | return NULL; |
Guido van Rossum | 00058aa | 2007-07-19 18:21:28 +0000 | [diff] [blame] | 534 | unicode->str[0] = Py_CHARMASK(*u); |
Christian Heimes | bbe741d | 2008-03-28 10:53:29 +0000 | [diff] [blame] | 535 | unicode_latin1[Py_CHARMASK(*u)] = unicode; |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 536 | } |
| 537 | Py_INCREF(unicode); |
| 538 | return (PyObject *)unicode; |
| 539 | } |
Martin v. Löwis | 9c12106 | 2007-08-05 20:26:11 +0000 | [diff] [blame] | 540 | |
| 541 | return PyUnicode_DecodeUTF8(u, size, NULL); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 542 | } |
| 543 | |
Walter Dörwald | 5550731 | 2007-05-18 13:12:10 +0000 | [diff] [blame] | 544 | unicode = _PyUnicode_New(size); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 545 | if (!unicode) |
| 546 | return NULL; |
| 547 | |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 548 | return (PyObject *)unicode; |
| 549 | } |
| 550 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 551 | PyObject *PyUnicode_FromString(const char *u) |
| 552 | { |
| 553 | size_t size = strlen(u); |
| 554 | if (size > PY_SSIZE_T_MAX) { |
| 555 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 556 | return NULL; |
| 557 | } |
| 558 | |
| 559 | return PyUnicode_FromStringAndSize(u, size); |
| 560 | } |
| 561 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 562 | #ifdef HAVE_WCHAR_H |
| 563 | |
| 564 | PyObject *PyUnicode_FromWideChar(register const wchar_t *w, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 565 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 566 | { |
| 567 | PyUnicodeObject *unicode; |
| 568 | |
| 569 | if (w == NULL) { |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 570 | if (size == 0) |
| 571 | return PyUnicode_FromStringAndSize(NULL, 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 572 | PyErr_BadInternalCall(); |
| 573 | return NULL; |
| 574 | } |
| 575 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 576 | if (size == -1) { |
| 577 | size = wcslen(w); |
| 578 | } |
| 579 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 580 | unicode = _PyUnicode_New(size); |
| 581 | if (!unicode) |
| 582 | return NULL; |
| 583 | |
| 584 | /* Copy the wchar_t data into the new object */ |
| 585 | #ifdef HAVE_USABLE_WCHAR_T |
| 586 | memcpy(unicode->str, w, size * sizeof(wchar_t)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 587 | #else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 588 | { |
| 589 | register Py_UNICODE *u; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 590 | register Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 591 | u = PyUnicode_AS_UNICODE(unicode); |
Marc-André Lemburg | 204bd6d | 2004-10-15 07:45:05 +0000 | [diff] [blame] | 592 | for (i = size; i > 0; i--) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 593 | *u++ = *w++; |
| 594 | } |
| 595 | #endif |
| 596 | |
| 597 | return (PyObject *)unicode; |
| 598 | } |
| 599 | |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 600 | static void |
| 601 | makefmt(char *fmt, int longflag, int size_tflag, int zeropad, int width, int precision, char c) |
| 602 | { |
| 603 | *fmt++ = '%'; |
| 604 | if (width) { |
| 605 | if (zeropad) |
| 606 | *fmt++ = '0'; |
| 607 | fmt += sprintf(fmt, "%d", width); |
| 608 | } |
| 609 | if (precision) |
| 610 | fmt += sprintf(fmt, ".%d", precision); |
| 611 | if (longflag) |
| 612 | *fmt++ = 'l'; |
| 613 | else if (size_tflag) { |
| 614 | char *f = PY_FORMAT_SIZE_T; |
| 615 | while (*f) |
| 616 | *fmt++ = *f++; |
| 617 | } |
| 618 | *fmt++ = c; |
| 619 | *fmt = '\0'; |
| 620 | } |
| 621 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 622 | #define appendstring(string) {for (copy = string;*copy;) *s++ = *copy++;} |
| 623 | |
| 624 | PyObject * |
| 625 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 626 | { |
| 627 | va_list count; |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 628 | Py_ssize_t callcount = 0; |
| 629 | PyObject **callresults = NULL; |
Walter Dörwald | 63a28be | 2007-06-20 15:11:12 +0000 | [diff] [blame] | 630 | PyObject **callresult = NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 631 | Py_ssize_t n = 0; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 632 | int width = 0; |
| 633 | int precision = 0; |
| 634 | int zeropad; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 635 | const char* f; |
| 636 | Py_UNICODE *s; |
| 637 | PyObject *string; |
| 638 | /* used by sprintf */ |
| 639 | char buffer[21]; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 640 | /* use abuffer instead of buffer, if we need more space |
| 641 | * (which can happen if there's a format specifier with width). */ |
| 642 | char *abuffer = NULL; |
| 643 | char *realbuffer; |
| 644 | Py_ssize_t abuffersize = 0; |
| 645 | char fmt[60]; /* should be enough for %0width.precisionld */ |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 646 | const char *copy; |
| 647 | |
| 648 | #ifdef VA_LIST_IS_ARRAY |
| 649 | Py_MEMCPY(count, vargs, sizeof(va_list)); |
| 650 | #else |
| 651 | #ifdef __va_copy |
| 652 | __va_copy(count, vargs); |
| 653 | #else |
| 654 | count = vargs; |
| 655 | #endif |
| 656 | #endif |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 657 | /* step 1: count the number of %S/%R/%A format specifications |
| 658 | * (we call PyObject_Str()/PyObject_Repr()/PyObject_ASCII() for |
| 659 | * these objects once during step 3 and put the result in |
| 660 | an array) */ |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 661 | for (f = format; *f; f++) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 662 | if (*f == '%' && (*(f+1)=='S' || *(f+1)=='R' || *(f+1)=='A')) |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 663 | ++callcount; |
| 664 | } |
Walter Dörwald | 1be7e3f | 2007-05-23 21:02:42 +0000 | [diff] [blame] | 665 | /* step 2: allocate memory for the results of |
Thomas Heller | 519a042 | 2007-11-15 20:48:54 +0000 | [diff] [blame] | 666 | * PyObject_Str()/PyObject_Repr() calls */ |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 667 | if (callcount) { |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 668 | callresults = PyObject_Malloc(sizeof(PyObject *)*callcount); |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 669 | if (!callresults) { |
| 670 | PyErr_NoMemory(); |
| 671 | return NULL; |
| 672 | } |
| 673 | callresult = callresults; |
| 674 | } |
| 675 | /* step 3: figure out how large a buffer we need */ |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 676 | for (f = format; *f; f++) { |
| 677 | if (*f == '%') { |
| 678 | const char* p = f; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 679 | width = 0; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 680 | while (ISDIGIT((unsigned)*f)) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 681 | width = (width*10) + *f++ - '0'; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 682 | while (*++f && *f != '%' && !ISALPHA((unsigned)*f)) |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 683 | ; |
| 684 | |
| 685 | /* skip the 'l' or 'z' in {%ld, %zd, %lu, %zu} since |
| 686 | * they don't affect the amount of space we reserve. |
| 687 | */ |
| 688 | if ((*f == 'l' || *f == 'z') && |
| 689 | (f[1] == 'd' || f[1] == 'u')) |
Eric Smith | ddd2582 | 2007-08-27 11:33:42 +0000 | [diff] [blame] | 690 | ++f; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 691 | |
| 692 | switch (*f) { |
| 693 | case 'c': |
| 694 | (void)va_arg(count, int); |
| 695 | /* fall through... */ |
| 696 | case '%': |
| 697 | n++; |
| 698 | break; |
| 699 | case 'd': case 'u': case 'i': case 'x': |
| 700 | (void) va_arg(count, int); |
| 701 | /* 20 bytes is enough to hold a 64-bit |
| 702 | integer. Decimal takes the most space. |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 703 | This isn't enough for octal. |
| 704 | If a width is specified we need more |
| 705 | (which we allocate later). */ |
| 706 | if (width < 20) |
| 707 | width = 20; |
| 708 | n += width; |
| 709 | if (abuffersize < width) |
| 710 | abuffersize = width; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 711 | break; |
| 712 | case 's': |
Martin v. Löwis | 90d1fcd | 2007-08-31 11:01:23 +0000 | [diff] [blame] | 713 | { |
| 714 | /* UTF-8 */ |
| 715 | unsigned char*s; |
| 716 | s = va_arg(count, unsigned char*); |
| 717 | while (*s) { |
| 718 | if (*s < 128) { |
| 719 | n++; s++; |
| 720 | } else if (*s < 0xc0) { |
| 721 | /* invalid UTF-8 */ |
| 722 | n++; s++; |
| 723 | } else if (*s < 0xc0) { |
| 724 | n++; |
| 725 | s++; if(!*s)break; |
| 726 | s++; |
| 727 | } else if (*s < 0xe0) { |
| 728 | n++; |
| 729 | s++; if(!*s)break; |
| 730 | s++; if(!*s)break; |
| 731 | s++; |
| 732 | } else { |
| 733 | #ifdef Py_UNICODE_WIDE |
| 734 | n++; |
| 735 | #else |
| 736 | n+=2; |
| 737 | #endif |
| 738 | s++; if(!*s)break; |
| 739 | s++; if(!*s)break; |
| 740 | s++; if(!*s)break; |
| 741 | s++; |
| 742 | } |
| 743 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 744 | break; |
Martin v. Löwis | 90d1fcd | 2007-08-31 11:01:23 +0000 | [diff] [blame] | 745 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 746 | case 'U': |
| 747 | { |
| 748 | PyObject *obj = va_arg(count, PyObject *); |
| 749 | assert(obj && PyUnicode_Check(obj)); |
| 750 | n += PyUnicode_GET_SIZE(obj); |
| 751 | break; |
| 752 | } |
Walter Dörwald | d7fb764 | 2007-06-11 16:36:59 +0000 | [diff] [blame] | 753 | case 'V': |
| 754 | { |
| 755 | PyObject *obj = va_arg(count, PyObject *); |
| 756 | const char *str = va_arg(count, const char *); |
| 757 | assert(obj || str); |
| 758 | assert(!obj || PyUnicode_Check(obj)); |
| 759 | if (obj) |
| 760 | n += PyUnicode_GET_SIZE(obj); |
| 761 | else |
| 762 | n += strlen(str); |
| 763 | break; |
| 764 | } |
Walter Dörwald | 1be7e3f | 2007-05-23 21:02:42 +0000 | [diff] [blame] | 765 | case 'S': |
| 766 | { |
| 767 | PyObject *obj = va_arg(count, PyObject *); |
| 768 | PyObject *str; |
| 769 | assert(obj); |
Thomas Heller | 519a042 | 2007-11-15 20:48:54 +0000 | [diff] [blame] | 770 | str = PyObject_Str(obj); |
Walter Dörwald | 1be7e3f | 2007-05-23 21:02:42 +0000 | [diff] [blame] | 771 | if (!str) |
| 772 | goto fail; |
| 773 | n += PyUnicode_GET_SIZE(str); |
| 774 | /* Remember the str and switch to the next slot */ |
| 775 | *callresult++ = str; |
| 776 | break; |
| 777 | } |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 778 | case 'R': |
| 779 | { |
| 780 | PyObject *obj = va_arg(count, PyObject *); |
| 781 | PyObject *repr; |
| 782 | assert(obj); |
| 783 | repr = PyObject_Repr(obj); |
| 784 | if (!repr) |
| 785 | goto fail; |
| 786 | n += PyUnicode_GET_SIZE(repr); |
| 787 | /* Remember the repr and switch to the next slot */ |
| 788 | *callresult++ = repr; |
| 789 | break; |
| 790 | } |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 791 | case 'A': |
| 792 | { |
| 793 | PyObject *obj = va_arg(count, PyObject *); |
| 794 | PyObject *ascii; |
| 795 | assert(obj); |
| 796 | ascii = PyObject_ASCII(obj); |
| 797 | if (!ascii) |
| 798 | goto fail; |
| 799 | n += PyUnicode_GET_SIZE(ascii); |
| 800 | /* Remember the repr and switch to the next slot */ |
| 801 | *callresult++ = ascii; |
| 802 | break; |
| 803 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 804 | case 'p': |
| 805 | (void) va_arg(count, int); |
| 806 | /* maximum 64-bit pointer representation: |
| 807 | * 0xffffffffffffffff |
| 808 | * so 19 characters is enough. |
| 809 | * XXX I count 18 -- what's the extra for? |
| 810 | */ |
| 811 | n += 19; |
| 812 | break; |
| 813 | default: |
| 814 | /* if we stumble upon an unknown |
| 815 | formatting code, copy the rest of |
| 816 | the format string to the output |
| 817 | string. (we cannot just skip the |
| 818 | code, since there's no way to know |
| 819 | what's in the argument list) */ |
| 820 | n += strlen(p); |
| 821 | goto expand; |
| 822 | } |
| 823 | } else |
| 824 | n++; |
| 825 | } |
| 826 | expand: |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 827 | if (abuffersize > 20) { |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 828 | abuffer = PyObject_Malloc(abuffersize); |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 829 | if (!abuffer) { |
| 830 | PyErr_NoMemory(); |
| 831 | goto fail; |
| 832 | } |
| 833 | realbuffer = abuffer; |
| 834 | } |
| 835 | else |
| 836 | realbuffer = buffer; |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 837 | /* step 4: fill the buffer */ |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 838 | /* Since we've analyzed how much space we need for the worst case, |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 839 | we don't have to resize the string. |
| 840 | There can be no errors beyond this point. */ |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 841 | string = PyUnicode_FromUnicode(NULL, n); |
| 842 | if (!string) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 843 | goto fail; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 844 | |
| 845 | s = PyUnicode_AS_UNICODE(string); |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 846 | callresult = callresults; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 847 | |
| 848 | for (f = format; *f; f++) { |
| 849 | if (*f == '%') { |
| 850 | const char* p = f++; |
| 851 | int longflag = 0; |
| 852 | int size_tflag = 0; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 853 | zeropad = (*f == '0'); |
| 854 | /* parse the width.precision part */ |
| 855 | width = 0; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 856 | while (ISDIGIT((unsigned)*f)) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 857 | width = (width*10) + *f++ - '0'; |
| 858 | precision = 0; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 859 | if (*f == '.') { |
| 860 | f++; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 861 | while (ISDIGIT((unsigned)*f)) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 862 | precision = (precision*10) + *f++ - '0'; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 863 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 864 | /* handle the long flag, but only for %ld and %lu. |
| 865 | others can be added when necessary. */ |
| 866 | if (*f == 'l' && (f[1] == 'd' || f[1] == 'u')) { |
| 867 | longflag = 1; |
| 868 | ++f; |
| 869 | } |
| 870 | /* handle the size_t flag. */ |
| 871 | if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) { |
| 872 | size_tflag = 1; |
| 873 | ++f; |
| 874 | } |
| 875 | |
| 876 | switch (*f) { |
| 877 | case 'c': |
| 878 | *s++ = va_arg(vargs, int); |
| 879 | break; |
| 880 | case 'd': |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 881 | makefmt(fmt, longflag, size_tflag, zeropad, width, precision, 'd'); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 882 | if (longflag) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 883 | sprintf(realbuffer, fmt, va_arg(vargs, long)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 884 | else if (size_tflag) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 885 | sprintf(realbuffer, fmt, va_arg(vargs, Py_ssize_t)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 886 | else |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 887 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 888 | appendstring(realbuffer); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 889 | break; |
| 890 | case 'u': |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 891 | makefmt(fmt, longflag, size_tflag, zeropad, width, precision, 'u'); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 892 | if (longflag) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 893 | sprintf(realbuffer, fmt, va_arg(vargs, unsigned long)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 894 | else if (size_tflag) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 895 | sprintf(realbuffer, fmt, va_arg(vargs, size_t)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 896 | else |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 897 | sprintf(realbuffer, fmt, va_arg(vargs, unsigned int)); |
| 898 | appendstring(realbuffer); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 899 | break; |
| 900 | case 'i': |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 901 | makefmt(fmt, 0, 0, zeropad, width, precision, 'i'); |
| 902 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 903 | appendstring(realbuffer); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 904 | break; |
| 905 | case 'x': |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 906 | makefmt(fmt, 0, 0, zeropad, width, precision, 'x'); |
| 907 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 908 | appendstring(realbuffer); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 909 | break; |
| 910 | case 's': |
Martin v. Löwis | 90d1fcd | 2007-08-31 11:01:23 +0000 | [diff] [blame] | 911 | { |
| 912 | /* Parameter must be UTF-8 encoded. |
| 913 | In case of encoding errors, use |
| 914 | the replacement character. */ |
| 915 | PyObject *u; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 916 | p = va_arg(vargs, char*); |
Martin v. Löwis | 90d1fcd | 2007-08-31 11:01:23 +0000 | [diff] [blame] | 917 | u = PyUnicode_DecodeUTF8(p, strlen(p), |
| 918 | "replace"); |
| 919 | if (!u) |
| 920 | goto fail; |
| 921 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(u), |
| 922 | PyUnicode_GET_SIZE(u)); |
| 923 | s += PyUnicode_GET_SIZE(u); |
| 924 | Py_DECREF(u); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 925 | break; |
Martin v. Löwis | 90d1fcd | 2007-08-31 11:01:23 +0000 | [diff] [blame] | 926 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 927 | case 'U': |
| 928 | { |
| 929 | PyObject *obj = va_arg(vargs, PyObject *); |
Walter Dörwald | 5c2fab6 | 2007-05-24 19:51:02 +0000 | [diff] [blame] | 930 | Py_ssize_t size = PyUnicode_GET_SIZE(obj); |
| 931 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size); |
| 932 | s += size; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 933 | break; |
| 934 | } |
Walter Dörwald | d7fb764 | 2007-06-11 16:36:59 +0000 | [diff] [blame] | 935 | case 'V': |
| 936 | { |
| 937 | PyObject *obj = va_arg(vargs, PyObject *); |
| 938 | const char *str = va_arg(vargs, const char *); |
| 939 | if (obj) { |
| 940 | Py_ssize_t size = PyUnicode_GET_SIZE(obj); |
| 941 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size); |
| 942 | s += size; |
| 943 | } else { |
| 944 | appendstring(str); |
| 945 | } |
| 946 | break; |
| 947 | } |
Walter Dörwald | 1be7e3f | 2007-05-23 21:02:42 +0000 | [diff] [blame] | 948 | case 'S': |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 949 | case 'R': |
| 950 | { |
Guido van Rossum | 755114a | 2007-06-13 01:04:27 +0000 | [diff] [blame] | 951 | Py_UNICODE *ucopy; |
| 952 | Py_ssize_t usize; |
| 953 | Py_ssize_t upos; |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 954 | /* unused, since we already have the result */ |
| 955 | (void) va_arg(vargs, PyObject *); |
Guido van Rossum | 755114a | 2007-06-13 01:04:27 +0000 | [diff] [blame] | 956 | ucopy = PyUnicode_AS_UNICODE(*callresult); |
| 957 | usize = PyUnicode_GET_SIZE(*callresult); |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 958 | for (upos = 0; upos<usize;) |
| 959 | *s++ = ucopy[upos++]; |
Walter Dörwald | 1be7e3f | 2007-05-23 21:02:42 +0000 | [diff] [blame] | 960 | /* We're done with the unicode()/repr() => forget it */ |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 961 | Py_DECREF(*callresult); |
Walter Dörwald | 1be7e3f | 2007-05-23 21:02:42 +0000 | [diff] [blame] | 962 | /* switch to next unicode()/repr() result */ |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 963 | ++callresult; |
| 964 | break; |
| 965 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 966 | case 'p': |
| 967 | sprintf(buffer, "%p", va_arg(vargs, void*)); |
| 968 | /* %p is ill-defined: ensure leading 0x. */ |
| 969 | if (buffer[1] == 'X') |
| 970 | buffer[1] = 'x'; |
| 971 | else if (buffer[1] != 'x') { |
| 972 | memmove(buffer+2, buffer, strlen(buffer)+1); |
| 973 | buffer[0] = '0'; |
| 974 | buffer[1] = 'x'; |
| 975 | } |
| 976 | appendstring(buffer); |
| 977 | break; |
| 978 | case '%': |
| 979 | *s++ = '%'; |
| 980 | break; |
| 981 | default: |
| 982 | appendstring(p); |
| 983 | goto end; |
| 984 | } |
| 985 | } else |
| 986 | *s++ = *f; |
| 987 | } |
| 988 | |
| 989 | end: |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 990 | if (callresults) |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 991 | PyObject_Free(callresults); |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 992 | if (abuffer) |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 993 | PyObject_Free(abuffer); |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 994 | PyUnicode_Resize(&string, s - PyUnicode_AS_UNICODE(string)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 995 | return string; |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 996 | fail: |
| 997 | if (callresults) { |
| 998 | PyObject **callresult2 = callresults; |
Guido van Rossum | 307fa8c | 2007-07-16 20:46:27 +0000 | [diff] [blame] | 999 | while (callresult2 < callresult) { |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 1000 | Py_DECREF(*callresult2); |
| 1001 | ++callresult2; |
| 1002 | } |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 1003 | PyObject_Free(callresults); |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 1004 | } |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 1005 | if (abuffer) |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 1006 | PyObject_Free(abuffer); |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 1007 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1008 | } |
| 1009 | |
| 1010 | #undef appendstring |
| 1011 | |
| 1012 | PyObject * |
| 1013 | PyUnicode_FromFormat(const char *format, ...) |
| 1014 | { |
| 1015 | PyObject* ret; |
| 1016 | va_list vargs; |
| 1017 | |
| 1018 | #ifdef HAVE_STDARG_PROTOTYPES |
| 1019 | va_start(vargs, format); |
| 1020 | #else |
| 1021 | va_start(vargs); |
| 1022 | #endif |
| 1023 | ret = PyUnicode_FromFormatV(format, vargs); |
| 1024 | va_end(vargs); |
| 1025 | return ret; |
| 1026 | } |
| 1027 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1028 | Py_ssize_t PyUnicode_AsWideChar(PyUnicodeObject *unicode, |
| 1029 | wchar_t *w, |
| 1030 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1031 | { |
| 1032 | if (unicode == NULL) { |
| 1033 | PyErr_BadInternalCall(); |
| 1034 | return -1; |
| 1035 | } |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 1036 | |
| 1037 | /* If possible, try to copy the 0-termination as well */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1038 | if (size > PyUnicode_GET_SIZE(unicode)) |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 1039 | size = PyUnicode_GET_SIZE(unicode) + 1; |
| 1040 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1041 | #ifdef HAVE_USABLE_WCHAR_T |
| 1042 | memcpy(w, unicode->str, size * sizeof(wchar_t)); |
| 1043 | #else |
| 1044 | { |
| 1045 | register Py_UNICODE *u; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1046 | register Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1047 | u = PyUnicode_AS_UNICODE(unicode); |
Marc-André Lemburg | 204bd6d | 2004-10-15 07:45:05 +0000 | [diff] [blame] | 1048 | for (i = size; i > 0; i--) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1049 | *w++ = *u++; |
| 1050 | } |
| 1051 | #endif |
| 1052 | |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 1053 | if (size > PyUnicode_GET_SIZE(unicode)) |
| 1054 | return PyUnicode_GET_SIZE(unicode); |
| 1055 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1056 | return size; |
| 1057 | } |
| 1058 | |
| 1059 | #endif |
| 1060 | |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1061 | PyObject *PyUnicode_FromOrdinal(int ordinal) |
| 1062 | { |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 1063 | Py_UNICODE s[2]; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1064 | |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1065 | if (ordinal < 0 || ordinal > 0x10ffff) { |
| 1066 | PyErr_SetString(PyExc_ValueError, |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 1067 | "chr() arg not in range(0x110000)"); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1068 | return NULL; |
| 1069 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 1070 | |
| 1071 | #ifndef Py_UNICODE_WIDE |
| 1072 | if (ordinal > 0xffff) { |
| 1073 | ordinal -= 0x10000; |
| 1074 | s[0] = 0xD800 | (ordinal >> 10); |
| 1075 | s[1] = 0xDC00 | (ordinal & 0x3FF); |
| 1076 | return PyUnicode_FromUnicode(s, 2); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1077 | } |
| 1078 | #endif |
| 1079 | |
Hye-Shik Chang | 4057483 | 2004-04-06 07:24:51 +0000 | [diff] [blame] | 1080 | s[0] = (Py_UNICODE)ordinal; |
| 1081 | return PyUnicode_FromUnicode(s, 1); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1082 | } |
| 1083 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1084 | PyObject *PyUnicode_FromObject(register PyObject *obj) |
| 1085 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1086 | /* XXX Perhaps we should make this API an alias of |
Thomas Heller | 519a042 | 2007-11-15 20:48:54 +0000 | [diff] [blame] | 1087 | PyObject_Str() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1088 | if (PyUnicode_CheckExact(obj)) { |
| 1089 | Py_INCREF(obj); |
| 1090 | return obj; |
| 1091 | } |
| 1092 | if (PyUnicode_Check(obj)) { |
| 1093 | /* For a Unicode subtype that's not a Unicode object, |
| 1094 | return a true Unicode object with the same data. */ |
| 1095 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj), |
| 1096 | PyUnicode_GET_SIZE(obj)); |
| 1097 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1098 | PyErr_Format(PyExc_TypeError, |
| 1099 | "Can't convert '%.100s' object to str implicitly", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1100 | Py_TYPE(obj)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1101 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1102 | } |
| 1103 | |
| 1104 | PyObject *PyUnicode_FromEncodedObject(register PyObject *obj, |
| 1105 | const char *encoding, |
| 1106 | const char *errors) |
| 1107 | { |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1108 | const char *s = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1109 | Py_ssize_t len; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1110 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1111 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1112 | if (obj == NULL) { |
| 1113 | PyErr_BadInternalCall(); |
| 1114 | return NULL; |
| 1115 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1116 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1117 | if (PyUnicode_Check(obj)) { |
| 1118 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1119 | "decoding str is not supported"); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1120 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1121 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1122 | |
| 1123 | /* Coerce object */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1124 | if (PyBytes_Check(obj)) { |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1125 | s = PyBytes_AS_STRING(obj); |
| 1126 | len = PyBytes_GET_SIZE(obj); |
| 1127 | } |
| 1128 | else if (PyByteArray_Check(obj)) { |
| 1129 | s = PyByteArray_AS_STRING(obj); |
| 1130 | len = PyByteArray_GET_SIZE(obj); |
| 1131 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1132 | else if (PyObject_AsCharBuffer(obj, &s, &len)) { |
| 1133 | /* Overwrite the error message with something more useful in |
| 1134 | case of a TypeError. */ |
| 1135 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1136 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1137 | "coercing to str: need string or buffer, " |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1138 | "%.80s found", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1139 | Py_TYPE(obj)->tp_name); |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1140 | goto onError; |
| 1141 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1142 | |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1143 | /* Convert to Unicode */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1144 | if (len == 0) { |
| 1145 | Py_INCREF(unicode_empty); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1146 | v = (PyObject *)unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1147 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1148 | else |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1149 | v = PyUnicode_Decode(s, len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 1150 | |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1151 | return v; |
| 1152 | |
| 1153 | onError: |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1154 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1155 | } |
| 1156 | |
| 1157 | PyObject *PyUnicode_Decode(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1158 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1159 | const char *encoding, |
| 1160 | const char *errors) |
| 1161 | { |
| 1162 | PyObject *buffer = NULL, *unicode; |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 1163 | Py_buffer info; |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1164 | char lower[20]; /* Enough for any encoding name we recognize */ |
| 1165 | char *l; |
| 1166 | const char *e; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1167 | |
| 1168 | if (encoding == NULL) |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1169 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1170 | |
| 1171 | /* Convert encoding to lower case and replace '_' with '-' in order to |
| 1172 | catch e.g. UTF_8 */ |
| 1173 | e = encoding; |
| 1174 | l = lower; |
| 1175 | while (*e && l < &lower[(sizeof lower) - 2]) { |
| 1176 | if (ISUPPER(*e)) { |
| 1177 | *l++ = TOLOWER(*e++); |
| 1178 | } |
| 1179 | else if (*e == '_') { |
| 1180 | *l++ = '-'; |
| 1181 | e++; |
| 1182 | } |
| 1183 | else { |
| 1184 | *l++ = *e++; |
| 1185 | } |
| 1186 | } |
| 1187 | *l = '\0'; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1188 | |
| 1189 | /* Shortcuts for common default encodings */ |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1190 | if (strcmp(lower, "utf-8") == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1191 | return PyUnicode_DecodeUTF8(s, size, errors); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1192 | else if ((strcmp(lower, "latin-1") == 0) || |
| 1193 | (strcmp(lower, "iso-8859-1") == 0)) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1194 | return PyUnicode_DecodeLatin1(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1195 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1196 | else if (strcmp(lower, "mbcs") == 0) |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1197 | return PyUnicode_DecodeMBCS(s, size, errors); |
| 1198 | #endif |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1199 | else if (strcmp(lower, "ascii") == 0) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1200 | return PyUnicode_DecodeASCII(s, size, errors); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1201 | else if (strcmp(lower, "utf-16") == 0) |
| 1202 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 1203 | else if (strcmp(lower, "utf-32") == 0) |
| 1204 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1205 | |
| 1206 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 1207 | buffer = NULL; |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 1208 | 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] | 1209 | goto onError; |
Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 1210 | buffer = PyMemoryView_FromBuffer(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1211 | if (buffer == NULL) |
| 1212 | goto onError; |
| 1213 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 1214 | if (unicode == NULL) |
| 1215 | goto onError; |
| 1216 | if (!PyUnicode_Check(unicode)) { |
| 1217 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1218 | "decoder did not return a str object (type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1219 | Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1220 | Py_DECREF(unicode); |
| 1221 | goto onError; |
| 1222 | } |
| 1223 | Py_DECREF(buffer); |
| 1224 | return unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1225 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1226 | onError: |
| 1227 | Py_XDECREF(buffer); |
| 1228 | return NULL; |
| 1229 | } |
| 1230 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1231 | PyObject *PyUnicode_AsDecodedObject(PyObject *unicode, |
| 1232 | const char *encoding, |
| 1233 | const char *errors) |
| 1234 | { |
| 1235 | PyObject *v; |
| 1236 | |
| 1237 | if (!PyUnicode_Check(unicode)) { |
| 1238 | PyErr_BadArgument(); |
| 1239 | goto onError; |
| 1240 | } |
| 1241 | |
| 1242 | if (encoding == NULL) |
| 1243 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1244 | |
| 1245 | /* Decode via the codec registry */ |
| 1246 | v = PyCodec_Decode(unicode, encoding, errors); |
| 1247 | if (v == NULL) |
| 1248 | goto onError; |
| 1249 | return v; |
| 1250 | |
| 1251 | onError: |
| 1252 | return NULL; |
| 1253 | } |
| 1254 | |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1255 | PyObject *PyUnicode_AsDecodedUnicode(PyObject *unicode, |
| 1256 | const char *encoding, |
| 1257 | const char *errors) |
| 1258 | { |
| 1259 | PyObject *v; |
| 1260 | |
| 1261 | if (!PyUnicode_Check(unicode)) { |
| 1262 | PyErr_BadArgument(); |
| 1263 | goto onError; |
| 1264 | } |
| 1265 | |
| 1266 | if (encoding == NULL) |
| 1267 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1268 | |
| 1269 | /* Decode via the codec registry */ |
| 1270 | v = PyCodec_Decode(unicode, encoding, errors); |
| 1271 | if (v == NULL) |
| 1272 | goto onError; |
| 1273 | if (!PyUnicode_Check(v)) { |
| 1274 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1275 | "decoder did not return a str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1276 | Py_TYPE(v)->tp_name); |
| 1277 | Py_DECREF(v); |
| 1278 | goto onError; |
| 1279 | } |
| 1280 | return v; |
| 1281 | |
| 1282 | onError: |
| 1283 | return NULL; |
| 1284 | } |
| 1285 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1286 | PyObject *PyUnicode_Encode(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1287 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1288 | const char *encoding, |
| 1289 | const char *errors) |
| 1290 | { |
| 1291 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1292 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1293 | unicode = PyUnicode_FromUnicode(s, size); |
| 1294 | if (unicode == NULL) |
| 1295 | return NULL; |
| 1296 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 1297 | Py_DECREF(unicode); |
| 1298 | return v; |
| 1299 | } |
| 1300 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1301 | PyObject *PyUnicode_AsEncodedObject(PyObject *unicode, |
| 1302 | const char *encoding, |
| 1303 | const char *errors) |
| 1304 | { |
| 1305 | PyObject *v; |
| 1306 | |
| 1307 | if (!PyUnicode_Check(unicode)) { |
| 1308 | PyErr_BadArgument(); |
| 1309 | goto onError; |
| 1310 | } |
| 1311 | |
| 1312 | if (encoding == NULL) |
| 1313 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1314 | |
| 1315 | /* Encode via the codec registry */ |
| 1316 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1317 | if (v == NULL) |
| 1318 | goto onError; |
| 1319 | return v; |
| 1320 | |
| 1321 | onError: |
| 1322 | return NULL; |
| 1323 | } |
| 1324 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1325 | PyObject *PyUnicode_AsEncodedString(PyObject *unicode, |
| 1326 | const char *encoding, |
| 1327 | const char *errors) |
| 1328 | { |
| 1329 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1330 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1331 | if (!PyUnicode_Check(unicode)) { |
| 1332 | PyErr_BadArgument(); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1333 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1334 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1335 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1336 | if (encoding == NULL) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1337 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1338 | |
| 1339 | /* Shortcuts for common default encodings */ |
| 1340 | if (errors == NULL) { |
| 1341 | if (strcmp(encoding, "utf-8") == 0) |
Jeremy Hylton | 9cea41c | 2001-05-29 17:13:15 +0000 | [diff] [blame] | 1342 | return PyUnicode_AsUTF8String(unicode); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1343 | else if (strcmp(encoding, "latin-1") == 0) |
| 1344 | return PyUnicode_AsLatin1String(unicode); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1345 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
| 1346 | else if (strcmp(encoding, "mbcs") == 0) |
| 1347 | return PyUnicode_AsMBCSString(unicode); |
| 1348 | #endif |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1349 | else if (strcmp(encoding, "ascii") == 0) |
| 1350 | return PyUnicode_AsASCIIString(unicode); |
Christian Heimes | 6a27efa | 2008-10-30 21:48:26 +0000 | [diff] [blame] | 1351 | /* During bootstrap, we may need to find the encodings |
| 1352 | package, to load the file system encoding, and require the |
| 1353 | file system encoding in order to load the encodings |
| 1354 | package. |
| 1355 | |
| 1356 | Break out of this dependency by assuming that the path to |
| 1357 | the encodings module is ASCII-only. XXX could try wcstombs |
| 1358 | instead, if the file system encoding is the locale's |
| 1359 | encoding. */ |
| 1360 | else if (Py_FileSystemDefaultEncoding && |
| 1361 | strcmp(encoding, Py_FileSystemDefaultEncoding) == 0 && |
| 1362 | !PyThreadState_GET()->interp->codecs_initialized) |
| 1363 | return PyUnicode_AsASCIIString(unicode); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1364 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1365 | |
| 1366 | /* Encode via the codec registry */ |
| 1367 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1368 | if (v == NULL) |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1369 | return NULL; |
| 1370 | |
| 1371 | /* The normal path */ |
| 1372 | if (PyBytes_Check(v)) |
| 1373 | return v; |
| 1374 | |
| 1375 | /* 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] | 1376 | if (PyByteArray_Check(v)) { |
| 1377 | char msg[100]; |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1378 | PyObject *b; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1379 | PyOS_snprintf(msg, sizeof(msg), |
| 1380 | "encoder %s returned buffer instead of bytes", |
| 1381 | encoding); |
| 1382 | if (PyErr_WarnEx(PyExc_RuntimeWarning, msg, 1) < 0) { |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1383 | Py_DECREF(v); |
| 1384 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1385 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1386 | |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1387 | b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
| 1388 | Py_DECREF(v); |
| 1389 | return b; |
| 1390 | } |
| 1391 | |
| 1392 | PyErr_Format(PyExc_TypeError, |
| 1393 | "encoder did not return a bytes object (type=%.400s)", |
| 1394 | Py_TYPE(v)->tp_name); |
| 1395 | Py_DECREF(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1396 | return NULL; |
| 1397 | } |
| 1398 | |
| 1399 | PyObject *PyUnicode_AsEncodedUnicode(PyObject *unicode, |
| 1400 | const char *encoding, |
| 1401 | const char *errors) |
| 1402 | { |
| 1403 | PyObject *v; |
| 1404 | |
| 1405 | if (!PyUnicode_Check(unicode)) { |
| 1406 | PyErr_BadArgument(); |
| 1407 | goto onError; |
| 1408 | } |
| 1409 | |
| 1410 | if (encoding == NULL) |
| 1411 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1412 | |
| 1413 | /* Encode via the codec registry */ |
| 1414 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1415 | if (v == NULL) |
| 1416 | goto onError; |
| 1417 | if (!PyUnicode_Check(v)) { |
| 1418 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1419 | "encoder did not return an str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1420 | Py_TYPE(v)->tp_name); |
| 1421 | Py_DECREF(v); |
| 1422 | goto onError; |
| 1423 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1424 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1425 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1426 | onError: |
| 1427 | return NULL; |
| 1428 | } |
| 1429 | |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1430 | PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode, |
| 1431 | const char *errors) |
| 1432 | { |
| 1433 | PyObject *v = ((PyUnicodeObject *)unicode)->defenc; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1434 | if (v) |
| 1435 | return v; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1436 | if (errors != NULL) |
| 1437 | Py_FatalError("non-NULL encoding in _PyUnicode_AsDefaultEncodedString"); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1438 | v = PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
Guido van Rossum | 0661009 | 2007-08-16 21:02:22 +0000 | [diff] [blame] | 1439 | PyUnicode_GET_SIZE(unicode), |
| 1440 | NULL); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1441 | if (!v) |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1442 | return NULL; |
Guido van Rossum | e7a0d39 | 2007-07-12 07:53:00 +0000 | [diff] [blame] | 1443 | ((PyUnicodeObject *)unicode)->defenc = v; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1444 | return v; |
| 1445 | } |
| 1446 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1447 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1448 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1449 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1450 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 1451 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1452 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1453 | PyObject* |
| 1454 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 1455 | { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1456 | /* During the early bootstrapping process, Py_FileSystemDefaultEncoding |
| 1457 | can be undefined. If it is case, decode using UTF-8. The following assumes |
| 1458 | that Py_FileSystemDefaultEncoding is set to a built-in encoding during the |
| 1459 | bootstrapping process where the codecs aren't ready yet. |
| 1460 | */ |
| 1461 | if (Py_FileSystemDefaultEncoding) { |
| 1462 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1463 | if (strcmp(Py_FileSystemDefaultEncoding, "mbcs") == 0) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1464 | return PyUnicode_DecodeMBCS(s, size, "replace"); |
| 1465 | } |
| 1466 | #elif defined(__APPLE__) |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1467 | if (strcmp(Py_FileSystemDefaultEncoding, "utf-8") == 0) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1468 | return PyUnicode_DecodeUTF8(s, size, "replace"); |
| 1469 | } |
| 1470 | #endif |
| 1471 | return PyUnicode_Decode(s, size, |
| 1472 | Py_FileSystemDefaultEncoding, |
| 1473 | "replace"); |
| 1474 | } |
| 1475 | else { |
| 1476 | return PyUnicode_DecodeUTF8(s, size, "replace"); |
| 1477 | } |
| 1478 | } |
| 1479 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1480 | char* |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1481 | _PyUnicode_AsStringAndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1482 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 1483 | PyObject *bytes; |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 1484 | if (!PyUnicode_Check(unicode)) { |
| 1485 | PyErr_BadArgument(); |
| 1486 | return NULL; |
| 1487 | } |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 1488 | bytes = _PyUnicode_AsDefaultEncodedString(unicode, NULL); |
| 1489 | if (bytes == NULL) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1490 | return NULL; |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1491 | if (psize != NULL) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1492 | *psize = PyBytes_GET_SIZE(bytes); |
| 1493 | return PyBytes_AS_STRING(bytes); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1494 | } |
| 1495 | |
| 1496 | char* |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1497 | _PyUnicode_AsString(PyObject *unicode) |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1498 | { |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1499 | return _PyUnicode_AsStringAndSize(unicode, NULL); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1500 | } |
| 1501 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1502 | Py_UNICODE *PyUnicode_AsUnicode(PyObject *unicode) |
| 1503 | { |
| 1504 | if (!PyUnicode_Check(unicode)) { |
| 1505 | PyErr_BadArgument(); |
| 1506 | goto onError; |
| 1507 | } |
| 1508 | return PyUnicode_AS_UNICODE(unicode); |
| 1509 | |
| 1510 | onError: |
| 1511 | return NULL; |
| 1512 | } |
| 1513 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1514 | Py_ssize_t PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1515 | { |
| 1516 | if (!PyUnicode_Check(unicode)) { |
| 1517 | PyErr_BadArgument(); |
| 1518 | goto onError; |
| 1519 | } |
| 1520 | return PyUnicode_GET_SIZE(unicode); |
| 1521 | |
| 1522 | onError: |
| 1523 | return -1; |
| 1524 | } |
| 1525 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 1526 | const char *PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1527 | { |
| 1528 | return unicode_default_encoding; |
| 1529 | } |
| 1530 | |
| 1531 | int PyUnicode_SetDefaultEncoding(const char *encoding) |
| 1532 | { |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1533 | if (strcmp(encoding, unicode_default_encoding) != 0) { |
| 1534 | PyErr_Format(PyExc_ValueError, |
| 1535 | "Can only set default encoding to %s", |
| 1536 | unicode_default_encoding); |
| 1537 | return -1; |
| 1538 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1539 | return 0; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1540 | } |
| 1541 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1542 | /* error handling callback helper: |
| 1543 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 1544 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1545 | and adjust various state variables. |
| 1546 | return 0 on success, -1 on error |
| 1547 | */ |
| 1548 | |
| 1549 | static |
| 1550 | int unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
| 1551 | const char *encoding, const char *reason, |
Walter Dörwald | a651d3d | 2007-08-30 15:29:21 +0000 | [diff] [blame] | 1552 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 1553 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1554 | PyUnicodeObject **output, Py_ssize_t *outpos, Py_UNICODE **outptr) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1555 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1556 | 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] | 1557 | |
| 1558 | PyObject *restuple = NULL; |
| 1559 | PyObject *repunicode = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1560 | Py_ssize_t outsize = PyUnicode_GET_SIZE(*output); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1561 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1562 | Py_ssize_t requiredsize; |
| 1563 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1564 | Py_UNICODE *repptr; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1565 | PyObject *inputobj = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1566 | Py_ssize_t repsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1567 | int res = -1; |
| 1568 | |
| 1569 | if (*errorHandler == NULL) { |
| 1570 | *errorHandler = PyCodec_LookupError(errors); |
| 1571 | if (*errorHandler == NULL) |
| 1572 | goto onError; |
| 1573 | } |
| 1574 | |
| 1575 | if (*exceptionObject == NULL) { |
| 1576 | *exceptionObject = PyUnicodeDecodeError_Create( |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1577 | encoding, *input, *inend-*input, *startinpos, *endinpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1578 | if (*exceptionObject == NULL) |
| 1579 | goto onError; |
| 1580 | } |
| 1581 | else { |
| 1582 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, *startinpos)) |
| 1583 | goto onError; |
| 1584 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, *endinpos)) |
| 1585 | goto onError; |
| 1586 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 1587 | goto onError; |
| 1588 | } |
| 1589 | |
| 1590 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 1591 | if (restuple == NULL) |
| 1592 | goto onError; |
| 1593 | if (!PyTuple_Check(restuple)) { |
| 1594 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 1595 | goto onError; |
| 1596 | } |
| 1597 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
| 1598 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1599 | |
| 1600 | /* Copy back the bytes variables, which might have been modified by the |
| 1601 | callback */ |
| 1602 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 1603 | if (!inputobj) |
| 1604 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1605 | if (!PyBytes_Check(inputobj)) { |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1606 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
| 1607 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1608 | *input = PyBytes_AS_STRING(inputobj); |
| 1609 | insize = PyBytes_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1610 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 1611 | /* we can DECREF safely, as the exception has another reference, |
| 1612 | so the object won't go away. */ |
| 1613 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1614 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1615 | if (newpos<0) |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 1616 | newpos = insize+newpos; |
| 1617 | if (newpos<0 || newpos>insize) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 1618 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 1619 | goto onError; |
| 1620 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1621 | |
| 1622 | /* need more space? (at least enough for what we |
| 1623 | have+the replacement+the rest of the string (starting |
| 1624 | at the new input position), so we won't have to check space |
| 1625 | when there are no errors in the rest of the string) */ |
| 1626 | repptr = PyUnicode_AS_UNICODE(repunicode); |
| 1627 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 1628 | requiredsize = *outpos + repsize + insize-newpos; |
| 1629 | if (requiredsize > outsize) { |
| 1630 | if (requiredsize<2*outsize) |
| 1631 | requiredsize = 2*outsize; |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1632 | if (_PyUnicode_Resize(output, requiredsize) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1633 | goto onError; |
| 1634 | *outptr = PyUnicode_AS_UNICODE(*output) + *outpos; |
| 1635 | } |
| 1636 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1637 | *inptr = *input + newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1638 | Py_UNICODE_COPY(*outptr, repptr, repsize); |
| 1639 | *outptr += repsize; |
| 1640 | *outpos += repsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1641 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1642 | /* we made it! */ |
| 1643 | res = 0; |
| 1644 | |
| 1645 | onError: |
| 1646 | Py_XDECREF(restuple); |
| 1647 | return res; |
| 1648 | } |
| 1649 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1650 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 1651 | |
| 1652 | /* see RFC2152 for details */ |
| 1653 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1654 | static |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1655 | char utf7_special[128] = { |
| 1656 | /* indicate whether a UTF-7 character is special i.e. cannot be directly |
| 1657 | encoded: |
| 1658 | 0 - not special |
| 1659 | 1 - special |
| 1660 | 2 - whitespace (optional) |
| 1661 | 3 - RFC2152 Set O (optional) */ |
| 1662 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 2, 1, 1, |
| 1663 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1664 | 2, 3, 3, 3, 3, 3, 3, 0, 0, 0, 3, 1, 0, 0, 0, 1, |
| 1665 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 0, |
| 1666 | 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1667 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 3, 3, 3, |
| 1668 | 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1669 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 1, 1, |
| 1670 | |
| 1671 | }; |
| 1672 | |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1673 | /* Note: The comparison (c) <= 0 is a trick to work-around gcc |
| 1674 | warnings about the comparison always being false; since |
| 1675 | utf7_special[0] is 1, we can safely make that one comparison |
| 1676 | true */ |
| 1677 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1678 | #define SPECIAL(c, encodeO, encodeWS) \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1679 | ((c) > 127 || (c) <= 0 || utf7_special[(c)] == 1 || \ |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1680 | (encodeWS && (utf7_special[(c)] == 2)) || \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1681 | (encodeO && (utf7_special[(c)] == 3))) |
| 1682 | |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1683 | #define B64(n) \ |
| 1684 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 1685 | #define B64CHAR(c) \ |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1686 | (ISALNUM(c) || (c) == '+' || (c) == '/') |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1687 | #define UB64(c) \ |
| 1688 | ((c) == '+' ? 62 : (c) == '/' ? 63 : (c) >= 'a' ? \ |
| 1689 | (c) - 71 : (c) >= 'A' ? (c) - 65 : (c) + 4 ) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1690 | |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1691 | #define ENCODE(out, ch, bits) \ |
| 1692 | while (bits >= 6) { \ |
| 1693 | *out++ = B64(ch >> (bits-6)); \ |
| 1694 | bits -= 6; \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1695 | } |
| 1696 | |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1697 | #define DECODE(out, ch, bits, surrogate) \ |
| 1698 | while (bits >= 16) { \ |
| 1699 | Py_UNICODE outCh = (Py_UNICODE) ((ch >> (bits-16)) & 0xffff); \ |
| 1700 | bits -= 16; \ |
| 1701 | if (surrogate) { \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1702 | /* We have already generated an error for the high surrogate \ |
| 1703 | so let's not bother seeing if the low surrogate is correct or not */ \ |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1704 | surrogate = 0; \ |
| 1705 | } else if (0xDC00 <= outCh && outCh <= 0xDFFF) { \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1706 | /* This is a surrogate pair. Unfortunately we can't represent \ |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1707 | it in a 16-bit character */ \ |
| 1708 | surrogate = 1; \ |
| 1709 | errmsg = "code pairs are not supported"; \ |
| 1710 | goto utf7Error; \ |
| 1711 | } else { \ |
| 1712 | *out++ = outCh; \ |
| 1713 | } \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1714 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1715 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1716 | PyObject *PyUnicode_DecodeUTF7(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1717 | Py_ssize_t size, |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1718 | const char *errors) |
| 1719 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 1720 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 1721 | } |
| 1722 | |
| 1723 | PyObject *PyUnicode_DecodeUTF7Stateful(const char *s, |
| 1724 | Py_ssize_t size, |
| 1725 | const char *errors, |
| 1726 | Py_ssize_t *consumed) |
| 1727 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1728 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1729 | Py_ssize_t startinpos; |
| 1730 | Py_ssize_t endinpos; |
| 1731 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1732 | const char *e; |
| 1733 | PyUnicodeObject *unicode; |
| 1734 | Py_UNICODE *p; |
| 1735 | const char *errmsg = ""; |
| 1736 | int inShift = 0; |
| 1737 | unsigned int bitsleft = 0; |
| 1738 | unsigned long charsleft = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1739 | int surrogate = 0; |
| 1740 | PyObject *errorHandler = NULL; |
| 1741 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1742 | |
| 1743 | unicode = _PyUnicode_New(size); |
| 1744 | if (!unicode) |
| 1745 | return NULL; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 1746 | if (size == 0) { |
| 1747 | if (consumed) |
| 1748 | *consumed = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1749 | return (PyObject *)unicode; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 1750 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1751 | |
| 1752 | p = unicode->str; |
| 1753 | e = s + size; |
| 1754 | |
| 1755 | while (s < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1756 | Py_UNICODE ch; |
| 1757 | restart: |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 1758 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1759 | |
| 1760 | if (inShift) { |
| 1761 | if ((ch == '-') || !B64CHAR(ch)) { |
| 1762 | inShift = 0; |
| 1763 | s++; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1764 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1765 | /* p, charsleft, bitsleft, surrogate = */ DECODE(p, charsleft, bitsleft, surrogate); |
| 1766 | if (bitsleft >= 6) { |
| 1767 | /* The shift sequence has a partial character in it. If |
| 1768 | bitsleft < 6 then we could just classify it as padding |
| 1769 | but that is not the case here */ |
| 1770 | |
| 1771 | errmsg = "partial character in shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1772 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1773 | } |
| 1774 | /* According to RFC2152 the remaining bits should be zero. We |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1775 | choose to signal an error/insert a replacement character |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1776 | here so indicate the potential of a misencoded character. */ |
| 1777 | |
| 1778 | /* On x86, a << b == a << (b%32) so make sure that bitsleft != 0 */ |
| 1779 | if (bitsleft && charsleft << (sizeof(charsleft) * 8 - bitsleft)) { |
| 1780 | errmsg = "non-zero padding bits in shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1781 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1782 | } |
| 1783 | |
| 1784 | if (ch == '-') { |
| 1785 | if ((s < e) && (*(s) == '-')) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1786 | *p++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1787 | inShift = 1; |
| 1788 | } |
| 1789 | } else if (SPECIAL(ch,0,0)) { |
| 1790 | errmsg = "unexpected special character"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1791 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1792 | } else { |
| 1793 | *p++ = ch; |
| 1794 | } |
| 1795 | } else { |
| 1796 | charsleft = (charsleft << 6) | UB64(ch); |
| 1797 | bitsleft += 6; |
| 1798 | s++; |
| 1799 | /* p, charsleft, bitsleft, surrogate = */ DECODE(p, charsleft, bitsleft, surrogate); |
| 1800 | } |
| 1801 | } |
| 1802 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1803 | startinpos = s-starts; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1804 | s++; |
| 1805 | if (s < e && *s == '-') { |
| 1806 | s++; |
| 1807 | *p++ = '+'; |
| 1808 | } else |
| 1809 | { |
| 1810 | inShift = 1; |
| 1811 | bitsleft = 0; |
| 1812 | } |
| 1813 | } |
| 1814 | else if (SPECIAL(ch,0,0)) { |
Walter Dörwald | 2b65c75 | 2007-08-30 15:35:26 +0000 | [diff] [blame] | 1815 | startinpos = s-starts; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1816 | errmsg = "unexpected special character"; |
| 1817 | s++; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1818 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1819 | } |
| 1820 | else { |
| 1821 | *p++ = ch; |
| 1822 | s++; |
| 1823 | } |
| 1824 | continue; |
| 1825 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1826 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1827 | endinpos = s-starts; |
| 1828 | if (unicode_decode_call_errorhandler( |
| 1829 | errors, &errorHandler, |
| 1830 | "utf7", errmsg, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1831 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1832 | &unicode, &outpos, &p)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1833 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1834 | } |
| 1835 | |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 1836 | if (inShift && !consumed) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1837 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1838 | endinpos = size; |
| 1839 | if (unicode_decode_call_errorhandler( |
| 1840 | errors, &errorHandler, |
| 1841 | "utf7", "unterminated shift sequence", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1842 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1843 | &unicode, &outpos, &p)) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1844 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1845 | if (s < e) |
| 1846 | goto restart; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1847 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 1848 | if (consumed) { |
| 1849 | if(inShift) |
| 1850 | *consumed = startinpos; |
| 1851 | else |
| 1852 | *consumed = s-starts; |
| 1853 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1854 | |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1855 | if (_PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1856 | goto onError; |
| 1857 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1858 | Py_XDECREF(errorHandler); |
| 1859 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1860 | return (PyObject *)unicode; |
| 1861 | |
| 1862 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1863 | Py_XDECREF(errorHandler); |
| 1864 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1865 | Py_DECREF(unicode); |
| 1866 | return NULL; |
| 1867 | } |
| 1868 | |
| 1869 | |
| 1870 | PyObject *PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1871 | Py_ssize_t size, |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1872 | int encodeSetO, |
| 1873 | int encodeWhiteSpace, |
| 1874 | const char *errors) |
| 1875 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 1876 | PyObject *v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1877 | /* It might be possible to tighten this worst case */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1878 | Py_ssize_t cbAllocated = 5 * size; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1879 | int inShift = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1880 | Py_ssize_t i = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1881 | unsigned int bitsleft = 0; |
| 1882 | unsigned long charsleft = 0; |
| 1883 | char * out; |
| 1884 | char * start; |
| 1885 | |
| 1886 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1887 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1888 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 1889 | if (cbAllocated / 5 != size) |
| 1890 | return PyErr_NoMemory(); |
| 1891 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 1892 | v = PyBytes_FromStringAndSize(NULL, cbAllocated); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1893 | if (v == NULL) |
| 1894 | return NULL; |
| 1895 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 1896 | start = out = PyBytes_AS_STRING(v); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1897 | for (;i < size; ++i) { |
| 1898 | Py_UNICODE ch = s[i]; |
| 1899 | |
| 1900 | if (!inShift) { |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1901 | if (ch == '+') { |
| 1902 | *out++ = '+'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1903 | *out++ = '-'; |
| 1904 | } else if (SPECIAL(ch, encodeSetO, encodeWhiteSpace)) { |
| 1905 | charsleft = ch; |
| 1906 | bitsleft = 16; |
| 1907 | *out++ = '+'; |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1908 | /* out, charsleft, bitsleft = */ ENCODE(out, charsleft, bitsleft); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1909 | inShift = bitsleft > 0; |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1910 | } else { |
| 1911 | *out++ = (char) ch; |
| 1912 | } |
| 1913 | } else { |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1914 | if (!SPECIAL(ch, encodeSetO, encodeWhiteSpace)) { |
| 1915 | *out++ = B64(charsleft << (6-bitsleft)); |
| 1916 | charsleft = 0; |
| 1917 | bitsleft = 0; |
| 1918 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 1919 | so no '-' is required, except if the character is itself a '-' */ |
| 1920 | if (B64CHAR(ch) || ch == '-') { |
| 1921 | *out++ = '-'; |
| 1922 | } |
| 1923 | inShift = 0; |
| 1924 | *out++ = (char) ch; |
| 1925 | } else { |
| 1926 | bitsleft += 16; |
| 1927 | charsleft = (charsleft << 16) | ch; |
| 1928 | /* out, charsleft, bitsleft = */ ENCODE(out, charsleft, bitsleft); |
| 1929 | |
| 1930 | /* If the next character is special then we dont' need to terminate |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1931 | the shift sequence. If the next character is not a BASE64 character |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1932 | or '-' then the shift sequence will be terminated implicitly and we |
| 1933 | don't have to insert a '-'. */ |
| 1934 | |
| 1935 | if (bitsleft == 0) { |
| 1936 | if (i + 1 < size) { |
| 1937 | Py_UNICODE ch2 = s[i+1]; |
| 1938 | |
| 1939 | if (SPECIAL(ch2, encodeSetO, encodeWhiteSpace)) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1940 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1941 | } else if (B64CHAR(ch2) || ch2 == '-') { |
| 1942 | *out++ = '-'; |
| 1943 | inShift = 0; |
| 1944 | } else { |
| 1945 | inShift = 0; |
| 1946 | } |
| 1947 | |
| 1948 | } |
| 1949 | else { |
| 1950 | *out++ = '-'; |
| 1951 | inShift = 0; |
| 1952 | } |
| 1953 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1954 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1955 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1956 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1957 | if (bitsleft) { |
| 1958 | *out++= B64(charsleft << (6-bitsleft) ); |
| 1959 | *out++ = '-'; |
| 1960 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 1961 | if (_PyBytes_Resize(&v, out - start) < 0) |
| 1962 | return NULL; |
| 1963 | return v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1964 | } |
| 1965 | |
| 1966 | #undef SPECIAL |
| 1967 | #undef B64 |
| 1968 | #undef B64CHAR |
| 1969 | #undef UB64 |
| 1970 | #undef ENCODE |
| 1971 | #undef DECODE |
| 1972 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1973 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 1974 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1975 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1976 | char utf8_code_length[256] = { |
| 1977 | /* Map UTF-8 encoded prefix byte to sequence length. zero means |
| 1978 | illegal prefix. see RFC 2279 for details */ |
| 1979 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1980 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1981 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1982 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1983 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1984 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1985 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1986 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1987 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1988 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1989 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1990 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1991 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 1992 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 1993 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 1994 | 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 0, 0 |
| 1995 | }; |
| 1996 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1997 | PyObject *PyUnicode_DecodeUTF8(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1998 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1999 | const char *errors) |
| 2000 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2001 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 2002 | } |
| 2003 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2004 | /* Mask to check or force alignment of a pointer to C 'long' boundaries */ |
| 2005 | #define LONG_PTR_MASK (size_t) (SIZEOF_LONG - 1) |
| 2006 | |
| 2007 | /* Mask to quickly check whether a C 'long' contains a |
| 2008 | non-ASCII, UTF8-encoded char. */ |
| 2009 | #if (SIZEOF_LONG == 8) |
| 2010 | # define ASCII_CHAR_MASK 0x8080808080808080L |
| 2011 | #elif (SIZEOF_LONG == 4) |
| 2012 | # define ASCII_CHAR_MASK 0x80808080L |
| 2013 | #else |
| 2014 | # error C 'long' size should be either 4 or 8! |
| 2015 | #endif |
| 2016 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2017 | PyObject *PyUnicode_DecodeUTF8Stateful(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2018 | Py_ssize_t size, |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2019 | const char *errors, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2020 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2021 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2022 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2023 | int n; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2024 | Py_ssize_t startinpos; |
| 2025 | Py_ssize_t endinpos; |
| 2026 | Py_ssize_t outpos; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2027 | const char *e, *aligned_end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2028 | PyUnicodeObject *unicode; |
| 2029 | Py_UNICODE *p; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2030 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2031 | PyObject *errorHandler = NULL; |
| 2032 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2033 | |
| 2034 | /* Note: size will always be longer than the resulting Unicode |
| 2035 | character count */ |
| 2036 | unicode = _PyUnicode_New(size); |
| 2037 | if (!unicode) |
| 2038 | return NULL; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2039 | if (size == 0) { |
| 2040 | if (consumed) |
| 2041 | *consumed = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2042 | return (PyObject *)unicode; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2043 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2044 | |
| 2045 | /* Unpack UTF-8 encoded data */ |
| 2046 | p = unicode->str; |
| 2047 | e = s + size; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2048 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2049 | |
| 2050 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2051 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2052 | |
| 2053 | if (ch < 0x80) { |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2054 | /* Fast path for runs of ASCII characters. Given that common UTF-8 |
| 2055 | input will consist of an overwhelming majority of ASCII |
| 2056 | characters, we try to optimize for this case by checking |
| 2057 | as many characters as a C 'long' can contain. |
| 2058 | First, check if we can do an aligned read, as most CPUs have |
| 2059 | a penalty for unaligned reads. |
| 2060 | */ |
| 2061 | if (!((size_t) s & LONG_PTR_MASK)) { |
| 2062 | /* Help register allocation */ |
| 2063 | register const char *_s = s; |
| 2064 | register Py_UNICODE *_p = p; |
| 2065 | while (_s < aligned_end) { |
| 2066 | /* Read a whole long at a time (either 4 or 8 bytes), |
| 2067 | and do a fast unrolled copy if it only contains ASCII |
| 2068 | characters. */ |
| 2069 | unsigned long data = *(unsigned long *) _s; |
| 2070 | if (data & ASCII_CHAR_MASK) |
| 2071 | break; |
| 2072 | _p[0] = (unsigned char) _s[0]; |
| 2073 | _p[1] = (unsigned char) _s[1]; |
| 2074 | _p[2] = (unsigned char) _s[2]; |
| 2075 | _p[3] = (unsigned char) _s[3]; |
| 2076 | #if (SIZEOF_LONG == 8) |
| 2077 | _p[4] = (unsigned char) _s[4]; |
| 2078 | _p[5] = (unsigned char) _s[5]; |
| 2079 | _p[6] = (unsigned char) _s[6]; |
| 2080 | _p[7] = (unsigned char) _s[7]; |
| 2081 | #endif |
| 2082 | _s += SIZEOF_LONG; |
| 2083 | _p += SIZEOF_LONG; |
| 2084 | } |
| 2085 | s = _s; |
| 2086 | p = _p; |
| 2087 | if (s == e) |
| 2088 | break; |
| 2089 | ch = (unsigned char)*s; |
| 2090 | } |
| 2091 | } |
| 2092 | |
| 2093 | if (ch < 0x80) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2094 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2095 | s++; |
| 2096 | continue; |
| 2097 | } |
| 2098 | |
| 2099 | n = utf8_code_length[ch]; |
| 2100 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2101 | if (s + n > e) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2102 | if (consumed) |
| 2103 | break; |
| 2104 | else { |
| 2105 | errmsg = "unexpected end of data"; |
| 2106 | startinpos = s-starts; |
| 2107 | endinpos = size; |
| 2108 | goto utf8Error; |
| 2109 | } |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2110 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2111 | |
| 2112 | switch (n) { |
| 2113 | |
| 2114 | case 0: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2115 | errmsg = "unexpected code byte"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2116 | startinpos = s-starts; |
| 2117 | endinpos = startinpos+1; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2118 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2119 | |
| 2120 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2121 | errmsg = "internal error"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2122 | startinpos = s-starts; |
| 2123 | endinpos = startinpos+1; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2124 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2125 | |
| 2126 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2127 | if ((s[1] & 0xc0) != 0x80) { |
| 2128 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2129 | startinpos = s-starts; |
| 2130 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2131 | goto utf8Error; |
| 2132 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2133 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2134 | if (ch < 0x80) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2135 | startinpos = s-starts; |
| 2136 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2137 | errmsg = "illegal encoding"; |
| 2138 | goto utf8Error; |
| 2139 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2140 | else |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2141 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2142 | break; |
| 2143 | |
| 2144 | case 3: |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2145 | if ((s[1] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2146 | (s[2] & 0xc0) != 0x80) { |
| 2147 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2148 | startinpos = s-starts; |
| 2149 | endinpos = startinpos+3; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2150 | goto utf8Error; |
| 2151 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2152 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 2153 | if (ch < 0x0800) { |
| 2154 | /* Note: UTF-8 encodings of surrogates are considered |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2155 | legal UTF-8 sequences; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 2156 | |
| 2157 | XXX For wide builds (UCS-4) we should probably try |
| 2158 | to recombine the surrogates into a single code |
| 2159 | unit. |
| 2160 | */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2161 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2162 | startinpos = s-starts; |
| 2163 | endinpos = startinpos+3; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2164 | goto utf8Error; |
| 2165 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2166 | else |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 2167 | *p++ = (Py_UNICODE)ch; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2168 | break; |
| 2169 | |
| 2170 | case 4: |
| 2171 | if ((s[1] & 0xc0) != 0x80 || |
| 2172 | (s[2] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2173 | (s[3] & 0xc0) != 0x80) { |
| 2174 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2175 | startinpos = s-starts; |
| 2176 | endinpos = startinpos+4; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2177 | goto utf8Error; |
| 2178 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2179 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
| 2180 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 2181 | /* validate and convert to UTF-16 */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2182 | if ((ch < 0x10000) /* minimum value allowed for 4 |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 2183 | byte encoding */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2184 | || (ch > 0x10ffff)) /* maximum value allowed for |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 2185 | UTF-16 */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2186 | { |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2187 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2188 | startinpos = s-starts; |
| 2189 | endinpos = startinpos+4; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2190 | goto utf8Error; |
| 2191 | } |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 2192 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2193 | *p++ = (Py_UNICODE)ch; |
| 2194 | #else |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2195 | /* compute and append the two surrogates: */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2196 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2197 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 2198 | ch -= 0x10000; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2199 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2200 | /* high surrogate = top 10 bits added to D800 */ |
| 2201 | *p++ = (Py_UNICODE)(0xD800 + (ch >> 10)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2202 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2203 | /* low surrogate = bottom 10 bits added to DC00 */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 2204 | *p++ = (Py_UNICODE)(0xDC00 + (ch & 0x03FF)); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2205 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2206 | break; |
| 2207 | |
| 2208 | default: |
| 2209 | /* Other sizes are only needed for UCS-4 */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2210 | errmsg = "unsupported Unicode code range"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2211 | startinpos = s-starts; |
| 2212 | endinpos = startinpos+n; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2213 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2214 | } |
| 2215 | s += n; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2216 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2217 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2218 | utf8Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2219 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2220 | if (unicode_decode_call_errorhandler( |
| 2221 | errors, &errorHandler, |
| 2222 | "utf8", errmsg, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2223 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 2224 | &unicode, &outpos, &p)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2225 | goto onError; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2226 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2227 | } |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2228 | if (consumed) |
| 2229 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2230 | |
| 2231 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2232 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2233 | goto onError; |
| 2234 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2235 | Py_XDECREF(errorHandler); |
| 2236 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2237 | return (PyObject *)unicode; |
| 2238 | |
| 2239 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2240 | Py_XDECREF(errorHandler); |
| 2241 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2242 | Py_DECREF(unicode); |
| 2243 | return NULL; |
| 2244 | } |
| 2245 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2246 | #undef ASCII_CHAR_MASK |
| 2247 | |
| 2248 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2249 | /* Allocation strategy: if the string is short, convert into a stack buffer |
| 2250 | and allocate exactly as much space needed at the end. Else allocate the |
| 2251 | maximum possible needed (4 result bytes per Unicode character), and return |
| 2252 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2253 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 2254 | PyObject * |
| 2255 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2256 | Py_ssize_t size, |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 2257 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2258 | { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2259 | #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] | 2260 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2261 | Py_ssize_t i; /* index into s of next input byte */ |
| 2262 | PyObject *result; /* result string object */ |
| 2263 | char *p; /* next free byte in output buffer */ |
| 2264 | Py_ssize_t nallocated; /* number of result bytes allocated */ |
| 2265 | Py_ssize_t nneeded; /* number of result bytes needed */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2266 | char stackbuf[MAX_SHORT_UNICHARS * 4]; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 2267 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2268 | assert(s != NULL); |
| 2269 | assert(size >= 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2270 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2271 | if (size <= MAX_SHORT_UNICHARS) { |
| 2272 | /* Write into the stack buffer; nallocated can't overflow. |
| 2273 | * At the end, we'll allocate exactly as much heap space as it |
| 2274 | * turns out we need. |
| 2275 | */ |
| 2276 | nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2277 | result = NULL; /* will allocate after we're done */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2278 | p = stackbuf; |
| 2279 | } |
| 2280 | else { |
| 2281 | /* Overallocate on the heap, and give the excess back at the end. */ |
| 2282 | nallocated = size * 4; |
| 2283 | if (nallocated / 4 != size) /* overflow! */ |
| 2284 | return PyErr_NoMemory(); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2285 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2286 | if (result == NULL) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2287 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2288 | p = PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2289 | } |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2290 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2291 | for (i = 0; i < size;) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2292 | Py_UCS4 ch = s[i++]; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2293 | |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2294 | if (ch < 0x80) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2295 | /* Encode ASCII */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2296 | *p++ = (char) ch; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2297 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2298 | else if (ch < 0x0800) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2299 | /* Encode Latin-1 */ |
Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 2300 | *p++ = (char)(0xc0 | (ch >> 6)); |
| 2301 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2302 | } |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2303 | else { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2304 | /* Encode UCS2 Unicode ordinals */ |
| 2305 | if (ch < 0x10000) { |
| 2306 | /* Special case: check for high surrogate */ |
| 2307 | if (0xD800 <= ch && ch <= 0xDBFF && i != size) { |
| 2308 | Py_UCS4 ch2 = s[i]; |
| 2309 | /* Check for low surrogate and combine the two to |
| 2310 | form a UCS4 value */ |
| 2311 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2312 | ch = ((ch - 0xD800) << 10 | (ch2 - 0xDC00)) + 0x10000; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2313 | i++; |
| 2314 | goto encodeUCS4; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2315 | } |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2316 | /* Fall through: handles isolated high surrogates */ |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2317 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2318 | *p++ = (char)(0xe0 | (ch >> 12)); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2319 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 2320 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 2321 | continue; |
| 2322 | } |
| 2323 | encodeUCS4: |
| 2324 | /* Encode UCS4 Unicode ordinals */ |
| 2325 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 2326 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
| 2327 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 2328 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 2329 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2330 | } |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 2331 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2332 | if (result == NULL) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2333 | /* This was stack allocated. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2334 | nneeded = p - stackbuf; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2335 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2336 | result = PyBytes_FromStringAndSize(stackbuf, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2337 | } |
| 2338 | else { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 2339 | /* Cut back to size actually needed. */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2340 | nneeded = p - PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2341 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2342 | _PyBytes_Resize(&result, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2343 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2344 | return result; |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2345 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2346 | #undef MAX_SHORT_UNICHARS |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2347 | } |
| 2348 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2349 | PyObject *PyUnicode_AsUTF8String(PyObject *unicode) |
| 2350 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2351 | if (!PyUnicode_Check(unicode)) { |
| 2352 | PyErr_BadArgument(); |
| 2353 | return NULL; |
| 2354 | } |
Barry Warsaw | 2dd4abf | 2000-08-18 06:58:15 +0000 | [diff] [blame] | 2355 | return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
| 2356 | PyUnicode_GET_SIZE(unicode), |
| 2357 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2358 | } |
| 2359 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2360 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 2361 | |
| 2362 | PyObject * |
| 2363 | PyUnicode_DecodeUTF32(const char *s, |
| 2364 | Py_ssize_t size, |
| 2365 | const char *errors, |
| 2366 | int *byteorder) |
| 2367 | { |
| 2368 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 2369 | } |
| 2370 | |
| 2371 | PyObject * |
| 2372 | PyUnicode_DecodeUTF32Stateful(const char *s, |
| 2373 | Py_ssize_t size, |
| 2374 | const char *errors, |
| 2375 | int *byteorder, |
| 2376 | Py_ssize_t *consumed) |
| 2377 | { |
| 2378 | const char *starts = s; |
| 2379 | Py_ssize_t startinpos; |
| 2380 | Py_ssize_t endinpos; |
| 2381 | Py_ssize_t outpos; |
| 2382 | PyUnicodeObject *unicode; |
| 2383 | Py_UNICODE *p; |
| 2384 | #ifndef Py_UNICODE_WIDE |
| 2385 | int i, pairs; |
| 2386 | #else |
| 2387 | const int pairs = 0; |
| 2388 | #endif |
| 2389 | const unsigned char *q, *e; |
| 2390 | int bo = 0; /* assume native ordering by default */ |
| 2391 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2392 | /* Offsets from q for retrieving bytes in the right order. */ |
| 2393 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2394 | int iorder[] = {0, 1, 2, 3}; |
| 2395 | #else |
| 2396 | int iorder[] = {3, 2, 1, 0}; |
| 2397 | #endif |
| 2398 | PyObject *errorHandler = NULL; |
| 2399 | PyObject *exc = NULL; |
Guido van Rossum | 8d991ed | 2007-08-17 15:41:00 +0000 | [diff] [blame] | 2400 | /* On narrow builds we split characters outside the BMP into two |
| 2401 | codepoints => count how much extra space we need. */ |
| 2402 | #ifndef Py_UNICODE_WIDE |
| 2403 | for (i = pairs = 0; i < size/4; i++) |
| 2404 | if (((Py_UCS4 *)s)[i] >= 0x10000) |
| 2405 | pairs++; |
| 2406 | #endif |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2407 | |
| 2408 | /* This might be one to much, because of a BOM */ |
| 2409 | unicode = _PyUnicode_New((size+3)/4+pairs); |
| 2410 | if (!unicode) |
| 2411 | return NULL; |
| 2412 | if (size == 0) |
| 2413 | return (PyObject *)unicode; |
| 2414 | |
| 2415 | /* Unpack UTF-32 encoded data */ |
| 2416 | p = unicode->str; |
| 2417 | q = (unsigned char *)s; |
| 2418 | e = q + size; |
| 2419 | |
| 2420 | if (byteorder) |
| 2421 | bo = *byteorder; |
| 2422 | |
| 2423 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 2424 | byte order setting accordingly. In native mode, the leading BOM |
| 2425 | mark is skipped, in all other modes, it is copied to the output |
| 2426 | stream as-is (giving a ZWNBSP character). */ |
| 2427 | if (bo == 0) { |
| 2428 | if (size >= 4) { |
| 2429 | const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 2430 | (q[iorder[1]] << 8) | q[iorder[0]]; |
| 2431 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2432 | if (bom == 0x0000FEFF) { |
| 2433 | q += 4; |
| 2434 | bo = -1; |
| 2435 | } |
| 2436 | else if (bom == 0xFFFE0000) { |
| 2437 | q += 4; |
| 2438 | bo = 1; |
| 2439 | } |
| 2440 | #else |
| 2441 | if (bom == 0x0000FEFF) { |
| 2442 | q += 4; |
| 2443 | bo = 1; |
| 2444 | } |
| 2445 | else if (bom == 0xFFFE0000) { |
| 2446 | q += 4; |
| 2447 | bo = -1; |
| 2448 | } |
| 2449 | #endif |
| 2450 | } |
| 2451 | } |
| 2452 | |
| 2453 | if (bo == -1) { |
| 2454 | /* force LE */ |
| 2455 | iorder[0] = 0; |
| 2456 | iorder[1] = 1; |
| 2457 | iorder[2] = 2; |
| 2458 | iorder[3] = 3; |
| 2459 | } |
| 2460 | else if (bo == 1) { |
| 2461 | /* force BE */ |
| 2462 | iorder[0] = 3; |
| 2463 | iorder[1] = 2; |
| 2464 | iorder[2] = 1; |
| 2465 | iorder[3] = 0; |
| 2466 | } |
| 2467 | |
| 2468 | while (q < e) { |
| 2469 | Py_UCS4 ch; |
| 2470 | /* remaining bytes at the end? (size should be divisible by 4) */ |
| 2471 | if (e-q<4) { |
| 2472 | if (consumed) |
| 2473 | break; |
| 2474 | errmsg = "truncated data"; |
| 2475 | startinpos = ((const char *)q)-starts; |
| 2476 | endinpos = ((const char *)e)-starts; |
| 2477 | goto utf32Error; |
| 2478 | /* The remaining input chars are ignored if the callback |
| 2479 | chooses to skip the input */ |
| 2480 | } |
| 2481 | ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 2482 | (q[iorder[1]] << 8) | q[iorder[0]]; |
| 2483 | |
| 2484 | if (ch >= 0x110000) |
| 2485 | { |
| 2486 | errmsg = "codepoint not in range(0x110000)"; |
| 2487 | startinpos = ((const char *)q)-starts; |
| 2488 | endinpos = startinpos+4; |
| 2489 | goto utf32Error; |
| 2490 | } |
| 2491 | #ifndef Py_UNICODE_WIDE |
| 2492 | if (ch >= 0x10000) |
| 2493 | { |
| 2494 | *p++ = 0xD800 | ((ch-0x10000) >> 10); |
| 2495 | *p++ = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 2496 | } |
| 2497 | else |
| 2498 | #endif |
| 2499 | *p++ = ch; |
| 2500 | q += 4; |
| 2501 | continue; |
| 2502 | utf32Error: |
| 2503 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2504 | if (unicode_decode_call_errorhandler( |
| 2505 | errors, &errorHandler, |
| 2506 | "utf32", errmsg, |
| 2507 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 2508 | &unicode, &outpos, &p)) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2509 | goto onError; |
| 2510 | } |
| 2511 | |
| 2512 | if (byteorder) |
| 2513 | *byteorder = bo; |
| 2514 | |
| 2515 | if (consumed) |
| 2516 | *consumed = (const char *)q-starts; |
| 2517 | |
| 2518 | /* Adjust length */ |
| 2519 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
| 2520 | goto onError; |
| 2521 | |
| 2522 | Py_XDECREF(errorHandler); |
| 2523 | Py_XDECREF(exc); |
| 2524 | return (PyObject *)unicode; |
| 2525 | |
| 2526 | onError: |
| 2527 | Py_DECREF(unicode); |
| 2528 | Py_XDECREF(errorHandler); |
| 2529 | Py_XDECREF(exc); |
| 2530 | return NULL; |
| 2531 | } |
| 2532 | |
| 2533 | PyObject * |
| 2534 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
| 2535 | Py_ssize_t size, |
| 2536 | const char *errors, |
| 2537 | int byteorder) |
| 2538 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2539 | PyObject *v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2540 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 2541 | Py_ssize_t nsize, bytesize; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2542 | #ifndef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 2543 | Py_ssize_t i, pairs; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2544 | #else |
| 2545 | const int pairs = 0; |
| 2546 | #endif |
| 2547 | /* Offsets from p for storing byte pairs in the right order. */ |
| 2548 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2549 | int iorder[] = {0, 1, 2, 3}; |
| 2550 | #else |
| 2551 | int iorder[] = {3, 2, 1, 0}; |
| 2552 | #endif |
| 2553 | |
| 2554 | #define STORECHAR(CH) \ |
| 2555 | do { \ |
| 2556 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 2557 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 2558 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 2559 | p[iorder[0]] = (CH) & 0xff; \ |
| 2560 | p += 4; \ |
| 2561 | } while(0) |
| 2562 | |
| 2563 | /* In narrow builds we can output surrogate pairs as one codepoint, |
| 2564 | so we need less space. */ |
| 2565 | #ifndef Py_UNICODE_WIDE |
| 2566 | for (i = pairs = 0; i < size-1; i++) |
| 2567 | if (0xD800 <= s[i] && s[i] <= 0xDBFF && |
| 2568 | 0xDC00 <= s[i+1] && s[i+1] <= 0xDFFF) |
| 2569 | pairs++; |
| 2570 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 2571 | nsize = (size - pairs + (byteorder == 0)); |
| 2572 | bytesize = nsize * 4; |
| 2573 | if (bytesize / 4 != nsize) |
| 2574 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2575 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2576 | if (v == NULL) |
| 2577 | return NULL; |
| 2578 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2579 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2580 | if (byteorder == 0) |
| 2581 | STORECHAR(0xFEFF); |
| 2582 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2583 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2584 | |
| 2585 | if (byteorder == -1) { |
| 2586 | /* force LE */ |
| 2587 | iorder[0] = 0; |
| 2588 | iorder[1] = 1; |
| 2589 | iorder[2] = 2; |
| 2590 | iorder[3] = 3; |
| 2591 | } |
| 2592 | else if (byteorder == 1) { |
| 2593 | /* force BE */ |
| 2594 | iorder[0] = 3; |
| 2595 | iorder[1] = 2; |
| 2596 | iorder[2] = 1; |
| 2597 | iorder[3] = 0; |
| 2598 | } |
| 2599 | |
| 2600 | while (size-- > 0) { |
| 2601 | Py_UCS4 ch = *s++; |
| 2602 | #ifndef Py_UNICODE_WIDE |
| 2603 | if (0xD800 <= ch && ch <= 0xDBFF && size > 0) { |
| 2604 | Py_UCS4 ch2 = *s; |
| 2605 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
| 2606 | ch = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
| 2607 | s++; |
| 2608 | size--; |
| 2609 | } |
| 2610 | } |
| 2611 | #endif |
| 2612 | STORECHAR(ch); |
| 2613 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2614 | |
| 2615 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2616 | return v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2617 | #undef STORECHAR |
| 2618 | } |
| 2619 | |
| 2620 | PyObject *PyUnicode_AsUTF32String(PyObject *unicode) |
| 2621 | { |
| 2622 | if (!PyUnicode_Check(unicode)) { |
| 2623 | PyErr_BadArgument(); |
| 2624 | return NULL; |
| 2625 | } |
| 2626 | return PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(unicode), |
| 2627 | PyUnicode_GET_SIZE(unicode), |
| 2628 | NULL, |
| 2629 | 0); |
| 2630 | } |
| 2631 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2632 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 2633 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2634 | PyObject * |
| 2635 | PyUnicode_DecodeUTF16(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2636 | Py_ssize_t size, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2637 | const char *errors, |
| 2638 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2639 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2640 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 2641 | } |
| 2642 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2643 | /* Two masks for fast checking of whether a C 'long' may contain |
| 2644 | UTF16-encoded surrogate characters. This is an efficient heuristic, |
| 2645 | assuming that non-surrogate characters with a code point >= 0x8000 are |
| 2646 | rare in most input. |
| 2647 | FAST_CHAR_MASK is used when the input is in native byte ordering, |
| 2648 | SWAPPED_FAST_CHAR_MASK when the input is in byteswapped ordering. |
| 2649 | */ |
| 2650 | #if (SIZEOF_LONG == 8) |
| 2651 | # define FAST_CHAR_MASK 0x8000800080008000L |
| 2652 | # define SWAPPED_FAST_CHAR_MASK 0x0080008000800080L |
| 2653 | #elif (SIZEOF_LONG == 4) |
| 2654 | # define FAST_CHAR_MASK 0x80008000L |
| 2655 | # define SWAPPED_FAST_CHAR_MASK 0x00800080L |
| 2656 | #else |
| 2657 | # error C 'long' size should be either 4 or 8! |
| 2658 | #endif |
| 2659 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2660 | PyObject * |
| 2661 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2662 | Py_ssize_t size, |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2663 | const char *errors, |
| 2664 | int *byteorder, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2665 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2666 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2667 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2668 | Py_ssize_t startinpos; |
| 2669 | Py_ssize_t endinpos; |
| 2670 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2671 | PyUnicodeObject *unicode; |
| 2672 | Py_UNICODE *p; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2673 | const unsigned char *q, *e, *aligned_end; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2674 | int bo = 0; /* assume native ordering by default */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2675 | int native_ordering = 0; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2676 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2677 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 2678 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2679 | int ihi = 1, ilo = 0; |
| 2680 | #else |
| 2681 | int ihi = 0, ilo = 1; |
| 2682 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2683 | PyObject *errorHandler = NULL; |
| 2684 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2685 | |
| 2686 | /* Note: size will always be longer than the resulting Unicode |
| 2687 | character count */ |
| 2688 | unicode = _PyUnicode_New(size); |
| 2689 | if (!unicode) |
| 2690 | return NULL; |
| 2691 | if (size == 0) |
| 2692 | return (PyObject *)unicode; |
| 2693 | |
| 2694 | /* Unpack UTF-16 encoded data */ |
| 2695 | p = unicode->str; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2696 | q = (unsigned char *)s; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2697 | e = q + size - 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2698 | |
| 2699 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2700 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2701 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2702 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 2703 | byte order setting accordingly. In native mode, the leading BOM |
| 2704 | mark is skipped, in all other modes, it is copied to the output |
| 2705 | stream as-is (giving a ZWNBSP character). */ |
| 2706 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2707 | if (size >= 2) { |
| 2708 | const Py_UNICODE bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2709 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2710 | if (bom == 0xFEFF) { |
| 2711 | q += 2; |
| 2712 | bo = -1; |
| 2713 | } |
| 2714 | else if (bom == 0xFFFE) { |
| 2715 | q += 2; |
| 2716 | bo = 1; |
| 2717 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2718 | #else |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2719 | if (bom == 0xFEFF) { |
| 2720 | q += 2; |
| 2721 | bo = 1; |
| 2722 | } |
| 2723 | else if (bom == 0xFFFE) { |
| 2724 | q += 2; |
| 2725 | bo = -1; |
| 2726 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2727 | #endif |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2728 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2729 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2730 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2731 | if (bo == -1) { |
| 2732 | /* force LE */ |
| 2733 | ihi = 1; |
| 2734 | ilo = 0; |
| 2735 | } |
| 2736 | else if (bo == 1) { |
| 2737 | /* force BE */ |
| 2738 | ihi = 0; |
| 2739 | ilo = 1; |
| 2740 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2741 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2742 | native_ordering = ilo < ihi; |
| 2743 | #else |
| 2744 | native_ordering = ilo > ihi; |
| 2745 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2746 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2747 | aligned_end = (const unsigned char *) ((size_t) e & ~LONG_PTR_MASK); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2748 | while (q < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2749 | Py_UNICODE ch; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2750 | /* First check for possible aligned read of a C 'long'. Unaligned |
| 2751 | reads are more expensive, better to defer to another iteration. */ |
| 2752 | if (!((size_t) q & LONG_PTR_MASK)) { |
| 2753 | /* Fast path for runs of non-surrogate chars. */ |
| 2754 | register const unsigned char *_q = q; |
| 2755 | Py_UNICODE *_p = p; |
| 2756 | if (native_ordering) { |
| 2757 | /* Native ordering is simple: as long as the input cannot |
| 2758 | possibly contain a surrogate char, do an unrolled copy |
| 2759 | of several 16-bit code points to the target object. |
| 2760 | The non-surrogate check is done on several input bytes |
| 2761 | at a time (as many as a C 'long' can contain). */ |
| 2762 | while (_q < aligned_end) { |
| 2763 | unsigned long data = * (unsigned long *) _q; |
| 2764 | if (data & FAST_CHAR_MASK) |
| 2765 | break; |
| 2766 | _p[0] = ((unsigned short *) _q)[0]; |
| 2767 | _p[1] = ((unsigned short *) _q)[1]; |
| 2768 | #if (SIZEOF_LONG == 8) |
| 2769 | _p[2] = ((unsigned short *) _q)[2]; |
| 2770 | _p[3] = ((unsigned short *) _q)[3]; |
| 2771 | #endif |
| 2772 | _q += SIZEOF_LONG; |
| 2773 | _p += SIZEOF_LONG / 2; |
| 2774 | } |
| 2775 | } |
| 2776 | else { |
| 2777 | /* Byteswapped ordering is similar, but we must decompose |
| 2778 | the copy bytewise, and take care of zero'ing out the |
| 2779 | upper bytes if the target object is in 32-bit units |
| 2780 | (that is, in UCS-4 builds). */ |
| 2781 | while (_q < aligned_end) { |
| 2782 | unsigned long data = * (unsigned long *) _q; |
| 2783 | if (data & SWAPPED_FAST_CHAR_MASK) |
| 2784 | break; |
| 2785 | /* Zero upper bytes in UCS-4 builds */ |
| 2786 | #if (Py_UNICODE_SIZE > 2) |
| 2787 | _p[0] = 0; |
| 2788 | _p[1] = 0; |
| 2789 | #if (SIZEOF_LONG == 8) |
| 2790 | _p[2] = 0; |
| 2791 | _p[3] = 0; |
| 2792 | #endif |
| 2793 | #endif |
| 2794 | ((unsigned char *) _p)[1] = _q[0]; |
| 2795 | ((unsigned char *) _p)[0] = _q[1]; |
| 2796 | ((unsigned char *) _p)[1 + Py_UNICODE_SIZE] = _q[2]; |
| 2797 | ((unsigned char *) _p)[0 + Py_UNICODE_SIZE] = _q[3]; |
| 2798 | #if (SIZEOF_LONG == 8) |
| 2799 | ((unsigned char *) _p)[1 + 2 * Py_UNICODE_SIZE] = _q[4]; |
| 2800 | ((unsigned char *) _p)[0 + 2 * Py_UNICODE_SIZE] = _q[5]; |
| 2801 | ((unsigned char *) _p)[1 + 3 * Py_UNICODE_SIZE] = _q[6]; |
| 2802 | ((unsigned char *) _p)[0 + 3 * Py_UNICODE_SIZE] = _q[7]; |
| 2803 | #endif |
| 2804 | _q += SIZEOF_LONG; |
| 2805 | _p += SIZEOF_LONG / 2; |
| 2806 | } |
| 2807 | } |
| 2808 | p = _p; |
| 2809 | q = _q; |
| 2810 | if (q >= e) |
| 2811 | break; |
| 2812 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2813 | ch = (q[ihi] << 8) | q[ilo]; |
| 2814 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2815 | q += 2; |
| 2816 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2817 | if (ch < 0xD800 || ch > 0xDFFF) { |
| 2818 | *p++ = ch; |
| 2819 | continue; |
| 2820 | } |
| 2821 | |
| 2822 | /* UTF-16 code pair: */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2823 | if (q > e) { |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2824 | errmsg = "unexpected end of data"; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2825 | startinpos = (((const char *)q) - 2) - starts; |
| 2826 | endinpos = ((const char *)e) + 1 - starts; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2827 | goto utf16Error; |
| 2828 | } |
Martin v. Löwis | ac93bc2 | 2001-06-26 22:43:40 +0000 | [diff] [blame] | 2829 | if (0xD800 <= ch && ch <= 0xDBFF) { |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2830 | Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; |
| 2831 | q += 2; |
Martin v. Löwis | ac93bc2 | 2001-06-26 22:43:40 +0000 | [diff] [blame] | 2832 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 2833 | #ifndef Py_UNICODE_WIDE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2834 | *p++ = ch; |
| 2835 | *p++ = ch2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2836 | #else |
| 2837 | *p++ = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2838 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2839 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2840 | } |
| 2841 | else { |
| 2842 | errmsg = "illegal UTF-16 surrogate"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2843 | startinpos = (((const char *)q)-4)-starts; |
| 2844 | endinpos = startinpos+2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2845 | goto utf16Error; |
| 2846 | } |
| 2847 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2848 | } |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2849 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2850 | startinpos = (((const char *)q)-2)-starts; |
| 2851 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2852 | /* Fall through to report the error */ |
| 2853 | |
| 2854 | utf16Error: |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2855 | outpos = p - PyUnicode_AS_UNICODE(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2856 | if (unicode_decode_call_errorhandler( |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2857 | errors, |
| 2858 | &errorHandler, |
| 2859 | "utf16", errmsg, |
| 2860 | &starts, |
| 2861 | (const char **)&e, |
| 2862 | &startinpos, |
| 2863 | &endinpos, |
| 2864 | &exc, |
| 2865 | (const char **)&q, |
| 2866 | &unicode, |
| 2867 | &outpos, |
| 2868 | &p)) |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2869 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2870 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2871 | /* remaining byte at the end? (size should be even) */ |
| 2872 | if (e == q) { |
| 2873 | if (!consumed) { |
| 2874 | errmsg = "truncated data"; |
| 2875 | startinpos = ((const char *)q) - starts; |
| 2876 | endinpos = ((const char *)e) + 1 - starts; |
| 2877 | outpos = p - PyUnicode_AS_UNICODE(unicode); |
| 2878 | if (unicode_decode_call_errorhandler( |
| 2879 | errors, |
| 2880 | &errorHandler, |
| 2881 | "utf16", errmsg, |
| 2882 | &starts, |
| 2883 | (const char **)&e, |
| 2884 | &startinpos, |
| 2885 | &endinpos, |
| 2886 | &exc, |
| 2887 | (const char **)&q, |
| 2888 | &unicode, |
| 2889 | &outpos, |
| 2890 | &p)) |
| 2891 | goto onError; |
| 2892 | /* The remaining input chars are ignored if the callback |
| 2893 | chooses to skip the input */ |
| 2894 | } |
| 2895 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2896 | |
| 2897 | if (byteorder) |
| 2898 | *byteorder = bo; |
| 2899 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2900 | if (consumed) |
| 2901 | *consumed = (const char *)q-starts; |
| 2902 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2903 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2904 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2905 | goto onError; |
| 2906 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2907 | Py_XDECREF(errorHandler); |
| 2908 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2909 | return (PyObject *)unicode; |
| 2910 | |
| 2911 | onError: |
| 2912 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2913 | Py_XDECREF(errorHandler); |
| 2914 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2915 | return NULL; |
| 2916 | } |
| 2917 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2918 | #undef FAST_CHAR_MASK |
| 2919 | #undef SWAPPED_FAST_CHAR_MASK |
| 2920 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2921 | PyObject * |
| 2922 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2923 | Py_ssize_t size, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2924 | const char *errors, |
| 2925 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2926 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2927 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2928 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 2929 | Py_ssize_t nsize, bytesize; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2930 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 2931 | Py_ssize_t i, pairs; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2932 | #else |
| 2933 | const int pairs = 0; |
| 2934 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2935 | /* Offsets from p for storing byte pairs in the right order. */ |
| 2936 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2937 | int ihi = 1, ilo = 0; |
| 2938 | #else |
| 2939 | int ihi = 0, ilo = 1; |
| 2940 | #endif |
| 2941 | |
| 2942 | #define STORECHAR(CH) \ |
| 2943 | do { \ |
| 2944 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 2945 | p[ilo] = (CH) & 0xff; \ |
| 2946 | p += 2; \ |
| 2947 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2948 | |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2949 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2950 | for (i = pairs = 0; i < size; i++) |
| 2951 | if (s[i] >= 0x10000) |
| 2952 | pairs++; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2953 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 2954 | /* 2 * (size + pairs + (byteorder == 0)) */ |
| 2955 | if (size > PY_SSIZE_T_MAX || |
| 2956 | size > PY_SSIZE_T_MAX - pairs - (byteorder == 0)) |
| 2957 | return PyErr_NoMemory(); |
| 2958 | nsize = size + pairs + (byteorder == 0); |
| 2959 | bytesize = nsize * 2; |
| 2960 | if (bytesize / 2 != nsize) |
| 2961 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2962 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2963 | if (v == NULL) |
| 2964 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2965 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2966 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2967 | if (byteorder == 0) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2968 | STORECHAR(0xFEFF); |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 2969 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2970 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2971 | |
| 2972 | if (byteorder == -1) { |
| 2973 | /* force LE */ |
| 2974 | ihi = 1; |
| 2975 | ilo = 0; |
| 2976 | } |
| 2977 | else if (byteorder == 1) { |
| 2978 | /* force BE */ |
| 2979 | ihi = 0; |
| 2980 | ilo = 1; |
| 2981 | } |
| 2982 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2983 | while (size-- > 0) { |
| 2984 | Py_UNICODE ch = *s++; |
| 2985 | Py_UNICODE ch2 = 0; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2986 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2987 | if (ch >= 0x10000) { |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2988 | ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 2989 | ch = 0xD800 | ((ch-0x10000) >> 10); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2990 | } |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2991 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2992 | STORECHAR(ch); |
| 2993 | if (ch2) |
| 2994 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2995 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2996 | |
| 2997 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2998 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2999 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3000 | } |
| 3001 | |
| 3002 | PyObject *PyUnicode_AsUTF16String(PyObject *unicode) |
| 3003 | { |
| 3004 | if (!PyUnicode_Check(unicode)) { |
| 3005 | PyErr_BadArgument(); |
| 3006 | return NULL; |
| 3007 | } |
| 3008 | return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode), |
| 3009 | PyUnicode_GET_SIZE(unicode), |
| 3010 | NULL, |
| 3011 | 0); |
| 3012 | } |
| 3013 | |
| 3014 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 3015 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 3016 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 3017 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3018 | PyObject *PyUnicode_DecodeUnicodeEscape(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3019 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3020 | const char *errors) |
| 3021 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3022 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3023 | Py_ssize_t startinpos; |
| 3024 | Py_ssize_t endinpos; |
| 3025 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3026 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3027 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3028 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3029 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3030 | char* message; |
| 3031 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3032 | PyObject *errorHandler = NULL; |
| 3033 | PyObject *exc = NULL; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3034 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3035 | /* Escaped strings will always be longer than the resulting |
| 3036 | 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] | 3037 | length after conversion to the true value. |
| 3038 | (but if the error callback returns a long replacement string |
| 3039 | we'll have to allocate more space) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3040 | v = _PyUnicode_New(size); |
| 3041 | if (v == NULL) |
| 3042 | goto onError; |
| 3043 | if (size == 0) |
| 3044 | return (PyObject *)v; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3045 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3046 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3047 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3048 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3049 | while (s < end) { |
| 3050 | unsigned char c; |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 3051 | Py_UNICODE x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3052 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3053 | |
| 3054 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 3055 | if (*s != '\\') { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3056 | *p++ = (unsigned char) *s++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3057 | continue; |
| 3058 | } |
| 3059 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3060 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3061 | /* \ - Escapes */ |
| 3062 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 3063 | c = *s++; |
| 3064 | if (s > end) |
| 3065 | c = '\0'; /* Invalid after \ */ |
| 3066 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3067 | |
| 3068 | /* \x escapes */ |
| 3069 | case '\n': break; |
| 3070 | case '\\': *p++ = '\\'; break; |
| 3071 | case '\'': *p++ = '\''; break; |
| 3072 | case '\"': *p++ = '\"'; break; |
| 3073 | case 'b': *p++ = '\b'; break; |
| 3074 | case 'f': *p++ = '\014'; break; /* FF */ |
| 3075 | case 't': *p++ = '\t'; break; |
| 3076 | case 'n': *p++ = '\n'; break; |
| 3077 | case 'r': *p++ = '\r'; break; |
| 3078 | case 'v': *p++ = '\013'; break; /* VT */ |
| 3079 | case 'a': *p++ = '\007'; break; /* BEL, not classic C */ |
| 3080 | |
| 3081 | /* \OOO (octal) escapes */ |
| 3082 | case '0': case '1': case '2': case '3': |
| 3083 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 3084 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 3085 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 3086 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 3087 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 3088 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3089 | } |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 3090 | *p++ = x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3091 | break; |
| 3092 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3093 | /* hex escapes */ |
| 3094 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3095 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3096 | digits = 2; |
| 3097 | message = "truncated \\xXX escape"; |
| 3098 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3099 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3100 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3101 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3102 | digits = 4; |
| 3103 | message = "truncated \\uXXXX escape"; |
| 3104 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3105 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3106 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3107 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3108 | digits = 8; |
| 3109 | message = "truncated \\UXXXXXXXX escape"; |
| 3110 | hexescape: |
| 3111 | chr = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3112 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3113 | if (s+digits>end) { |
| 3114 | endinpos = size; |
| 3115 | if (unicode_decode_call_errorhandler( |
| 3116 | errors, &errorHandler, |
| 3117 | "unicodeescape", "end of string in escape sequence", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3118 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 3119 | &v, &outpos, &p)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3120 | goto onError; |
| 3121 | goto nextByte; |
| 3122 | } |
| 3123 | for (i = 0; i < digits; ++i) { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3124 | c = (unsigned char) s[i]; |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 3125 | if (!ISXDIGIT(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3126 | endinpos = (s+i+1)-starts; |
| 3127 | if (unicode_decode_call_errorhandler( |
| 3128 | errors, &errorHandler, |
| 3129 | "unicodeescape", message, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3130 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 3131 | &v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3132 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3133 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3134 | } |
| 3135 | chr = (chr<<4) & ~0xF; |
| 3136 | if (c >= '0' && c <= '9') |
| 3137 | chr += c - '0'; |
| 3138 | else if (c >= 'a' && c <= 'f') |
| 3139 | chr += 10 + c - 'a'; |
| 3140 | else |
| 3141 | chr += 10 + c - 'A'; |
| 3142 | } |
| 3143 | s += i; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 3144 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3145 | /* _decoding_error will have already written into the |
| 3146 | target buffer. */ |
| 3147 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3148 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3149 | /* when we get here, chr is a 32-bit unicode character */ |
| 3150 | if (chr <= 0xffff) |
| 3151 | /* UCS-2 character */ |
| 3152 | *p++ = (Py_UNICODE) chr; |
| 3153 | else if (chr <= 0x10ffff) { |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3154 | /* UCS-4 character. Either store directly, or as |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3155 | surrogate pair. */ |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 3156 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3157 | *p++ = chr; |
| 3158 | #else |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3159 | chr -= 0x10000L; |
| 3160 | *p++ = 0xD800 + (Py_UNICODE) (chr >> 10); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 3161 | *p++ = 0xDC00 + (Py_UNICODE) (chr & 0x03FF); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3162 | #endif |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3163 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3164 | endinpos = s-starts; |
| 3165 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3166 | if (unicode_decode_call_errorhandler( |
| 3167 | errors, &errorHandler, |
| 3168 | "unicodeescape", "illegal Unicode character", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3169 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 3170 | &v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3171 | goto onError; |
| 3172 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3173 | break; |
| 3174 | |
| 3175 | /* \N{name} */ |
| 3176 | case 'N': |
| 3177 | message = "malformed \\N character escape"; |
| 3178 | if (ucnhash_CAPI == NULL) { |
| 3179 | /* load the unicode data module */ |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 3180 | PyObject *m, *api; |
Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 3181 | m = PyImport_ImportModuleNoBlock("unicodedata"); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3182 | if (m == NULL) |
| 3183 | goto ucnhashError; |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 3184 | api = PyObject_GetAttrString(m, "ucnhash_CAPI"); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3185 | Py_DECREF(m); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 3186 | if (api == NULL) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3187 | goto ucnhashError; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3188 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCObject_AsVoidPtr(api); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 3189 | Py_DECREF(api); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3190 | if (ucnhash_CAPI == NULL) |
| 3191 | goto ucnhashError; |
| 3192 | } |
| 3193 | if (*s == '{') { |
| 3194 | const char *start = s+1; |
| 3195 | /* look for the closing brace */ |
| 3196 | while (*s != '}' && s < end) |
| 3197 | s++; |
| 3198 | if (s > start && s < end && *s == '}') { |
| 3199 | /* found a name. look it up in the unicode database */ |
| 3200 | message = "unknown Unicode character name"; |
| 3201 | s++; |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 3202 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), &chr)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3203 | goto store; |
| 3204 | } |
| 3205 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3206 | endinpos = s-starts; |
| 3207 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3208 | if (unicode_decode_call_errorhandler( |
| 3209 | errors, &errorHandler, |
| 3210 | "unicodeescape", message, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3211 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 3212 | &v, &outpos, &p)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3213 | goto onError; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3214 | break; |
| 3215 | |
| 3216 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3217 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3218 | message = "\\ at end of string"; |
| 3219 | s--; |
| 3220 | endinpos = s-starts; |
| 3221 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3222 | if (unicode_decode_call_errorhandler( |
| 3223 | errors, &errorHandler, |
| 3224 | "unicodeescape", message, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3225 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 3226 | &v, &outpos, &p)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3227 | goto onError; |
| 3228 | } |
| 3229 | else { |
| 3230 | *p++ = '\\'; |
| 3231 | *p++ = (unsigned char)s[-1]; |
| 3232 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3233 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3234 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3235 | nextByte: |
| 3236 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3237 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3238 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3239 | goto onError; |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 3240 | Py_XDECREF(errorHandler); |
| 3241 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3242 | return (PyObject *)v; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3243 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3244 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 3245 | PyErr_SetString( |
| 3246 | PyExc_UnicodeError, |
| 3247 | "\\N escapes not supported (can't load unicodedata module)" |
| 3248 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 3249 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3250 | Py_XDECREF(errorHandler); |
| 3251 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 3252 | return NULL; |
| 3253 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3254 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3255 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3256 | Py_XDECREF(errorHandler); |
| 3257 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3258 | return NULL; |
| 3259 | } |
| 3260 | |
| 3261 | /* Return a Unicode-Escape string version of the Unicode object. |
| 3262 | |
| 3263 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 3264 | appropriate. |
| 3265 | |
| 3266 | */ |
| 3267 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3268 | Py_LOCAL_INLINE(const Py_UNICODE *) findchar(const Py_UNICODE *s, |
| 3269 | Py_ssize_t size, |
| 3270 | Py_UNICODE ch) |
| 3271 | { |
| 3272 | /* like wcschr, but doesn't stop at NULL characters */ |
| 3273 | |
| 3274 | while (size-- > 0) { |
| 3275 | if (*s == ch) |
| 3276 | return s; |
| 3277 | s++; |
| 3278 | } |
| 3279 | |
| 3280 | return NULL; |
| 3281 | } |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 3282 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3283 | static const char *hexdigits = "0123456789abcdef"; |
| 3284 | |
| 3285 | PyObject *PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
| 3286 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3287 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3288 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3289 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3290 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3291 | #ifdef Py_UNICODE_WIDE |
| 3292 | const Py_ssize_t expandsize = 10; |
| 3293 | #else |
| 3294 | const Py_ssize_t expandsize = 6; |
| 3295 | #endif |
| 3296 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3297 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 3298 | better to choose a different scheme. Perhaps scan the |
| 3299 | first N-chars of the string and allocate based on that size. |
| 3300 | */ |
| 3301 | /* Initial allocation is based on the longest-possible unichr |
| 3302 | escape. |
| 3303 | |
| 3304 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 3305 | unichr, so in this case it's the longest unichr escape. In |
| 3306 | narrow (UTF-16) builds this is five chars per source unichr |
| 3307 | since there are two unichrs in the surrogate pair, so in narrow |
| 3308 | (UTF-16) builds it's not the longest unichr escape. |
| 3309 | |
| 3310 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 3311 | so in the narrow (UTF-16) build case it's the longest unichr |
| 3312 | escape. |
| 3313 | */ |
| 3314 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3315 | if (size == 0) |
| 3316 | return PyBytes_FromStringAndSize(NULL, 0); |
| 3317 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3318 | if (size > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
| 3319 | return PyErr_NoMemory(); |
| 3320 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3321 | repr = PyBytes_FromStringAndSize(NULL, |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3322 | 2 |
| 3323 | + expandsize*size |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3324 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3325 | if (repr == NULL) |
| 3326 | return NULL; |
| 3327 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3328 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3329 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3330 | while (size-- > 0) { |
| 3331 | Py_UNICODE ch = *s++; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3332 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3333 | /* Escape backslashes */ |
| 3334 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3335 | *p++ = '\\'; |
| 3336 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3337 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3338 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3339 | |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 3340 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3341 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 3342 | else if (ch >= 0x10000) { |
| 3343 | *p++ = '\\'; |
| 3344 | *p++ = 'U'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3345 | *p++ = hexdigits[(ch >> 28) & 0x0000000F]; |
| 3346 | *p++ = hexdigits[(ch >> 24) & 0x0000000F]; |
| 3347 | *p++ = hexdigits[(ch >> 20) & 0x0000000F]; |
| 3348 | *p++ = hexdigits[(ch >> 16) & 0x0000000F]; |
| 3349 | *p++ = hexdigits[(ch >> 12) & 0x0000000F]; |
| 3350 | *p++ = hexdigits[(ch >> 8) & 0x0000000F]; |
| 3351 | *p++ = hexdigits[(ch >> 4) & 0x0000000F]; |
| 3352 | *p++ = hexdigits[ch & 0x0000000F]; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3353 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3354 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3355 | #else |
| 3356 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3357 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 3358 | Py_UNICODE ch2; |
| 3359 | Py_UCS4 ucs; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3360 | |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3361 | ch2 = *s++; |
| 3362 | size--; |
| 3363 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
| 3364 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 3365 | *p++ = '\\'; |
| 3366 | *p++ = 'U'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3367 | *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; |
| 3368 | *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; |
| 3369 | *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; |
| 3370 | *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; |
| 3371 | *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; |
| 3372 | *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; |
| 3373 | *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; |
| 3374 | *p++ = hexdigits[ucs & 0x0000000F]; |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3375 | continue; |
| 3376 | } |
| 3377 | /* Fall through: isolated surrogates are copied as-is */ |
| 3378 | s--; |
| 3379 | size++; |
| 3380 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3381 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3382 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3383 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3384 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3385 | *p++ = '\\'; |
| 3386 | *p++ = 'u'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3387 | *p++ = hexdigits[(ch >> 12) & 0x000F]; |
| 3388 | *p++ = hexdigits[(ch >> 8) & 0x000F]; |
| 3389 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 3390 | *p++ = hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3391 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3392 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3393 | /* Map special whitespace to '\t', \n', '\r' */ |
| 3394 | else if (ch == '\t') { |
| 3395 | *p++ = '\\'; |
| 3396 | *p++ = 't'; |
| 3397 | } |
| 3398 | else if (ch == '\n') { |
| 3399 | *p++ = '\\'; |
| 3400 | *p++ = 'n'; |
| 3401 | } |
| 3402 | else if (ch == '\r') { |
| 3403 | *p++ = '\\'; |
| 3404 | *p++ = 'r'; |
| 3405 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3406 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3407 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 3408 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3409 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3410 | *p++ = 'x'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3411 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 3412 | *p++ = hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3413 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3414 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3415 | /* Copy everything else as-is */ |
| 3416 | else |
| 3417 | *p++ = (char) ch; |
| 3418 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3419 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3420 | assert(p - PyBytes_AS_STRING(repr) > 0); |
| 3421 | if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) |
| 3422 | return NULL; |
| 3423 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3424 | } |
| 3425 | |
Alexandre Vassalotti | 2056bed | 2008-12-27 19:46:35 +0000 | [diff] [blame] | 3426 | PyObject *PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3427 | { |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 3428 | PyObject *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3429 | if (!PyUnicode_Check(unicode)) { |
| 3430 | PyErr_BadArgument(); |
| 3431 | return NULL; |
| 3432 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3433 | s = PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 3434 | PyUnicode_GET_SIZE(unicode)); |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 3435 | return s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3436 | } |
| 3437 | |
| 3438 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 3439 | |
| 3440 | PyObject *PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3441 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3442 | const char *errors) |
| 3443 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3444 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3445 | Py_ssize_t startinpos; |
| 3446 | Py_ssize_t endinpos; |
| 3447 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3448 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3449 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3450 | const char *end; |
| 3451 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3452 | PyObject *errorHandler = NULL; |
| 3453 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3454 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3455 | /* Escaped strings will always be longer than the resulting |
| 3456 | 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] | 3457 | length after conversion to the true value. (But decoding error |
| 3458 | handler might have to resize the string) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3459 | v = _PyUnicode_New(size); |
| 3460 | if (v == NULL) |
| 3461 | goto onError; |
| 3462 | if (size == 0) |
| 3463 | return (PyObject *)v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3464 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3465 | end = s + size; |
| 3466 | while (s < end) { |
| 3467 | unsigned char c; |
Martin v. Löwis | 047c05e | 2002-03-21 08:55:28 +0000 | [diff] [blame] | 3468 | Py_UCS4 x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3469 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3470 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3471 | |
| 3472 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 3473 | if (*s != '\\') { |
| 3474 | *p++ = (unsigned char)*s++; |
| 3475 | continue; |
| 3476 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3477 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3478 | |
| 3479 | /* \u-escapes are only interpreted iff the number of leading |
| 3480 | backslashes if odd */ |
| 3481 | bs = s; |
| 3482 | for (;s < end;) { |
| 3483 | if (*s != '\\') |
| 3484 | break; |
| 3485 | *p++ = (unsigned char)*s++; |
| 3486 | } |
| 3487 | if (((s - bs) & 1) == 0 || |
| 3488 | s >= end || |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3489 | (*s != 'u' && *s != 'U')) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3490 | continue; |
| 3491 | } |
| 3492 | p--; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3493 | count = *s=='u' ? 4 : 8; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3494 | s++; |
| 3495 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3496 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3497 | outpos = p-PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3498 | for (x = 0, i = 0; i < count; ++i, ++s) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3499 | c = (unsigned char)*s; |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 3500 | if (!ISXDIGIT(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3501 | endinpos = s-starts; |
| 3502 | if (unicode_decode_call_errorhandler( |
| 3503 | errors, &errorHandler, |
| 3504 | "rawunicodeescape", "truncated \\uXXXX", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3505 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 3506 | &v, &outpos, &p)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3507 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3508 | goto nextByte; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3509 | } |
| 3510 | x = (x<<4) & ~0xF; |
| 3511 | if (c >= '0' && c <= '9') |
| 3512 | x += c - '0'; |
| 3513 | else if (c >= 'a' && c <= 'f') |
| 3514 | x += 10 + c - 'a'; |
| 3515 | else |
| 3516 | x += 10 + c - 'A'; |
| 3517 | } |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3518 | if (x <= 0xffff) |
| 3519 | /* UCS-2 character */ |
| 3520 | *p++ = (Py_UNICODE) x; |
| 3521 | else if (x <= 0x10ffff) { |
| 3522 | /* UCS-4 character. Either store directly, or as |
| 3523 | surrogate pair. */ |
| 3524 | #ifdef Py_UNICODE_WIDE |
Christian Heimes | cc47b05 | 2008-03-25 14:56:36 +0000 | [diff] [blame] | 3525 | *p++ = (Py_UNICODE) x; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3526 | #else |
| 3527 | x -= 0x10000L; |
| 3528 | *p++ = 0xD800 + (Py_UNICODE) (x >> 10); |
| 3529 | *p++ = 0xDC00 + (Py_UNICODE) (x & 0x03FF); |
| 3530 | #endif |
| 3531 | } else { |
| 3532 | endinpos = s-starts; |
| 3533 | outpos = p-PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3534 | if (unicode_decode_call_errorhandler( |
| 3535 | errors, &errorHandler, |
| 3536 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3537 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 3538 | &v, &outpos, &p)) |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3539 | goto onError; |
| 3540 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3541 | nextByte: |
| 3542 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3543 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3544 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 3545 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3546 | Py_XDECREF(errorHandler); |
| 3547 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3548 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3549 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3550 | onError: |
| 3551 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3552 | Py_XDECREF(errorHandler); |
| 3553 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3554 | return NULL; |
| 3555 | } |
| 3556 | |
| 3557 | PyObject *PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3558 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3559 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3560 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3561 | char *p; |
| 3562 | char *q; |
| 3563 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3564 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3565 | const Py_ssize_t expandsize = 10; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3566 | #else |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3567 | const Py_ssize_t expandsize = 6; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3568 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3569 | |
| 3570 | if (size > PY_SSIZE_T_MAX / expandsize) |
| 3571 | return PyErr_NoMemory(); |
| 3572 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3573 | repr = PyBytes_FromStringAndSize(NULL, expandsize * size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3574 | if (repr == NULL) |
| 3575 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 3576 | if (size == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3577 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3578 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3579 | p = q = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3580 | while (size-- > 0) { |
| 3581 | Py_UNICODE ch = *s++; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3582 | #ifdef Py_UNICODE_WIDE |
| 3583 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 3584 | if (ch >= 0x10000) { |
| 3585 | *p++ = '\\'; |
| 3586 | *p++ = 'U'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 3587 | *p++ = hexdigits[(ch >> 28) & 0xf]; |
| 3588 | *p++ = hexdigits[(ch >> 24) & 0xf]; |
| 3589 | *p++ = hexdigits[(ch >> 20) & 0xf]; |
| 3590 | *p++ = hexdigits[(ch >> 16) & 0xf]; |
| 3591 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 3592 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 3593 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 3594 | *p++ = hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3595 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3596 | else |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3597 | #else |
| 3598 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 3599 | if (ch >= 0xD800 && ch < 0xDC00) { |
| 3600 | Py_UNICODE ch2; |
| 3601 | Py_UCS4 ucs; |
| 3602 | |
| 3603 | ch2 = *s++; |
| 3604 | size--; |
| 3605 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
| 3606 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 3607 | *p++ = '\\'; |
| 3608 | *p++ = 'U'; |
| 3609 | *p++ = hexdigits[(ucs >> 28) & 0xf]; |
| 3610 | *p++ = hexdigits[(ucs >> 24) & 0xf]; |
| 3611 | *p++ = hexdigits[(ucs >> 20) & 0xf]; |
| 3612 | *p++ = hexdigits[(ucs >> 16) & 0xf]; |
| 3613 | *p++ = hexdigits[(ucs >> 12) & 0xf]; |
| 3614 | *p++ = hexdigits[(ucs >> 8) & 0xf]; |
| 3615 | *p++ = hexdigits[(ucs >> 4) & 0xf]; |
| 3616 | *p++ = hexdigits[ucs & 0xf]; |
| 3617 | continue; |
| 3618 | } |
| 3619 | /* Fall through: isolated surrogates are copied as-is */ |
| 3620 | s--; |
| 3621 | size++; |
| 3622 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3623 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3624 | /* Map 16-bit characters to '\uxxxx' */ |
| 3625 | if (ch >= 256) { |
| 3626 | *p++ = '\\'; |
| 3627 | *p++ = 'u'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 3628 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 3629 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 3630 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 3631 | *p++ = hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3632 | } |
| 3633 | /* Copy everything else as-is */ |
| 3634 | else |
| 3635 | *p++ = (char) ch; |
| 3636 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3637 | size = p - q; |
| 3638 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3639 | assert(size > 0); |
| 3640 | if (_PyBytes_Resize(&repr, size) < 0) |
| 3641 | return NULL; |
| 3642 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3643 | } |
| 3644 | |
| 3645 | PyObject *PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
| 3646 | { |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 3647 | PyObject *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3648 | if (!PyUnicode_Check(unicode)) { |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 3649 | PyErr_BadArgument(); |
| 3650 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3651 | } |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 3652 | s = PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 3653 | PyUnicode_GET_SIZE(unicode)); |
| 3654 | |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 3655 | return s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3656 | } |
| 3657 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3658 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 3659 | |
| 3660 | PyObject *_PyUnicode_DecodeUnicodeInternal(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3661 | Py_ssize_t size, |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3662 | const char *errors) |
| 3663 | { |
| 3664 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3665 | Py_ssize_t startinpos; |
| 3666 | Py_ssize_t endinpos; |
| 3667 | Py_ssize_t outpos; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3668 | PyUnicodeObject *v; |
| 3669 | Py_UNICODE *p; |
| 3670 | const char *end; |
| 3671 | const char *reason; |
| 3672 | PyObject *errorHandler = NULL; |
| 3673 | PyObject *exc = NULL; |
| 3674 | |
Neal Norwitz | d43069c | 2006-01-08 01:12:10 +0000 | [diff] [blame] | 3675 | #ifdef Py_UNICODE_WIDE |
| 3676 | Py_UNICODE unimax = PyUnicode_GetMax(); |
| 3677 | #endif |
| 3678 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3679 | /* XXX overflow detection missing */ |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3680 | v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE); |
| 3681 | if (v == NULL) |
| 3682 | goto onError; |
| 3683 | if (PyUnicode_GetSize((PyObject *)v) == 0) |
| 3684 | return (PyObject *)v; |
| 3685 | p = PyUnicode_AS_UNICODE(v); |
| 3686 | end = s + size; |
| 3687 | |
| 3688 | while (s < end) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3689 | memcpy(p, s, sizeof(Py_UNICODE)); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3690 | /* We have to sanity check the raw data, otherwise doom looms for |
| 3691 | some malformed UCS-4 data. */ |
| 3692 | if ( |
| 3693 | #ifdef Py_UNICODE_WIDE |
| 3694 | *p > unimax || *p < 0 || |
| 3695 | #endif |
| 3696 | end-s < Py_UNICODE_SIZE |
| 3697 | ) |
| 3698 | { |
| 3699 | startinpos = s - starts; |
| 3700 | if (end-s < Py_UNICODE_SIZE) { |
| 3701 | endinpos = end-starts; |
| 3702 | reason = "truncated input"; |
| 3703 | } |
| 3704 | else { |
| 3705 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 3706 | reason = "illegal code point (> 0x10FFFF)"; |
| 3707 | } |
| 3708 | outpos = p - PyUnicode_AS_UNICODE(v); |
| 3709 | if (unicode_decode_call_errorhandler( |
| 3710 | errors, &errorHandler, |
| 3711 | "unicode_internal", reason, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3712 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 3713 | &v, &outpos, &p)) { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3714 | goto onError; |
| 3715 | } |
| 3716 | } |
| 3717 | else { |
| 3718 | p++; |
| 3719 | s += Py_UNICODE_SIZE; |
| 3720 | } |
| 3721 | } |
| 3722 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3723 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3724 | goto onError; |
| 3725 | Py_XDECREF(errorHandler); |
| 3726 | Py_XDECREF(exc); |
| 3727 | return (PyObject *)v; |
| 3728 | |
| 3729 | onError: |
| 3730 | Py_XDECREF(v); |
| 3731 | Py_XDECREF(errorHandler); |
| 3732 | Py_XDECREF(exc); |
| 3733 | return NULL; |
| 3734 | } |
| 3735 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3736 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 3737 | |
| 3738 | PyObject *PyUnicode_DecodeLatin1(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3739 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3740 | const char *errors) |
| 3741 | { |
| 3742 | PyUnicodeObject *v; |
| 3743 | Py_UNICODE *p; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3744 | const char *e, *unrolled_end; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3745 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3746 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3747 | if (size == 1) { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 3748 | Py_UNICODE r = *(unsigned char*)s; |
| 3749 | return PyUnicode_FromUnicode(&r, 1); |
| 3750 | } |
| 3751 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3752 | v = _PyUnicode_New(size); |
| 3753 | if (v == NULL) |
| 3754 | goto onError; |
| 3755 | if (size == 0) |
| 3756 | return (PyObject *)v; |
| 3757 | p = PyUnicode_AS_UNICODE(v); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3758 | e = s + size; |
| 3759 | /* Unrolling the copy makes it much faster by reducing the looping |
| 3760 | overhead. This is similar to what many memcpy() implementations do. */ |
| 3761 | unrolled_end = e - 4; |
| 3762 | while (s < unrolled_end) { |
| 3763 | p[0] = (unsigned char) s[0]; |
| 3764 | p[1] = (unsigned char) s[1]; |
| 3765 | p[2] = (unsigned char) s[2]; |
| 3766 | p[3] = (unsigned char) s[3]; |
| 3767 | s += 4; |
| 3768 | p += 4; |
| 3769 | } |
| 3770 | while (s < e) |
| 3771 | *p++ = (unsigned char) *s++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3772 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3773 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3774 | onError: |
| 3775 | Py_XDECREF(v); |
| 3776 | return NULL; |
| 3777 | } |
| 3778 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3779 | /* create or adjust a UnicodeEncodeError */ |
| 3780 | static void make_encode_exception(PyObject **exceptionObject, |
| 3781 | const char *encoding, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3782 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 3783 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3784 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3785 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3786 | if (*exceptionObject == NULL) { |
| 3787 | *exceptionObject = PyUnicodeEncodeError_Create( |
| 3788 | encoding, unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3789 | } |
| 3790 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3791 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 3792 | goto onError; |
| 3793 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 3794 | goto onError; |
| 3795 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 3796 | goto onError; |
| 3797 | return; |
| 3798 | onError: |
| 3799 | Py_DECREF(*exceptionObject); |
| 3800 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3801 | } |
| 3802 | } |
| 3803 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3804 | /* raises a UnicodeEncodeError */ |
| 3805 | static void raise_encode_exception(PyObject **exceptionObject, |
| 3806 | const char *encoding, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3807 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 3808 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3809 | const char *reason) |
| 3810 | { |
| 3811 | make_encode_exception(exceptionObject, |
| 3812 | encoding, unicode, size, startpos, endpos, reason); |
| 3813 | if (*exceptionObject != NULL) |
| 3814 | PyCodec_StrictErrors(*exceptionObject); |
| 3815 | } |
| 3816 | |
| 3817 | /* error handling callback helper: |
| 3818 | build arguments, call the callback and check the arguments, |
| 3819 | put the result into newpos and return the replacement string, which |
| 3820 | has to be freed by the caller */ |
| 3821 | static PyObject *unicode_encode_call_errorhandler(const char *errors, |
| 3822 | PyObject **errorHandler, |
| 3823 | const char *encoding, const char *reason, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3824 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 3825 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 3826 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3827 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3828 | static char *argparse = "O!n;encoding error handler must return (str, int) tuple"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3829 | |
| 3830 | PyObject *restuple; |
| 3831 | PyObject *resunicode; |
| 3832 | |
| 3833 | if (*errorHandler == NULL) { |
| 3834 | *errorHandler = PyCodec_LookupError(errors); |
| 3835 | if (*errorHandler == NULL) |
| 3836 | return NULL; |
| 3837 | } |
| 3838 | |
| 3839 | make_encode_exception(exceptionObject, |
| 3840 | encoding, unicode, size, startpos, endpos, reason); |
| 3841 | if (*exceptionObject == NULL) |
| 3842 | return NULL; |
| 3843 | |
| 3844 | restuple = PyObject_CallFunctionObjArgs( |
| 3845 | *errorHandler, *exceptionObject, NULL); |
| 3846 | if (restuple == NULL) |
| 3847 | return NULL; |
| 3848 | if (!PyTuple_Check(restuple)) { |
| 3849 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 3850 | Py_DECREF(restuple); |
| 3851 | return NULL; |
| 3852 | } |
| 3853 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
| 3854 | &resunicode, newpos)) { |
| 3855 | Py_DECREF(restuple); |
| 3856 | return NULL; |
| 3857 | } |
| 3858 | if (*newpos<0) |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3859 | *newpos = size+*newpos; |
| 3860 | if (*newpos<0 || *newpos>size) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 3861 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3862 | Py_DECREF(restuple); |
| 3863 | return NULL; |
| 3864 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3865 | Py_INCREF(resunicode); |
| 3866 | Py_DECREF(restuple); |
| 3867 | return resunicode; |
| 3868 | } |
| 3869 | |
| 3870 | static PyObject *unicode_encode_ucs1(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3871 | Py_ssize_t size, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3872 | const char *errors, |
| 3873 | int limit) |
| 3874 | { |
| 3875 | /* output object */ |
| 3876 | PyObject *res; |
| 3877 | /* pointers to the beginning and end+1 of input */ |
| 3878 | const Py_UNICODE *startp = p; |
| 3879 | const Py_UNICODE *endp = p + size; |
| 3880 | /* pointer to the beginning of the unencodable characters */ |
| 3881 | /* const Py_UNICODE *badp = NULL; */ |
| 3882 | /* pointer into the output */ |
| 3883 | char *str; |
| 3884 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3885 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3886 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 3887 | 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] | 3888 | PyObject *errorHandler = NULL; |
| 3889 | PyObject *exc = NULL; |
| 3890 | /* the following variable is used for caching string comparisons |
| 3891 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 3892 | int known_errorHandler = -1; |
| 3893 | |
| 3894 | /* allocate enough for a simple encoding without |
| 3895 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3896 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3897 | return PyBytes_FromStringAndSize(NULL, 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3898 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3899 | if (res == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3900 | return NULL; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3901 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3902 | ressize = size; |
| 3903 | |
| 3904 | while (p<endp) { |
| 3905 | Py_UNICODE c = *p; |
| 3906 | |
| 3907 | /* can we encode this? */ |
| 3908 | if (c<limit) { |
| 3909 | /* no overflow check, because we know that the space is enough */ |
| 3910 | *str++ = (char)c; |
| 3911 | ++p; |
| 3912 | } |
| 3913 | else { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3914 | Py_ssize_t unicodepos = p-startp; |
| 3915 | Py_ssize_t requiredsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3916 | PyObject *repunicode; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3917 | Py_ssize_t repsize; |
| 3918 | Py_ssize_t newpos; |
| 3919 | Py_ssize_t respos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3920 | Py_UNICODE *uni2; |
| 3921 | /* startpos for collecting unencodable chars */ |
| 3922 | const Py_UNICODE *collstart = p; |
| 3923 | const Py_UNICODE *collend = p; |
| 3924 | /* find all unecodable characters */ |
| 3925 | while ((collend < endp) && ((*collend)>=limit)) |
| 3926 | ++collend; |
| 3927 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 3928 | if (known_errorHandler==-1) { |
| 3929 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 3930 | known_errorHandler = 1; |
| 3931 | else if (!strcmp(errors, "replace")) |
| 3932 | known_errorHandler = 2; |
| 3933 | else if (!strcmp(errors, "ignore")) |
| 3934 | known_errorHandler = 3; |
| 3935 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 3936 | known_errorHandler = 4; |
| 3937 | else |
| 3938 | known_errorHandler = 0; |
| 3939 | } |
| 3940 | switch (known_errorHandler) { |
| 3941 | case 1: /* strict */ |
| 3942 | raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason); |
| 3943 | goto onError; |
| 3944 | case 2: /* replace */ |
| 3945 | while (collstart++<collend) |
| 3946 | *str++ = '?'; /* fall through */ |
| 3947 | case 3: /* ignore */ |
| 3948 | p = collend; |
| 3949 | break; |
| 3950 | case 4: /* xmlcharrefreplace */ |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3951 | respos = str - PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3952 | /* determine replacement size (temporarily (mis)uses p) */ |
| 3953 | for (p = collstart, repsize = 0; p < collend; ++p) { |
| 3954 | if (*p<10) |
| 3955 | repsize += 2+1+1; |
| 3956 | else if (*p<100) |
| 3957 | repsize += 2+2+1; |
| 3958 | else if (*p<1000) |
| 3959 | repsize += 2+3+1; |
| 3960 | else if (*p<10000) |
| 3961 | repsize += 2+4+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 3962 | #ifndef Py_UNICODE_WIDE |
| 3963 | else |
| 3964 | repsize += 2+5+1; |
| 3965 | #else |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3966 | else if (*p<100000) |
| 3967 | repsize += 2+5+1; |
| 3968 | else if (*p<1000000) |
| 3969 | repsize += 2+6+1; |
| 3970 | else |
| 3971 | repsize += 2+7+1; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3972 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3973 | } |
| 3974 | requiredsize = respos+repsize+(endp-collend); |
| 3975 | if (requiredsize > ressize) { |
| 3976 | if (requiredsize<2*ressize) |
| 3977 | requiredsize = 2*ressize; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3978 | if (_PyBytes_Resize(&res, requiredsize)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3979 | goto onError; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3980 | str = PyBytes_AS_STRING(res) + respos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3981 | ressize = requiredsize; |
| 3982 | } |
| 3983 | /* generate replacement (temporarily (mis)uses p) */ |
| 3984 | for (p = collstart; p < collend; ++p) { |
| 3985 | str += sprintf(str, "&#%d;", (int)*p); |
| 3986 | } |
| 3987 | p = collend; |
| 3988 | break; |
| 3989 | default: |
| 3990 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 3991 | encoding, reason, startp, size, &exc, |
| 3992 | collstart-startp, collend-startp, &newpos); |
| 3993 | if (repunicode == NULL) |
| 3994 | goto onError; |
| 3995 | /* need more space? (at least enough for what we |
| 3996 | have+the replacement+the rest of the string, so |
| 3997 | we won't have to check space for encodable characters) */ |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3998 | respos = str - PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3999 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 4000 | requiredsize = respos+repsize+(endp-collend); |
| 4001 | if (requiredsize > ressize) { |
| 4002 | if (requiredsize<2*ressize) |
| 4003 | requiredsize = 2*ressize; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4004 | if (_PyBytes_Resize(&res, requiredsize)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4005 | Py_DECREF(repunicode); |
| 4006 | goto onError; |
| 4007 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4008 | str = PyBytes_AS_STRING(res) + respos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4009 | ressize = requiredsize; |
| 4010 | } |
| 4011 | /* check if there is anything unencodable in the replacement |
| 4012 | and copy it to the output */ |
| 4013 | for (uni2 = PyUnicode_AS_UNICODE(repunicode);repsize-->0; ++uni2, ++str) { |
| 4014 | c = *uni2; |
| 4015 | if (c >= limit) { |
| 4016 | raise_encode_exception(&exc, encoding, startp, size, |
| 4017 | unicodepos, unicodepos+1, reason); |
| 4018 | Py_DECREF(repunicode); |
| 4019 | goto onError; |
| 4020 | } |
| 4021 | *str = (char)c; |
| 4022 | } |
| 4023 | p = startp + newpos; |
| 4024 | Py_DECREF(repunicode); |
| 4025 | } |
| 4026 | } |
| 4027 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4028 | /* Resize if we allocated to much */ |
| 4029 | size = str - PyBytes_AS_STRING(res); |
| 4030 | if (size < ressize) { /* If this falls res will be NULL */ |
Alexandre Vassalotti | bad1b92 | 2008-12-27 09:49:09 +0000 | [diff] [blame] | 4031 | assert(size >= 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4032 | if (_PyBytes_Resize(&res, size) < 0) |
| 4033 | goto onError; |
| 4034 | } |
| 4035 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4036 | Py_XDECREF(errorHandler); |
| 4037 | Py_XDECREF(exc); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4038 | return res; |
| 4039 | |
| 4040 | onError: |
| 4041 | Py_XDECREF(res); |
| 4042 | Py_XDECREF(errorHandler); |
| 4043 | Py_XDECREF(exc); |
| 4044 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4045 | } |
| 4046 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4047 | PyObject *PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4048 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4049 | const char *errors) |
| 4050 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4051 | return unicode_encode_ucs1(p, size, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4052 | } |
| 4053 | |
| 4054 | PyObject *PyUnicode_AsLatin1String(PyObject *unicode) |
| 4055 | { |
| 4056 | if (!PyUnicode_Check(unicode)) { |
| 4057 | PyErr_BadArgument(); |
| 4058 | return NULL; |
| 4059 | } |
| 4060 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
| 4061 | PyUnicode_GET_SIZE(unicode), |
| 4062 | NULL); |
| 4063 | } |
| 4064 | |
| 4065 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 4066 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4067 | PyObject *PyUnicode_DecodeASCII(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4068 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4069 | const char *errors) |
| 4070 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4071 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4072 | PyUnicodeObject *v; |
| 4073 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4074 | Py_ssize_t startinpos; |
| 4075 | Py_ssize_t endinpos; |
| 4076 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4077 | const char *e; |
| 4078 | PyObject *errorHandler = NULL; |
| 4079 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4080 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4081 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4082 | if (size == 1 && *(unsigned char*)s < 128) { |
| 4083 | Py_UNICODE r = *(unsigned char*)s; |
| 4084 | return PyUnicode_FromUnicode(&r, 1); |
| 4085 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4086 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4087 | v = _PyUnicode_New(size); |
| 4088 | if (v == NULL) |
| 4089 | goto onError; |
| 4090 | if (size == 0) |
| 4091 | return (PyObject *)v; |
| 4092 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4093 | e = s + size; |
| 4094 | while (s < e) { |
| 4095 | register unsigned char c = (unsigned char)*s; |
| 4096 | if (c < 128) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4097 | *p++ = c; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4098 | ++s; |
| 4099 | } |
| 4100 | else { |
| 4101 | startinpos = s-starts; |
| 4102 | endinpos = startinpos + 1; |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 4103 | outpos = p - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4104 | if (unicode_decode_call_errorhandler( |
| 4105 | errors, &errorHandler, |
| 4106 | "ascii", "ordinal not in range(128)", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4107 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 4108 | &v, &outpos, &p)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4109 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4110 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4111 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 4112 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4113 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 4114 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4115 | Py_XDECREF(errorHandler); |
| 4116 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4117 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4118 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4119 | onError: |
| 4120 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4121 | Py_XDECREF(errorHandler); |
| 4122 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4123 | return NULL; |
| 4124 | } |
| 4125 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4126 | PyObject *PyUnicode_EncodeASCII(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4127 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4128 | const char *errors) |
| 4129 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4130 | return unicode_encode_ucs1(p, size, errors, 128); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4131 | } |
| 4132 | |
| 4133 | PyObject *PyUnicode_AsASCIIString(PyObject *unicode) |
| 4134 | { |
| 4135 | if (!PyUnicode_Check(unicode)) { |
| 4136 | PyErr_BadArgument(); |
| 4137 | return NULL; |
| 4138 | } |
| 4139 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
| 4140 | PyUnicode_GET_SIZE(unicode), |
| 4141 | NULL); |
| 4142 | } |
| 4143 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4144 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 4145 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4146 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 4147 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4148 | #if SIZEOF_INT < SIZEOF_SSIZE_T |
| 4149 | #define NEED_RETRY |
| 4150 | #endif |
| 4151 | |
| 4152 | /* XXX This code is limited to "true" double-byte encodings, as |
| 4153 | a) it assumes an incomplete character consists of a single byte, and |
| 4154 | b) IsDBCSLeadByte (probably) does not work for non-DBCS multi-byte |
| 4155 | encodings, see IsDBCSLeadByteEx documentation. */ |
| 4156 | |
| 4157 | static int is_dbcs_lead_byte(const char *s, int offset) |
| 4158 | { |
| 4159 | const char *curr = s + offset; |
| 4160 | |
| 4161 | if (IsDBCSLeadByte(*curr)) { |
| 4162 | const char *prev = CharPrev(s, curr); |
| 4163 | return (prev == curr) || !IsDBCSLeadByte(*prev) || (curr - prev == 2); |
| 4164 | } |
| 4165 | return 0; |
| 4166 | } |
| 4167 | |
| 4168 | /* |
| 4169 | * Decode MBCS string into unicode object. If 'final' is set, converts |
| 4170 | * trailing lead-byte too. Returns consumed size if succeed, -1 otherwise. |
| 4171 | */ |
| 4172 | static int decode_mbcs(PyUnicodeObject **v, |
| 4173 | const char *s, /* MBCS string */ |
| 4174 | int size, /* sizeof MBCS string */ |
| 4175 | int final) |
| 4176 | { |
| 4177 | Py_UNICODE *p; |
| 4178 | Py_ssize_t n = 0; |
| 4179 | int usize = 0; |
| 4180 | |
| 4181 | assert(size >= 0); |
| 4182 | |
| 4183 | /* Skip trailing lead-byte unless 'final' is set */ |
| 4184 | if (!final && size >= 1 && is_dbcs_lead_byte(s, size - 1)) |
| 4185 | --size; |
| 4186 | |
| 4187 | /* First get the size of the result */ |
| 4188 | if (size > 0) { |
| 4189 | usize = MultiByteToWideChar(CP_ACP, 0, s, size, NULL, 0); |
| 4190 | if (usize == 0) { |
| 4191 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 4192 | return -1; |
| 4193 | } |
| 4194 | } |
| 4195 | |
| 4196 | if (*v == NULL) { |
| 4197 | /* Create unicode object */ |
| 4198 | *v = _PyUnicode_New(usize); |
| 4199 | if (*v == NULL) |
| 4200 | return -1; |
| 4201 | } |
| 4202 | else { |
| 4203 | /* Extend unicode object */ |
| 4204 | n = PyUnicode_GET_SIZE(*v); |
| 4205 | if (_PyUnicode_Resize(v, n + usize) < 0) |
| 4206 | return -1; |
| 4207 | } |
| 4208 | |
| 4209 | /* Do the conversion */ |
| 4210 | if (size > 0) { |
| 4211 | p = PyUnicode_AS_UNICODE(*v) + n; |
| 4212 | if (0 == MultiByteToWideChar(CP_ACP, 0, s, size, p, usize)) { |
| 4213 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 4214 | return -1; |
| 4215 | } |
| 4216 | } |
| 4217 | |
| 4218 | return size; |
| 4219 | } |
| 4220 | |
| 4221 | PyObject *PyUnicode_DecodeMBCSStateful(const char *s, |
| 4222 | Py_ssize_t size, |
| 4223 | const char *errors, |
| 4224 | Py_ssize_t *consumed) |
| 4225 | { |
| 4226 | PyUnicodeObject *v = NULL; |
| 4227 | int done; |
| 4228 | |
| 4229 | if (consumed) |
| 4230 | *consumed = 0; |
| 4231 | |
| 4232 | #ifdef NEED_RETRY |
| 4233 | retry: |
| 4234 | if (size > INT_MAX) |
| 4235 | done = decode_mbcs(&v, s, INT_MAX, 0); |
| 4236 | else |
| 4237 | #endif |
| 4238 | done = decode_mbcs(&v, s, (int)size, !consumed); |
| 4239 | |
| 4240 | if (done < 0) { |
| 4241 | Py_XDECREF(v); |
| 4242 | return NULL; |
| 4243 | } |
| 4244 | |
| 4245 | if (consumed) |
| 4246 | *consumed += done; |
| 4247 | |
| 4248 | #ifdef NEED_RETRY |
| 4249 | if (size > INT_MAX) { |
| 4250 | s += done; |
| 4251 | size -= done; |
| 4252 | goto retry; |
| 4253 | } |
| 4254 | #endif |
| 4255 | |
| 4256 | return (PyObject *)v; |
| 4257 | } |
| 4258 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4259 | PyObject *PyUnicode_DecodeMBCS(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4260 | Py_ssize_t size, |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4261 | const char *errors) |
| 4262 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4263 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 4264 | } |
| 4265 | |
| 4266 | /* |
| 4267 | * Convert unicode into string object (MBCS). |
| 4268 | * Returns 0 if succeed, -1 otherwise. |
| 4269 | */ |
| 4270 | static int encode_mbcs(PyObject **repr, |
| 4271 | const Py_UNICODE *p, /* unicode */ |
| 4272 | int size) /* size of unicode */ |
| 4273 | { |
| 4274 | int mbcssize = 0; |
| 4275 | Py_ssize_t n = 0; |
| 4276 | |
| 4277 | assert(size >= 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4278 | |
| 4279 | /* First get the size of the result */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4280 | if (size > 0) { |
| 4281 | mbcssize = WideCharToMultiByte(CP_ACP, 0, p, size, NULL, 0, NULL, NULL); |
| 4282 | if (mbcssize == 0) { |
| 4283 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 4284 | return -1; |
| 4285 | } |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4286 | } |
| 4287 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4288 | if (*repr == NULL) { |
| 4289 | /* Create string object */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4290 | *repr = PyBytes_FromStringAndSize(NULL, mbcssize); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4291 | if (*repr == NULL) |
| 4292 | return -1; |
| 4293 | } |
| 4294 | else { |
| 4295 | /* Extend string object */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4296 | n = PyBytes_Size(*repr); |
Hirokazu Yamamoto | d88e8fa | 2008-12-27 14:58:17 +0000 | [diff] [blame] | 4297 | if (_PyBytes_Resize(repr, n + mbcssize) < 0) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4298 | return -1; |
| 4299 | } |
| 4300 | |
| 4301 | /* Do the conversion */ |
| 4302 | if (size > 0) { |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4303 | char *s = PyBytes_AS_STRING(*repr) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4304 | if (0 == WideCharToMultiByte(CP_ACP, 0, p, size, s, mbcssize, NULL, NULL)) { |
| 4305 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 4306 | return -1; |
| 4307 | } |
| 4308 | } |
| 4309 | |
| 4310 | return 0; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4311 | } |
| 4312 | |
| 4313 | PyObject *PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4314 | Py_ssize_t size, |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4315 | const char *errors) |
| 4316 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4317 | PyObject *repr = NULL; |
| 4318 | int ret; |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 4319 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4320 | #ifdef NEED_RETRY |
| 4321 | retry: |
| 4322 | if (size > INT_MAX) |
| 4323 | ret = encode_mbcs(&repr, p, INT_MAX); |
| 4324 | else |
| 4325 | #endif |
| 4326 | ret = encode_mbcs(&repr, p, (int)size); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4327 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4328 | if (ret < 0) { |
| 4329 | Py_XDECREF(repr); |
| 4330 | return NULL; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4331 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4332 | |
| 4333 | #ifdef NEED_RETRY |
| 4334 | if (size > INT_MAX) { |
| 4335 | p += INT_MAX; |
| 4336 | size -= INT_MAX; |
| 4337 | goto retry; |
| 4338 | } |
| 4339 | #endif |
| 4340 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4341 | return repr; |
| 4342 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 4343 | |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 4344 | PyObject *PyUnicode_AsMBCSString(PyObject *unicode) |
| 4345 | { |
| 4346 | if (!PyUnicode_Check(unicode)) { |
| 4347 | PyErr_BadArgument(); |
| 4348 | return NULL; |
| 4349 | } |
| 4350 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 4351 | PyUnicode_GET_SIZE(unicode), |
| 4352 | NULL); |
| 4353 | } |
| 4354 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4355 | #undef NEED_RETRY |
| 4356 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4357 | #endif /* MS_WINDOWS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4358 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4359 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 4360 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4361 | PyObject *PyUnicode_DecodeCharmap(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4362 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4363 | PyObject *mapping, |
| 4364 | const char *errors) |
| 4365 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4366 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4367 | Py_ssize_t startinpos; |
| 4368 | Py_ssize_t endinpos; |
| 4369 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4370 | const char *e; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4371 | PyUnicodeObject *v; |
| 4372 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4373 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4374 | PyObject *errorHandler = NULL; |
| 4375 | PyObject *exc = NULL; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4376 | Py_UNICODE *mapstring = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4377 | Py_ssize_t maplen = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4378 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4379 | /* Default to Latin-1 */ |
| 4380 | if (mapping == NULL) |
| 4381 | return PyUnicode_DecodeLatin1(s, size, errors); |
| 4382 | |
| 4383 | v = _PyUnicode_New(size); |
| 4384 | if (v == NULL) |
| 4385 | goto onError; |
| 4386 | if (size == 0) |
| 4387 | return (PyObject *)v; |
| 4388 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4389 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4390 | if (PyUnicode_CheckExact(mapping)) { |
| 4391 | mapstring = PyUnicode_AS_UNICODE(mapping); |
| 4392 | maplen = PyUnicode_GET_SIZE(mapping); |
| 4393 | while (s < e) { |
| 4394 | unsigned char ch = *s; |
| 4395 | Py_UNICODE x = 0xfffe; /* illegal value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4396 | |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4397 | if (ch < maplen) |
| 4398 | x = mapstring[ch]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4399 | |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4400 | if (x == 0xfffe) { |
| 4401 | /* undefined mapping */ |
| 4402 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 4403 | startinpos = s-starts; |
| 4404 | endinpos = startinpos+1; |
| 4405 | if (unicode_decode_call_errorhandler( |
| 4406 | errors, &errorHandler, |
| 4407 | "charmap", "character maps to <undefined>", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4408 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 4409 | &v, &outpos, &p)) { |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4410 | goto onError; |
Marc-André Lemburg | ec233e5 | 2001-01-06 14:59:58 +0000 | [diff] [blame] | 4411 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4412 | continue; |
Marc-André Lemburg | ec233e5 | 2001-01-06 14:59:58 +0000 | [diff] [blame] | 4413 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4414 | *p++ = x; |
| 4415 | ++s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4416 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4417 | } |
| 4418 | else { |
| 4419 | while (s < e) { |
| 4420 | unsigned char ch = *s; |
| 4421 | PyObject *w, *x; |
| 4422 | |
| 4423 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4424 | w = PyLong_FromLong((long)ch); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4425 | if (w == NULL) |
| 4426 | goto onError; |
| 4427 | x = PyObject_GetItem(mapping, w); |
| 4428 | Py_DECREF(w); |
| 4429 | if (x == NULL) { |
| 4430 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 4431 | /* No mapping found means: mapping is undefined. */ |
| 4432 | PyErr_Clear(); |
| 4433 | x = Py_None; |
| 4434 | Py_INCREF(x); |
| 4435 | } else |
| 4436 | goto onError; |
| 4437 | } |
| 4438 | |
| 4439 | /* Apply mapping */ |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4440 | if (PyLong_Check(x)) { |
| 4441 | long value = PyLong_AS_LONG(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4442 | if (value < 0 || value > 65535) { |
| 4443 | PyErr_SetString(PyExc_TypeError, |
| 4444 | "character mapping must be in range(65536)"); |
| 4445 | Py_DECREF(x); |
| 4446 | goto onError; |
| 4447 | } |
| 4448 | *p++ = (Py_UNICODE)value; |
| 4449 | } |
| 4450 | else if (x == Py_None) { |
| 4451 | /* undefined mapping */ |
| 4452 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 4453 | startinpos = s-starts; |
| 4454 | endinpos = startinpos+1; |
| 4455 | if (unicode_decode_call_errorhandler( |
| 4456 | errors, &errorHandler, |
| 4457 | "charmap", "character maps to <undefined>", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4458 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 4459 | &v, &outpos, &p)) { |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4460 | Py_DECREF(x); |
| 4461 | goto onError; |
| 4462 | } |
Walter Dörwald | d4fff17 | 2005-11-28 22:15:56 +0000 | [diff] [blame] | 4463 | Py_DECREF(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4464 | continue; |
| 4465 | } |
| 4466 | else if (PyUnicode_Check(x)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4467 | Py_ssize_t targetsize = PyUnicode_GET_SIZE(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4468 | |
| 4469 | if (targetsize == 1) |
| 4470 | /* 1-1 mapping */ |
| 4471 | *p++ = *PyUnicode_AS_UNICODE(x); |
| 4472 | |
| 4473 | else if (targetsize > 1) { |
| 4474 | /* 1-n mapping */ |
| 4475 | if (targetsize > extrachars) { |
| 4476 | /* resize first */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4477 | Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v); |
| 4478 | Py_ssize_t needed = (targetsize - extrachars) + \ |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4479 | (targetsize << 2); |
| 4480 | extrachars += needed; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4481 | /* XXX overflow detection missing */ |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4482 | if (_PyUnicode_Resize(&v, |
| 4483 | PyUnicode_GET_SIZE(v) + needed) < 0) { |
| 4484 | Py_DECREF(x); |
| 4485 | goto onError; |
| 4486 | } |
| 4487 | p = PyUnicode_AS_UNICODE(v) + oldpos; |
| 4488 | } |
| 4489 | Py_UNICODE_COPY(p, |
| 4490 | PyUnicode_AS_UNICODE(x), |
| 4491 | targetsize); |
| 4492 | p += targetsize; |
| 4493 | extrachars -= targetsize; |
| 4494 | } |
| 4495 | /* 1-0 mapping: skip the character */ |
| 4496 | } |
| 4497 | else { |
| 4498 | /* wrong return value */ |
| 4499 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 4500 | "character mapping must return integer, None or str"); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4501 | Py_DECREF(x); |
| 4502 | goto onError; |
| 4503 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4504 | Py_DECREF(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4505 | ++s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4506 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4507 | } |
| 4508 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4509 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4510 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4511 | Py_XDECREF(errorHandler); |
| 4512 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4513 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4514 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4515 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4516 | Py_XDECREF(errorHandler); |
| 4517 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4518 | Py_XDECREF(v); |
| 4519 | return NULL; |
| 4520 | } |
| 4521 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4522 | /* Charmap encoding: the lookup table */ |
| 4523 | |
| 4524 | struct encoding_map{ |
| 4525 | PyObject_HEAD |
| 4526 | unsigned char level1[32]; |
| 4527 | int count2, count3; |
| 4528 | unsigned char level23[1]; |
| 4529 | }; |
| 4530 | |
| 4531 | static PyObject* |
| 4532 | encoding_map_size(PyObject *obj, PyObject* args) |
| 4533 | { |
| 4534 | struct encoding_map *map = (struct encoding_map*)obj; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4535 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4536 | 128*map->count3); |
| 4537 | } |
| 4538 | |
| 4539 | static PyMethodDef encoding_map_methods[] = { |
| 4540 | {"size", encoding_map_size, METH_NOARGS, |
| 4541 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 4542 | { 0 } |
| 4543 | }; |
| 4544 | |
| 4545 | static void |
| 4546 | encoding_map_dealloc(PyObject* o) |
| 4547 | { |
| 4548 | PyObject_FREE(o); |
| 4549 | } |
| 4550 | |
| 4551 | static PyTypeObject EncodingMapType = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 4552 | PyVarObject_HEAD_INIT(NULL, 0) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4553 | "EncodingMap", /*tp_name*/ |
| 4554 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 4555 | 0, /*tp_itemsize*/ |
| 4556 | /* methods */ |
| 4557 | encoding_map_dealloc, /*tp_dealloc*/ |
| 4558 | 0, /*tp_print*/ |
| 4559 | 0, /*tp_getattr*/ |
| 4560 | 0, /*tp_setattr*/ |
| 4561 | 0, /*tp_compare*/ |
| 4562 | 0, /*tp_repr*/ |
| 4563 | 0, /*tp_as_number*/ |
| 4564 | 0, /*tp_as_sequence*/ |
| 4565 | 0, /*tp_as_mapping*/ |
| 4566 | 0, /*tp_hash*/ |
| 4567 | 0, /*tp_call*/ |
| 4568 | 0, /*tp_str*/ |
| 4569 | 0, /*tp_getattro*/ |
| 4570 | 0, /*tp_setattro*/ |
| 4571 | 0, /*tp_as_buffer*/ |
| 4572 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 4573 | 0, /*tp_doc*/ |
| 4574 | 0, /*tp_traverse*/ |
| 4575 | 0, /*tp_clear*/ |
| 4576 | 0, /*tp_richcompare*/ |
| 4577 | 0, /*tp_weaklistoffset*/ |
| 4578 | 0, /*tp_iter*/ |
| 4579 | 0, /*tp_iternext*/ |
| 4580 | encoding_map_methods, /*tp_methods*/ |
| 4581 | 0, /*tp_members*/ |
| 4582 | 0, /*tp_getset*/ |
| 4583 | 0, /*tp_base*/ |
| 4584 | 0, /*tp_dict*/ |
| 4585 | 0, /*tp_descr_get*/ |
| 4586 | 0, /*tp_descr_set*/ |
| 4587 | 0, /*tp_dictoffset*/ |
| 4588 | 0, /*tp_init*/ |
| 4589 | 0, /*tp_alloc*/ |
| 4590 | 0, /*tp_new*/ |
| 4591 | 0, /*tp_free*/ |
| 4592 | 0, /*tp_is_gc*/ |
| 4593 | }; |
| 4594 | |
| 4595 | PyObject* |
| 4596 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 4597 | { |
| 4598 | Py_UNICODE *decode; |
| 4599 | PyObject *result; |
| 4600 | struct encoding_map *mresult; |
| 4601 | int i; |
| 4602 | int need_dict = 0; |
| 4603 | unsigned char level1[32]; |
| 4604 | unsigned char level2[512]; |
| 4605 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 4606 | int count2 = 0, count3 = 0; |
| 4607 | |
| 4608 | if (!PyUnicode_Check(string) || PyUnicode_GetSize(string) != 256) { |
| 4609 | PyErr_BadArgument(); |
| 4610 | return NULL; |
| 4611 | } |
| 4612 | decode = PyUnicode_AS_UNICODE(string); |
| 4613 | memset(level1, 0xFF, sizeof level1); |
| 4614 | memset(level2, 0xFF, sizeof level2); |
| 4615 | |
| 4616 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 4617 | or if there are non-BMP characters, we need to use |
| 4618 | a mapping dictionary. */ |
| 4619 | if (decode[0] != 0) |
| 4620 | need_dict = 1; |
| 4621 | for (i = 1; i < 256; i++) { |
| 4622 | int l1, l2; |
| 4623 | if (decode[i] == 0 |
| 4624 | #ifdef Py_UNICODE_WIDE |
| 4625 | || decode[i] > 0xFFFF |
| 4626 | #endif |
| 4627 | ) { |
| 4628 | need_dict = 1; |
| 4629 | break; |
| 4630 | } |
| 4631 | if (decode[i] == 0xFFFE) |
| 4632 | /* unmapped character */ |
| 4633 | continue; |
| 4634 | l1 = decode[i] >> 11; |
| 4635 | l2 = decode[i] >> 7; |
| 4636 | if (level1[l1] == 0xFF) |
| 4637 | level1[l1] = count2++; |
| 4638 | if (level2[l2] == 0xFF) |
| 4639 | level2[l2] = count3++; |
| 4640 | } |
| 4641 | |
| 4642 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 4643 | need_dict = 1; |
| 4644 | |
| 4645 | if (need_dict) { |
| 4646 | PyObject *result = PyDict_New(); |
| 4647 | PyObject *key, *value; |
| 4648 | if (!result) |
| 4649 | return NULL; |
| 4650 | for (i = 0; i < 256; i++) { |
| 4651 | key = value = NULL; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4652 | key = PyLong_FromLong(decode[i]); |
| 4653 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4654 | if (!key || !value) |
| 4655 | goto failed1; |
| 4656 | if (PyDict_SetItem(result, key, value) == -1) |
| 4657 | goto failed1; |
| 4658 | Py_DECREF(key); |
| 4659 | Py_DECREF(value); |
| 4660 | } |
| 4661 | return result; |
| 4662 | failed1: |
| 4663 | Py_XDECREF(key); |
| 4664 | Py_XDECREF(value); |
| 4665 | Py_DECREF(result); |
| 4666 | return NULL; |
| 4667 | } |
| 4668 | |
| 4669 | /* Create a three-level trie */ |
| 4670 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 4671 | 16*count2 + 128*count3 - 1); |
| 4672 | if (!result) |
| 4673 | return PyErr_NoMemory(); |
| 4674 | PyObject_Init(result, &EncodingMapType); |
| 4675 | mresult = (struct encoding_map*)result; |
| 4676 | mresult->count2 = count2; |
| 4677 | mresult->count3 = count3; |
| 4678 | mlevel1 = mresult->level1; |
| 4679 | mlevel2 = mresult->level23; |
| 4680 | mlevel3 = mresult->level23 + 16*count2; |
| 4681 | memcpy(mlevel1, level1, 32); |
| 4682 | memset(mlevel2, 0xFF, 16*count2); |
| 4683 | memset(mlevel3, 0, 128*count3); |
| 4684 | count3 = 0; |
| 4685 | for (i = 1; i < 256; i++) { |
| 4686 | int o1, o2, o3, i2, i3; |
| 4687 | if (decode[i] == 0xFFFE) |
| 4688 | /* unmapped character */ |
| 4689 | continue; |
| 4690 | o1 = decode[i]>>11; |
| 4691 | o2 = (decode[i]>>7) & 0xF; |
| 4692 | i2 = 16*mlevel1[o1] + o2; |
| 4693 | if (mlevel2[i2] == 0xFF) |
| 4694 | mlevel2[i2] = count3++; |
| 4695 | o3 = decode[i] & 0x7F; |
| 4696 | i3 = 128*mlevel2[i2] + o3; |
| 4697 | mlevel3[i3] = i; |
| 4698 | } |
| 4699 | return result; |
| 4700 | } |
| 4701 | |
| 4702 | static int |
| 4703 | encoding_map_lookup(Py_UNICODE c, PyObject *mapping) |
| 4704 | { |
| 4705 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 4706 | int l1 = c>>11; |
| 4707 | int l2 = (c>>7) & 0xF; |
| 4708 | int l3 = c & 0x7F; |
| 4709 | int i; |
| 4710 | |
| 4711 | #ifdef Py_UNICODE_WIDE |
| 4712 | if (c > 0xFFFF) { |
| 4713 | return -1; |
| 4714 | } |
| 4715 | #endif |
| 4716 | if (c == 0) |
| 4717 | return 0; |
| 4718 | /* level 1*/ |
| 4719 | i = map->level1[l1]; |
| 4720 | if (i == 0xFF) { |
| 4721 | return -1; |
| 4722 | } |
| 4723 | /* level 2*/ |
| 4724 | i = map->level23[16*i+l2]; |
| 4725 | if (i == 0xFF) { |
| 4726 | return -1; |
| 4727 | } |
| 4728 | /* level 3 */ |
| 4729 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 4730 | if (i == 0) { |
| 4731 | return -1; |
| 4732 | } |
| 4733 | return i; |
| 4734 | } |
| 4735 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4736 | /* Lookup the character ch in the mapping. If the character |
| 4737 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 4738 | error occurred). */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4739 | static PyObject *charmapencode_lookup(Py_UNICODE c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4740 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4741 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4742 | PyObject *x; |
| 4743 | |
| 4744 | if (w == NULL) |
| 4745 | return NULL; |
| 4746 | x = PyObject_GetItem(mapping, w); |
| 4747 | Py_DECREF(w); |
| 4748 | if (x == NULL) { |
| 4749 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 4750 | /* No mapping found means: mapping is undefined. */ |
| 4751 | PyErr_Clear(); |
| 4752 | x = Py_None; |
| 4753 | Py_INCREF(x); |
| 4754 | return x; |
| 4755 | } else |
| 4756 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4757 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 4758 | else if (x == Py_None) |
| 4759 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4760 | else if (PyLong_Check(x)) { |
| 4761 | long value = PyLong_AS_LONG(x); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4762 | if (value < 0 || value > 255) { |
| 4763 | PyErr_SetString(PyExc_TypeError, |
| 4764 | "character mapping must be in range(256)"); |
| 4765 | Py_DECREF(x); |
| 4766 | return NULL; |
| 4767 | } |
| 4768 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4769 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4770 | else if (PyBytes_Check(x)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4771 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4772 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4773 | /* wrong return value */ |
Walter Dörwald | 580ceed | 2007-05-09 10:39:19 +0000 | [diff] [blame] | 4774 | PyErr_Format(PyExc_TypeError, |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 4775 | "character mapping must return integer, bytes or None, not %.400s", |
Walter Dörwald | 580ceed | 2007-05-09 10:39:19 +0000 | [diff] [blame] | 4776 | x->ob_type->tp_name); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4777 | Py_DECREF(x); |
| 4778 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4779 | } |
| 4780 | } |
| 4781 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4782 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4783 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4784 | { |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4785 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4786 | /* exponentially overallocate to minimize reallocations */ |
| 4787 | if (requiredsize < 2*outsize) |
| 4788 | requiredsize = 2*outsize; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4789 | if (_PyBytes_Resize(outobj, requiredsize)) |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4790 | return -1; |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4791 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4792 | } |
| 4793 | |
| 4794 | typedef enum charmapencode_result { |
| 4795 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
| 4796 | }charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4797 | /* 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] | 4798 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4799 | space is available. Return a new reference to the object that |
| 4800 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 4801 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 4802 | reallocation error occurred. The caller must decref the result */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4803 | static |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4804 | charmapencode_result charmapencode_output(Py_UNICODE c, PyObject *mapping, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4805 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4806 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4807 | PyObject *rep; |
| 4808 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4809 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4810 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 4811 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4812 | int res = encoding_map_lookup(c, mapping); |
| 4813 | Py_ssize_t requiredsize = *outpos+1; |
| 4814 | if (res == -1) |
| 4815 | return enc_FAILED; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4816 | if (outsize<requiredsize) |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4817 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4818 | return enc_EXCEPTION; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4819 | outstart = PyBytes_AS_STRING(*outobj); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4820 | outstart[(*outpos)++] = (char)res; |
| 4821 | return enc_SUCCESS; |
| 4822 | } |
| 4823 | |
| 4824 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4825 | if (rep==NULL) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4826 | return enc_EXCEPTION; |
| 4827 | else if (rep==Py_None) { |
| 4828 | Py_DECREF(rep); |
| 4829 | return enc_FAILED; |
| 4830 | } else { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4831 | if (PyLong_Check(rep)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4832 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4833 | if (outsize<requiredsize) |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4834 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4835 | Py_DECREF(rep); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4836 | return enc_EXCEPTION; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4837 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4838 | outstart = PyBytes_AS_STRING(*outobj); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4839 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4840 | } |
| 4841 | else { |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4842 | const char *repchars = PyBytes_AS_STRING(rep); |
| 4843 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4844 | Py_ssize_t requiredsize = *outpos+repsize; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4845 | if (outsize<requiredsize) |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4846 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4847 | Py_DECREF(rep); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4848 | return enc_EXCEPTION; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4849 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4850 | outstart = PyBytes_AS_STRING(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4851 | memcpy(outstart + *outpos, repchars, repsize); |
| 4852 | *outpos += repsize; |
| 4853 | } |
| 4854 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4855 | Py_DECREF(rep); |
| 4856 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4857 | } |
| 4858 | |
| 4859 | /* handle an error in PyUnicode_EncodeCharmap |
| 4860 | Return 0 on success, -1 on error */ |
| 4861 | static |
| 4862 | int charmap_encoding_error( |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4863 | 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] | 4864 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 4865 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4866 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4867 | { |
| 4868 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4869 | Py_ssize_t repsize; |
| 4870 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4871 | Py_UNICODE *uni2; |
| 4872 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4873 | Py_ssize_t collstartpos = *inpos; |
| 4874 | Py_ssize_t collendpos = *inpos+1; |
| 4875 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4876 | char *encoding = "charmap"; |
| 4877 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4878 | charmapencode_result x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4879 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4880 | /* find all unencodable characters */ |
| 4881 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4882 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 4883 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4884 | int res = encoding_map_lookup(p[collendpos], mapping); |
| 4885 | if (res != -1) |
| 4886 | break; |
| 4887 | ++collendpos; |
| 4888 | continue; |
| 4889 | } |
| 4890 | |
| 4891 | rep = charmapencode_lookup(p[collendpos], mapping); |
| 4892 | if (rep==NULL) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4893 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4894 | else if (rep!=Py_None) { |
| 4895 | Py_DECREF(rep); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4896 | break; |
| 4897 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4898 | Py_DECREF(rep); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4899 | ++collendpos; |
| 4900 | } |
| 4901 | /* cache callback name lookup |
| 4902 | * (if not done yet, i.e. it's the first error) */ |
| 4903 | if (*known_errorHandler==-1) { |
| 4904 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 4905 | *known_errorHandler = 1; |
| 4906 | else if (!strcmp(errors, "replace")) |
| 4907 | *known_errorHandler = 2; |
| 4908 | else if (!strcmp(errors, "ignore")) |
| 4909 | *known_errorHandler = 3; |
| 4910 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 4911 | *known_errorHandler = 4; |
| 4912 | else |
| 4913 | *known_errorHandler = 0; |
| 4914 | } |
| 4915 | switch (*known_errorHandler) { |
| 4916 | case 1: /* strict */ |
| 4917 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 4918 | return -1; |
| 4919 | case 2: /* replace */ |
| 4920 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
| 4921 | x = charmapencode_output('?', mapping, res, respos); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4922 | if (x==enc_EXCEPTION) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4923 | return -1; |
| 4924 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4925 | else if (x==enc_FAILED) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4926 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 4927 | return -1; |
| 4928 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4929 | } |
| 4930 | /* fall through */ |
| 4931 | case 3: /* ignore */ |
| 4932 | *inpos = collendpos; |
| 4933 | break; |
| 4934 | case 4: /* xmlcharrefreplace */ |
| 4935 | /* generate replacement (temporarily (mis)uses p) */ |
| 4936 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
| 4937 | char buffer[2+29+1+1]; |
| 4938 | char *cp; |
| 4939 | sprintf(buffer, "&#%d;", (int)p[collpos]); |
| 4940 | for (cp = buffer; *cp; ++cp) { |
| 4941 | x = charmapencode_output(*cp, mapping, res, respos); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4942 | if (x==enc_EXCEPTION) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4943 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4944 | else if (x==enc_FAILED) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4945 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 4946 | return -1; |
| 4947 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4948 | } |
| 4949 | } |
| 4950 | *inpos = collendpos; |
| 4951 | break; |
| 4952 | default: |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 4953 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4954 | encoding, reason, p, size, exceptionObject, |
| 4955 | collstartpos, collendpos, &newpos); |
| 4956 | if (repunicode == NULL) |
| 4957 | return -1; |
| 4958 | /* generate replacement */ |
| 4959 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 4960 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 4961 | x = charmapencode_output(*uni2, mapping, res, respos); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4962 | if (x==enc_EXCEPTION) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4963 | return -1; |
| 4964 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4965 | else if (x==enc_FAILED) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4966 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4967 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 4968 | return -1; |
| 4969 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4970 | } |
| 4971 | *inpos = newpos; |
| 4972 | Py_DECREF(repunicode); |
| 4973 | } |
| 4974 | return 0; |
| 4975 | } |
| 4976 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4977 | PyObject *PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4978 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4979 | PyObject *mapping, |
| 4980 | const char *errors) |
| 4981 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4982 | /* output object */ |
| 4983 | PyObject *res = NULL; |
| 4984 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4985 | Py_ssize_t inpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4986 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4987 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4988 | PyObject *errorHandler = NULL; |
| 4989 | PyObject *exc = NULL; |
| 4990 | /* the following variable is used for caching string comparisons |
| 4991 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 4992 | * 3=ignore, 4=xmlcharrefreplace */ |
| 4993 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4994 | |
| 4995 | /* Default to Latin-1 */ |
| 4996 | if (mapping == NULL) |
| 4997 | return PyUnicode_EncodeLatin1(p, size, errors); |
| 4998 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4999 | /* allocate enough for a simple encoding without |
| 5000 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5001 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5002 | if (res == NULL) |
| 5003 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 5004 | if (size == 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5005 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5006 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5007 | while (inpos<size) { |
| 5008 | /* try to encode it */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5009 | charmapencode_result x = charmapencode_output(p[inpos], mapping, &res, &respos); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5010 | if (x==enc_EXCEPTION) /* error */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5011 | goto onError; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5012 | if (x==enc_FAILED) { /* unencodable character */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5013 | if (charmap_encoding_error(p, size, &inpos, mapping, |
| 5014 | &exc, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 5015 | &known_errorHandler, &errorHandler, errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5016 | &res, &respos)) { |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 5017 | goto onError; |
Walter Dörwald | 9b30f20 | 2003-08-15 16:26:34 +0000 | [diff] [blame] | 5018 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5019 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5020 | else |
| 5021 | /* done with this character => adjust input position */ |
| 5022 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5023 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5024 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5025 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5026 | if (respos<PyBytes_GET_SIZE(res)) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5027 | if (_PyBytes_Resize(&res, respos) < 0) |
| 5028 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5029 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5030 | Py_XDECREF(exc); |
| 5031 | Py_XDECREF(errorHandler); |
| 5032 | return res; |
| 5033 | |
| 5034 | onError: |
| 5035 | Py_XDECREF(res); |
| 5036 | Py_XDECREF(exc); |
| 5037 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5038 | return NULL; |
| 5039 | } |
| 5040 | |
| 5041 | PyObject *PyUnicode_AsCharmapString(PyObject *unicode, |
| 5042 | PyObject *mapping) |
| 5043 | { |
| 5044 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
| 5045 | PyErr_BadArgument(); |
| 5046 | return NULL; |
| 5047 | } |
| 5048 | return PyUnicode_EncodeCharmap(PyUnicode_AS_UNICODE(unicode), |
| 5049 | PyUnicode_GET_SIZE(unicode), |
| 5050 | mapping, |
| 5051 | NULL); |
| 5052 | } |
| 5053 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5054 | /* create or adjust a UnicodeTranslateError */ |
| 5055 | static void make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5056 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 5057 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5058 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5059 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5060 | if (*exceptionObject == NULL) { |
| 5061 | *exceptionObject = PyUnicodeTranslateError_Create( |
| 5062 | unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5063 | } |
| 5064 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5065 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 5066 | goto onError; |
| 5067 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 5068 | goto onError; |
| 5069 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 5070 | goto onError; |
| 5071 | return; |
| 5072 | onError: |
| 5073 | Py_DECREF(*exceptionObject); |
| 5074 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5075 | } |
| 5076 | } |
| 5077 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5078 | /* raises a UnicodeTranslateError */ |
| 5079 | static void raise_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5080 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 5081 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5082 | const char *reason) |
| 5083 | { |
| 5084 | make_translate_exception(exceptionObject, |
| 5085 | unicode, size, startpos, endpos, reason); |
| 5086 | if (*exceptionObject != NULL) |
| 5087 | PyCodec_StrictErrors(*exceptionObject); |
| 5088 | } |
| 5089 | |
| 5090 | /* error handling callback helper: |
| 5091 | build arguments, call the callback and check the arguments, |
| 5092 | put the result into newpos and return the replacement string, which |
| 5093 | has to be freed by the caller */ |
| 5094 | static PyObject *unicode_translate_call_errorhandler(const char *errors, |
| 5095 | PyObject **errorHandler, |
| 5096 | const char *reason, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5097 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 5098 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 5099 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5100 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 5101 | 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] | 5102 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5103 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5104 | PyObject *restuple; |
| 5105 | PyObject *resunicode; |
| 5106 | |
| 5107 | if (*errorHandler == NULL) { |
| 5108 | *errorHandler = PyCodec_LookupError(errors); |
| 5109 | if (*errorHandler == NULL) |
| 5110 | return NULL; |
| 5111 | } |
| 5112 | |
| 5113 | make_translate_exception(exceptionObject, |
| 5114 | unicode, size, startpos, endpos, reason); |
| 5115 | if (*exceptionObject == NULL) |
| 5116 | return NULL; |
| 5117 | |
| 5118 | restuple = PyObject_CallFunctionObjArgs( |
| 5119 | *errorHandler, *exceptionObject, NULL); |
| 5120 | if (restuple == NULL) |
| 5121 | return NULL; |
| 5122 | if (!PyTuple_Check(restuple)) { |
| 5123 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 5124 | Py_DECREF(restuple); |
| 5125 | return NULL; |
| 5126 | } |
| 5127 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5128 | &resunicode, &i_newpos)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5129 | Py_DECREF(restuple); |
| 5130 | return NULL; |
| 5131 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5132 | if (i_newpos<0) |
| 5133 | *newpos = size+i_newpos; |
| 5134 | else |
| 5135 | *newpos = i_newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 5136 | if (*newpos<0 || *newpos>size) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 5137 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 5138 | Py_DECREF(restuple); |
| 5139 | return NULL; |
| 5140 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5141 | Py_INCREF(resunicode); |
| 5142 | Py_DECREF(restuple); |
| 5143 | return resunicode; |
| 5144 | } |
| 5145 | |
| 5146 | /* Lookup the character ch in the mapping and put the result in result, |
| 5147 | which must be decrefed by the caller. |
| 5148 | Return 0 on success, -1 on error */ |
| 5149 | static |
| 5150 | int charmaptranslate_lookup(Py_UNICODE c, PyObject *mapping, PyObject **result) |
| 5151 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5152 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5153 | PyObject *x; |
| 5154 | |
| 5155 | if (w == NULL) |
| 5156 | return -1; |
| 5157 | x = PyObject_GetItem(mapping, w); |
| 5158 | Py_DECREF(w); |
| 5159 | if (x == NULL) { |
| 5160 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 5161 | /* No mapping found means: use 1:1 mapping. */ |
| 5162 | PyErr_Clear(); |
| 5163 | *result = NULL; |
| 5164 | return 0; |
| 5165 | } else |
| 5166 | return -1; |
| 5167 | } |
| 5168 | else if (x == Py_None) { |
| 5169 | *result = x; |
| 5170 | return 0; |
| 5171 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5172 | else if (PyLong_Check(x)) { |
| 5173 | long value = PyLong_AS_LONG(x); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5174 | long max = PyUnicode_GetMax(); |
| 5175 | if (value < 0 || value > max) { |
| 5176 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 5177 | "character mapping must be in range(0x%x)", max+1); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5178 | Py_DECREF(x); |
| 5179 | return -1; |
| 5180 | } |
| 5181 | *result = x; |
| 5182 | return 0; |
| 5183 | } |
| 5184 | else if (PyUnicode_Check(x)) { |
| 5185 | *result = x; |
| 5186 | return 0; |
| 5187 | } |
| 5188 | else { |
| 5189 | /* wrong return value */ |
| 5190 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 5191 | "character mapping must return integer, None or str"); |
Walter Dörwald | 150523e | 2003-08-15 16:52:19 +0000 | [diff] [blame] | 5192 | Py_DECREF(x); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5193 | return -1; |
| 5194 | } |
| 5195 | } |
| 5196 | /* ensure that *outobj is at least requiredsize characters long, |
| 5197 | if not reallocate and adjust various state variables. |
| 5198 | Return 0 on success, -1 on error */ |
| 5199 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5200 | int charmaptranslate_makespace(PyObject **outobj, Py_UNICODE **outp, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5201 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5202 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5203 | Py_ssize_t oldsize = PyUnicode_GET_SIZE(*outobj); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5204 | if (requiredsize > oldsize) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5205 | /* remember old output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5206 | Py_ssize_t outpos = *outp-PyUnicode_AS_UNICODE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5207 | /* exponentially overallocate to minimize reallocations */ |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5208 | if (requiredsize < 2 * oldsize) |
| 5209 | requiredsize = 2 * oldsize; |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 5210 | if (PyUnicode_Resize(outobj, requiredsize) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5211 | return -1; |
| 5212 | *outp = PyUnicode_AS_UNICODE(*outobj) + outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5213 | } |
| 5214 | return 0; |
| 5215 | } |
| 5216 | /* lookup the character, put the result in the output string and adjust |
| 5217 | various state variables. Return a new reference to the object that |
| 5218 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 5219 | undefined (in which case no character was written). |
| 5220 | The called must decref result. |
| 5221 | Return 0 on success, -1 on error. */ |
| 5222 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5223 | int charmaptranslate_output(const Py_UNICODE *startinp, const Py_UNICODE *curinp, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5224 | Py_ssize_t insize, PyObject *mapping, PyObject **outobj, Py_UNICODE **outp, |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5225 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5226 | { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5227 | if (charmaptranslate_lookup(*curinp, mapping, res)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5228 | return -1; |
| 5229 | if (*res==NULL) { |
| 5230 | /* not found => default to 1:1 mapping */ |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5231 | *(*outp)++ = *curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5232 | } |
| 5233 | else if (*res==Py_None) |
| 5234 | ; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5235 | else if (PyLong_Check(*res)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5236 | /* no overflow check, because we know that the space is enough */ |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5237 | *(*outp)++ = (Py_UNICODE)PyLong_AS_LONG(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5238 | } |
| 5239 | else if (PyUnicode_Check(*res)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5240 | Py_ssize_t repsize = PyUnicode_GET_SIZE(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5241 | if (repsize==1) { |
| 5242 | /* no overflow check, because we know that the space is enough */ |
| 5243 | *(*outp)++ = *PyUnicode_AS_UNICODE(*res); |
| 5244 | } |
| 5245 | else if (repsize!=0) { |
| 5246 | /* more than one character */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5247 | Py_ssize_t requiredsize = (*outp-PyUnicode_AS_UNICODE(*outobj)) + |
Walter Dörwald | cd736e7 | 2004-02-05 17:36:00 +0000 | [diff] [blame] | 5248 | (insize - (curinp-startinp)) + |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5249 | repsize - 1; |
| 5250 | if (charmaptranslate_makespace(outobj, outp, requiredsize)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5251 | return -1; |
| 5252 | memcpy(*outp, PyUnicode_AS_UNICODE(*res), sizeof(Py_UNICODE)*repsize); |
| 5253 | *outp += repsize; |
| 5254 | } |
| 5255 | } |
| 5256 | else |
| 5257 | return -1; |
| 5258 | return 0; |
| 5259 | } |
| 5260 | |
| 5261 | PyObject *PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5262 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5263 | PyObject *mapping, |
| 5264 | const char *errors) |
| 5265 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5266 | /* output object */ |
| 5267 | PyObject *res = NULL; |
| 5268 | /* pointers to the beginning and end+1 of input */ |
| 5269 | const Py_UNICODE *startp = p; |
| 5270 | const Py_UNICODE *endp = p + size; |
| 5271 | /* pointer into the output */ |
| 5272 | Py_UNICODE *str; |
| 5273 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5274 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5275 | char *reason = "character maps to <undefined>"; |
| 5276 | PyObject *errorHandler = NULL; |
| 5277 | PyObject *exc = NULL; |
| 5278 | /* the following variable is used for caching string comparisons |
| 5279 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 5280 | * 3=ignore, 4=xmlcharrefreplace */ |
| 5281 | int known_errorHandler = -1; |
| 5282 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5283 | if (mapping == NULL) { |
| 5284 | PyErr_BadArgument(); |
| 5285 | return NULL; |
| 5286 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5287 | |
| 5288 | /* allocate enough for a simple 1:1 translation without |
| 5289 | replacements, if we need more, we'll resize */ |
| 5290 | res = PyUnicode_FromUnicode(NULL, size); |
| 5291 | if (res == NULL) |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5292 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5293 | if (size == 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5294 | return res; |
| 5295 | str = PyUnicode_AS_UNICODE(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5296 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5297 | while (p<endp) { |
| 5298 | /* try to encode it */ |
| 5299 | PyObject *x = NULL; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5300 | if (charmaptranslate_output(startp, p, size, mapping, &res, &str, &x)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5301 | Py_XDECREF(x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5302 | goto onError; |
| 5303 | } |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 5304 | Py_XDECREF(x); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5305 | if (x!=Py_None) /* it worked => adjust input pointer */ |
| 5306 | ++p; |
| 5307 | else { /* untranslatable character */ |
| 5308 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5309 | Py_ssize_t repsize; |
| 5310 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5311 | Py_UNICODE *uni2; |
| 5312 | /* startpos for collecting untranslatable chars */ |
| 5313 | const Py_UNICODE *collstart = p; |
| 5314 | const Py_UNICODE *collend = p+1; |
| 5315 | const Py_UNICODE *coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5316 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5317 | /* find all untranslatable characters */ |
| 5318 | while (collend < endp) { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5319 | if (charmaptranslate_lookup(*collend, mapping, &x)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5320 | goto onError; |
| 5321 | Py_XDECREF(x); |
| 5322 | if (x!=Py_None) |
| 5323 | break; |
| 5324 | ++collend; |
| 5325 | } |
| 5326 | /* cache callback name lookup |
| 5327 | * (if not done yet, i.e. it's the first error) */ |
| 5328 | if (known_errorHandler==-1) { |
| 5329 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 5330 | known_errorHandler = 1; |
| 5331 | else if (!strcmp(errors, "replace")) |
| 5332 | known_errorHandler = 2; |
| 5333 | else if (!strcmp(errors, "ignore")) |
| 5334 | known_errorHandler = 3; |
| 5335 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 5336 | known_errorHandler = 4; |
| 5337 | else |
| 5338 | known_errorHandler = 0; |
| 5339 | } |
| 5340 | switch (known_errorHandler) { |
| 5341 | case 1: /* strict */ |
| 5342 | raise_translate_exception(&exc, startp, size, collstart-startp, collend-startp, reason); |
| 5343 | goto onError; |
| 5344 | case 2: /* replace */ |
| 5345 | /* No need to check for space, this is a 1:1 replacement */ |
| 5346 | for (coll = collstart; coll<collend; ++coll) |
| 5347 | *str++ = '?'; |
| 5348 | /* fall through */ |
| 5349 | case 3: /* ignore */ |
| 5350 | p = collend; |
| 5351 | break; |
| 5352 | case 4: /* xmlcharrefreplace */ |
| 5353 | /* generate replacement (temporarily (mis)uses p) */ |
| 5354 | for (p = collstart; p < collend; ++p) { |
| 5355 | char buffer[2+29+1+1]; |
| 5356 | char *cp; |
| 5357 | sprintf(buffer, "&#%d;", (int)*p); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5358 | if (charmaptranslate_makespace(&res, &str, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5359 | (str-PyUnicode_AS_UNICODE(res))+strlen(buffer)+(endp-collend))) |
| 5360 | goto onError; |
| 5361 | for (cp = buffer; *cp; ++cp) |
| 5362 | *str++ = *cp; |
| 5363 | } |
| 5364 | p = collend; |
| 5365 | break; |
| 5366 | default: |
| 5367 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
| 5368 | reason, startp, size, &exc, |
| 5369 | collstart-startp, collend-startp, &newpos); |
| 5370 | if (repunicode == NULL) |
| 5371 | goto onError; |
| 5372 | /* generate replacement */ |
| 5373 | repsize = PyUnicode_GET_SIZE(repunicode); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5374 | if (charmaptranslate_makespace(&res, &str, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5375 | (str-PyUnicode_AS_UNICODE(res))+repsize+(endp-collend))) { |
| 5376 | Py_DECREF(repunicode); |
| 5377 | goto onError; |
| 5378 | } |
| 5379 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) |
| 5380 | *str++ = *uni2; |
| 5381 | p = startp + newpos; |
| 5382 | Py_DECREF(repunicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5383 | } |
| 5384 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5385 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5386 | /* Resize if we allocated to much */ |
| 5387 | respos = str-PyUnicode_AS_UNICODE(res); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5388 | if (respos<PyUnicode_GET_SIZE(res)) { |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 5389 | if (PyUnicode_Resize(&res, respos) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 5390 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5391 | } |
| 5392 | Py_XDECREF(exc); |
| 5393 | Py_XDECREF(errorHandler); |
| 5394 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5395 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5396 | onError: |
| 5397 | Py_XDECREF(res); |
| 5398 | Py_XDECREF(exc); |
| 5399 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5400 | return NULL; |
| 5401 | } |
| 5402 | |
| 5403 | PyObject *PyUnicode_Translate(PyObject *str, |
| 5404 | PyObject *mapping, |
| 5405 | const char *errors) |
| 5406 | { |
| 5407 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5408 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5409 | str = PyUnicode_FromObject(str); |
| 5410 | if (str == NULL) |
| 5411 | goto onError; |
| 5412 | result = PyUnicode_TranslateCharmap(PyUnicode_AS_UNICODE(str), |
| 5413 | PyUnicode_GET_SIZE(str), |
| 5414 | mapping, |
| 5415 | errors); |
| 5416 | Py_DECREF(str); |
| 5417 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5418 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5419 | onError: |
| 5420 | Py_XDECREF(str); |
| 5421 | return NULL; |
| 5422 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5423 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5424 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 5425 | |
| 5426 | int PyUnicode_EncodeDecimal(Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5427 | Py_ssize_t length, |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5428 | char *output, |
| 5429 | const char *errors) |
| 5430 | { |
| 5431 | Py_UNICODE *p, *end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5432 | PyObject *errorHandler = NULL; |
| 5433 | PyObject *exc = NULL; |
| 5434 | const char *encoding = "decimal"; |
| 5435 | const char *reason = "invalid decimal Unicode string"; |
| 5436 | /* the following variable is used for caching string comparisons |
| 5437 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 5438 | int known_errorHandler = -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5439 | |
| 5440 | if (output == NULL) { |
| 5441 | PyErr_BadArgument(); |
| 5442 | return -1; |
| 5443 | } |
| 5444 | |
| 5445 | p = s; |
| 5446 | end = s + length; |
| 5447 | while (p < end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5448 | register Py_UNICODE ch = *p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5449 | int decimal; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5450 | PyObject *repunicode; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5451 | Py_ssize_t repsize; |
| 5452 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5453 | Py_UNICODE *uni2; |
| 5454 | Py_UNICODE *collstart; |
| 5455 | Py_UNICODE *collend; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5456 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5457 | if (Py_UNICODE_ISSPACE(ch)) { |
| 5458 | *output++ = ' '; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5459 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5460 | continue; |
| 5461 | } |
| 5462 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 5463 | if (decimal >= 0) { |
| 5464 | *output++ = '0' + decimal; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5465 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5466 | continue; |
| 5467 | } |
Guido van Rossum | ba47704 | 2000-04-06 18:18:10 +0000 | [diff] [blame] | 5468 | if (0 < ch && ch < 256) { |
Guido van Rossum | 42c29aa | 2000-05-03 23:58:29 +0000 | [diff] [blame] | 5469 | *output++ = (char)ch; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5470 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5471 | continue; |
| 5472 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5473 | /* All other characters are considered unencodable */ |
| 5474 | collstart = p; |
| 5475 | collend = p+1; |
| 5476 | while (collend < end) { |
| 5477 | if ((0 < *collend && *collend < 256) || |
| 5478 | !Py_UNICODE_ISSPACE(*collend) || |
| 5479 | Py_UNICODE_TODECIMAL(*collend)) |
| 5480 | break; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5481 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5482 | /* cache callback name lookup |
| 5483 | * (if not done yet, i.e. it's the first error) */ |
| 5484 | if (known_errorHandler==-1) { |
| 5485 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 5486 | known_errorHandler = 1; |
| 5487 | else if (!strcmp(errors, "replace")) |
| 5488 | known_errorHandler = 2; |
| 5489 | else if (!strcmp(errors, "ignore")) |
| 5490 | known_errorHandler = 3; |
| 5491 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 5492 | known_errorHandler = 4; |
| 5493 | else |
| 5494 | known_errorHandler = 0; |
| 5495 | } |
| 5496 | switch (known_errorHandler) { |
| 5497 | case 1: /* strict */ |
| 5498 | raise_encode_exception(&exc, encoding, s, length, collstart-s, collend-s, reason); |
| 5499 | goto onError; |
| 5500 | case 2: /* replace */ |
| 5501 | for (p = collstart; p < collend; ++p) |
| 5502 | *output++ = '?'; |
| 5503 | /* fall through */ |
| 5504 | case 3: /* ignore */ |
| 5505 | p = collend; |
| 5506 | break; |
| 5507 | case 4: /* xmlcharrefreplace */ |
| 5508 | /* generate replacement (temporarily (mis)uses p) */ |
| 5509 | for (p = collstart; p < collend; ++p) |
| 5510 | output += sprintf(output, "&#%d;", (int)*p); |
| 5511 | p = collend; |
| 5512 | break; |
| 5513 | default: |
| 5514 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 5515 | encoding, reason, s, length, &exc, |
| 5516 | collstart-s, collend-s, &newpos); |
| 5517 | if (repunicode == NULL) |
| 5518 | goto onError; |
| 5519 | /* generate replacement */ |
| 5520 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 5521 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 5522 | Py_UNICODE ch = *uni2; |
| 5523 | if (Py_UNICODE_ISSPACE(ch)) |
| 5524 | *output++ = ' '; |
| 5525 | else { |
| 5526 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 5527 | if (decimal >= 0) |
| 5528 | *output++ = '0' + decimal; |
| 5529 | else if (0 < ch && ch < 256) |
| 5530 | *output++ = (char)ch; |
| 5531 | else { |
| 5532 | Py_DECREF(repunicode); |
| 5533 | raise_encode_exception(&exc, encoding, |
| 5534 | s, length, collstart-s, collend-s, reason); |
| 5535 | goto onError; |
| 5536 | } |
| 5537 | } |
| 5538 | } |
| 5539 | p = s + newpos; |
| 5540 | Py_DECREF(repunicode); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5541 | } |
| 5542 | } |
| 5543 | /* 0-terminate the output string */ |
| 5544 | *output++ = '\0'; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5545 | Py_XDECREF(exc); |
| 5546 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5547 | return 0; |
| 5548 | |
| 5549 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5550 | Py_XDECREF(exc); |
| 5551 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5552 | return -1; |
| 5553 | } |
| 5554 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5555 | /* --- Helpers ------------------------------------------------------------ */ |
| 5556 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 5557 | #include "stringlib/unicodedefs.h" |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5558 | #include "stringlib/fastsearch.h" |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5559 | #include "stringlib/count.h" |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 5560 | /* Include _ParseTupleFinds from find.h */ |
| 5561 | #define FROM_UNICODE |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5562 | #include "stringlib/find.h" |
| 5563 | #include "stringlib/partition.h" |
| 5564 | |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 5565 | #define _Py_InsertThousandsGrouping _PyUnicode_InsertThousandsGrouping |
| 5566 | #include "stringlib/localeutil.h" |
| 5567 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5568 | /* helper macro to fixup start/end slice values */ |
| 5569 | #define FIX_START_END(obj) \ |
| 5570 | if (start < 0) \ |
| 5571 | start += (obj)->length; \ |
| 5572 | if (start < 0) \ |
| 5573 | start = 0; \ |
| 5574 | if (end > (obj)->length) \ |
| 5575 | end = (obj)->length; \ |
| 5576 | if (end < 0) \ |
| 5577 | end += (obj)->length; \ |
| 5578 | if (end < 0) \ |
| 5579 | end = 0; |
| 5580 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5581 | Py_ssize_t PyUnicode_Count(PyObject *str, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5582 | PyObject *substr, |
| 5583 | Py_ssize_t start, |
| 5584 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5585 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5586 | Py_ssize_t result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5587 | PyUnicodeObject* str_obj; |
| 5588 | PyUnicodeObject* sub_obj; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5589 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5590 | str_obj = (PyUnicodeObject*) PyUnicode_FromObject(str); |
| 5591 | if (!str_obj) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5592 | return -1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5593 | sub_obj = (PyUnicodeObject*) PyUnicode_FromObject(substr); |
| 5594 | if (!sub_obj) { |
| 5595 | Py_DECREF(str_obj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5596 | return -1; |
| 5597 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5598 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5599 | FIX_START_END(str_obj); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5600 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5601 | result = stringlib_count( |
| 5602 | str_obj->str + start, end - start, sub_obj->str, sub_obj->length |
| 5603 | ); |
| 5604 | |
| 5605 | Py_DECREF(sub_obj); |
| 5606 | Py_DECREF(str_obj); |
| 5607 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5608 | return result; |
| 5609 | } |
| 5610 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5611 | Py_ssize_t PyUnicode_Find(PyObject *str, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5612 | PyObject *sub, |
| 5613 | Py_ssize_t start, |
| 5614 | Py_ssize_t end, |
| 5615 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5616 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5617 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5618 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5619 | str = PyUnicode_FromObject(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5620 | if (!str) |
Marc-André Lemburg | 4da6fd6 | 2002-05-29 11:33:13 +0000 | [diff] [blame] | 5621 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5622 | sub = PyUnicode_FromObject(sub); |
| 5623 | if (!sub) { |
Marc-André Lemburg | 4164439 | 2002-05-29 13:46:29 +0000 | [diff] [blame] | 5624 | Py_DECREF(str); |
Marc-André Lemburg | 4da6fd6 | 2002-05-29 11:33:13 +0000 | [diff] [blame] | 5625 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5626 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5627 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5628 | if (direction > 0) |
| 5629 | result = stringlib_find_slice( |
| 5630 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 5631 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 5632 | start, end |
| 5633 | ); |
| 5634 | else |
| 5635 | result = stringlib_rfind_slice( |
| 5636 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 5637 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 5638 | start, end |
| 5639 | ); |
| 5640 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5641 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5642 | Py_DECREF(sub); |
| 5643 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5644 | return result; |
| 5645 | } |
| 5646 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5647 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5648 | int tailmatch(PyUnicodeObject *self, |
| 5649 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5650 | Py_ssize_t start, |
| 5651 | Py_ssize_t end, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5652 | int direction) |
| 5653 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5654 | if (substring->length == 0) |
| 5655 | return 1; |
| 5656 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5657 | FIX_START_END(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5658 | |
| 5659 | end -= substring->length; |
| 5660 | if (end < start) |
| 5661 | return 0; |
| 5662 | |
| 5663 | if (direction > 0) { |
| 5664 | if (Py_UNICODE_MATCH(self, end, substring)) |
| 5665 | return 1; |
| 5666 | } else { |
| 5667 | if (Py_UNICODE_MATCH(self, start, substring)) |
| 5668 | return 1; |
| 5669 | } |
| 5670 | |
| 5671 | return 0; |
| 5672 | } |
| 5673 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5674 | Py_ssize_t PyUnicode_Tailmatch(PyObject *str, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5675 | PyObject *substr, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5676 | Py_ssize_t start, |
| 5677 | Py_ssize_t end, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5678 | int direction) |
| 5679 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5680 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5681 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5682 | str = PyUnicode_FromObject(str); |
| 5683 | if (str == NULL) |
| 5684 | return -1; |
| 5685 | substr = PyUnicode_FromObject(substr); |
| 5686 | if (substr == NULL) { |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 5687 | Py_DECREF(str); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5688 | return -1; |
| 5689 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5690 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5691 | result = tailmatch((PyUnicodeObject *)str, |
| 5692 | (PyUnicodeObject *)substr, |
| 5693 | start, end, direction); |
| 5694 | Py_DECREF(str); |
| 5695 | Py_DECREF(substr); |
| 5696 | return result; |
| 5697 | } |
| 5698 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5699 | /* Apply fixfct filter to the Unicode object self and return a |
| 5700 | reference to the modified object */ |
| 5701 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5702 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5703 | PyObject *fixup(PyUnicodeObject *self, |
| 5704 | int (*fixfct)(PyUnicodeObject *s)) |
| 5705 | { |
| 5706 | |
| 5707 | PyUnicodeObject *u; |
| 5708 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 5709 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5710 | if (u == NULL) |
| 5711 | return NULL; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 5712 | |
| 5713 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 5714 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5715 | if (!fixfct(u) && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5716 | /* fixfct should return TRUE if it modified the buffer. If |
| 5717 | FALSE, return a reference to the original buffer instead |
| 5718 | (to save space, not time) */ |
| 5719 | Py_INCREF(self); |
| 5720 | Py_DECREF(u); |
| 5721 | return (PyObject*) self; |
| 5722 | } |
| 5723 | return (PyObject*) u; |
| 5724 | } |
| 5725 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5726 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5727 | int fixupper(PyUnicodeObject *self) |
| 5728 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5729 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5730 | Py_UNICODE *s = self->str; |
| 5731 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5732 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5733 | while (len-- > 0) { |
| 5734 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5735 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5736 | ch = Py_UNICODE_TOUPPER(*s); |
| 5737 | if (ch != *s) { |
| 5738 | status = 1; |
| 5739 | *s = ch; |
| 5740 | } |
| 5741 | s++; |
| 5742 | } |
| 5743 | |
| 5744 | return status; |
| 5745 | } |
| 5746 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5747 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5748 | int fixlower(PyUnicodeObject *self) |
| 5749 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5750 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5751 | Py_UNICODE *s = self->str; |
| 5752 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5753 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5754 | while (len-- > 0) { |
| 5755 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5756 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5757 | ch = Py_UNICODE_TOLOWER(*s); |
| 5758 | if (ch != *s) { |
| 5759 | status = 1; |
| 5760 | *s = ch; |
| 5761 | } |
| 5762 | s++; |
| 5763 | } |
| 5764 | |
| 5765 | return status; |
| 5766 | } |
| 5767 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5768 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5769 | int fixswapcase(PyUnicodeObject *self) |
| 5770 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5771 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5772 | Py_UNICODE *s = self->str; |
| 5773 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5774 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5775 | while (len-- > 0) { |
| 5776 | if (Py_UNICODE_ISUPPER(*s)) { |
| 5777 | *s = Py_UNICODE_TOLOWER(*s); |
| 5778 | status = 1; |
| 5779 | } else if (Py_UNICODE_ISLOWER(*s)) { |
| 5780 | *s = Py_UNICODE_TOUPPER(*s); |
| 5781 | status = 1; |
| 5782 | } |
| 5783 | s++; |
| 5784 | } |
| 5785 | |
| 5786 | return status; |
| 5787 | } |
| 5788 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5789 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5790 | int fixcapitalize(PyUnicodeObject *self) |
| 5791 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5792 | Py_ssize_t len = self->length; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 5793 | Py_UNICODE *s = self->str; |
| 5794 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5795 | |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 5796 | if (len == 0) |
| 5797 | return 0; |
| 5798 | if (Py_UNICODE_ISLOWER(*s)) { |
| 5799 | *s = Py_UNICODE_TOUPPER(*s); |
| 5800 | status = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5801 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 5802 | s++; |
| 5803 | while (--len > 0) { |
| 5804 | if (Py_UNICODE_ISUPPER(*s)) { |
| 5805 | *s = Py_UNICODE_TOLOWER(*s); |
| 5806 | status = 1; |
| 5807 | } |
| 5808 | s++; |
| 5809 | } |
| 5810 | return status; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5811 | } |
| 5812 | |
| 5813 | static |
| 5814 | int fixtitle(PyUnicodeObject *self) |
| 5815 | { |
| 5816 | register Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5817 | register Py_UNICODE *e; |
| 5818 | int previous_is_cased; |
| 5819 | |
| 5820 | /* Shortcut for single character strings */ |
| 5821 | if (PyUnicode_GET_SIZE(self) == 1) { |
| 5822 | Py_UNICODE ch = Py_UNICODE_TOTITLE(*p); |
| 5823 | if (*p != ch) { |
| 5824 | *p = ch; |
| 5825 | return 1; |
| 5826 | } |
| 5827 | else |
| 5828 | return 0; |
| 5829 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5830 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5831 | e = p + PyUnicode_GET_SIZE(self); |
| 5832 | previous_is_cased = 0; |
| 5833 | for (; p < e; p++) { |
| 5834 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5835 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5836 | if (previous_is_cased) |
| 5837 | *p = Py_UNICODE_TOLOWER(ch); |
| 5838 | else |
| 5839 | *p = Py_UNICODE_TOTITLE(ch); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5840 | |
| 5841 | if (Py_UNICODE_ISLOWER(ch) || |
| 5842 | Py_UNICODE_ISUPPER(ch) || |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5843 | Py_UNICODE_ISTITLE(ch)) |
| 5844 | previous_is_cased = 1; |
| 5845 | else |
| 5846 | previous_is_cased = 0; |
| 5847 | } |
| 5848 | return 1; |
| 5849 | } |
| 5850 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5851 | PyObject * |
| 5852 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5853 | { |
Skip Montanaro | 6543b45 | 2004-09-16 03:28:13 +0000 | [diff] [blame] | 5854 | const Py_UNICODE blank = ' '; |
| 5855 | const Py_UNICODE *sep = ␣ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5856 | Py_ssize_t seplen = 1; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5857 | PyUnicodeObject *res = NULL; /* the result */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5858 | Py_UNICODE *res_p; /* pointer to free byte in res's string area */ |
| 5859 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 5860 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
| 5861 | PyObject **items; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5862 | PyObject *item; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 5863 | Py_ssize_t sz, i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5864 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5865 | fseq = PySequence_Fast(seq, ""); |
| 5866 | if (fseq == NULL) { |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5867 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5868 | } |
| 5869 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 5870 | /* NOTE: the following code can't call back into Python code, |
| 5871 | * so we are sure that fseq won't be mutated. |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 5872 | */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 5873 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5874 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 5875 | /* If empty sequence, return u"". */ |
| 5876 | if (seqlen == 0) { |
| 5877 | res = _PyUnicode_New(0); /* empty sequence; return u"" */ |
| 5878 | goto Done; |
| 5879 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 5880 | items = PySequence_Fast_ITEMS(fseq); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5881 | /* If singleton sequence with an exact Unicode, return that. */ |
| 5882 | if (seqlen == 1) { |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 5883 | item = items[0]; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5884 | if (PyUnicode_CheckExact(item)) { |
| 5885 | Py_INCREF(item); |
| 5886 | res = (PyUnicodeObject *)item; |
| 5887 | goto Done; |
| 5888 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5889 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 5890 | else { |
| 5891 | /* Set up sep and seplen */ |
| 5892 | if (separator == NULL) { |
| 5893 | sep = ␣ |
| 5894 | seplen = 1; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5895 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 5896 | else { |
| 5897 | if (!PyUnicode_Check(separator)) { |
| 5898 | PyErr_Format(PyExc_TypeError, |
| 5899 | "separator: expected str instance," |
| 5900 | " %.80s found", |
| 5901 | Py_TYPE(separator)->tp_name); |
| 5902 | goto onError; |
| 5903 | } |
| 5904 | sep = PyUnicode_AS_UNICODE(separator); |
| 5905 | seplen = PyUnicode_GET_SIZE(separator); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5906 | } |
| 5907 | } |
| 5908 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 5909 | /* There are at least two things to join, or else we have a subclass |
| 5910 | * of str in the sequence. |
| 5911 | * Do a pre-pass to figure out the total amount of space we'll |
| 5912 | * need (sz), and see whether all argument are strings. |
| 5913 | */ |
| 5914 | sz = 0; |
| 5915 | for (i = 0; i < seqlen; i++) { |
| 5916 | const Py_ssize_t old_sz = sz; |
| 5917 | item = items[i]; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5918 | if (!PyUnicode_Check(item)) { |
| 5919 | PyErr_Format(PyExc_TypeError, |
| 5920 | "sequence item %zd: expected str instance," |
| 5921 | " %.80s found", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 5922 | i, Py_TYPE(item)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5923 | goto onError; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5924 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 5925 | sz += PyUnicode_GET_SIZE(item); |
| 5926 | if (i != 0) |
| 5927 | sz += seplen; |
| 5928 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
| 5929 | PyErr_SetString(PyExc_OverflowError, |
| 5930 | "join() result is too long for a Python string"); |
| 5931 | goto onError; |
| 5932 | } |
| 5933 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5934 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 5935 | res = _PyUnicode_New(sz); |
| 5936 | if (res == NULL) |
| 5937 | goto onError; |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 5938 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 5939 | /* Catenate everything. */ |
| 5940 | res_p = PyUnicode_AS_UNICODE(res); |
| 5941 | for (i = 0; i < seqlen; ++i) { |
| 5942 | Py_ssize_t itemlen; |
| 5943 | item = items[i]; |
| 5944 | itemlen = PyUnicode_GET_SIZE(item); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5945 | /* Copy item, and maybe the separator. */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 5946 | if (i) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5947 | Py_UNICODE_COPY(res_p, sep, seplen); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5948 | res_p += seplen; |
| 5949 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 5950 | Py_UNICODE_COPY(res_p, PyUnicode_AS_UNICODE(item), itemlen); |
| 5951 | res_p += itemlen; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5952 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5953 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5954 | Done: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5955 | Py_DECREF(fseq); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5956 | return (PyObject *)res; |
| 5957 | |
| 5958 | onError: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5959 | Py_DECREF(fseq); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5960 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5961 | return NULL; |
| 5962 | } |
| 5963 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5964 | static |
| 5965 | PyUnicodeObject *pad(PyUnicodeObject *self, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5966 | Py_ssize_t left, |
| 5967 | Py_ssize_t right, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5968 | Py_UNICODE fill) |
| 5969 | { |
| 5970 | PyUnicodeObject *u; |
| 5971 | |
| 5972 | if (left < 0) |
| 5973 | left = 0; |
| 5974 | if (right < 0) |
| 5975 | right = 0; |
| 5976 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5977 | if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5978 | Py_INCREF(self); |
| 5979 | return self; |
| 5980 | } |
| 5981 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5982 | if (left > PY_SSIZE_T_MAX - self->length || |
| 5983 | right > PY_SSIZE_T_MAX - (left + self->length)) { |
| 5984 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 5985 | return NULL; |
| 5986 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5987 | u = _PyUnicode_New(left + self->length + right); |
| 5988 | if (u) { |
| 5989 | if (left) |
| 5990 | Py_UNICODE_FILL(u->str, fill, left); |
| 5991 | Py_UNICODE_COPY(u->str + left, self->str, self->length); |
| 5992 | if (right) |
| 5993 | Py_UNICODE_FILL(u->str + left + self->length, fill, right); |
| 5994 | } |
| 5995 | |
| 5996 | return u; |
| 5997 | } |
| 5998 | |
| 5999 | #define SPLIT_APPEND(data, left, right) \ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6000 | str = PyUnicode_FromUnicode((data) + (left), (right) - (left)); \ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6001 | if (!str) \ |
| 6002 | goto onError; \ |
| 6003 | if (PyList_Append(list, str)) { \ |
| 6004 | Py_DECREF(str); \ |
| 6005 | goto onError; \ |
| 6006 | } \ |
| 6007 | else \ |
| 6008 | Py_DECREF(str); |
| 6009 | |
| 6010 | static |
| 6011 | PyObject *split_whitespace(PyUnicodeObject *self, |
| 6012 | PyObject *list, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6013 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6014 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6015 | register Py_ssize_t i; |
| 6016 | register Py_ssize_t j; |
| 6017 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6018 | PyObject *str; |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 6019 | register const Py_UNICODE *buf = self->str; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6020 | |
| 6021 | for (i = j = 0; i < len; ) { |
| 6022 | /* find a token */ |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 6023 | while (i < len && Py_UNICODE_ISSPACE(buf[i])) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6024 | i++; |
| 6025 | j = i; |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 6026 | while (i < len && !Py_UNICODE_ISSPACE(buf[i])) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6027 | i++; |
| 6028 | if (j < i) { |
| 6029 | if (maxcount-- <= 0) |
| 6030 | break; |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 6031 | SPLIT_APPEND(buf, j, i); |
| 6032 | while (i < len && Py_UNICODE_ISSPACE(buf[i])) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6033 | i++; |
| 6034 | j = i; |
| 6035 | } |
| 6036 | } |
| 6037 | if (j < len) { |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 6038 | SPLIT_APPEND(buf, j, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6039 | } |
| 6040 | return list; |
| 6041 | |
| 6042 | onError: |
| 6043 | Py_DECREF(list); |
| 6044 | return NULL; |
| 6045 | } |
| 6046 | |
| 6047 | PyObject *PyUnicode_Splitlines(PyObject *string, |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 6048 | int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6049 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6050 | register Py_ssize_t i; |
| 6051 | register Py_ssize_t j; |
| 6052 | Py_ssize_t len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6053 | PyObject *list; |
| 6054 | PyObject *str; |
| 6055 | Py_UNICODE *data; |
| 6056 | |
| 6057 | string = PyUnicode_FromObject(string); |
| 6058 | if (string == NULL) |
| 6059 | return NULL; |
| 6060 | data = PyUnicode_AS_UNICODE(string); |
| 6061 | len = PyUnicode_GET_SIZE(string); |
| 6062 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6063 | list = PyList_New(0); |
| 6064 | if (!list) |
| 6065 | goto onError; |
| 6066 | |
| 6067 | for (i = j = 0; i < len; ) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6068 | Py_ssize_t eol; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6069 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6070 | /* Find a line and append it */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6071 | while (i < len && !BLOOM_LINEBREAK(data[i])) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6072 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6073 | |
| 6074 | /* Skip the line break reading CRLF as one line break */ |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 6075 | eol = i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6076 | if (i < len) { |
| 6077 | if (data[i] == '\r' && i + 1 < len && |
| 6078 | data[i+1] == '\n') |
| 6079 | i += 2; |
| 6080 | else |
| 6081 | i++; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 6082 | if (keepends) |
| 6083 | eol = i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6084 | } |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 6085 | SPLIT_APPEND(data, j, eol); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6086 | j = i; |
| 6087 | } |
| 6088 | if (j < len) { |
| 6089 | SPLIT_APPEND(data, j, len); |
| 6090 | } |
| 6091 | |
| 6092 | Py_DECREF(string); |
| 6093 | return list; |
| 6094 | |
| 6095 | onError: |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 6096 | Py_XDECREF(list); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6097 | Py_DECREF(string); |
| 6098 | return NULL; |
| 6099 | } |
| 6100 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6101 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6102 | PyObject *split_char(PyUnicodeObject *self, |
| 6103 | PyObject *list, |
| 6104 | Py_UNICODE ch, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6105 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6106 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6107 | register Py_ssize_t i; |
| 6108 | register Py_ssize_t j; |
| 6109 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6110 | PyObject *str; |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 6111 | register const Py_UNICODE *buf = self->str; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6112 | |
| 6113 | for (i = j = 0; i < len; ) { |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 6114 | if (buf[i] == ch) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6115 | if (maxcount-- <= 0) |
| 6116 | break; |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 6117 | SPLIT_APPEND(buf, j, i); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6118 | i = j = i + 1; |
| 6119 | } else |
| 6120 | i++; |
| 6121 | } |
| 6122 | if (j <= len) { |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 6123 | SPLIT_APPEND(buf, j, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6124 | } |
| 6125 | return list; |
| 6126 | |
| 6127 | onError: |
| 6128 | Py_DECREF(list); |
| 6129 | return NULL; |
| 6130 | } |
| 6131 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6132 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6133 | PyObject *split_substring(PyUnicodeObject *self, |
| 6134 | PyObject *list, |
| 6135 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6136 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6137 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6138 | register Py_ssize_t i; |
| 6139 | register Py_ssize_t j; |
| 6140 | Py_ssize_t len = self->length; |
| 6141 | Py_ssize_t sublen = substring->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6142 | PyObject *str; |
| 6143 | |
Guido van Rossum | cda4f9a | 2000-12-19 02:23:19 +0000 | [diff] [blame] | 6144 | for (i = j = 0; i <= len - sublen; ) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6145 | if (Py_UNICODE_MATCH(self, i, substring)) { |
| 6146 | if (maxcount-- <= 0) |
| 6147 | break; |
| 6148 | SPLIT_APPEND(self->str, j, i); |
| 6149 | i = j = i + sublen; |
| 6150 | } else |
| 6151 | i++; |
| 6152 | } |
| 6153 | if (j <= len) { |
| 6154 | SPLIT_APPEND(self->str, j, len); |
| 6155 | } |
| 6156 | return list; |
| 6157 | |
| 6158 | onError: |
| 6159 | Py_DECREF(list); |
| 6160 | return NULL; |
| 6161 | } |
| 6162 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6163 | static |
| 6164 | PyObject *rsplit_whitespace(PyUnicodeObject *self, |
| 6165 | PyObject *list, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6166 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6167 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6168 | register Py_ssize_t i; |
| 6169 | register Py_ssize_t j; |
| 6170 | Py_ssize_t len = self->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6171 | PyObject *str; |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 6172 | register const Py_UNICODE *buf = self->str; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6173 | |
| 6174 | for (i = j = len - 1; i >= 0; ) { |
| 6175 | /* find a token */ |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 6176 | while (i >= 0 && Py_UNICODE_ISSPACE(buf[i])) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6177 | i--; |
| 6178 | j = i; |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 6179 | while (i >= 0 && !Py_UNICODE_ISSPACE(buf[i])) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6180 | i--; |
| 6181 | if (j > i) { |
| 6182 | if (maxcount-- <= 0) |
| 6183 | break; |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 6184 | SPLIT_APPEND(buf, i + 1, j + 1); |
| 6185 | while (i >= 0 && Py_UNICODE_ISSPACE(buf[i])) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6186 | i--; |
| 6187 | j = i; |
| 6188 | } |
| 6189 | } |
| 6190 | if (j >= 0) { |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 6191 | SPLIT_APPEND(buf, 0, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6192 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6193 | if (PyList_Reverse(list) < 0) |
| 6194 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6195 | return list; |
| 6196 | |
| 6197 | onError: |
| 6198 | Py_DECREF(list); |
| 6199 | return NULL; |
| 6200 | } |
| 6201 | |
| 6202 | static |
| 6203 | PyObject *rsplit_char(PyUnicodeObject *self, |
| 6204 | PyObject *list, |
| 6205 | Py_UNICODE ch, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6206 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6207 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6208 | register Py_ssize_t i; |
| 6209 | register Py_ssize_t j; |
| 6210 | Py_ssize_t len = self->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6211 | PyObject *str; |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 6212 | register const Py_UNICODE *buf = self->str; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6213 | |
| 6214 | for (i = j = len - 1; i >= 0; ) { |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 6215 | if (buf[i] == ch) { |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6216 | if (maxcount-- <= 0) |
| 6217 | break; |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 6218 | SPLIT_APPEND(buf, i + 1, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6219 | j = i = i - 1; |
| 6220 | } else |
| 6221 | i--; |
| 6222 | } |
Hye-Shik Chang | 7fc4cf5 | 2003-12-23 09:10:16 +0000 | [diff] [blame] | 6223 | if (j >= -1) { |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 6224 | SPLIT_APPEND(buf, 0, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6225 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6226 | if (PyList_Reverse(list) < 0) |
| 6227 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6228 | return list; |
| 6229 | |
| 6230 | onError: |
| 6231 | Py_DECREF(list); |
| 6232 | return NULL; |
| 6233 | } |
| 6234 | |
| 6235 | static |
| 6236 | PyObject *rsplit_substring(PyUnicodeObject *self, |
| 6237 | PyObject *list, |
| 6238 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6239 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6240 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6241 | register Py_ssize_t i; |
| 6242 | register Py_ssize_t j; |
| 6243 | Py_ssize_t len = self->length; |
| 6244 | Py_ssize_t sublen = substring->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6245 | PyObject *str; |
| 6246 | |
| 6247 | for (i = len - sublen, j = len; i >= 0; ) { |
| 6248 | if (Py_UNICODE_MATCH(self, i, substring)) { |
| 6249 | if (maxcount-- <= 0) |
| 6250 | break; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6251 | SPLIT_APPEND(self->str, i + sublen, j); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6252 | j = i; |
| 6253 | i -= sublen; |
| 6254 | } else |
| 6255 | i--; |
| 6256 | } |
| 6257 | if (j >= 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6258 | SPLIT_APPEND(self->str, 0, j); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6259 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6260 | if (PyList_Reverse(list) < 0) |
| 6261 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6262 | return list; |
| 6263 | |
| 6264 | onError: |
| 6265 | Py_DECREF(list); |
| 6266 | return NULL; |
| 6267 | } |
| 6268 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6269 | #undef SPLIT_APPEND |
| 6270 | |
| 6271 | static |
| 6272 | PyObject *split(PyUnicodeObject *self, |
| 6273 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6274 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6275 | { |
| 6276 | PyObject *list; |
| 6277 | |
| 6278 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6279 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6280 | |
| 6281 | list = PyList_New(0); |
| 6282 | if (!list) |
| 6283 | return NULL; |
| 6284 | |
| 6285 | if (substring == NULL) |
| 6286 | return split_whitespace(self,list,maxcount); |
| 6287 | |
| 6288 | else if (substring->length == 1) |
| 6289 | return split_char(self,list,substring->str[0],maxcount); |
| 6290 | |
| 6291 | else if (substring->length == 0) { |
| 6292 | Py_DECREF(list); |
| 6293 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 6294 | return NULL; |
| 6295 | } |
| 6296 | else |
| 6297 | return split_substring(self,list,substring,maxcount); |
| 6298 | } |
| 6299 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6300 | static |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6301 | PyObject *rsplit(PyUnicodeObject *self, |
| 6302 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6303 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6304 | { |
| 6305 | PyObject *list; |
| 6306 | |
| 6307 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6308 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6309 | |
| 6310 | list = PyList_New(0); |
| 6311 | if (!list) |
| 6312 | return NULL; |
| 6313 | |
| 6314 | if (substring == NULL) |
| 6315 | return rsplit_whitespace(self,list,maxcount); |
| 6316 | |
| 6317 | else if (substring->length == 1) |
| 6318 | return rsplit_char(self,list,substring->str[0],maxcount); |
| 6319 | |
| 6320 | else if (substring->length == 0) { |
| 6321 | Py_DECREF(list); |
| 6322 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 6323 | return NULL; |
| 6324 | } |
| 6325 | else |
| 6326 | return rsplit_substring(self,list,substring,maxcount); |
| 6327 | } |
| 6328 | |
| 6329 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6330 | PyObject *replace(PyUnicodeObject *self, |
| 6331 | PyUnicodeObject *str1, |
| 6332 | PyUnicodeObject *str2, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6333 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6334 | { |
| 6335 | PyUnicodeObject *u; |
| 6336 | |
| 6337 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6338 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6339 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6340 | if (str1->length == str2->length) { |
| 6341 | /* same length */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6342 | Py_ssize_t i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6343 | if (str1->length == 1) { |
| 6344 | /* replace characters */ |
| 6345 | Py_UNICODE u1, u2; |
| 6346 | if (!findchar(self->str, self->length, str1->str[0])) |
| 6347 | goto nothing; |
| 6348 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 6349 | if (!u) |
| 6350 | return NULL; |
| 6351 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 6352 | u1 = str1->str[0]; |
| 6353 | u2 = str2->str[0]; |
| 6354 | for (i = 0; i < u->length; i++) |
| 6355 | if (u->str[i] == u1) { |
| 6356 | if (--maxcount < 0) |
| 6357 | break; |
| 6358 | u->str[i] = u2; |
| 6359 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6360 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6361 | i = fastsearch( |
| 6362 | self->str, self->length, str1->str, str1->length, FAST_SEARCH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6363 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6364 | if (i < 0) |
| 6365 | goto nothing; |
| 6366 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 6367 | if (!u) |
| 6368 | return NULL; |
| 6369 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 6370 | while (i <= self->length - str1->length) |
| 6371 | if (Py_UNICODE_MATCH(self, i, str1)) { |
| 6372 | if (--maxcount < 0) |
| 6373 | break; |
| 6374 | Py_UNICODE_COPY(u->str+i, str2->str, str2->length); |
| 6375 | i += str1->length; |
| 6376 | } else |
| 6377 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6378 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6379 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6380 | |
| 6381 | Py_ssize_t n, i, j, e; |
| 6382 | Py_ssize_t product, new_size, delta; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6383 | Py_UNICODE *p; |
| 6384 | |
| 6385 | /* replace strings */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6386 | n = stringlib_count(self->str, self->length, str1->str, str1->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6387 | if (n > maxcount) |
| 6388 | n = maxcount; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6389 | if (n == 0) |
| 6390 | goto nothing; |
| 6391 | /* new_size = self->length + n * (str2->length - str1->length)); */ |
| 6392 | delta = (str2->length - str1->length); |
| 6393 | if (delta == 0) { |
| 6394 | new_size = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6395 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6396 | product = n * (str2->length - str1->length); |
| 6397 | if ((product / (str2->length - str1->length)) != n) { |
| 6398 | PyErr_SetString(PyExc_OverflowError, |
| 6399 | "replace string is too long"); |
| 6400 | return NULL; |
| 6401 | } |
| 6402 | new_size = self->length + product; |
| 6403 | if (new_size < 0) { |
| 6404 | PyErr_SetString(PyExc_OverflowError, |
| 6405 | "replace string is too long"); |
| 6406 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6407 | } |
| 6408 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6409 | u = _PyUnicode_New(new_size); |
| 6410 | if (!u) |
| 6411 | return NULL; |
| 6412 | i = 0; |
| 6413 | p = u->str; |
| 6414 | e = self->length - str1->length; |
| 6415 | if (str1->length > 0) { |
| 6416 | while (n-- > 0) { |
| 6417 | /* look for next match */ |
| 6418 | j = i; |
| 6419 | while (j <= e) { |
| 6420 | if (Py_UNICODE_MATCH(self, j, str1)) |
| 6421 | break; |
| 6422 | j++; |
| 6423 | } |
| 6424 | if (j > i) { |
| 6425 | if (j > e) |
| 6426 | break; |
| 6427 | /* copy unchanged part [i:j] */ |
| 6428 | Py_UNICODE_COPY(p, self->str+i, j-i); |
| 6429 | p += j - i; |
| 6430 | } |
| 6431 | /* copy substitution string */ |
| 6432 | if (str2->length > 0) { |
| 6433 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 6434 | p += str2->length; |
| 6435 | } |
| 6436 | i = j + str1->length; |
| 6437 | } |
| 6438 | if (i < self->length) |
| 6439 | /* copy tail [i:] */ |
| 6440 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 6441 | } else { |
| 6442 | /* interleave */ |
| 6443 | while (n > 0) { |
| 6444 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 6445 | p += str2->length; |
| 6446 | if (--n <= 0) |
| 6447 | break; |
| 6448 | *p++ = self->str[i++]; |
| 6449 | } |
| 6450 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 6451 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6452 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6453 | return (PyObject *) u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6454 | |
| 6455 | nothing: |
| 6456 | /* nothing to replace; return original string (when possible) */ |
| 6457 | if (PyUnicode_CheckExact(self)) { |
| 6458 | Py_INCREF(self); |
| 6459 | return (PyObject *) self; |
| 6460 | } |
| 6461 | return PyUnicode_FromUnicode(self->str, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6462 | } |
| 6463 | |
| 6464 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 6465 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6466 | PyDoc_STRVAR(title__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 6467 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6468 | \n\ |
| 6469 | 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] | 6470 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6471 | |
| 6472 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6473 | unicode_title(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6474 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6475 | return fixup(self, fixtitle); |
| 6476 | } |
| 6477 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6478 | PyDoc_STRVAR(capitalize__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 6479 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6480 | \n\ |
| 6481 | Return a capitalized version of S, i.e. make the first character\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6482 | have upper case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6483 | |
| 6484 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6485 | unicode_capitalize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6486 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6487 | return fixup(self, fixcapitalize); |
| 6488 | } |
| 6489 | |
| 6490 | #if 0 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6491 | PyDoc_STRVAR(capwords__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 6492 | "S.capwords() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6493 | \n\ |
| 6494 | 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] | 6495 | normalized whitespace (all whitespace strings are replaced by ' ')."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6496 | |
| 6497 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6498 | unicode_capwords(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6499 | { |
| 6500 | PyObject *list; |
| 6501 | PyObject *item; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6502 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6503 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6504 | /* Split into words */ |
| 6505 | list = split(self, NULL, -1); |
| 6506 | if (!list) |
| 6507 | return NULL; |
| 6508 | |
| 6509 | /* Capitalize each word */ |
| 6510 | for (i = 0; i < PyList_GET_SIZE(list); i++) { |
| 6511 | item = fixup((PyUnicodeObject *)PyList_GET_ITEM(list, i), |
| 6512 | fixcapitalize); |
| 6513 | if (item == NULL) |
| 6514 | goto onError; |
| 6515 | Py_DECREF(PyList_GET_ITEM(list, i)); |
| 6516 | PyList_SET_ITEM(list, i, item); |
| 6517 | } |
| 6518 | |
| 6519 | /* Join the words to form a new string */ |
| 6520 | item = PyUnicode_Join(NULL, list); |
| 6521 | |
| 6522 | onError: |
| 6523 | Py_DECREF(list); |
| 6524 | return (PyObject *)item; |
| 6525 | } |
| 6526 | #endif |
| 6527 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6528 | /* Argument converter. Coerces to a single unicode character */ |
| 6529 | |
| 6530 | static int |
| 6531 | convert_uc(PyObject *obj, void *addr) |
| 6532 | { |
| 6533 | Py_UNICODE *fillcharloc = (Py_UNICODE *)addr; |
| 6534 | PyObject *uniobj; |
| 6535 | Py_UNICODE *unistr; |
| 6536 | |
| 6537 | uniobj = PyUnicode_FromObject(obj); |
| 6538 | if (uniobj == NULL) { |
| 6539 | PyErr_SetString(PyExc_TypeError, |
| 6540 | "The fill character cannot be converted to Unicode"); |
| 6541 | return 0; |
| 6542 | } |
| 6543 | if (PyUnicode_GET_SIZE(uniobj) != 1) { |
| 6544 | PyErr_SetString(PyExc_TypeError, |
| 6545 | "The fill character must be exactly one character long"); |
| 6546 | Py_DECREF(uniobj); |
| 6547 | return 0; |
| 6548 | } |
| 6549 | unistr = PyUnicode_AS_UNICODE(uniobj); |
| 6550 | *fillcharloc = unistr[0]; |
| 6551 | Py_DECREF(uniobj); |
| 6552 | return 1; |
| 6553 | } |
| 6554 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6555 | PyDoc_STRVAR(center__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 6556 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6557 | \n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 6558 | Return S centered in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6559 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6560 | |
| 6561 | static PyObject * |
| 6562 | unicode_center(PyUnicodeObject *self, PyObject *args) |
| 6563 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6564 | Py_ssize_t marg, left; |
| 6565 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6566 | Py_UNICODE fillchar = ' '; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6567 | |
Thomas Wouters | de01774 | 2006-02-16 19:34:37 +0000 | [diff] [blame] | 6568 | if (!PyArg_ParseTuple(args, "n|O&:center", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6569 | return NULL; |
| 6570 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6571 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6572 | Py_INCREF(self); |
| 6573 | return (PyObject*) self; |
| 6574 | } |
| 6575 | |
| 6576 | marg = width - self->length; |
| 6577 | left = marg / 2 + (marg & width & 1); |
| 6578 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6579 | return (PyObject*) pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6580 | } |
| 6581 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6582 | #if 0 |
| 6583 | |
| 6584 | /* This code should go into some future Unicode collation support |
| 6585 | module. The basic comparison should compare ordinals on a naive |
Trent Mick | 20abf57 | 2000-08-12 22:14:34 +0000 | [diff] [blame] | 6586 | basis (this is what Java does and thus JPython too). */ |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6587 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6588 | /* speedy UTF-16 code point order comparison */ |
| 6589 | /* gleaned from: */ |
| 6590 | /* http://www-4.ibm.com/software/developer/library/utf16.html?dwzone=unicode */ |
| 6591 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 6592 | static short utf16Fixup[32] = |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6593 | { |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6594 | 0, 0, 0, 0, 0, 0, 0, 0, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6595 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 6596 | 0, 0, 0, 0, 0, 0, 0, 0, |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 6597 | 0, 0, 0, 0x2000, -0x800, -0x800, -0x800, -0x800 |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6598 | }; |
| 6599 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6600 | static int |
| 6601 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 6602 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6603 | Py_ssize_t len1, len2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6604 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6605 | Py_UNICODE *s1 = str1->str; |
| 6606 | Py_UNICODE *s2 = str2->str; |
| 6607 | |
| 6608 | len1 = str1->length; |
| 6609 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6610 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6611 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6612 | Py_UNICODE c1, c2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6613 | |
| 6614 | c1 = *s1++; |
| 6615 | c2 = *s2++; |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6616 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6617 | if (c1 > (1<<11) * 26) |
| 6618 | c1 += utf16Fixup[c1>>11]; |
| 6619 | if (c2 > (1<<11) * 26) |
| 6620 | c2 += utf16Fixup[c2>>11]; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6621 | /* now c1 and c2 are in UTF-32-compatible order */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6622 | |
| 6623 | if (c1 != c2) |
| 6624 | return (c1 < c2) ? -1 : 1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6625 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6626 | len1--; len2--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6627 | } |
| 6628 | |
| 6629 | return (len1 < len2) ? -1 : (len1 != len2); |
| 6630 | } |
| 6631 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6632 | #else |
| 6633 | |
| 6634 | static int |
| 6635 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 6636 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6637 | register Py_ssize_t len1, len2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6638 | |
| 6639 | Py_UNICODE *s1 = str1->str; |
| 6640 | Py_UNICODE *s2 = str2->str; |
| 6641 | |
| 6642 | len1 = str1->length; |
| 6643 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6644 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6645 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6646 | Py_UNICODE c1, c2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6647 | |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6648 | c1 = *s1++; |
| 6649 | c2 = *s2++; |
| 6650 | |
| 6651 | if (c1 != c2) |
| 6652 | return (c1 < c2) ? -1 : 1; |
| 6653 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6654 | len1--; len2--; |
| 6655 | } |
| 6656 | |
| 6657 | return (len1 < len2) ? -1 : (len1 != len2); |
| 6658 | } |
| 6659 | |
| 6660 | #endif |
| 6661 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6662 | int PyUnicode_Compare(PyObject *left, |
| 6663 | PyObject *right) |
| 6664 | { |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 6665 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) |
| 6666 | return unicode_compare((PyUnicodeObject *)left, |
| 6667 | (PyUnicodeObject *)right); |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 6668 | PyErr_Format(PyExc_TypeError, |
| 6669 | "Can't compare %.100s and %.100s", |
| 6670 | left->ob_type->tp_name, |
| 6671 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6672 | return -1; |
| 6673 | } |
| 6674 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 6675 | int |
| 6676 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 6677 | { |
| 6678 | int i; |
| 6679 | Py_UNICODE *id; |
| 6680 | assert(PyUnicode_Check(uni)); |
| 6681 | id = PyUnicode_AS_UNICODE(uni); |
| 6682 | /* Compare Unicode string and source character set string */ |
| 6683 | for (i = 0; id[i] && str[i]; i++) |
| 6684 | if (id[i] != str[i]) |
| 6685 | return ((int)id[i] < (int)str[i]) ? -1 : 1; |
| 6686 | if (id[i]) |
| 6687 | return 1; /* uni is longer */ |
| 6688 | if (str[i]) |
| 6689 | return -1; /* str is longer */ |
| 6690 | return 0; |
| 6691 | } |
| 6692 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 6693 | |
| 6694 | #define TEST_COND(cond) \ |
| 6695 | ((cond) ? Py_True : Py_False) |
| 6696 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 6697 | PyObject *PyUnicode_RichCompare(PyObject *left, |
| 6698 | PyObject *right, |
| 6699 | int op) |
| 6700 | { |
| 6701 | int result; |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 6702 | |
| 6703 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 6704 | PyObject *v; |
| 6705 | if (((PyUnicodeObject *) left)->length != |
| 6706 | ((PyUnicodeObject *) right)->length) { |
| 6707 | if (op == Py_EQ) { |
| 6708 | Py_INCREF(Py_False); |
| 6709 | return Py_False; |
| 6710 | } |
| 6711 | if (op == Py_NE) { |
| 6712 | Py_INCREF(Py_True); |
| 6713 | return Py_True; |
| 6714 | } |
| 6715 | } |
| 6716 | if (left == right) |
| 6717 | result = 0; |
| 6718 | else |
| 6719 | result = unicode_compare((PyUnicodeObject *)left, |
| 6720 | (PyUnicodeObject *)right); |
| 6721 | |
| 6722 | /* Convert the return value to a Boolean */ |
| 6723 | switch (op) { |
| 6724 | case Py_EQ: |
| 6725 | v = TEST_COND(result == 0); |
| 6726 | break; |
| 6727 | case Py_NE: |
| 6728 | v = TEST_COND(result != 0); |
| 6729 | break; |
| 6730 | case Py_LE: |
| 6731 | v = TEST_COND(result <= 0); |
| 6732 | break; |
| 6733 | case Py_GE: |
| 6734 | v = TEST_COND(result >= 0); |
| 6735 | break; |
| 6736 | case Py_LT: |
| 6737 | v = TEST_COND(result == -1); |
| 6738 | break; |
| 6739 | case Py_GT: |
| 6740 | v = TEST_COND(result == 1); |
| 6741 | break; |
| 6742 | default: |
| 6743 | PyErr_BadArgument(); |
| 6744 | return NULL; |
| 6745 | } |
| 6746 | Py_INCREF(v); |
| 6747 | return v; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 6748 | } |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 6749 | |
| 6750 | Py_INCREF(Py_NotImplemented); |
| 6751 | return Py_NotImplemented; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 6752 | } |
| 6753 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6754 | int PyUnicode_Contains(PyObject *container, |
| 6755 | PyObject *element) |
| 6756 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6757 | PyObject *str, *sub; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6758 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6759 | |
| 6760 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6761 | sub = PyUnicode_FromObject(element); |
| 6762 | if (!sub) { |
Walter Dörwald | 26e0f51 | 2007-06-12 16:51:31 +0000 | [diff] [blame] | 6763 | PyErr_Format(PyExc_TypeError, |
| 6764 | "'in <string>' requires string as left operand, not %s", |
| 6765 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6766 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6767 | } |
| 6768 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6769 | str = PyUnicode_FromObject(container); |
| 6770 | if (!str) { |
| 6771 | Py_DECREF(sub); |
| 6772 | return -1; |
| 6773 | } |
| 6774 | |
| 6775 | result = stringlib_contains_obj(str, sub); |
| 6776 | |
| 6777 | Py_DECREF(str); |
| 6778 | Py_DECREF(sub); |
| 6779 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6780 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6781 | } |
| 6782 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6783 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 6784 | |
| 6785 | PyObject *PyUnicode_Concat(PyObject *left, |
| 6786 | PyObject *right) |
| 6787 | { |
| 6788 | PyUnicodeObject *u = NULL, *v = NULL, *w; |
| 6789 | |
| 6790 | /* Coerce the two arguments */ |
| 6791 | u = (PyUnicodeObject *)PyUnicode_FromObject(left); |
| 6792 | if (u == NULL) |
| 6793 | goto onError; |
| 6794 | v = (PyUnicodeObject *)PyUnicode_FromObject(right); |
| 6795 | if (v == NULL) |
| 6796 | goto onError; |
| 6797 | |
| 6798 | /* Shortcuts */ |
| 6799 | if (v == unicode_empty) { |
| 6800 | Py_DECREF(v); |
| 6801 | return (PyObject *)u; |
| 6802 | } |
| 6803 | if (u == unicode_empty) { |
| 6804 | Py_DECREF(u); |
| 6805 | return (PyObject *)v; |
| 6806 | } |
| 6807 | |
| 6808 | /* Concat the two Unicode strings */ |
| 6809 | w = _PyUnicode_New(u->length + v->length); |
| 6810 | if (w == NULL) |
| 6811 | goto onError; |
| 6812 | Py_UNICODE_COPY(w->str, u->str, u->length); |
| 6813 | Py_UNICODE_COPY(w->str + u->length, v->str, v->length); |
| 6814 | |
| 6815 | Py_DECREF(u); |
| 6816 | Py_DECREF(v); |
| 6817 | return (PyObject *)w; |
| 6818 | |
| 6819 | onError: |
| 6820 | Py_XDECREF(u); |
| 6821 | Py_XDECREF(v); |
| 6822 | return NULL; |
| 6823 | } |
| 6824 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 6825 | void |
| 6826 | PyUnicode_Append(PyObject **pleft, PyObject *right) |
| 6827 | { |
| 6828 | PyObject *new; |
| 6829 | if (*pleft == NULL) |
| 6830 | return; |
| 6831 | if (right == NULL || !PyUnicode_Check(*pleft)) { |
| 6832 | Py_DECREF(*pleft); |
| 6833 | *pleft = NULL; |
| 6834 | return; |
| 6835 | } |
| 6836 | new = PyUnicode_Concat(*pleft, right); |
| 6837 | Py_DECREF(*pleft); |
| 6838 | *pleft = new; |
| 6839 | } |
| 6840 | |
| 6841 | void |
| 6842 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 6843 | { |
| 6844 | PyUnicode_Append(pleft, right); |
| 6845 | Py_XDECREF(right); |
| 6846 | } |
| 6847 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6848 | PyDoc_STRVAR(count__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6849 | "S.count(sub[, start[, end]]) -> int\n\ |
| 6850 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6851 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 6852 | 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] | 6853 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6854 | |
| 6855 | static PyObject * |
| 6856 | unicode_count(PyUnicodeObject *self, PyObject *args) |
| 6857 | { |
| 6858 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6859 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6860 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6861 | PyObject *result; |
| 6862 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6863 | if (!PyArg_ParseTuple(args, "O|O&O&:count", &substring, |
| 6864 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6865 | return NULL; |
| 6866 | |
| 6867 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6868 | (PyObject *)substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6869 | if (substring == NULL) |
| 6870 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6871 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6872 | FIX_START_END(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6873 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 6874 | result = PyLong_FromSsize_t( |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6875 | stringlib_count(self->str + start, end - start, |
| 6876 | substring->str, substring->length) |
| 6877 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6878 | |
| 6879 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6880 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6881 | return result; |
| 6882 | } |
| 6883 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6884 | PyDoc_STRVAR(encode__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 6885 | "S.encode([encoding[, errors]]) -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6886 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 6887 | Encode S using the codec registered for encoding. encoding defaults\n\ |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6888 | to the default encoding. errors may be given to set a different error\n\ |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 6889 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6890 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 6891 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 6892 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6893 | |
| 6894 | static PyObject * |
| 6895 | unicode_encode(PyUnicodeObject *self, PyObject *args) |
| 6896 | { |
| 6897 | char *encoding = NULL; |
| 6898 | char *errors = NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6899 | PyObject *v; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 6900 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6901 | if (!PyArg_ParseTuple(args, "|ss:encode", &encoding, &errors)) |
| 6902 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 6903 | v = PyUnicode_AsEncodedString((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 6904 | if (v == NULL) |
| 6905 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6906 | if (!PyBytes_Check(v)) { |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6907 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 6908 | "encoder did not return a bytes object " |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6909 | "(type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 6910 | Py_TYPE(v)->tp_name); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6911 | Py_DECREF(v); |
| 6912 | return NULL; |
| 6913 | } |
| 6914 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 6915 | |
| 6916 | onError: |
| 6917 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6918 | } |
| 6919 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6920 | PyDoc_STRVAR(expandtabs__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 6921 | "S.expandtabs([tabsize]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6922 | \n\ |
| 6923 | 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] | 6924 | 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] | 6925 | |
| 6926 | static PyObject* |
| 6927 | unicode_expandtabs(PyUnicodeObject *self, PyObject *args) |
| 6928 | { |
| 6929 | Py_UNICODE *e; |
| 6930 | Py_UNICODE *p; |
| 6931 | Py_UNICODE *q; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 6932 | Py_UNICODE *qe; |
| 6933 | Py_ssize_t i, j, incr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6934 | PyUnicodeObject *u; |
| 6935 | int tabsize = 8; |
| 6936 | |
| 6937 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
| 6938 | return NULL; |
| 6939 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 6940 | /* First pass: determine size of output string */ |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 6941 | i = 0; /* chars up to and including most recent \n or \r */ |
| 6942 | j = 0; /* chars since most recent \n or \r (use in tab calculations) */ |
| 6943 | e = self->str + self->length; /* end of input */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6944 | for (p = self->str; p < e; p++) |
| 6945 | if (*p == '\t') { |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6946 | if (tabsize > 0) { |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 6947 | incr = tabsize - (j % tabsize); /* cannot overflow */ |
| 6948 | if (j > PY_SSIZE_T_MAX - incr) |
| 6949 | goto overflow1; |
| 6950 | j += incr; |
| 6951 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6952 | } |
| 6953 | else { |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 6954 | if (j > PY_SSIZE_T_MAX - 1) |
| 6955 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6956 | j++; |
| 6957 | if (*p == '\n' || *p == '\r') { |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 6958 | if (i > PY_SSIZE_T_MAX - j) |
| 6959 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6960 | i += j; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 6961 | j = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6962 | } |
| 6963 | } |
| 6964 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 6965 | if (i > PY_SSIZE_T_MAX - j) |
| 6966 | goto overflow1; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6967 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6968 | /* Second pass: create output string and fill it */ |
| 6969 | u = _PyUnicode_New(i + j); |
| 6970 | if (!u) |
| 6971 | return NULL; |
| 6972 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 6973 | j = 0; /* same as in first pass */ |
| 6974 | q = u->str; /* next output char */ |
| 6975 | qe = u->str + u->length; /* end of output */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6976 | |
| 6977 | for (p = self->str; p < e; p++) |
| 6978 | if (*p == '\t') { |
| 6979 | if (tabsize > 0) { |
| 6980 | i = tabsize - (j % tabsize); |
| 6981 | j += i; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 6982 | while (i--) { |
| 6983 | if (q >= qe) |
| 6984 | goto overflow2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6985 | *q++ = ' '; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 6986 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6987 | } |
| 6988 | } |
| 6989 | else { |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 6990 | if (q >= qe) |
| 6991 | goto overflow2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6992 | *q++ = *p; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 6993 | j++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6994 | if (*p == '\n' || *p == '\r') |
| 6995 | j = 0; |
| 6996 | } |
| 6997 | |
| 6998 | return (PyObject*) u; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 6999 | |
| 7000 | overflow2: |
| 7001 | Py_DECREF(u); |
| 7002 | overflow1: |
| 7003 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 7004 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7005 | } |
| 7006 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7007 | PyDoc_STRVAR(find__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 7008 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7009 | \n\ |
| 7010 | Return the lowest index in S where substring sub is found,\n\ |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 7011 | such that sub is contained within s[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7012 | arguments start and end are interpreted as in slice notation.\n\ |
| 7013 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7014 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7015 | |
| 7016 | static PyObject * |
| 7017 | unicode_find(PyUnicodeObject *self, PyObject *args) |
| 7018 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7019 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7020 | Py_ssize_t start; |
| 7021 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7022 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7023 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7024 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7025 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7026 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7027 | result = stringlib_find_slice( |
| 7028 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 7029 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 7030 | start, end |
| 7031 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7032 | |
| 7033 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7034 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7035 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7036 | } |
| 7037 | |
| 7038 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7039 | unicode_getitem(PyUnicodeObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7040 | { |
| 7041 | if (index < 0 || index >= self->length) { |
| 7042 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 7043 | return NULL; |
| 7044 | } |
| 7045 | |
| 7046 | return (PyObject*) PyUnicode_FromUnicode(&self->str[index], 1); |
| 7047 | } |
| 7048 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7049 | /* Believe it or not, this produces the same value for ASCII strings |
| 7050 | as string_hash(). */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7051 | static long |
Neil Schemenauer | f8c37d1 | 2007-09-07 20:49:04 +0000 | [diff] [blame] | 7052 | unicode_hash(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7053 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7054 | Py_ssize_t len; |
| 7055 | Py_UNICODE *p; |
| 7056 | long x; |
| 7057 | |
| 7058 | if (self->hash != -1) |
| 7059 | return self->hash; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7060 | len = Py_SIZE(self); |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7061 | p = self->str; |
| 7062 | x = *p << 7; |
| 7063 | while (--len >= 0) |
| 7064 | x = (1000003*x) ^ *p++; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7065 | x ^= Py_SIZE(self); |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7066 | if (x == -1) |
| 7067 | x = -2; |
| 7068 | self->hash = x; |
| 7069 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7070 | } |
| 7071 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7072 | PyDoc_STRVAR(index__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 7073 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7074 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7075 | 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] | 7076 | |
| 7077 | static PyObject * |
| 7078 | unicode_index(PyUnicodeObject *self, PyObject *args) |
| 7079 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7080 | Py_ssize_t result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7081 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7082 | Py_ssize_t start; |
| 7083 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7084 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7085 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7086 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7087 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7088 | result = stringlib_find_slice( |
| 7089 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 7090 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 7091 | start, end |
| 7092 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7093 | |
| 7094 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7095 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7096 | if (result < 0) { |
| 7097 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 7098 | return NULL; |
| 7099 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7100 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7101 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7102 | } |
| 7103 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7104 | PyDoc_STRVAR(islower__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7105 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7106 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7107 | 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] | 7108 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7109 | |
| 7110 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7111 | unicode_islower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7112 | { |
| 7113 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7114 | register const Py_UNICODE *e; |
| 7115 | int cased; |
| 7116 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7117 | /* Shortcut for single character strings */ |
| 7118 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7119 | return PyBool_FromLong(Py_UNICODE_ISLOWER(*p)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7120 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7121 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7122 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7123 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7124 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7125 | e = p + PyUnicode_GET_SIZE(self); |
| 7126 | cased = 0; |
| 7127 | for (; p < e; p++) { |
| 7128 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7129 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7130 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7131 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7132 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 7133 | cased = 1; |
| 7134 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7135 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7136 | } |
| 7137 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7138 | PyDoc_STRVAR(isupper__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7139 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7140 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7141 | 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] | 7142 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7143 | |
| 7144 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7145 | unicode_isupper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7146 | { |
| 7147 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7148 | register const Py_UNICODE *e; |
| 7149 | int cased; |
| 7150 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7151 | /* Shortcut for single character strings */ |
| 7152 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7153 | return PyBool_FromLong(Py_UNICODE_ISUPPER(*p) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7154 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7155 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7156 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7157 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7158 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7159 | e = p + PyUnicode_GET_SIZE(self); |
| 7160 | cased = 0; |
| 7161 | for (; p < e; p++) { |
| 7162 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7163 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7164 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7165 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7166 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 7167 | cased = 1; |
| 7168 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7169 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7170 | } |
| 7171 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7172 | PyDoc_STRVAR(istitle__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7173 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7174 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7175 | Return True if S is a titlecased string and there is at least one\n\ |
| 7176 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 7177 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 7178 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7179 | |
| 7180 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7181 | unicode_istitle(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7182 | { |
| 7183 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7184 | register const Py_UNICODE *e; |
| 7185 | int cased, previous_is_cased; |
| 7186 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7187 | /* Shortcut for single character strings */ |
| 7188 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7189 | return PyBool_FromLong((Py_UNICODE_ISTITLE(*p) != 0) || |
| 7190 | (Py_UNICODE_ISUPPER(*p) != 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7191 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7192 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7193 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7194 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7195 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7196 | e = p + PyUnicode_GET_SIZE(self); |
| 7197 | cased = 0; |
| 7198 | previous_is_cased = 0; |
| 7199 | for (; p < e; p++) { |
| 7200 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7201 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7202 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 7203 | if (previous_is_cased) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7204 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7205 | previous_is_cased = 1; |
| 7206 | cased = 1; |
| 7207 | } |
| 7208 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 7209 | if (!previous_is_cased) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7210 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7211 | previous_is_cased = 1; |
| 7212 | cased = 1; |
| 7213 | } |
| 7214 | else |
| 7215 | previous_is_cased = 0; |
| 7216 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7217 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7218 | } |
| 7219 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7220 | PyDoc_STRVAR(isspace__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7221 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7222 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7223 | Return True if all characters in S are whitespace\n\ |
| 7224 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7225 | |
| 7226 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7227 | unicode_isspace(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7228 | { |
| 7229 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7230 | register const Py_UNICODE *e; |
| 7231 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7232 | /* Shortcut for single character strings */ |
| 7233 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 7234 | Py_UNICODE_ISSPACE(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7235 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7236 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7237 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7238 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7239 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7240 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7241 | e = p + PyUnicode_GET_SIZE(self); |
| 7242 | for (; p < e; p++) { |
| 7243 | if (!Py_UNICODE_ISSPACE(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7244 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7245 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7246 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7247 | } |
| 7248 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7249 | PyDoc_STRVAR(isalpha__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7250 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7251 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7252 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7253 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7254 | |
| 7255 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7256 | unicode_isalpha(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7257 | { |
| 7258 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7259 | register const Py_UNICODE *e; |
| 7260 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7261 | /* Shortcut for single character strings */ |
| 7262 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 7263 | Py_UNICODE_ISALPHA(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7264 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7265 | |
| 7266 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7267 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7268 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7269 | |
| 7270 | e = p + PyUnicode_GET_SIZE(self); |
| 7271 | for (; p < e; p++) { |
| 7272 | if (!Py_UNICODE_ISALPHA(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7273 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7274 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7275 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7276 | } |
| 7277 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7278 | PyDoc_STRVAR(isalnum__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7279 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7280 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7281 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7282 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7283 | |
| 7284 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7285 | unicode_isalnum(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7286 | { |
| 7287 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7288 | register const Py_UNICODE *e; |
| 7289 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7290 | /* Shortcut for single character strings */ |
| 7291 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 7292 | Py_UNICODE_ISALNUM(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7293 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7294 | |
| 7295 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7296 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7297 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7298 | |
| 7299 | e = p + PyUnicode_GET_SIZE(self); |
| 7300 | for (; p < e; p++) { |
| 7301 | if (!Py_UNICODE_ISALNUM(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7302 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7303 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7304 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7305 | } |
| 7306 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7307 | PyDoc_STRVAR(isdecimal__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7308 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7309 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7310 | 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] | 7311 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7312 | |
| 7313 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7314 | unicode_isdecimal(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7315 | { |
| 7316 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7317 | register const Py_UNICODE *e; |
| 7318 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7319 | /* Shortcut for single character strings */ |
| 7320 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 7321 | Py_UNICODE_ISDECIMAL(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7322 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7323 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7324 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7325 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7326 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7327 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7328 | e = p + PyUnicode_GET_SIZE(self); |
| 7329 | for (; p < e; p++) { |
| 7330 | if (!Py_UNICODE_ISDECIMAL(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7331 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7332 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7333 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7334 | } |
| 7335 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7336 | PyDoc_STRVAR(isdigit__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7337 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7338 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7339 | Return True if all characters in S are digits\n\ |
| 7340 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7341 | |
| 7342 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7343 | unicode_isdigit(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7344 | { |
| 7345 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7346 | register const Py_UNICODE *e; |
| 7347 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7348 | /* Shortcut for single character strings */ |
| 7349 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 7350 | Py_UNICODE_ISDIGIT(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7351 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7352 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7353 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7354 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7355 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7356 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7357 | e = p + PyUnicode_GET_SIZE(self); |
| 7358 | for (; p < e; p++) { |
| 7359 | if (!Py_UNICODE_ISDIGIT(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7360 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7361 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7362 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7363 | } |
| 7364 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7365 | PyDoc_STRVAR(isnumeric__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7366 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7367 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7368 | 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] | 7369 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7370 | |
| 7371 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7372 | unicode_isnumeric(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7373 | { |
| 7374 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7375 | register const Py_UNICODE *e; |
| 7376 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7377 | /* Shortcut for single character strings */ |
| 7378 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 7379 | Py_UNICODE_ISNUMERIC(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7380 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7381 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7382 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7383 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7384 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7385 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7386 | e = p + PyUnicode_GET_SIZE(self); |
| 7387 | for (; p < e; p++) { |
| 7388 | if (!Py_UNICODE_ISNUMERIC(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7389 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7390 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7391 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7392 | } |
| 7393 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7394 | int |
| 7395 | PyUnicode_IsIdentifier(PyObject *self) |
| 7396 | { |
| 7397 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE((PyUnicodeObject*)self); |
| 7398 | register const Py_UNICODE *e; |
| 7399 | |
| 7400 | /* Special case for empty strings */ |
| 7401 | if (PyUnicode_GET_SIZE(self) == 0) |
| 7402 | return 0; |
| 7403 | |
| 7404 | /* PEP 3131 says that the first character must be in |
| 7405 | XID_Start and subsequent characters in XID_Continue, |
| 7406 | and for the ASCII range, the 2.x rules apply (i.e |
| 7407 | start with letters and underscore, continue with |
| 7408 | letters, digits, underscore). However, given the current |
| 7409 | definition of XID_Start and XID_Continue, it is sufficient |
| 7410 | to check just for these, except that _ must be allowed |
| 7411 | as starting an identifier. */ |
| 7412 | if (!_PyUnicode_IsXidStart(*p) && *p != 0x5F /* LOW LINE */) |
| 7413 | return 0; |
| 7414 | |
| 7415 | e = p + PyUnicode_GET_SIZE(self); |
| 7416 | for (p++; p < e; p++) { |
| 7417 | if (!_PyUnicode_IsXidContinue(*p)) |
| 7418 | return 0; |
| 7419 | } |
| 7420 | return 1; |
| 7421 | } |
| 7422 | |
| 7423 | PyDoc_STRVAR(isidentifier__doc__, |
| 7424 | "S.isidentifier() -> bool\n\ |
| 7425 | \n\ |
| 7426 | Return True if S is a valid identifier according\n\ |
| 7427 | to the language definition."); |
| 7428 | |
| 7429 | static PyObject* |
| 7430 | unicode_isidentifier(PyObject *self) |
| 7431 | { |
| 7432 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 7433 | } |
| 7434 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7435 | PyDoc_STRVAR(isprintable__doc__, |
| 7436 | "S.isprintable() -> bool\n\ |
| 7437 | \n\ |
| 7438 | Return True if all characters in S are considered\n\ |
| 7439 | printable in repr() or S is empty, False otherwise."); |
| 7440 | |
| 7441 | static PyObject* |
| 7442 | unicode_isprintable(PyObject *self) |
| 7443 | { |
| 7444 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7445 | register const Py_UNICODE *e; |
| 7446 | |
| 7447 | /* Shortcut for single character strings */ |
| 7448 | if (PyUnicode_GET_SIZE(self) == 1 && Py_UNICODE_ISPRINTABLE(*p)) { |
| 7449 | Py_RETURN_TRUE; |
| 7450 | } |
| 7451 | |
| 7452 | e = p + PyUnicode_GET_SIZE(self); |
| 7453 | for (; p < e; p++) { |
| 7454 | if (!Py_UNICODE_ISPRINTABLE(*p)) { |
| 7455 | Py_RETURN_FALSE; |
| 7456 | } |
| 7457 | } |
| 7458 | Py_RETURN_TRUE; |
| 7459 | } |
| 7460 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7461 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 7462 | "S.join(sequence) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7463 | \n\ |
| 7464 | Return a string which is the concatenation of the strings in the\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7465 | sequence. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7466 | |
| 7467 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7468 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7469 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7470 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7471 | } |
| 7472 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7473 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7474 | unicode_length(PyUnicodeObject *self) |
| 7475 | { |
| 7476 | return self->length; |
| 7477 | } |
| 7478 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7479 | PyDoc_STRVAR(ljust__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 7480 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7481 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 7482 | 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] | 7483 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7484 | |
| 7485 | static PyObject * |
| 7486 | unicode_ljust(PyUnicodeObject *self, PyObject *args) |
| 7487 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7488 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7489 | Py_UNICODE fillchar = ' '; |
| 7490 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7491 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7492 | return NULL; |
| 7493 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7494 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7495 | Py_INCREF(self); |
| 7496 | return (PyObject*) self; |
| 7497 | } |
| 7498 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7499 | return (PyObject*) pad(self, 0, width - self->length, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7500 | } |
| 7501 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7502 | PyDoc_STRVAR(lower__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 7503 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7504 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7505 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7506 | |
| 7507 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7508 | unicode_lower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7509 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7510 | return fixup(self, fixlower); |
| 7511 | } |
| 7512 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7513 | #define LEFTSTRIP 0 |
| 7514 | #define RIGHTSTRIP 1 |
| 7515 | #define BOTHSTRIP 2 |
| 7516 | |
| 7517 | /* Arrays indexed by above */ |
| 7518 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 7519 | |
| 7520 | #define STRIPNAME(i) (stripformat[i]+3) |
| 7521 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7522 | /* externally visible for str.strip(unicode) */ |
| 7523 | PyObject * |
| 7524 | _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj) |
| 7525 | { |
| 7526 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7527 | Py_ssize_t len = PyUnicode_GET_SIZE(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7528 | Py_UNICODE *sep = PyUnicode_AS_UNICODE(sepobj); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7529 | Py_ssize_t seplen = PyUnicode_GET_SIZE(sepobj); |
| 7530 | Py_ssize_t i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7531 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7532 | BLOOM_MASK sepmask = make_bloom_mask(sep, seplen); |
| 7533 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7534 | i = 0; |
| 7535 | if (striptype != RIGHTSTRIP) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7536 | while (i < len && BLOOM_MEMBER(sepmask, s[i], sep, seplen)) { |
| 7537 | i++; |
| 7538 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7539 | } |
| 7540 | |
| 7541 | j = len; |
| 7542 | if (striptype != LEFTSTRIP) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7543 | do { |
| 7544 | j--; |
| 7545 | } while (j >= i && BLOOM_MEMBER(sepmask, s[j], sep, seplen)); |
| 7546 | j++; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7547 | } |
| 7548 | |
| 7549 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7550 | Py_INCREF(self); |
| 7551 | return (PyObject*)self; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7552 | } |
| 7553 | else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7554 | return PyUnicode_FromUnicode(s+i, j-i); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7555 | } |
| 7556 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7557 | |
| 7558 | static PyObject * |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7559 | do_strip(PyUnicodeObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7560 | { |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7561 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7562 | Py_ssize_t len = PyUnicode_GET_SIZE(self), i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7563 | |
| 7564 | i = 0; |
| 7565 | if (striptype != RIGHTSTRIP) { |
| 7566 | while (i < len && Py_UNICODE_ISSPACE(s[i])) { |
| 7567 | i++; |
| 7568 | } |
| 7569 | } |
| 7570 | |
| 7571 | j = len; |
| 7572 | if (striptype != LEFTSTRIP) { |
| 7573 | do { |
| 7574 | j--; |
| 7575 | } while (j >= i && Py_UNICODE_ISSPACE(s[j])); |
| 7576 | j++; |
| 7577 | } |
| 7578 | |
| 7579 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
| 7580 | Py_INCREF(self); |
| 7581 | return (PyObject*)self; |
| 7582 | } |
| 7583 | else |
| 7584 | return PyUnicode_FromUnicode(s+i, j-i); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7585 | } |
| 7586 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7587 | |
| 7588 | static PyObject * |
| 7589 | do_argstrip(PyUnicodeObject *self, int striptype, PyObject *args) |
| 7590 | { |
| 7591 | PyObject *sep = NULL; |
| 7592 | |
| 7593 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 7594 | return NULL; |
| 7595 | |
| 7596 | if (sep != NULL && sep != Py_None) { |
| 7597 | if (PyUnicode_Check(sep)) |
| 7598 | return _PyUnicode_XStrip(self, striptype, sep); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7599 | else { |
| 7600 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7601 | "%s arg must be None or str", |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7602 | STRIPNAME(striptype)); |
| 7603 | return NULL; |
| 7604 | } |
| 7605 | } |
| 7606 | |
| 7607 | return do_strip(self, striptype); |
| 7608 | } |
| 7609 | |
| 7610 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7611 | PyDoc_STRVAR(strip__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 7612 | "S.strip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7613 | \n\ |
| 7614 | Return a copy of the string S with leading and trailing\n\ |
| 7615 | whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7616 | 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] | 7617 | |
| 7618 | static PyObject * |
| 7619 | unicode_strip(PyUnicodeObject *self, PyObject *args) |
| 7620 | { |
| 7621 | if (PyTuple_GET_SIZE(args) == 0) |
| 7622 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 7623 | else |
| 7624 | return do_argstrip(self, BOTHSTRIP, args); |
| 7625 | } |
| 7626 | |
| 7627 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7628 | PyDoc_STRVAR(lstrip__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 7629 | "S.lstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7630 | \n\ |
| 7631 | Return a copy of the string S with leading whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7632 | 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] | 7633 | |
| 7634 | static PyObject * |
| 7635 | unicode_lstrip(PyUnicodeObject *self, PyObject *args) |
| 7636 | { |
| 7637 | if (PyTuple_GET_SIZE(args) == 0) |
| 7638 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 7639 | else |
| 7640 | return do_argstrip(self, LEFTSTRIP, args); |
| 7641 | } |
| 7642 | |
| 7643 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7644 | PyDoc_STRVAR(rstrip__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 7645 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7646 | \n\ |
| 7647 | Return a copy of the string S with trailing whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7648 | 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] | 7649 | |
| 7650 | static PyObject * |
| 7651 | unicode_rstrip(PyUnicodeObject *self, PyObject *args) |
| 7652 | { |
| 7653 | if (PyTuple_GET_SIZE(args) == 0) |
| 7654 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 7655 | else |
| 7656 | return do_argstrip(self, RIGHTSTRIP, args); |
| 7657 | } |
| 7658 | |
| 7659 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7660 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7661 | unicode_repeat(PyUnicodeObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7662 | { |
| 7663 | PyUnicodeObject *u; |
| 7664 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7665 | Py_ssize_t nchars; |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 7666 | size_t nbytes; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7667 | |
| 7668 | if (len < 0) |
| 7669 | len = 0; |
| 7670 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7671 | if (len == 1 && PyUnicode_CheckExact(str)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7672 | /* no repeat, return original string */ |
| 7673 | Py_INCREF(str); |
| 7674 | return (PyObject*) str; |
| 7675 | } |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 7676 | |
| 7677 | /* ensure # of chars needed doesn't overflow int and # of bytes |
| 7678 | * needed doesn't overflow size_t |
| 7679 | */ |
| 7680 | nchars = len * str->length; |
| 7681 | if (len && nchars / len != str->length) { |
| 7682 | PyErr_SetString(PyExc_OverflowError, |
| 7683 | "repeated string is too long"); |
| 7684 | return NULL; |
| 7685 | } |
| 7686 | nbytes = (nchars + 1) * sizeof(Py_UNICODE); |
| 7687 | if (nbytes / sizeof(Py_UNICODE) != (size_t)(nchars + 1)) { |
| 7688 | PyErr_SetString(PyExc_OverflowError, |
| 7689 | "repeated string is too long"); |
| 7690 | return NULL; |
| 7691 | } |
| 7692 | u = _PyUnicode_New(nchars); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7693 | if (!u) |
| 7694 | return NULL; |
| 7695 | |
| 7696 | p = u->str; |
| 7697 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7698 | if (str->length == 1 && len > 0) { |
| 7699 | Py_UNICODE_FILL(p, str->str[0], len); |
| 7700 | } else { |
| 7701 | Py_ssize_t done = 0; /* number of characters copied this far */ |
| 7702 | if (done < nchars) { |
| 7703 | Py_UNICODE_COPY(p, str->str, str->length); |
| 7704 | done = str->length; |
| 7705 | } |
| 7706 | while (done < nchars) { |
Christian Heimes | cc47b05 | 2008-03-25 14:56:36 +0000 | [diff] [blame] | 7707 | Py_ssize_t n = (done <= nchars-done) ? done : nchars-done; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7708 | Py_UNICODE_COPY(p+done, p, n); |
| 7709 | done += n; |
| 7710 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7711 | } |
| 7712 | |
| 7713 | return (PyObject*) u; |
| 7714 | } |
| 7715 | |
| 7716 | PyObject *PyUnicode_Replace(PyObject *obj, |
| 7717 | PyObject *subobj, |
| 7718 | PyObject *replobj, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7719 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7720 | { |
| 7721 | PyObject *self; |
| 7722 | PyObject *str1; |
| 7723 | PyObject *str2; |
| 7724 | PyObject *result; |
| 7725 | |
| 7726 | self = PyUnicode_FromObject(obj); |
| 7727 | if (self == NULL) |
| 7728 | return NULL; |
| 7729 | str1 = PyUnicode_FromObject(subobj); |
| 7730 | if (str1 == NULL) { |
| 7731 | Py_DECREF(self); |
| 7732 | return NULL; |
| 7733 | } |
| 7734 | str2 = PyUnicode_FromObject(replobj); |
| 7735 | if (str2 == NULL) { |
| 7736 | Py_DECREF(self); |
| 7737 | Py_DECREF(str1); |
| 7738 | return NULL; |
| 7739 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7740 | result = replace((PyUnicodeObject *)self, |
| 7741 | (PyUnicodeObject *)str1, |
| 7742 | (PyUnicodeObject *)str2, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7743 | maxcount); |
| 7744 | Py_DECREF(self); |
| 7745 | Py_DECREF(str1); |
| 7746 | Py_DECREF(str2); |
| 7747 | return result; |
| 7748 | } |
| 7749 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7750 | PyDoc_STRVAR(replace__doc__, |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 7751 | "S.replace (old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7752 | \n\ |
| 7753 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 7754 | old replaced by new. If the optional argument count is\n\ |
| 7755 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7756 | |
| 7757 | static PyObject* |
| 7758 | unicode_replace(PyUnicodeObject *self, PyObject *args) |
| 7759 | { |
| 7760 | PyUnicodeObject *str1; |
| 7761 | PyUnicodeObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7762 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7763 | PyObject *result; |
| 7764 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7765 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7766 | return NULL; |
| 7767 | str1 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str1); |
| 7768 | if (str1 == NULL) |
| 7769 | return NULL; |
| 7770 | str2 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str2); |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 7771 | if (str2 == NULL) { |
| 7772 | Py_DECREF(str1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7773 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 7774 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7775 | |
| 7776 | result = replace(self, str1, str2, maxcount); |
| 7777 | |
| 7778 | Py_DECREF(str1); |
| 7779 | Py_DECREF(str2); |
| 7780 | return result; |
| 7781 | } |
| 7782 | |
| 7783 | static |
| 7784 | PyObject *unicode_repr(PyObject *unicode) |
| 7785 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7786 | PyObject *repr; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7787 | Py_UNICODE *p; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7788 | Py_UNICODE *s = PyUnicode_AS_UNICODE(unicode); |
| 7789 | Py_ssize_t size = PyUnicode_GET_SIZE(unicode); |
| 7790 | |
| 7791 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 7792 | better to choose a different scheme. Perhaps scan the |
| 7793 | first N-chars of the string and allocate based on that size. |
| 7794 | */ |
| 7795 | /* Initial allocation is based on the longest-possible unichr |
| 7796 | escape. |
| 7797 | |
| 7798 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 7799 | unichr, so in this case it's the longest unichr escape. In |
| 7800 | narrow (UTF-16) builds this is five chars per source unichr |
| 7801 | since there are two unichrs in the surrogate pair, so in narrow |
| 7802 | (UTF-16) builds it's not the longest unichr escape. |
| 7803 | |
| 7804 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 7805 | so in the narrow (UTF-16) build case it's the longest unichr |
| 7806 | escape. |
| 7807 | */ |
| 7808 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7809 | repr = PyUnicode_FromUnicode(NULL, |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7810 | 2 /* quotes */ |
| 7811 | #ifdef Py_UNICODE_WIDE |
| 7812 | + 10*size |
| 7813 | #else |
| 7814 | + 6*size |
| 7815 | #endif |
| 7816 | + 1); |
| 7817 | if (repr == NULL) |
| 7818 | return NULL; |
| 7819 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7820 | p = PyUnicode_AS_UNICODE(repr); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7821 | |
| 7822 | /* Add quote */ |
| 7823 | *p++ = (findchar(s, size, '\'') && |
| 7824 | !findchar(s, size, '"')) ? '"' : '\''; |
| 7825 | while (size-- > 0) { |
| 7826 | Py_UNICODE ch = *s++; |
| 7827 | |
| 7828 | /* Escape quotes and backslashes */ |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7829 | if ((ch == PyUnicode_AS_UNICODE(repr)[0]) || (ch == '\\')) { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7830 | *p++ = '\\'; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7831 | *p++ = ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7832 | continue; |
| 7833 | } |
| 7834 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7835 | /* Map special whitespace to '\t', \n', '\r' */ |
| 7836 | if (ch == '\t') { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7837 | *p++ = '\\'; |
| 7838 | *p++ = 't'; |
| 7839 | } |
| 7840 | else if (ch == '\n') { |
| 7841 | *p++ = '\\'; |
| 7842 | *p++ = 'n'; |
| 7843 | } |
| 7844 | else if (ch == '\r') { |
| 7845 | *p++ = '\\'; |
| 7846 | *p++ = 'r'; |
| 7847 | } |
| 7848 | |
| 7849 | /* Map non-printable US ASCII to '\xhh' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7850 | else if (ch < ' ' || ch == 0x7F) { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7851 | *p++ = '\\'; |
| 7852 | *p++ = 'x'; |
| 7853 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 7854 | *p++ = hexdigits[ch & 0x000F]; |
| 7855 | } |
| 7856 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7857 | /* Copy ASCII characters as-is */ |
| 7858 | else if (ch < 0x7F) { |
| 7859 | *p++ = ch; |
| 7860 | } |
| 7861 | |
| 7862 | /* Non-ASCII characters */ |
| 7863 | else { |
| 7864 | Py_UCS4 ucs = ch; |
| 7865 | |
| 7866 | #ifndef Py_UNICODE_WIDE |
| 7867 | Py_UNICODE ch2 = 0; |
| 7868 | /* Get code point from surrogate pair */ |
| 7869 | if (size > 0) { |
| 7870 | ch2 = *s; |
| 7871 | if (ch >= 0xD800 && ch < 0xDC00 && ch2 >= 0xDC00 |
| 7872 | && ch2 <= 0xDFFF) { |
| 7873 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) |
| 7874 | + 0x00010000; |
| 7875 | s++; |
| 7876 | size--; |
| 7877 | } |
| 7878 | } |
| 7879 | #endif |
| 7880 | /* Map Unicode whitespace and control characters |
| 7881 | (categories Z* and C* except ASCII space) |
| 7882 | */ |
| 7883 | if (!Py_UNICODE_ISPRINTABLE(ucs)) { |
| 7884 | /* Map 8-bit characters to '\xhh' */ |
| 7885 | if (ucs <= 0xff) { |
| 7886 | *p++ = '\\'; |
| 7887 | *p++ = 'x'; |
| 7888 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 7889 | *p++ = hexdigits[ch & 0x000F]; |
| 7890 | } |
| 7891 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 7892 | else if (ucs >= 0x10000) { |
| 7893 | *p++ = '\\'; |
| 7894 | *p++ = 'U'; |
| 7895 | *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; |
| 7896 | *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; |
| 7897 | *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; |
| 7898 | *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; |
| 7899 | *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; |
| 7900 | *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; |
| 7901 | *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; |
| 7902 | *p++ = hexdigits[ucs & 0x0000000F]; |
| 7903 | } |
| 7904 | /* Map 16-bit characters to '\uxxxx' */ |
| 7905 | else { |
| 7906 | *p++ = '\\'; |
| 7907 | *p++ = 'u'; |
| 7908 | *p++ = hexdigits[(ucs >> 12) & 0x000F]; |
| 7909 | *p++ = hexdigits[(ucs >> 8) & 0x000F]; |
| 7910 | *p++ = hexdigits[(ucs >> 4) & 0x000F]; |
| 7911 | *p++ = hexdigits[ucs & 0x000F]; |
| 7912 | } |
| 7913 | } |
| 7914 | /* Copy characters as-is */ |
| 7915 | else { |
| 7916 | *p++ = ch; |
| 7917 | #ifndef Py_UNICODE_WIDE |
| 7918 | if (ucs >= 0x10000) |
| 7919 | *p++ = ch2; |
| 7920 | #endif |
| 7921 | } |
| 7922 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7923 | } |
| 7924 | /* Add quote */ |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7925 | *p++ = PyUnicode_AS_UNICODE(repr)[0]; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7926 | |
| 7927 | *p = '\0'; |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 7928 | PyUnicode_Resize(&repr, p - PyUnicode_AS_UNICODE(repr)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7929 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7930 | } |
| 7931 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7932 | PyDoc_STRVAR(rfind__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 7933 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7934 | \n\ |
| 7935 | Return the highest index in S where substring sub is found,\n\ |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 7936 | such that sub is contained within s[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7937 | arguments start and end are interpreted as in slice notation.\n\ |
| 7938 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7939 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7940 | |
| 7941 | static PyObject * |
| 7942 | unicode_rfind(PyUnicodeObject *self, PyObject *args) |
| 7943 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7944 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7945 | Py_ssize_t start; |
| 7946 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7947 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7948 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7949 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
| 7950 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7951 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7952 | result = stringlib_rfind_slice( |
| 7953 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 7954 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 7955 | start, end |
| 7956 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7957 | |
| 7958 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7959 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7960 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7961 | } |
| 7962 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7963 | PyDoc_STRVAR(rindex__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 7964 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7965 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7966 | 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] | 7967 | |
| 7968 | static PyObject * |
| 7969 | unicode_rindex(PyUnicodeObject *self, PyObject *args) |
| 7970 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7971 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7972 | Py_ssize_t start; |
| 7973 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7974 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7975 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7976 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
| 7977 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7978 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7979 | result = stringlib_rfind_slice( |
| 7980 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 7981 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 7982 | start, end |
| 7983 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7984 | |
| 7985 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7986 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7987 | if (result < 0) { |
| 7988 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 7989 | return NULL; |
| 7990 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7991 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7992 | } |
| 7993 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7994 | PyDoc_STRVAR(rjust__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 7995 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7996 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 7997 | 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] | 7998 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7999 | |
| 8000 | static PyObject * |
| 8001 | unicode_rjust(PyUnicodeObject *self, PyObject *args) |
| 8002 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8003 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 8004 | Py_UNICODE fillchar = ' '; |
| 8005 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8006 | if (!PyArg_ParseTuple(args, "n|O&:rjust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8007 | return NULL; |
| 8008 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 8009 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8010 | Py_INCREF(self); |
| 8011 | return (PyObject*) self; |
| 8012 | } |
| 8013 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 8014 | return (PyObject*) pad(self, width - self->length, 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8015 | } |
| 8016 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8017 | PyObject *PyUnicode_Split(PyObject *s, |
| 8018 | PyObject *sep, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8019 | Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8020 | { |
| 8021 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8022 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8023 | s = PyUnicode_FromObject(s); |
| 8024 | if (s == NULL) |
| 8025 | return NULL; |
| 8026 | if (sep != NULL) { |
| 8027 | sep = PyUnicode_FromObject(sep); |
| 8028 | if (sep == NULL) { |
| 8029 | Py_DECREF(s); |
| 8030 | return NULL; |
| 8031 | } |
| 8032 | } |
| 8033 | |
| 8034 | result = split((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 8035 | |
| 8036 | Py_DECREF(s); |
| 8037 | Py_XDECREF(sep); |
| 8038 | return result; |
| 8039 | } |
| 8040 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8041 | PyDoc_STRVAR(split__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 8042 | "S.split([sep[, maxsplit]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8043 | \n\ |
| 8044 | Return a list of the words in S, using sep as the\n\ |
| 8045 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 8046 | 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] | 8047 | whitespace string is a separator and empty strings are\n\ |
| 8048 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8049 | |
| 8050 | static PyObject* |
| 8051 | unicode_split(PyUnicodeObject *self, PyObject *args) |
| 8052 | { |
| 8053 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8054 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8055 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8056 | if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8057 | return NULL; |
| 8058 | |
| 8059 | if (substring == Py_None) |
| 8060 | return split(self, NULL, maxcount); |
| 8061 | else if (PyUnicode_Check(substring)) |
| 8062 | return split(self, (PyUnicodeObject *)substring, maxcount); |
| 8063 | else |
| 8064 | return PyUnicode_Split((PyObject *)self, substring, maxcount); |
| 8065 | } |
| 8066 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8067 | PyObject * |
| 8068 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 8069 | { |
| 8070 | PyObject* str_obj; |
| 8071 | PyObject* sep_obj; |
| 8072 | PyObject* out; |
| 8073 | |
| 8074 | str_obj = PyUnicode_FromObject(str_in); |
| 8075 | if (!str_obj) |
| 8076 | return NULL; |
| 8077 | sep_obj = PyUnicode_FromObject(sep_in); |
| 8078 | if (!sep_obj) { |
| 8079 | Py_DECREF(str_obj); |
| 8080 | return NULL; |
| 8081 | } |
| 8082 | |
| 8083 | out = stringlib_partition( |
| 8084 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 8085 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 8086 | ); |
| 8087 | |
| 8088 | Py_DECREF(sep_obj); |
| 8089 | Py_DECREF(str_obj); |
| 8090 | |
| 8091 | return out; |
| 8092 | } |
| 8093 | |
| 8094 | |
| 8095 | PyObject * |
| 8096 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 8097 | { |
| 8098 | PyObject* str_obj; |
| 8099 | PyObject* sep_obj; |
| 8100 | PyObject* out; |
| 8101 | |
| 8102 | str_obj = PyUnicode_FromObject(str_in); |
| 8103 | if (!str_obj) |
| 8104 | return NULL; |
| 8105 | sep_obj = PyUnicode_FromObject(sep_in); |
| 8106 | if (!sep_obj) { |
| 8107 | Py_DECREF(str_obj); |
| 8108 | return NULL; |
| 8109 | } |
| 8110 | |
| 8111 | out = stringlib_rpartition( |
| 8112 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 8113 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 8114 | ); |
| 8115 | |
| 8116 | Py_DECREF(sep_obj); |
| 8117 | Py_DECREF(str_obj); |
| 8118 | |
| 8119 | return out; |
| 8120 | } |
| 8121 | |
| 8122 | PyDoc_STRVAR(partition__doc__, |
| 8123 | "S.partition(sep) -> (head, sep, tail)\n\ |
| 8124 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 8125 | 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] | 8126 | 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] | 8127 | found, return S and two empty strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8128 | |
| 8129 | static PyObject* |
| 8130 | unicode_partition(PyUnicodeObject *self, PyObject *separator) |
| 8131 | { |
| 8132 | return PyUnicode_Partition((PyObject *)self, separator); |
| 8133 | } |
| 8134 | |
| 8135 | PyDoc_STRVAR(rpartition__doc__, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 8136 | "S.rpartition(sep) -> (tail, sep, head)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8137 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 8138 | 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] | 8139 | 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] | 8140 | separator is not found, return two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8141 | |
| 8142 | static PyObject* |
| 8143 | unicode_rpartition(PyUnicodeObject *self, PyObject *separator) |
| 8144 | { |
| 8145 | return PyUnicode_RPartition((PyObject *)self, separator); |
| 8146 | } |
| 8147 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8148 | PyObject *PyUnicode_RSplit(PyObject *s, |
| 8149 | PyObject *sep, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8150 | Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8151 | { |
| 8152 | PyObject *result; |
| 8153 | |
| 8154 | s = PyUnicode_FromObject(s); |
| 8155 | if (s == NULL) |
| 8156 | return NULL; |
| 8157 | if (sep != NULL) { |
| 8158 | sep = PyUnicode_FromObject(sep); |
| 8159 | if (sep == NULL) { |
| 8160 | Py_DECREF(s); |
| 8161 | return NULL; |
| 8162 | } |
| 8163 | } |
| 8164 | |
| 8165 | result = rsplit((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 8166 | |
| 8167 | Py_DECREF(s); |
| 8168 | Py_XDECREF(sep); |
| 8169 | return result; |
| 8170 | } |
| 8171 | |
| 8172 | PyDoc_STRVAR(rsplit__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 8173 | "S.rsplit([sep[, maxsplit]]) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8174 | \n\ |
| 8175 | Return a list of the words in S, using sep as the\n\ |
| 8176 | delimiter string, starting at the end of the string and\n\ |
| 8177 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 8178 | splits are done. If sep is not specified, any whitespace string\n\ |
| 8179 | is a separator."); |
| 8180 | |
| 8181 | static PyObject* |
| 8182 | unicode_rsplit(PyUnicodeObject *self, PyObject *args) |
| 8183 | { |
| 8184 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8185 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8186 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8187 | if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8188 | return NULL; |
| 8189 | |
| 8190 | if (substring == Py_None) |
| 8191 | return rsplit(self, NULL, maxcount); |
| 8192 | else if (PyUnicode_Check(substring)) |
| 8193 | return rsplit(self, (PyUnicodeObject *)substring, maxcount); |
| 8194 | else |
| 8195 | return PyUnicode_RSplit((PyObject *)self, substring, maxcount); |
| 8196 | } |
| 8197 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8198 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 4469d0c | 2008-11-30 22:46:23 +0000 | [diff] [blame] | 8199 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8200 | \n\ |
| 8201 | 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] | 8202 | 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] | 8203 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8204 | |
| 8205 | static PyObject* |
| 8206 | unicode_splitlines(PyUnicodeObject *self, PyObject *args) |
| 8207 | { |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 8208 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8209 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 8210 | if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8211 | return NULL; |
| 8212 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 8213 | return PyUnicode_Splitlines((PyObject *)self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8214 | } |
| 8215 | |
| 8216 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 8217 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8218 | { |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 8219 | if (PyUnicode_CheckExact(self)) { |
| 8220 | Py_INCREF(self); |
| 8221 | return self; |
| 8222 | } else |
| 8223 | /* Subtype -- return genuine unicode string with the same value. */ |
| 8224 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(self), |
| 8225 | PyUnicode_GET_SIZE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8226 | } |
| 8227 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8228 | PyDoc_STRVAR(swapcase__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 8229 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8230 | \n\ |
| 8231 | 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] | 8232 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8233 | |
| 8234 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8235 | unicode_swapcase(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8236 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8237 | return fixup(self, fixswapcase); |
| 8238 | } |
| 8239 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8240 | PyDoc_STRVAR(maketrans__doc__, |
| 8241 | "str.maketrans(x[, y[, z]]) -> dict (static method)\n\ |
| 8242 | \n\ |
| 8243 | Return a translation table usable for str.translate().\n\ |
| 8244 | If there is only one argument, it must be a dictionary mapping Unicode\n\ |
| 8245 | ordinals (integers) or characters to Unicode ordinals, strings or None.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8246 | Character keys will be then converted to ordinals.\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8247 | If there are two arguments, they must be strings of equal length, and\n\ |
| 8248 | in the resulting dictionary, each character in x will be mapped to the\n\ |
| 8249 | character at the same position in y. If there is a third argument, it\n\ |
| 8250 | must be a string, whose characters will be mapped to None in the result."); |
| 8251 | |
| 8252 | static PyObject* |
| 8253 | unicode_maketrans(PyUnicodeObject *null, PyObject *args) |
| 8254 | { |
| 8255 | PyObject *x, *y = NULL, *z = NULL; |
| 8256 | PyObject *new = NULL, *key, *value; |
| 8257 | Py_ssize_t i = 0; |
| 8258 | int res; |
| 8259 | |
| 8260 | if (!PyArg_ParseTuple(args, "O|UU:maketrans", &x, &y, &z)) |
| 8261 | return NULL; |
| 8262 | new = PyDict_New(); |
| 8263 | if (!new) |
| 8264 | return NULL; |
| 8265 | if (y != NULL) { |
| 8266 | /* x must be a string too, of equal length */ |
| 8267 | Py_ssize_t ylen = PyUnicode_GET_SIZE(y); |
| 8268 | if (!PyUnicode_Check(x)) { |
| 8269 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 8270 | "be a string if there is a second argument"); |
| 8271 | goto err; |
| 8272 | } |
| 8273 | if (PyUnicode_GET_SIZE(x) != ylen) { |
| 8274 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 8275 | "arguments must have equal length"); |
| 8276 | goto err; |
| 8277 | } |
| 8278 | /* create entries for translating chars in x to those in y */ |
| 8279 | for (i = 0; i < PyUnicode_GET_SIZE(x); i++) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8280 | key = PyLong_FromLong(PyUnicode_AS_UNICODE(x)[i]); |
| 8281 | value = PyLong_FromLong(PyUnicode_AS_UNICODE(y)[i]); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8282 | if (!key || !value) |
| 8283 | goto err; |
| 8284 | res = PyDict_SetItem(new, key, value); |
| 8285 | Py_DECREF(key); |
| 8286 | Py_DECREF(value); |
| 8287 | if (res < 0) |
| 8288 | goto err; |
| 8289 | } |
| 8290 | /* create entries for deleting chars in z */ |
| 8291 | if (z != NULL) { |
| 8292 | for (i = 0; i < PyUnicode_GET_SIZE(z); i++) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8293 | key = PyLong_FromLong(PyUnicode_AS_UNICODE(z)[i]); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8294 | if (!key) |
| 8295 | goto err; |
| 8296 | res = PyDict_SetItem(new, key, Py_None); |
| 8297 | Py_DECREF(key); |
| 8298 | if (res < 0) |
| 8299 | goto err; |
| 8300 | } |
| 8301 | } |
| 8302 | } else { |
| 8303 | /* x must be a dict */ |
| 8304 | if (!PyDict_Check(x)) { |
| 8305 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 8306 | "to maketrans it must be a dict"); |
| 8307 | goto err; |
| 8308 | } |
| 8309 | /* copy entries into the new dict, converting string keys to int keys */ |
| 8310 | while (PyDict_Next(x, &i, &key, &value)) { |
| 8311 | if (PyUnicode_Check(key)) { |
| 8312 | /* convert string keys to integer keys */ |
| 8313 | PyObject *newkey; |
| 8314 | if (PyUnicode_GET_SIZE(key) != 1) { |
| 8315 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 8316 | "table must be of length 1"); |
| 8317 | goto err; |
| 8318 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8319 | newkey = PyLong_FromLong(PyUnicode_AS_UNICODE(key)[0]); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8320 | if (!newkey) |
| 8321 | goto err; |
| 8322 | res = PyDict_SetItem(new, newkey, value); |
| 8323 | Py_DECREF(newkey); |
| 8324 | if (res < 0) |
| 8325 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8326 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8327 | /* just keep integer keys */ |
| 8328 | if (PyDict_SetItem(new, key, value) < 0) |
| 8329 | goto err; |
| 8330 | } else { |
| 8331 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 8332 | "be strings or integers"); |
| 8333 | goto err; |
| 8334 | } |
| 8335 | } |
| 8336 | } |
| 8337 | return new; |
| 8338 | err: |
| 8339 | Py_DECREF(new); |
| 8340 | return NULL; |
| 8341 | } |
| 8342 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8343 | PyDoc_STRVAR(translate__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 8344 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8345 | \n\ |
| 8346 | Return a copy of the string S, where all characters have been mapped\n\ |
| 8347 | through the given translation table, which must be a mapping of\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8348 | Unicode ordinals to Unicode ordinals, strings, or None.\n\ |
Walter Dörwald | 5c1ee17 | 2002-09-04 20:31:32 +0000 | [diff] [blame] | 8349 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 8350 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8351 | |
| 8352 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8353 | unicode_translate(PyUnicodeObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8354 | { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8355 | return PyUnicode_TranslateCharmap(self->str, self->length, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8356 | } |
| 8357 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8358 | PyDoc_STRVAR(upper__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 8359 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8360 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8361 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8362 | |
| 8363 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8364 | unicode_upper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8365 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8366 | return fixup(self, fixupper); |
| 8367 | } |
| 8368 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8369 | PyDoc_STRVAR(zfill__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 8370 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8371 | \n\ |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 8372 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 8373 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8374 | |
| 8375 | static PyObject * |
| 8376 | unicode_zfill(PyUnicodeObject *self, PyObject *args) |
| 8377 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8378 | Py_ssize_t fill; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8379 | PyUnicodeObject *u; |
| 8380 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8381 | Py_ssize_t width; |
| 8382 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8383 | return NULL; |
| 8384 | |
| 8385 | if (self->length >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 8386 | if (PyUnicode_CheckExact(self)) { |
| 8387 | Py_INCREF(self); |
| 8388 | return (PyObject*) self; |
| 8389 | } |
| 8390 | else |
| 8391 | return PyUnicode_FromUnicode( |
| 8392 | PyUnicode_AS_UNICODE(self), |
| 8393 | PyUnicode_GET_SIZE(self) |
| 8394 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8395 | } |
| 8396 | |
| 8397 | fill = width - self->length; |
| 8398 | |
| 8399 | u = pad(self, fill, 0, '0'); |
| 8400 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 8401 | if (u == NULL) |
| 8402 | return NULL; |
| 8403 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8404 | if (u->str[fill] == '+' || u->str[fill] == '-') { |
| 8405 | /* move sign to beginning of string */ |
| 8406 | u->str[0] = u->str[fill]; |
| 8407 | u->str[fill] = '0'; |
| 8408 | } |
| 8409 | |
| 8410 | return (PyObject*) u; |
| 8411 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8412 | |
| 8413 | #if 0 |
| 8414 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8415 | unicode_freelistsize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8416 | { |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 8417 | return PyLong_FromLong(numfree); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8418 | } |
| 8419 | #endif |
| 8420 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8421 | PyDoc_STRVAR(startswith__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 8422 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8423 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 8424 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 8425 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8426 | With optional end, stop comparing S at that position.\n\ |
| 8427 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8428 | |
| 8429 | static PyObject * |
| 8430 | unicode_startswith(PyUnicodeObject *self, |
| 8431 | PyObject *args) |
| 8432 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8433 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8434 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8435 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8436 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8437 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8438 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8439 | if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj, |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 8440 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8441 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8442 | if (PyTuple_Check(subobj)) { |
| 8443 | Py_ssize_t i; |
| 8444 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 8445 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 8446 | PyTuple_GET_ITEM(subobj, i)); |
| 8447 | if (substring == NULL) |
| 8448 | return NULL; |
| 8449 | result = tailmatch(self, substring, start, end, -1); |
| 8450 | Py_DECREF(substring); |
| 8451 | if (result) { |
| 8452 | Py_RETURN_TRUE; |
| 8453 | } |
| 8454 | } |
| 8455 | /* nothing matched */ |
| 8456 | Py_RETURN_FALSE; |
| 8457 | } |
| 8458 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8459 | if (substring == NULL) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8460 | return NULL; |
| 8461 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8462 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8463 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8464 | } |
| 8465 | |
| 8466 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8467 | PyDoc_STRVAR(endswith__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 8468 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8469 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 8470 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 8471 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8472 | With optional end, stop comparing S at that position.\n\ |
| 8473 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8474 | |
| 8475 | static PyObject * |
| 8476 | unicode_endswith(PyUnicodeObject *self, |
| 8477 | PyObject *args) |
| 8478 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8479 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8480 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8481 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8482 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8483 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8484 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8485 | if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj, |
| 8486 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8487 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8488 | if (PyTuple_Check(subobj)) { |
| 8489 | Py_ssize_t i; |
| 8490 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 8491 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 8492 | PyTuple_GET_ITEM(subobj, i)); |
| 8493 | if (substring == NULL) |
| 8494 | return NULL; |
| 8495 | result = tailmatch(self, substring, start, end, +1); |
| 8496 | Py_DECREF(substring); |
| 8497 | if (result) { |
| 8498 | Py_RETURN_TRUE; |
| 8499 | } |
| 8500 | } |
| 8501 | Py_RETURN_FALSE; |
| 8502 | } |
| 8503 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8504 | if (substring == NULL) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8505 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8506 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8507 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8508 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8509 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8510 | } |
| 8511 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8512 | #include "stringlib/string_format.h" |
| 8513 | |
| 8514 | PyDoc_STRVAR(format__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 8515 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8516 | \n\ |
| 8517 | "); |
| 8518 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 8519 | static PyObject * |
| 8520 | unicode__format__(PyObject* self, PyObject* args) |
| 8521 | { |
| 8522 | PyObject *format_spec; |
| 8523 | |
| 8524 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 8525 | return NULL; |
| 8526 | |
| 8527 | return _PyUnicode_FormatAdvanced(self, |
| 8528 | PyUnicode_AS_UNICODE(format_spec), |
| 8529 | PyUnicode_GET_SIZE(format_spec)); |
| 8530 | } |
| 8531 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8532 | PyDoc_STRVAR(p_format__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 8533 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8534 | \n\ |
| 8535 | "); |
| 8536 | |
| 8537 | static PyObject * |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 8538 | unicode__sizeof__(PyUnicodeObject *v) |
| 8539 | { |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 8540 | return PyLong_FromSsize_t(sizeof(PyUnicodeObject) + |
| 8541 | sizeof(Py_UNICODE) * (v->length + 1)); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 8542 | } |
| 8543 | |
| 8544 | PyDoc_STRVAR(sizeof__doc__, |
| 8545 | "S.__sizeof__() -> size of S in memory, in bytes"); |
| 8546 | |
| 8547 | static PyObject * |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 8548 | unicode_getnewargs(PyUnicodeObject *v) |
| 8549 | { |
| 8550 | return Py_BuildValue("(u#)", v->str, v->length); |
| 8551 | } |
| 8552 | |
| 8553 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8554 | static PyMethodDef unicode_methods[] = { |
| 8555 | |
| 8556 | /* Order is according to common usage: often used methods should |
| 8557 | appear first, since lookup is done sequentially. */ |
| 8558 | |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8559 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS, encode__doc__}, |
| 8560 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 8561 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8562 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8563 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 8564 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
| 8565 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 8566 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 8567 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 8568 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 8569 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8570 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8571 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 8572 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 8573 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8574 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8575 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 8576 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 8577 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8578 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8579 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8580 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8581 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8582 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 8583 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 8584 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 8585 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 8586 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 8587 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 8588 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 8589 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 8590 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 8591 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 8592 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 8593 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 8594 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 8595 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 8596 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8597 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8598 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 8599 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 8600 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Eric Smith | f6db409 | 2007-08-27 23:52:26 +0000 | [diff] [blame] | 8601 | {"_formatter_field_name_split", (PyCFunction) formatter_field_name_split, METH_NOARGS}, |
| 8602 | {"_formatter_parser", (PyCFunction) formatter_parser, METH_NOARGS}, |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8603 | {"maketrans", (PyCFunction) unicode_maketrans, |
| 8604 | METH_VARARGS | METH_STATIC, maketrans__doc__}, |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 8605 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 8606 | #if 0 |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8607 | {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8608 | #endif |
| 8609 | |
| 8610 | #if 0 |
| 8611 | /* This one is just used for debugging the implementation. */ |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8612 | {"freelistsize", (PyCFunction) unicode_freelistsize, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8613 | #endif |
| 8614 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 8615 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8616 | {NULL, NULL} |
| 8617 | }; |
| 8618 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8619 | static PyObject * |
| 8620 | unicode_mod(PyObject *v, PyObject *w) |
| 8621 | { |
| 8622 | if (!PyUnicode_Check(v)) { |
| 8623 | Py_INCREF(Py_NotImplemented); |
| 8624 | return Py_NotImplemented; |
| 8625 | } |
| 8626 | return PyUnicode_Format(v, w); |
| 8627 | } |
| 8628 | |
| 8629 | static PyNumberMethods unicode_as_number = { |
| 8630 | 0, /*nb_add*/ |
| 8631 | 0, /*nb_subtract*/ |
| 8632 | 0, /*nb_multiply*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8633 | unicode_mod, /*nb_remainder*/ |
| 8634 | }; |
| 8635 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8636 | static PySequenceMethods unicode_as_sequence = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8637 | (lenfunc) unicode_length, /* sq_length */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8638 | PyUnicode_Concat, /* sq_concat */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8639 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 8640 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
Thomas Wouters | d2cf20e | 2007-08-30 22:57:53 +0000 | [diff] [blame] | 8641 | 0, /* sq_slice */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8642 | 0, /* sq_ass_item */ |
| 8643 | 0, /* sq_ass_slice */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8644 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8645 | }; |
| 8646 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8647 | static PyObject* |
| 8648 | unicode_subscript(PyUnicodeObject* self, PyObject* item) |
| 8649 | { |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 8650 | if (PyIndex_Check(item)) { |
| 8651 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8652 | if (i == -1 && PyErr_Occurred()) |
| 8653 | return NULL; |
| 8654 | if (i < 0) |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 8655 | i += PyUnicode_GET_SIZE(self); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8656 | return unicode_getitem(self, i); |
| 8657 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8658 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8659 | Py_UNICODE* source_buf; |
| 8660 | Py_UNICODE* result_buf; |
| 8661 | PyObject* result; |
| 8662 | |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 8663 | if (PySlice_GetIndicesEx((PySliceObject*)item, PyUnicode_GET_SIZE(self), |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8664 | &start, &stop, &step, &slicelength) < 0) { |
| 8665 | return NULL; |
| 8666 | } |
| 8667 | |
| 8668 | if (slicelength <= 0) { |
| 8669 | return PyUnicode_FromUnicode(NULL, 0); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 8670 | } else if (start == 0 && step == 1 && slicelength == self->length && |
| 8671 | PyUnicode_CheckExact(self)) { |
| 8672 | Py_INCREF(self); |
| 8673 | return (PyObject *)self; |
| 8674 | } else if (step == 1) { |
| 8675 | return PyUnicode_FromUnicode(self->str + start, slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8676 | } else { |
| 8677 | source_buf = PyUnicode_AS_UNICODE((PyObject*)self); |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 8678 | result_buf = (Py_UNICODE *)PyObject_MALLOC(slicelength* |
| 8679 | sizeof(Py_UNICODE)); |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 8680 | |
| 8681 | if (result_buf == NULL) |
| 8682 | return PyErr_NoMemory(); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8683 | |
| 8684 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 8685 | result_buf[i] = source_buf[cur]; |
| 8686 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8687 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8688 | result = PyUnicode_FromUnicode(result_buf, slicelength); |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 8689 | PyObject_FREE(result_buf); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8690 | return result; |
| 8691 | } |
| 8692 | } else { |
| 8693 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 8694 | return NULL; |
| 8695 | } |
| 8696 | } |
| 8697 | |
| 8698 | static PyMappingMethods unicode_as_mapping = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8699 | (lenfunc)unicode_length, /* mp_length */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8700 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 8701 | (objobjargproc)0, /* mp_ass_subscript */ |
| 8702 | }; |
| 8703 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8704 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8705 | /* Helpers for PyUnicode_Format() */ |
| 8706 | |
| 8707 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8708 | 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] | 8709 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8710 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8711 | if (argidx < arglen) { |
| 8712 | (*p_argidx)++; |
| 8713 | if (arglen < 0) |
| 8714 | return args; |
| 8715 | else |
| 8716 | return PyTuple_GetItem(args, argidx); |
| 8717 | } |
| 8718 | PyErr_SetString(PyExc_TypeError, |
| 8719 | "not enough arguments for format string"); |
| 8720 | return NULL; |
| 8721 | } |
| 8722 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8723 | static Py_ssize_t |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8724 | strtounicode(Py_UNICODE *buffer, const char *charbuffer) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8725 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8726 | register Py_ssize_t i; |
| 8727 | Py_ssize_t len = strlen(charbuffer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8728 | for (i = len - 1; i >= 0; i--) |
| 8729 | buffer[i] = (Py_UNICODE) charbuffer[i]; |
| 8730 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8731 | return len; |
| 8732 | } |
| 8733 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8734 | static int |
| 8735 | doubletounicode(Py_UNICODE *buffer, size_t len, const char *format, double x) |
| 8736 | { |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 8737 | Py_ssize_t result; |
| 8738 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8739 | PyOS_ascii_formatd((char *)buffer, len, format, x); |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 8740 | result = strtounicode(buffer, (char *)buffer); |
| 8741 | return Py_SAFE_DOWNCAST(result, Py_ssize_t, int); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8742 | } |
| 8743 | |
Christian Heimes | 3fd1399 | 2008-03-21 01:05:49 +0000 | [diff] [blame] | 8744 | #if 0 |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8745 | static int |
| 8746 | longtounicode(Py_UNICODE *buffer, size_t len, const char *format, long x) |
| 8747 | { |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 8748 | Py_ssize_t result; |
| 8749 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8750 | PyOS_snprintf((char *)buffer, len, format, x); |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 8751 | result = strtounicode(buffer, (char *)buffer); |
| 8752 | return Py_SAFE_DOWNCAST(result, Py_ssize_t, int); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8753 | } |
Christian Heimes | 3fd1399 | 2008-03-21 01:05:49 +0000 | [diff] [blame] | 8754 | #endif |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8755 | |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 8756 | /* XXX To save some code duplication, formatfloat/long/int could have been |
| 8757 | shared with stringobject.c, converting from 8-bit to Unicode after the |
| 8758 | formatting is done. */ |
| 8759 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8760 | static int |
| 8761 | formatfloat(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8762 | size_t buflen, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8763 | int flags, |
| 8764 | int prec, |
| 8765 | int type, |
| 8766 | PyObject *v) |
| 8767 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8768 | /* fmt = '%#.' + `prec` + `type` |
| 8769 | worst case length = 3 + 10 (len of INT_MAX) + 1 = 14 (use 20)*/ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8770 | char fmt[20]; |
| 8771 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8772 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8773 | x = PyFloat_AsDouble(v); |
| 8774 | if (x == -1.0 && PyErr_Occurred()) |
| 8775 | return -1; |
| 8776 | if (prec < 0) |
| 8777 | prec = 6; |
Eric Smith | 22b85b3 | 2008-07-17 19:18:29 +0000 | [diff] [blame] | 8778 | if (type == 'f' && (fabs(x) / 1e25) >= 1e25) |
| 8779 | type = 'g'; |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8780 | /* Worst case length calc to ensure no buffer overrun: |
| 8781 | |
| 8782 | 'g' formats: |
| 8783 | fmt = %#.<prec>g |
| 8784 | buf = '-' + [0-9]*prec + '.' + 'e+' + (longest exp |
| 8785 | for any double rep.) |
| 8786 | len = 1 + prec + 1 + 2 + 5 = 9 + prec |
| 8787 | |
| 8788 | 'f' formats: |
| 8789 | buf = '-' + [0-9]*x + '.' + [0-9]*prec (with x < 50) |
| 8790 | len = 1 + 50 + 1 + prec = 52 + prec |
| 8791 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8792 | If prec=0 the effective precision is 1 (the leading digit is |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8793 | always given), therefore increase the length by one. |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8794 | |
| 8795 | */ |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 8796 | if (((type == 'g' || type == 'G') && |
| 8797 | buflen <= (size_t)10 + (size_t)prec) || |
Eric Smith | 22b85b3 | 2008-07-17 19:18:29 +0000 | [diff] [blame] | 8798 | (type == 'f' && buflen <= (size_t)53 + (size_t)prec)) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8799 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8800 | "formatted float is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8801 | return -1; |
| 8802 | } |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8803 | PyOS_snprintf(fmt, sizeof(fmt), "%%%s.%d%c", |
| 8804 | (flags&F_ALT) ? "#" : "", |
| 8805 | prec, type); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8806 | return doubletounicode(buf, buflen, fmt, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8807 | } |
| 8808 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8809 | static PyObject* |
| 8810 | formatlong(PyObject *val, int flags, int prec, int type) |
| 8811 | { |
| 8812 | char *buf; |
Walter Dörwald | 63a28be | 2007-06-20 15:11:12 +0000 | [diff] [blame] | 8813 | int len; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8814 | PyObject *str; /* temporary string object. */ |
Walter Dörwald | 63a28be | 2007-06-20 15:11:12 +0000 | [diff] [blame] | 8815 | PyObject *result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8816 | |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8817 | str = _PyBytes_FormatLong(val, flags, prec, type, &buf, &len); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8818 | if (!str) |
| 8819 | return NULL; |
Walter Dörwald | 63a28be | 2007-06-20 15:11:12 +0000 | [diff] [blame] | 8820 | result = PyUnicode_FromStringAndSize(buf, len); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8821 | Py_DECREF(str); |
Walter Dörwald | 63a28be | 2007-06-20 15:11:12 +0000 | [diff] [blame] | 8822 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8823 | } |
| 8824 | |
Christian Heimes | 3fd1399 | 2008-03-21 01:05:49 +0000 | [diff] [blame] | 8825 | #if 0 |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8826 | static int |
| 8827 | formatint(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8828 | size_t buflen, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8829 | int flags, |
| 8830 | int prec, |
| 8831 | int type, |
| 8832 | PyObject *v) |
| 8833 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8834 | /* fmt = '%#.' + `prec` + 'l' + `type` |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8835 | * worst case length = 3 + 19 (worst len of INT_MAX on 64-bit machine) |
| 8836 | * + 1 + 1 |
| 8837 | * = 24 |
| 8838 | */ |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8839 | char fmt[64]; /* plenty big enough! */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8840 | char *sign; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8841 | long x; |
| 8842 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8843 | x = PyLong_AsLong(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8844 | if (x == -1 && PyErr_Occurred()) |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8845 | return -1; |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8846 | if (x < 0 && type == 'u') { |
| 8847 | type = 'd'; |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 8848 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8849 | if (x < 0 && (type == 'x' || type == 'X' || type == 'o')) |
| 8850 | sign = "-"; |
| 8851 | else |
| 8852 | sign = ""; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8853 | if (prec < 0) |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8854 | prec = 1; |
| 8855 | |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8856 | /* buf = '+'/'-'/'' + '0'/'0x'/'' + '[0-9]'*max(prec, len(x in octal)) |
| 8857 | * worst case buf = '-0x' + [0-9]*prec, where prec >= 11 |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8858 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8859 | if (buflen <= 14 || buflen <= (size_t)3 + (size_t)prec) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8860 | PyErr_SetString(PyExc_OverflowError, |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8861 | "formatted integer is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8862 | return -1; |
| 8863 | } |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8864 | |
| 8865 | if ((flags & F_ALT) && |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 8866 | (type == 'x' || type == 'X' || type == 'o')) { |
| 8867 | /* When converting under %#o, %#x or %#X, there are a number |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8868 | * of issues that cause pain: |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 8869 | * - for %#o, we want a different base marker than C |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8870 | * - when 0 is being converted, the C standard leaves off |
| 8871 | * the '0x' or '0X', which is inconsistent with other |
| 8872 | * %#x/%#X conversions and inconsistent with Python's |
| 8873 | * hex() function |
| 8874 | * - there are platforms that violate the standard and |
| 8875 | * convert 0 with the '0x' or '0X' |
| 8876 | * (Metrowerks, Compaq Tru64) |
| 8877 | * - there are platforms that give '0x' when converting |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8878 | * under %#X, but convert 0 in accordance with the |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8879 | * standard (OS/2 EMX) |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8880 | * |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8881 | * We can achieve the desired consistency by inserting our |
| 8882 | * own '0x' or '0X' prefix, and substituting %x/%X in place |
| 8883 | * of %#x/%#X. |
| 8884 | * |
| 8885 | * Note that this is the same approach as used in |
| 8886 | * formatint() in stringobject.c |
Andrew MacIntyre | c487439 | 2002-02-26 11:36:35 +0000 | [diff] [blame] | 8887 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8888 | PyOS_snprintf(fmt, sizeof(fmt), "%s0%c%%.%dl%c", |
| 8889 | sign, type, prec, type); |
Andrew MacIntyre | c487439 | 2002-02-26 11:36:35 +0000 | [diff] [blame] | 8890 | } |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8891 | else { |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8892 | PyOS_snprintf(fmt, sizeof(fmt), "%s%%%s.%dl%c", |
| 8893 | sign, (flags&F_ALT) ? "#" : "", |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8894 | prec, type); |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 8895 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8896 | if (sign[0]) |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8897 | return longtounicode(buf, buflen, fmt, -x); |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8898 | else |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8899 | return longtounicode(buf, buflen, fmt, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8900 | } |
Christian Heimes | 3fd1399 | 2008-03-21 01:05:49 +0000 | [diff] [blame] | 8901 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8902 | |
| 8903 | static int |
| 8904 | formatchar(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8905 | size_t buflen, |
| 8906 | PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8907 | { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 8908 | /* presume that the buffer is at least 3 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8909 | if (PyUnicode_Check(v)) { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 8910 | if (PyUnicode_GET_SIZE(v) == 1) { |
| 8911 | buf[0] = PyUnicode_AS_UNICODE(v)[0]; |
| 8912 | buf[1] = '\0'; |
| 8913 | return 1; |
| 8914 | } |
| 8915 | #ifndef Py_UNICODE_WIDE |
| 8916 | if (PyUnicode_GET_SIZE(v) == 2) { |
| 8917 | /* Decode a valid surrogate pair */ |
| 8918 | int c0 = PyUnicode_AS_UNICODE(v)[0]; |
| 8919 | int c1 = PyUnicode_AS_UNICODE(v)[1]; |
| 8920 | if (0xD800 <= c0 && c0 <= 0xDBFF && |
| 8921 | 0xDC00 <= c1 && c1 <= 0xDFFF) { |
| 8922 | buf[0] = c0; |
| 8923 | buf[1] = c1; |
| 8924 | buf[2] = '\0'; |
| 8925 | return 2; |
| 8926 | } |
| 8927 | } |
| 8928 | #endif |
| 8929 | goto onError; |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8930 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8931 | else { |
| 8932 | /* Integer input truncated to a character */ |
| 8933 | long x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8934 | x = PyLong_AsLong(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8935 | if (x == -1 && PyErr_Occurred()) |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8936 | goto onError; |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 8937 | |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 8938 | if (x < 0 || x > 0x10ffff) { |
Walter Dörwald | 44f527f | 2003-04-02 16:37:24 +0000 | [diff] [blame] | 8939 | PyErr_SetString(PyExc_OverflowError, |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 8940 | "%c arg not in range(0x110000)"); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 8941 | return -1; |
| 8942 | } |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 8943 | |
| 8944 | #ifndef Py_UNICODE_WIDE |
| 8945 | if (x > 0xffff) { |
| 8946 | x -= 0x10000; |
| 8947 | buf[0] = (Py_UNICODE)(0xD800 | (x >> 10)); |
| 8948 | buf[1] = (Py_UNICODE)(0xDC00 | (x & 0x3FF)); |
| 8949 | return 2; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 8950 | } |
| 8951 | #endif |
| 8952 | buf[0] = (Py_UNICODE) x; |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 8953 | buf[1] = '\0'; |
| 8954 | return 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8955 | } |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8956 | |
| 8957 | onError: |
| 8958 | PyErr_SetString(PyExc_TypeError, |
| 8959 | "%c requires int or char"); |
| 8960 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8961 | } |
| 8962 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8963 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) |
| 8964 | |
| 8965 | FORMATBUFLEN is the length of the buffer in which the floats, ints, & |
| 8966 | chars are formatted. XXX This is a magic number. Each formatting |
| 8967 | routine does bounds checking to ensure no overflow, but a better |
| 8968 | solution may be to malloc a buffer of appropriate size for each |
| 8969 | format. For now, the current solution is sufficient. |
| 8970 | */ |
| 8971 | #define FORMATBUFLEN (size_t)120 |
| 8972 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8973 | PyObject *PyUnicode_Format(PyObject *format, |
| 8974 | PyObject *args) |
| 8975 | { |
| 8976 | Py_UNICODE *fmt, *res; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8977 | Py_ssize_t fmtcnt, rescnt, reslen, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8978 | int args_owned = 0; |
| 8979 | PyUnicodeObject *result = NULL; |
| 8980 | PyObject *dict = NULL; |
| 8981 | PyObject *uformat; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8982 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8983 | if (format == NULL || args == NULL) { |
| 8984 | PyErr_BadInternalCall(); |
| 8985 | return NULL; |
| 8986 | } |
| 8987 | uformat = PyUnicode_FromObject(format); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 8988 | if (uformat == NULL) |
| 8989 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8990 | fmt = PyUnicode_AS_UNICODE(uformat); |
| 8991 | fmtcnt = PyUnicode_GET_SIZE(uformat); |
| 8992 | |
| 8993 | reslen = rescnt = fmtcnt + 100; |
| 8994 | result = _PyUnicode_New(reslen); |
| 8995 | if (result == NULL) |
| 8996 | goto onError; |
| 8997 | res = PyUnicode_AS_UNICODE(result); |
| 8998 | |
| 8999 | if (PyTuple_Check(args)) { |
| 9000 | arglen = PyTuple_Size(args); |
| 9001 | argidx = 0; |
| 9002 | } |
| 9003 | else { |
| 9004 | arglen = -1; |
| 9005 | argidx = -2; |
| 9006 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 9007 | if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) && |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 9008 | !PyUnicode_Check(args)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9009 | dict = args; |
| 9010 | |
| 9011 | while (--fmtcnt >= 0) { |
| 9012 | if (*fmt != '%') { |
| 9013 | if (--rescnt < 0) { |
| 9014 | rescnt = fmtcnt + 100; |
| 9015 | reslen += rescnt; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9016 | if (_PyUnicode_Resize(&result, reslen) < 0) |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 9017 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9018 | res = PyUnicode_AS_UNICODE(result) + reslen - rescnt; |
| 9019 | --rescnt; |
| 9020 | } |
| 9021 | *res++ = *fmt++; |
| 9022 | } |
| 9023 | else { |
| 9024 | /* Got a format specifier */ |
| 9025 | int flags = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9026 | Py_ssize_t width = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9027 | int prec = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9028 | Py_UNICODE c = '\0'; |
| 9029 | Py_UNICODE fill; |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 9030 | int isnumok; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9031 | PyObject *v = NULL; |
| 9032 | PyObject *temp = NULL; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 9033 | Py_UNICODE *pbuf; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9034 | Py_UNICODE sign; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9035 | Py_ssize_t len; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 9036 | Py_UNICODE formatbuf[FORMATBUFLEN]; /* For format{float,int,char}() */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9037 | |
| 9038 | fmt++; |
| 9039 | if (*fmt == '(') { |
| 9040 | Py_UNICODE *keystart; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9041 | Py_ssize_t keylen; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9042 | PyObject *key; |
| 9043 | int pcount = 1; |
| 9044 | |
| 9045 | if (dict == NULL) { |
| 9046 | PyErr_SetString(PyExc_TypeError, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9047 | "format requires a mapping"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9048 | goto onError; |
| 9049 | } |
| 9050 | ++fmt; |
| 9051 | --fmtcnt; |
| 9052 | keystart = fmt; |
| 9053 | /* Skip over balanced parentheses */ |
| 9054 | while (pcount > 0 && --fmtcnt >= 0) { |
| 9055 | if (*fmt == ')') |
| 9056 | --pcount; |
| 9057 | else if (*fmt == '(') |
| 9058 | ++pcount; |
| 9059 | fmt++; |
| 9060 | } |
| 9061 | keylen = fmt - keystart - 1; |
| 9062 | if (fmtcnt < 0 || pcount > 0) { |
| 9063 | PyErr_SetString(PyExc_ValueError, |
| 9064 | "incomplete format key"); |
| 9065 | goto onError; |
| 9066 | } |
Marc-André Lemburg | 72f8213 | 2001-11-20 15:18:49 +0000 | [diff] [blame] | 9067 | #if 0 |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 9068 | /* keys are converted to strings using UTF-8 and |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9069 | then looked up since Python uses strings to hold |
| 9070 | variables names etc. in its namespaces and we |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 9071 | wouldn't want to break common idioms. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9072 | key = PyUnicode_EncodeUTF8(keystart, |
| 9073 | keylen, |
| 9074 | NULL); |
Marc-André Lemburg | 72f8213 | 2001-11-20 15:18:49 +0000 | [diff] [blame] | 9075 | #else |
| 9076 | key = PyUnicode_FromUnicode(keystart, keylen); |
| 9077 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9078 | if (key == NULL) |
| 9079 | goto onError; |
| 9080 | if (args_owned) { |
| 9081 | Py_DECREF(args); |
| 9082 | args_owned = 0; |
| 9083 | } |
| 9084 | args = PyObject_GetItem(dict, key); |
| 9085 | Py_DECREF(key); |
| 9086 | if (args == NULL) { |
| 9087 | goto onError; |
| 9088 | } |
| 9089 | args_owned = 1; |
| 9090 | arglen = -1; |
| 9091 | argidx = -2; |
| 9092 | } |
| 9093 | while (--fmtcnt >= 0) { |
| 9094 | switch (c = *fmt++) { |
| 9095 | case '-': flags |= F_LJUST; continue; |
| 9096 | case '+': flags |= F_SIGN; continue; |
| 9097 | case ' ': flags |= F_BLANK; continue; |
| 9098 | case '#': flags |= F_ALT; continue; |
| 9099 | case '0': flags |= F_ZERO; continue; |
| 9100 | } |
| 9101 | break; |
| 9102 | } |
| 9103 | if (c == '*') { |
| 9104 | v = getnextarg(args, arglen, &argidx); |
| 9105 | if (v == NULL) |
| 9106 | goto onError; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 9107 | if (!PyLong_Check(v)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9108 | PyErr_SetString(PyExc_TypeError, |
| 9109 | "* wants int"); |
| 9110 | goto onError; |
| 9111 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 9112 | width = PyLong_AsLong(v); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 9113 | if (width == -1 && PyErr_Occurred()) |
| 9114 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9115 | if (width < 0) { |
| 9116 | flags |= F_LJUST; |
| 9117 | width = -width; |
| 9118 | } |
| 9119 | if (--fmtcnt >= 0) |
| 9120 | c = *fmt++; |
| 9121 | } |
| 9122 | else if (c >= '0' && c <= '9') { |
| 9123 | width = c - '0'; |
| 9124 | while (--fmtcnt >= 0) { |
| 9125 | c = *fmt++; |
| 9126 | if (c < '0' || c > '9') |
| 9127 | break; |
| 9128 | if ((width*10) / 10 != width) { |
| 9129 | PyErr_SetString(PyExc_ValueError, |
| 9130 | "width too big"); |
| 9131 | goto onError; |
| 9132 | } |
| 9133 | width = width*10 + (c - '0'); |
| 9134 | } |
| 9135 | } |
| 9136 | if (c == '.') { |
| 9137 | prec = 0; |
| 9138 | if (--fmtcnt >= 0) |
| 9139 | c = *fmt++; |
| 9140 | if (c == '*') { |
| 9141 | v = getnextarg(args, arglen, &argidx); |
| 9142 | if (v == NULL) |
| 9143 | goto onError; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 9144 | if (!PyLong_Check(v)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9145 | PyErr_SetString(PyExc_TypeError, |
| 9146 | "* wants int"); |
| 9147 | goto onError; |
| 9148 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 9149 | prec = PyLong_AsLong(v); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 9150 | if (prec == -1 && PyErr_Occurred()) |
| 9151 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9152 | if (prec < 0) |
| 9153 | prec = 0; |
| 9154 | if (--fmtcnt >= 0) |
| 9155 | c = *fmt++; |
| 9156 | } |
| 9157 | else if (c >= '0' && c <= '9') { |
| 9158 | prec = c - '0'; |
| 9159 | while (--fmtcnt >= 0) { |
| 9160 | c = Py_CHARMASK(*fmt++); |
| 9161 | if (c < '0' || c > '9') |
| 9162 | break; |
| 9163 | if ((prec*10) / 10 != prec) { |
| 9164 | PyErr_SetString(PyExc_ValueError, |
| 9165 | "prec too big"); |
| 9166 | goto onError; |
| 9167 | } |
| 9168 | prec = prec*10 + (c - '0'); |
| 9169 | } |
| 9170 | } |
| 9171 | } /* prec */ |
| 9172 | if (fmtcnt >= 0) { |
| 9173 | if (c == 'h' || c == 'l' || c == 'L') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9174 | if (--fmtcnt >= 0) |
| 9175 | c = *fmt++; |
| 9176 | } |
| 9177 | } |
| 9178 | if (fmtcnt < 0) { |
| 9179 | PyErr_SetString(PyExc_ValueError, |
| 9180 | "incomplete format"); |
| 9181 | goto onError; |
| 9182 | } |
| 9183 | if (c != '%') { |
| 9184 | v = getnextarg(args, arglen, &argidx); |
| 9185 | if (v == NULL) |
| 9186 | goto onError; |
| 9187 | } |
| 9188 | sign = 0; |
| 9189 | fill = ' '; |
| 9190 | switch (c) { |
| 9191 | |
| 9192 | case '%': |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 9193 | pbuf = formatbuf; |
| 9194 | /* presume that buffer length is at least 1 */ |
| 9195 | pbuf[0] = '%'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9196 | len = 1; |
| 9197 | break; |
| 9198 | |
| 9199 | case 's': |
| 9200 | case 'r': |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 9201 | case 'a': |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9202 | if (PyUnicode_Check(v) && c == 's') { |
| 9203 | temp = v; |
| 9204 | Py_INCREF(temp); |
| 9205 | } |
| 9206 | else { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9207 | if (c == 's') |
Thomas Heller | 519a042 | 2007-11-15 20:48:54 +0000 | [diff] [blame] | 9208 | temp = PyObject_Str(v); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 9209 | else if (c == 'r') |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9210 | temp = PyObject_Repr(v); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 9211 | else |
| 9212 | temp = PyObject_ASCII(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9213 | if (temp == NULL) |
| 9214 | goto onError; |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 9215 | if (PyUnicode_Check(temp)) |
| 9216 | /* nothing to do */; |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 9217 | else { |
| 9218 | Py_DECREF(temp); |
| 9219 | PyErr_SetString(PyExc_TypeError, |
| 9220 | "%s argument has non-string str()"); |
| 9221 | goto onError; |
| 9222 | } |
| 9223 | } |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 9224 | pbuf = PyUnicode_AS_UNICODE(temp); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9225 | len = PyUnicode_GET_SIZE(temp); |
| 9226 | if (prec >= 0 && len > prec) |
| 9227 | len = prec; |
| 9228 | break; |
| 9229 | |
| 9230 | case 'i': |
| 9231 | case 'd': |
| 9232 | case 'u': |
| 9233 | case 'o': |
| 9234 | case 'x': |
| 9235 | case 'X': |
| 9236 | if (c == 'i') |
| 9237 | c = 'd'; |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 9238 | isnumok = 0; |
| 9239 | if (PyNumber_Check(v)) { |
| 9240 | PyObject *iobj=NULL; |
| 9241 | |
| 9242 | if (PyLong_Check(v)) { |
| 9243 | iobj = v; |
| 9244 | Py_INCREF(iobj); |
| 9245 | } |
| 9246 | else { |
| 9247 | iobj = PyNumber_Long(v); |
| 9248 | } |
| 9249 | if (iobj!=NULL) { |
| 9250 | if (PyLong_Check(iobj)) { |
| 9251 | isnumok = 1; |
| 9252 | temp = formatlong(iobj, flags, prec, c); |
| 9253 | Py_DECREF(iobj); |
| 9254 | if (!temp) |
| 9255 | goto onError; |
| 9256 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 9257 | len = PyUnicode_GET_SIZE(temp); |
| 9258 | sign = 1; |
| 9259 | } |
| 9260 | else { |
| 9261 | Py_DECREF(iobj); |
| 9262 | } |
| 9263 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9264 | } |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 9265 | if (!isnumok) { |
| 9266 | PyErr_Format(PyExc_TypeError, |
| 9267 | "%%%c format: a number is required, " |
Martin v. Löwis | 5a6f458 | 2008-04-07 03:22:07 +0000 | [diff] [blame] | 9268 | "not %.200s", (char)c, Py_TYPE(v)->tp_name); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 9269 | goto onError; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 9270 | } |
| 9271 | if (flags & F_ZERO) |
| 9272 | fill = '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9273 | break; |
| 9274 | |
| 9275 | case 'e': |
| 9276 | case 'E': |
| 9277 | case 'f': |
Raymond Hettinger | 9bfe533 | 2003-08-27 04:55:52 +0000 | [diff] [blame] | 9278 | case 'F': |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9279 | case 'g': |
| 9280 | case 'G': |
Eric Smith | 22b85b3 | 2008-07-17 19:18:29 +0000 | [diff] [blame] | 9281 | if (c == 'F') |
| 9282 | c = 'f'; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 9283 | pbuf = formatbuf; |
| 9284 | len = formatfloat(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), |
| 9285 | flags, prec, c, v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9286 | if (len < 0) |
| 9287 | goto onError; |
| 9288 | sign = 1; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 9289 | if (flags & F_ZERO) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9290 | fill = '0'; |
| 9291 | break; |
| 9292 | |
| 9293 | case 'c': |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 9294 | pbuf = formatbuf; |
| 9295 | len = formatchar(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9296 | if (len < 0) |
| 9297 | goto onError; |
| 9298 | break; |
| 9299 | |
| 9300 | default: |
| 9301 | PyErr_Format(PyExc_ValueError, |
Andrew M. Kuchling | 6ca8917 | 2000-12-15 13:07:46 +0000 | [diff] [blame] | 9302 | "unsupported format character '%c' (0x%x) " |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 9303 | "at index %zd", |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9304 | (31<=c && c<=126) ? (char)c : '?', |
Marc-André Lemburg | 24e53b6 | 2002-09-24 09:32:14 +0000 | [diff] [blame] | 9305 | (int)c, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 9306 | (Py_ssize_t)(fmt - 1 - |
| 9307 | PyUnicode_AS_UNICODE(uformat))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9308 | goto onError; |
| 9309 | } |
| 9310 | if (sign) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 9311 | if (*pbuf == '-' || *pbuf == '+') { |
| 9312 | sign = *pbuf++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9313 | len--; |
| 9314 | } |
| 9315 | else if (flags & F_SIGN) |
| 9316 | sign = '+'; |
| 9317 | else if (flags & F_BLANK) |
| 9318 | sign = ' '; |
| 9319 | else |
| 9320 | sign = 0; |
| 9321 | } |
| 9322 | if (width < len) |
| 9323 | width = len; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 9324 | if (rescnt - (sign != 0) < width) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9325 | reslen -= rescnt; |
| 9326 | rescnt = width + fmtcnt + 100; |
| 9327 | reslen += rescnt; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 9328 | if (reslen < 0) { |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 9329 | Py_XDECREF(temp); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 9330 | PyErr_NoMemory(); |
| 9331 | goto onError; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 9332 | } |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 9333 | if (_PyUnicode_Resize(&result, reslen) < 0) { |
| 9334 | Py_XDECREF(temp); |
| 9335 | goto onError; |
| 9336 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9337 | res = PyUnicode_AS_UNICODE(result) |
| 9338 | + reslen - rescnt; |
| 9339 | } |
| 9340 | if (sign) { |
| 9341 | if (fill != ' ') |
| 9342 | *res++ = sign; |
| 9343 | rescnt--; |
| 9344 | if (width > len) |
| 9345 | width--; |
| 9346 | } |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 9347 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 9348 | assert(pbuf[0] == '0'); |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 9349 | assert(pbuf[1] == c); |
| 9350 | if (fill != ' ') { |
| 9351 | *res++ = *pbuf++; |
| 9352 | *res++ = *pbuf++; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 9353 | } |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 9354 | rescnt -= 2; |
| 9355 | width -= 2; |
| 9356 | if (width < 0) |
| 9357 | width = 0; |
| 9358 | len -= 2; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 9359 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9360 | if (width > len && !(flags & F_LJUST)) { |
| 9361 | do { |
| 9362 | --rescnt; |
| 9363 | *res++ = fill; |
| 9364 | } while (--width > len); |
| 9365 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 9366 | if (fill == ' ') { |
| 9367 | if (sign) |
| 9368 | *res++ = sign; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 9369 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 9370 | assert(pbuf[0] == '0'); |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 9371 | assert(pbuf[1] == c); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 9372 | *res++ = *pbuf++; |
| 9373 | *res++ = *pbuf++; |
| 9374 | } |
| 9375 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9376 | Py_UNICODE_COPY(res, pbuf, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9377 | res += len; |
| 9378 | rescnt -= len; |
| 9379 | while (--width >= len) { |
| 9380 | --rescnt; |
| 9381 | *res++ = ' '; |
| 9382 | } |
| 9383 | if (dict && (argidx < arglen) && c != '%') { |
| 9384 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 0ebac97 | 2002-05-21 15:14:57 +0000 | [diff] [blame] | 9385 | "not all arguments converted during string formatting"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 9386 | Py_XDECREF(temp); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9387 | goto onError; |
| 9388 | } |
| 9389 | Py_XDECREF(temp); |
| 9390 | } /* '%' */ |
| 9391 | } /* until end */ |
| 9392 | if (argidx < arglen && !dict) { |
| 9393 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 0ebac97 | 2002-05-21 15:14:57 +0000 | [diff] [blame] | 9394 | "not all arguments converted during string formatting"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9395 | goto onError; |
| 9396 | } |
| 9397 | |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 9398 | if (_PyUnicode_Resize(&result, reslen - rescnt) < 0) |
| 9399 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9400 | if (args_owned) { |
| 9401 | Py_DECREF(args); |
| 9402 | } |
| 9403 | Py_DECREF(uformat); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9404 | return (PyObject *)result; |
| 9405 | |
| 9406 | onError: |
| 9407 | Py_XDECREF(result); |
| 9408 | Py_DECREF(uformat); |
| 9409 | if (args_owned) { |
| 9410 | Py_DECREF(args); |
| 9411 | } |
| 9412 | return NULL; |
| 9413 | } |
| 9414 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 9415 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9416 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 9417 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9418 | static PyObject * |
| 9419 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 9420 | { |
| 9421 | PyObject *x = NULL; |
Guido van Rossum | 55b4a7b | 2007-07-11 09:28:11 +0000 | [diff] [blame] | 9422 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9423 | char *encoding = NULL; |
| 9424 | char *errors = NULL; |
| 9425 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9426 | if (type != &PyUnicode_Type) |
| 9427 | return unicode_subtype_new(type, args, kwds); |
Alexandre Vassalotti | 999679a | 2008-05-03 04:42:16 +0000 | [diff] [blame] | 9428 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9429 | kwlist, &x, &encoding, &errors)) |
| 9430 | return NULL; |
| 9431 | if (x == NULL) |
| 9432 | return (PyObject *)_PyUnicode_New(0); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 9433 | if (encoding == NULL && errors == NULL) |
Thomas Heller | 519a042 | 2007-11-15 20:48:54 +0000 | [diff] [blame] | 9434 | return PyObject_Str(x); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 9435 | else |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9436 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
| 9437 | } |
| 9438 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9439 | static PyObject * |
| 9440 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 9441 | { |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 9442 | PyUnicodeObject *tmp, *pnew; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9443 | Py_ssize_t n; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9444 | |
| 9445 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
| 9446 | tmp = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); |
| 9447 | if (tmp == NULL) |
| 9448 | return NULL; |
| 9449 | assert(PyUnicode_Check(tmp)); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 9450 | pnew = (PyUnicodeObject *) type->tp_alloc(type, n = tmp->length); |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 9451 | if (pnew == NULL) { |
| 9452 | Py_DECREF(tmp); |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9453 | return NULL; |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 9454 | } |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 9455 | pnew->str = (Py_UNICODE*) PyObject_MALLOC(sizeof(Py_UNICODE) * (n+1)); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 9456 | if (pnew->str == NULL) { |
| 9457 | _Py_ForgetReference((PyObject *)pnew); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 9458 | PyObject_Del(pnew); |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 9459 | Py_DECREF(tmp); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 9460 | return PyErr_NoMemory(); |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9461 | } |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 9462 | Py_UNICODE_COPY(pnew->str, tmp->str, n+1); |
| 9463 | pnew->length = n; |
| 9464 | pnew->hash = tmp->hash; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9465 | Py_DECREF(tmp); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 9466 | return (PyObject *)pnew; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9467 | } |
| 9468 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9469 | PyDoc_STRVAR(unicode_doc, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 9470 | "str(string[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9471 | \n\ |
Collin Winter | d474ce8 | 2007-08-07 19:42:11 +0000 | [diff] [blame] | 9472 | Create a new string object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 9473 | encoding defaults to the current default string encoding.\n\ |
| 9474 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9475 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9476 | static PyObject *unicode_iter(PyObject *seq); |
| 9477 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9478 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 9479 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Guido van Rossum | 84fc66d | 2007-05-03 17:18:26 +0000 | [diff] [blame] | 9480 | "str", /* tp_name */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9481 | sizeof(PyUnicodeObject), /* tp_size */ |
| 9482 | 0, /* tp_itemsize */ |
| 9483 | /* Slots */ |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 9484 | (destructor)unicode_dealloc, /* tp_dealloc */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9485 | 0, /* tp_print */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9486 | 0, /* tp_getattr */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9487 | 0, /* tp_setattr */ |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 9488 | 0, /* tp_compare */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9489 | unicode_repr, /* tp_repr */ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 9490 | &unicode_as_number, /* tp_as_number */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9491 | &unicode_as_sequence, /* tp_as_sequence */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 9492 | &unicode_as_mapping, /* tp_as_mapping */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9493 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 9494 | 0, /* tp_call*/ |
| 9495 | (reprfunc) unicode_str, /* tp_str */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9496 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 9497 | 0, /* tp_setattro */ |
Alexandre Vassalotti | 70a2371 | 2007-10-14 02:05:51 +0000 | [diff] [blame] | 9498 | 0, /* tp_as_buffer */ |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 9499 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
| 9500 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9501 | unicode_doc, /* tp_doc */ |
| 9502 | 0, /* tp_traverse */ |
| 9503 | 0, /* tp_clear */ |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 9504 | PyUnicode_RichCompare, /* tp_richcompare */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9505 | 0, /* tp_weaklistoffset */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9506 | unicode_iter, /* tp_iter */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9507 | 0, /* tp_iternext */ |
| 9508 | unicode_methods, /* tp_methods */ |
| 9509 | 0, /* tp_members */ |
| 9510 | 0, /* tp_getset */ |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 9511 | &PyBaseObject_Type, /* tp_base */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9512 | 0, /* tp_dict */ |
| 9513 | 0, /* tp_descr_get */ |
| 9514 | 0, /* tp_descr_set */ |
| 9515 | 0, /* tp_dictoffset */ |
| 9516 | 0, /* tp_init */ |
| 9517 | 0, /* tp_alloc */ |
| 9518 | unicode_new, /* tp_new */ |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 9519 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9520 | }; |
| 9521 | |
| 9522 | /* Initialize the Unicode implementation */ |
| 9523 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 9524 | void _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9525 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9526 | int i; |
| 9527 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9528 | /* XXX - move this array to unicodectype.c ? */ |
| 9529 | Py_UNICODE linebreak[] = { |
| 9530 | 0x000A, /* LINE FEED */ |
| 9531 | 0x000D, /* CARRIAGE RETURN */ |
| 9532 | 0x001C, /* FILE SEPARATOR */ |
| 9533 | 0x001D, /* GROUP SEPARATOR */ |
| 9534 | 0x001E, /* RECORD SEPARATOR */ |
| 9535 | 0x0085, /* NEXT LINE */ |
| 9536 | 0x2028, /* LINE SEPARATOR */ |
| 9537 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 9538 | }; |
| 9539 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 9540 | /* Init the implementation */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 9541 | free_list = NULL; |
| 9542 | numfree = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9543 | unicode_empty = _PyUnicode_New(0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9544 | if (!unicode_empty) |
| 9545 | return; |
| 9546 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9547 | for (i = 0; i < 256; i++) |
| 9548 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 9549 | if (PyType_Ready(&PyUnicode_Type) < 0) |
| 9550 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9551 | |
| 9552 | /* initialize the linebreak bloom filter */ |
| 9553 | bloom_linebreak = make_bloom_mask( |
| 9554 | linebreak, sizeof(linebreak) / sizeof(linebreak[0]) |
| 9555 | ); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9556 | |
| 9557 | PyType_Ready(&EncodingMapType); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9558 | } |
| 9559 | |
| 9560 | /* Finalize the Unicode implementation */ |
| 9561 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 9562 | int |
| 9563 | PyUnicode_ClearFreeList(void) |
| 9564 | { |
| 9565 | int freelist_size = numfree; |
| 9566 | PyUnicodeObject *u; |
| 9567 | |
| 9568 | for (u = free_list; u != NULL;) { |
| 9569 | PyUnicodeObject *v = u; |
| 9570 | u = *(PyUnicodeObject **)u; |
| 9571 | if (v->str) |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 9572 | PyObject_DEL(v->str); |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 9573 | Py_XDECREF(v->defenc); |
| 9574 | PyObject_Del(v); |
| 9575 | numfree--; |
| 9576 | } |
| 9577 | free_list = NULL; |
| 9578 | assert(numfree == 0); |
| 9579 | return freelist_size; |
| 9580 | } |
| 9581 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9582 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 9583 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9584 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9585 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9586 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 9587 | Py_XDECREF(unicode_empty); |
| 9588 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 9589 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9590 | for (i = 0; i < 256; i++) { |
| 9591 | if (unicode_latin1[i]) { |
| 9592 | Py_DECREF(unicode_latin1[i]); |
| 9593 | unicode_latin1[i] = NULL; |
| 9594 | } |
| 9595 | } |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 9596 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9597 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 9598 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9599 | void |
| 9600 | PyUnicode_InternInPlace(PyObject **p) |
| 9601 | { |
| 9602 | register PyUnicodeObject *s = (PyUnicodeObject *)(*p); |
| 9603 | PyObject *t; |
| 9604 | if (s == NULL || !PyUnicode_Check(s)) |
| 9605 | Py_FatalError( |
| 9606 | "PyUnicode_InternInPlace: unicode strings only please!"); |
| 9607 | /* If it's a subclass, we don't really know what putting |
| 9608 | it in the interned dict might do. */ |
| 9609 | if (!PyUnicode_CheckExact(s)) |
| 9610 | return; |
| 9611 | if (PyUnicode_CHECK_INTERNED(s)) |
| 9612 | return; |
| 9613 | if (interned == NULL) { |
| 9614 | interned = PyDict_New(); |
| 9615 | if (interned == NULL) { |
| 9616 | PyErr_Clear(); /* Don't leave an exception */ |
| 9617 | return; |
| 9618 | } |
| 9619 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9620 | /* It might be that the GetItem call fails even |
| 9621 | though the key is present in the dictionary, |
| 9622 | namely when this happens during a stack overflow. */ |
| 9623 | Py_ALLOW_RECURSION |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9624 | t = PyDict_GetItem(interned, (PyObject *)s); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9625 | Py_END_ALLOW_RECURSION |
| 9626 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9627 | if (t) { |
| 9628 | Py_INCREF(t); |
| 9629 | Py_DECREF(*p); |
| 9630 | *p = t; |
| 9631 | return; |
| 9632 | } |
| 9633 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9634 | PyThreadState_GET()->recursion_critical = 1; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9635 | if (PyDict_SetItem(interned, (PyObject *)s, (PyObject *)s) < 0) { |
| 9636 | PyErr_Clear(); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9637 | PyThreadState_GET()->recursion_critical = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9638 | return; |
| 9639 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9640 | PyThreadState_GET()->recursion_critical = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9641 | /* The two references in interned are not counted by refcnt. |
| 9642 | The deallocator will take care of this */ |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 9643 | Py_REFCNT(s) -= 2; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9644 | PyUnicode_CHECK_INTERNED(s) = SSTATE_INTERNED_MORTAL; |
| 9645 | } |
| 9646 | |
| 9647 | void |
| 9648 | PyUnicode_InternImmortal(PyObject **p) |
| 9649 | { |
| 9650 | PyUnicode_InternInPlace(p); |
| 9651 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
| 9652 | PyUnicode_CHECK_INTERNED(*p) = SSTATE_INTERNED_IMMORTAL; |
| 9653 | Py_INCREF(*p); |
| 9654 | } |
| 9655 | } |
| 9656 | |
| 9657 | PyObject * |
| 9658 | PyUnicode_InternFromString(const char *cp) |
| 9659 | { |
| 9660 | PyObject *s = PyUnicode_FromString(cp); |
| 9661 | if (s == NULL) |
| 9662 | return NULL; |
| 9663 | PyUnicode_InternInPlace(&s); |
| 9664 | return s; |
| 9665 | } |
| 9666 | |
| 9667 | void _Py_ReleaseInternedUnicodeStrings(void) |
| 9668 | { |
| 9669 | PyObject *keys; |
| 9670 | PyUnicodeObject *s; |
| 9671 | Py_ssize_t i, n; |
| 9672 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
| 9673 | |
| 9674 | if (interned == NULL || !PyDict_Check(interned)) |
| 9675 | return; |
| 9676 | keys = PyDict_Keys(interned); |
| 9677 | if (keys == NULL || !PyList_Check(keys)) { |
| 9678 | PyErr_Clear(); |
| 9679 | return; |
| 9680 | } |
| 9681 | |
| 9682 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 9683 | detector, interned unicode strings are not forcibly deallocated; |
| 9684 | rather, we give them their stolen references back, and then clear |
| 9685 | and DECREF the interned dict. */ |
| 9686 | |
| 9687 | n = PyList_GET_SIZE(keys); |
| 9688 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
| 9689 | n); |
| 9690 | for (i = 0; i < n; i++) { |
| 9691 | s = (PyUnicodeObject *) PyList_GET_ITEM(keys, i); |
| 9692 | switch (s->state) { |
| 9693 | case SSTATE_NOT_INTERNED: |
| 9694 | /* XXX Shouldn't happen */ |
| 9695 | break; |
| 9696 | case SSTATE_INTERNED_IMMORTAL: |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 9697 | Py_REFCNT(s) += 1; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9698 | immortal_size += s->length; |
| 9699 | break; |
| 9700 | case SSTATE_INTERNED_MORTAL: |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 9701 | Py_REFCNT(s) += 2; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9702 | mortal_size += s->length; |
| 9703 | break; |
| 9704 | default: |
| 9705 | Py_FatalError("Inconsistent interned string state."); |
| 9706 | } |
| 9707 | s->state = SSTATE_NOT_INTERNED; |
| 9708 | } |
| 9709 | fprintf(stderr, "total size of all interned strings: " |
| 9710 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 9711 | "mortal/immortal\n", mortal_size, immortal_size); |
| 9712 | Py_DECREF(keys); |
| 9713 | PyDict_Clear(interned); |
| 9714 | Py_DECREF(interned); |
| 9715 | interned = NULL; |
| 9716 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9717 | |
| 9718 | |
| 9719 | /********************* Unicode Iterator **************************/ |
| 9720 | |
| 9721 | typedef struct { |
| 9722 | PyObject_HEAD |
Guido van Rossum | 49d6b07 | 2006-08-17 21:11:47 +0000 | [diff] [blame] | 9723 | Py_ssize_t it_index; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9724 | PyUnicodeObject *it_seq; /* Set to NULL when iterator is exhausted */ |
| 9725 | } unicodeiterobject; |
| 9726 | |
| 9727 | static void |
| 9728 | unicodeiter_dealloc(unicodeiterobject *it) |
| 9729 | { |
| 9730 | _PyObject_GC_UNTRACK(it); |
| 9731 | Py_XDECREF(it->it_seq); |
| 9732 | PyObject_GC_Del(it); |
| 9733 | } |
| 9734 | |
| 9735 | static int |
| 9736 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 9737 | { |
| 9738 | Py_VISIT(it->it_seq); |
| 9739 | return 0; |
| 9740 | } |
| 9741 | |
| 9742 | static PyObject * |
| 9743 | unicodeiter_next(unicodeiterobject *it) |
| 9744 | { |
| 9745 | PyUnicodeObject *seq; |
| 9746 | PyObject *item; |
| 9747 | |
| 9748 | assert(it != NULL); |
| 9749 | seq = it->it_seq; |
| 9750 | if (seq == NULL) |
| 9751 | return NULL; |
| 9752 | assert(PyUnicode_Check(seq)); |
| 9753 | |
| 9754 | if (it->it_index < PyUnicode_GET_SIZE(seq)) { |
Guido van Rossum | 49d6b07 | 2006-08-17 21:11:47 +0000 | [diff] [blame] | 9755 | item = PyUnicode_FromUnicode( |
| 9756 | PyUnicode_AS_UNICODE(seq)+it->it_index, 1); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9757 | if (item != NULL) |
| 9758 | ++it->it_index; |
| 9759 | return item; |
| 9760 | } |
| 9761 | |
| 9762 | Py_DECREF(seq); |
| 9763 | it->it_seq = NULL; |
| 9764 | return NULL; |
| 9765 | } |
| 9766 | |
| 9767 | static PyObject * |
| 9768 | unicodeiter_len(unicodeiterobject *it) |
| 9769 | { |
| 9770 | Py_ssize_t len = 0; |
| 9771 | if (it->it_seq) |
| 9772 | len = PyUnicode_GET_SIZE(it->it_seq) - it->it_index; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 9773 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9774 | } |
| 9775 | |
| 9776 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 9777 | |
| 9778 | static PyMethodDef unicodeiter_methods[] = { |
Guido van Rossum | 49d6b07 | 2006-08-17 21:11:47 +0000 | [diff] [blame] | 9779 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
| 9780 | length_hint_doc}, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9781 | {NULL, NULL} /* sentinel */ |
| 9782 | }; |
| 9783 | |
| 9784 | PyTypeObject PyUnicodeIter_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 9785 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Christian Heimes | f83be4e | 2007-11-28 09:44:38 +0000 | [diff] [blame] | 9786 | "str_iterator", /* tp_name */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9787 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 9788 | 0, /* tp_itemsize */ |
| 9789 | /* methods */ |
Guido van Rossum | 49d6b07 | 2006-08-17 21:11:47 +0000 | [diff] [blame] | 9790 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9791 | 0, /* tp_print */ |
| 9792 | 0, /* tp_getattr */ |
| 9793 | 0, /* tp_setattr */ |
| 9794 | 0, /* tp_compare */ |
| 9795 | 0, /* tp_repr */ |
| 9796 | 0, /* tp_as_number */ |
| 9797 | 0, /* tp_as_sequence */ |
| 9798 | 0, /* tp_as_mapping */ |
| 9799 | 0, /* tp_hash */ |
| 9800 | 0, /* tp_call */ |
| 9801 | 0, /* tp_str */ |
| 9802 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 9803 | 0, /* tp_setattro */ |
| 9804 | 0, /* tp_as_buffer */ |
| 9805 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 9806 | 0, /* tp_doc */ |
| 9807 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 9808 | 0, /* tp_clear */ |
| 9809 | 0, /* tp_richcompare */ |
| 9810 | 0, /* tp_weaklistoffset */ |
| 9811 | PyObject_SelfIter, /* tp_iter */ |
| 9812 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 9813 | unicodeiter_methods, /* tp_methods */ |
| 9814 | 0, |
| 9815 | }; |
| 9816 | |
| 9817 | static PyObject * |
| 9818 | unicode_iter(PyObject *seq) |
| 9819 | { |
| 9820 | unicodeiterobject *it; |
| 9821 | |
| 9822 | if (!PyUnicode_Check(seq)) { |
| 9823 | PyErr_BadInternalCall(); |
| 9824 | return NULL; |
| 9825 | } |
| 9826 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 9827 | if (it == NULL) |
| 9828 | return NULL; |
| 9829 | it->it_index = 0; |
| 9830 | Py_INCREF(seq); |
| 9831 | it->it_seq = (PyUnicodeObject *)seq; |
| 9832 | _PyObject_GC_TRACK(it); |
| 9833 | return (PyObject *)it; |
| 9834 | } |
| 9835 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9836 | size_t |
| 9837 | Py_UNICODE_strlen(const Py_UNICODE *u) |
| 9838 | { |
| 9839 | int res = 0; |
| 9840 | while(*u++) |
| 9841 | res++; |
| 9842 | return res; |
| 9843 | } |
| 9844 | |
| 9845 | Py_UNICODE* |
| 9846 | Py_UNICODE_strcpy(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 9847 | { |
| 9848 | Py_UNICODE *u = s1; |
| 9849 | while ((*u++ = *s2++)); |
| 9850 | return s1; |
| 9851 | } |
| 9852 | |
| 9853 | Py_UNICODE* |
| 9854 | Py_UNICODE_strncpy(Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 9855 | { |
| 9856 | Py_UNICODE *u = s1; |
| 9857 | while ((*u++ = *s2++)) |
| 9858 | if (n-- == 0) |
| 9859 | break; |
| 9860 | return s1; |
| 9861 | } |
| 9862 | |
| 9863 | int |
| 9864 | Py_UNICODE_strcmp(const Py_UNICODE *s1, const Py_UNICODE *s2) |
| 9865 | { |
| 9866 | while (*s1 && *s2 && *s1 == *s2) |
| 9867 | s1++, s2++; |
| 9868 | if (*s1 && *s2) |
| 9869 | return (*s1 < *s2) ? -1 : +1; |
| 9870 | if (*s1) |
| 9871 | return 1; |
| 9872 | if (*s2) |
| 9873 | return -1; |
| 9874 | return 0; |
| 9875 | } |
| 9876 | |
| 9877 | Py_UNICODE* |
| 9878 | Py_UNICODE_strchr(const Py_UNICODE *s, Py_UNICODE c) |
| 9879 | { |
| 9880 | const Py_UNICODE *p; |
| 9881 | for (p = s; *p; p++) |
| 9882 | if (*p == c) |
| 9883 | return (Py_UNICODE*)p; |
| 9884 | return NULL; |
| 9885 | } |
| 9886 | |
| 9887 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9888 | #ifdef __cplusplus |
| 9889 | } |
| 9890 | #endif |
| 9891 | |
| 9892 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 9893 | /* |
| 9894 | Local variables: |
| 9895 | c-basic-offset: 4 |
| 9896 | indent-tabs-mode: nil |
| 9897 | End: |
| 9898 | */ |