]> git.proxmox.com Git - mirror_edk2.git/blobdiff - AppPkg/Applications/Python/Python-2.7.10/Objects/unicodeobject.c
edk2: Remove AppPkg, StdLib, StdLibPrivateInternalFiles
[mirror_edk2.git] / AppPkg / Applications / Python / Python-2.7.10 / Objects / unicodeobject.c
diff --git a/AppPkg/Applications/Python/Python-2.7.10/Objects/unicodeobject.c b/AppPkg/Applications/Python/Python-2.7.10/Objects/unicodeobject.c
deleted file mode 100644 (file)
index 41b6f0e..0000000
+++ /dev/null
@@ -1,8994 +0,0 @@
-/*\r
-\r
-Unicode implementation based on original code by Fredrik Lundh,\r
-modified by Marc-Andre Lemburg <mal@lemburg.com> according to the\r
-Unicode Integration Proposal (see file Misc/unicode.txt).\r
-\r
-Major speed upgrades to the method implementations at the Reykjavik\r
-NeedForSpeed sprint, by Fredrik Lundh and Andrew Dalke.\r
-\r
-Copyright (c) Corporation for National Research Initiatives.\r
-\r
---------------------------------------------------------------------\r
-The original string type implementation is:\r
-\r
-  Copyright (c) 1999 by Secret Labs AB\r
-  Copyright (c) 1999 by Fredrik Lundh\r
-\r
-By obtaining, using, and/or copying this software and/or its\r
-associated documentation, you agree that you have read, understood,\r
-and will comply with the following terms and conditions:\r
-\r
-Permission to use, copy, modify, and distribute this software and its\r
-associated documentation for any purpose and without fee is hereby\r
-granted, provided that the above copyright notice appears in all\r
-copies, and that both that copyright notice and this permission notice\r
-appear in supporting documentation, and that the name of Secret Labs\r
-AB or the author not be used in advertising or publicity pertaining to\r
-distribution of the software without specific, written prior\r
-permission.\r
-\r
-SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO\r
-THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND\r
-FITNESS.  IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR BE LIABLE FOR\r
-ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES\r
-WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN\r
-ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT\r
-OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.\r
---------------------------------------------------------------------\r
-\r
-*/\r
-\r
-#define PY_SSIZE_T_CLEAN\r
-#include "Python.h"\r
-\r
-#include "unicodeobject.h"\r
-#include "ucnhash.h"\r
-\r
-#ifdef MS_WINDOWS\r
-#include <windows.h>\r
-#endif\r
-\r
-/* Limit for the Unicode object free list */\r
-\r
-#define PyUnicode_MAXFREELIST       1024\r
-\r
-/* Limit for the Unicode object free list stay alive optimization.\r
-\r
-   The implementation will keep allocated Unicode memory intact for\r
-   all objects on the free list having a size less than this\r
-   limit. This reduces malloc() overhead for small Unicode objects.\r
-\r
-   At worst this will result in PyUnicode_MAXFREELIST *\r
-   (sizeof(PyUnicodeObject) + KEEPALIVE_SIZE_LIMIT +\r
-   malloc()-overhead) bytes of unused garbage.\r
-\r
-   Setting the limit to 0 effectively turns the feature off.\r
-\r
-   Note: This is an experimental feature ! If you get core dumps when\r
-   using Unicode objects, turn this feature off.\r
-\r
-*/\r
-\r
-#define KEEPALIVE_SIZE_LIMIT       9\r
-\r
-/* Endianness switches; defaults to little endian */\r
-\r
-#ifdef WORDS_BIGENDIAN\r
-# define BYTEORDER_IS_BIG_ENDIAN\r
-#else\r
-# define BYTEORDER_IS_LITTLE_ENDIAN\r
-#endif\r
-\r
-/* --- Globals ------------------------------------------------------------\r
-\r
-NOTE: In the interpreter's initialization phase, some globals are currently\r
-      initialized dynamically as needed. In the process Unicode objects may\r
-      be created before the Unicode type is ready.\r
-\r
-*/\r
-\r
-\r
-#ifdef __cplusplus\r
-extern "C" {\r
-#endif\r
-\r
-/* Free list for Unicode objects */\r
-static PyUnicodeObject *free_list = NULL;\r
-static int numfree = 0;\r
-\r
-/* The empty Unicode object is shared to improve performance. */\r
-static PyUnicodeObject *unicode_empty = NULL;\r
-\r
-#define _Py_RETURN_UNICODE_EMPTY()                      \\r
-    do {                                                \\r
-        if (unicode_empty != NULL)                      \\r
-            Py_INCREF(unicode_empty);                   \\r
-        else {                                          \\r
-            unicode_empty = _PyUnicode_New(0);          \\r
-            if (unicode_empty != NULL)                  \\r
-                Py_INCREF(unicode_empty);               \\r
-        }                                               \\r
-        return (PyObject *)unicode_empty;               \\r
-    } while (0)\r
-\r
-/* Single character Unicode strings in the Latin-1 range are being\r
-   shared as well. */\r
-static PyUnicodeObject *unicode_latin1[256] = {NULL};\r
-\r
-/* Default encoding to use and assume when NULL is passed as encoding\r
-   parameter; it is initialized by _PyUnicode_Init().\r
-\r
-   Always use the PyUnicode_SetDefaultEncoding() and\r
-   PyUnicode_GetDefaultEncoding() APIs to access this global.\r
-\r
-*/\r
-static char unicode_default_encoding[100 + 1] = "ascii";\r
-\r
-/* Fast detection of the most frequent whitespace characters */\r
-const unsigned char _Py_ascii_whitespace[] = {\r
-    0, 0, 0, 0, 0, 0, 0, 0,\r
-/*     case 0x0009: * CHARACTER TABULATION */\r
-/*     case 0x000A: * LINE FEED */\r
-/*     case 0x000B: * LINE TABULATION */\r
-/*     case 0x000C: * FORM FEED */\r
-/*     case 0x000D: * CARRIAGE RETURN */\r
-    0, 1, 1, 1, 1, 1, 0, 0,\r
-    0, 0, 0, 0, 0, 0, 0, 0,\r
-/*     case 0x001C: * FILE SEPARATOR */\r
-/*     case 0x001D: * GROUP SEPARATOR */\r
-/*     case 0x001E: * RECORD SEPARATOR */\r
-/*     case 0x001F: * UNIT SEPARATOR */\r
-    0, 0, 0, 0, 1, 1, 1, 1,\r
-/*     case 0x0020: * SPACE */\r
-    1, 0, 0, 0, 0, 0, 0, 0,\r
-    0, 0, 0, 0, 0, 0, 0, 0,\r
-    0, 0, 0, 0, 0, 0, 0, 0,\r
-    0, 0, 0, 0, 0, 0, 0, 0,\r
-\r
-    0, 0, 0, 0, 0, 0, 0, 0,\r
-    0, 0, 0, 0, 0, 0, 0, 0,\r
-    0, 0, 0, 0, 0, 0, 0, 0,\r
-    0, 0, 0, 0, 0, 0, 0, 0,\r
-    0, 0, 0, 0, 0, 0, 0, 0,\r
-    0, 0, 0, 0, 0, 0, 0, 0,\r
-    0, 0, 0, 0, 0, 0, 0, 0,\r
-    0, 0, 0, 0, 0, 0, 0, 0\r
-};\r
-\r
-/* Same for linebreaks */\r
-static unsigned char ascii_linebreak[] = {\r
-    0, 0, 0, 0, 0, 0, 0, 0,\r
-/*         0x000A, * LINE FEED */\r
-/*         0x000B, * LINE TABULATION */\r
-/*         0x000C, * FORM FEED */\r
-/*         0x000D, * CARRIAGE RETURN */\r
-    0, 0, 1, 1, 1, 1, 0, 0,\r
-    0, 0, 0, 0, 0, 0, 0, 0,\r
-/*         0x001C, * FILE SEPARATOR */\r
-/*         0x001D, * GROUP SEPARATOR */\r
-/*         0x001E, * RECORD SEPARATOR */\r
-    0, 0, 0, 0, 1, 1, 1, 0,\r
-    0, 0, 0, 0, 0, 0, 0, 0,\r
-    0, 0, 0, 0, 0, 0, 0, 0,\r
-    0, 0, 0, 0, 0, 0, 0, 0,\r
-    0, 0, 0, 0, 0, 0, 0, 0,\r
-\r
-    0, 0, 0, 0, 0, 0, 0, 0,\r
-    0, 0, 0, 0, 0, 0, 0, 0,\r
-    0, 0, 0, 0, 0, 0, 0, 0,\r
-    0, 0, 0, 0, 0, 0, 0, 0,\r
-    0, 0, 0, 0, 0, 0, 0, 0,\r
-    0, 0, 0, 0, 0, 0, 0, 0,\r
-    0, 0, 0, 0, 0, 0, 0, 0,\r
-    0, 0, 0, 0, 0, 0, 0, 0\r
-};\r
-\r
-\r
-Py_UNICODE\r
-PyUnicode_GetMax(void)\r
-{\r
-#ifdef Py_UNICODE_WIDE\r
-    return 0x10FFFF;\r
-#else\r
-    /* This is actually an illegal character, so it should\r
-       not be passed to unichr. */\r
-    return 0xFFFF;\r
-#endif\r
-}\r
-\r
-/* --- Bloom Filters ----------------------------------------------------- */\r
-\r
-/* stuff to implement simple "bloom filters" for Unicode characters.\r
-   to keep things simple, we use a single bitmask, using the least 5\r
-   bits from each unicode characters as the bit index. */\r
-\r
-/* the linebreak mask is set up by Unicode_Init below */\r
-\r
-#if LONG_BIT >= 128\r
-#define BLOOM_WIDTH 128\r
-#elif LONG_BIT >= 64\r
-#define BLOOM_WIDTH 64\r
-#elif LONG_BIT >= 32\r
-#define BLOOM_WIDTH 32\r
-#else\r
-#error "LONG_BIT is smaller than 32"\r
-#endif\r
-\r
-#define BLOOM_MASK unsigned long\r
-\r
-static BLOOM_MASK bloom_linebreak = ~(BLOOM_MASK)0;\r
-\r
-#define BLOOM_ADD(mask, ch) ((mask |= (1UL << ((ch) & (BLOOM_WIDTH - 1)))))\r
-#define BLOOM(mask, ch)     ((mask &  (1UL << ((ch) & (BLOOM_WIDTH - 1)))))\r
-\r
-#define BLOOM_LINEBREAK(ch)                                             \\r
-    ((ch) < 128U ? ascii_linebreak[(ch)] :                              \\r
-     (BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK(ch)))\r
-\r
-Py_LOCAL_INLINE(BLOOM_MASK) make_bloom_mask(Py_UNICODE* ptr, Py_ssize_t len)\r
-{\r
-    /* calculate simple bloom-style bitmask for a given unicode string */\r
-\r
-    BLOOM_MASK mask;\r
-    Py_ssize_t i;\r
-\r
-    mask = 0;\r
-    for (i = 0; i < len; i++)\r
-        BLOOM_ADD(mask, ptr[i]);\r
-\r
-    return mask;\r
-}\r
-\r
-Py_LOCAL_INLINE(int) unicode_member(Py_UNICODE chr, Py_UNICODE* set, Py_ssize_t setlen)\r
-{\r
-    Py_ssize_t i;\r
-\r
-    for (i = 0; i < setlen; i++)\r
-        if (set[i] == chr)\r
-            return 1;\r
-\r
-    return 0;\r
-}\r
-\r
-#define BLOOM_MEMBER(mask, chr, set, setlen)                    \\r
-    BLOOM(mask, chr) && unicode_member(chr, set, setlen)\r
-\r
-/* --- Unicode Object ----------------------------------------------------- */\r
-\r
-static\r
-int unicode_resize(register PyUnicodeObject *unicode,\r
-                   Py_ssize_t length)\r
-{\r
-    void *oldstr;\r
-\r
-    /* Shortcut if there's nothing much to do. */\r
-    if (unicode->length == length)\r
-        goto reset;\r
-\r
-    /* Resizing shared object (unicode_empty or single character\r
-       objects) in-place is not allowed. Use PyUnicode_Resize()\r
-       instead ! */\r
-\r
-    if (unicode == unicode_empty ||\r
-        (unicode->length == 1 &&\r
-         unicode->str[0] < 256U &&\r
-         unicode_latin1[unicode->str[0]] == unicode)) {\r
-        PyErr_SetString(PyExc_SystemError,\r
-                        "can't resize shared unicode objects");\r
-        return -1;\r
-    }\r
-\r
-    /* We allocate one more byte to make sure the string is Ux0000 terminated.\r
-       The overallocation is also used by fastsearch, which assumes that it's\r
-       safe to look at str[length] (without making any assumptions about what\r
-       it contains). */\r
-\r
-    oldstr = unicode->str;\r
-    unicode->str = PyObject_REALLOC(unicode->str,\r
-                                    sizeof(Py_UNICODE) * (length + 1));\r
-    if (!unicode->str) {\r
-        unicode->str = (Py_UNICODE *)oldstr;\r
-        PyErr_NoMemory();\r
-        return -1;\r
-    }\r
-    unicode->str[length] = 0;\r
-    unicode->length = length;\r
-\r
-  reset:\r
-    /* Reset the object caches */\r
-    if (unicode->defenc) {\r
-        Py_CLEAR(unicode->defenc);\r
-    }\r
-    unicode->hash = -1;\r
-\r
-    return 0;\r
-}\r
-\r
-/* We allocate one more byte to make sure the string is\r
-   Ux0000 terminated; some code relies on that.\r
-\r
-   XXX This allocator could further be enhanced by assuring that the\r
-   free list never reduces its size below 1.\r
-\r
-*/\r
-\r
-static\r
-PyUnicodeObject *_PyUnicode_New(Py_ssize_t length)\r
-{\r
-    register PyUnicodeObject *unicode;\r
-\r
-    /* Optimization for empty strings */\r
-    if (length == 0 && unicode_empty != NULL) {\r
-        Py_INCREF(unicode_empty);\r
-        return unicode_empty;\r
-    }\r
-\r
-    /* Ensure we won't overflow the size. */\r
-    if (length > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) {\r
-        return (PyUnicodeObject *)PyErr_NoMemory();\r
-    }\r
-\r
-    /* Unicode freelist & memory allocation */\r
-    if (free_list) {\r
-        unicode = free_list;\r
-        free_list = *(PyUnicodeObject **)unicode;\r
-        numfree--;\r
-        if (unicode->str) {\r
-            /* Keep-Alive optimization: we only upsize the buffer,\r
-               never downsize it. */\r
-            if ((unicode->length < length) &&\r
-                unicode_resize(unicode, length) < 0) {\r
-                PyObject_DEL(unicode->str);\r
-                unicode->str = NULL;\r
-            }\r
-        }\r
-        else {\r
-            size_t new_size = sizeof(Py_UNICODE) * ((size_t)length + 1);\r
-            unicode->str = (Py_UNICODE*) PyObject_MALLOC(new_size);\r
-        }\r
-        PyObject_INIT(unicode, &PyUnicode_Type);\r
-    }\r
-    else {\r
-        size_t new_size;\r
-        unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type);\r
-        if (unicode == NULL)\r
-            return NULL;\r
-        new_size = sizeof(Py_UNICODE) * ((size_t)length + 1);\r
-        unicode->str = (Py_UNICODE*) PyObject_MALLOC(new_size);\r
-    }\r
-\r
-    if (!unicode->str) {\r
-        PyErr_NoMemory();\r
-        goto onError;\r
-    }\r
-    /* Initialize the first element to guard against cases where\r
-     * the caller fails before initializing str -- unicode_resize()\r
-     * reads str[0], and the Keep-Alive optimization can keep memory\r
-     * allocated for str alive across a call to unicode_dealloc(unicode).\r
-     * We don't want unicode_resize to read uninitialized memory in\r
-     * that case.\r
-     */\r
-    unicode->str[0] = 0;\r
-    unicode->str[length] = 0;\r
-    unicode->length = length;\r
-    unicode->hash = -1;\r
-    unicode->defenc = NULL;\r
-    return unicode;\r
-\r
-  onError:\r
-    /* XXX UNREF/NEWREF interface should be more symmetrical */\r
-    _Py_DEC_REFTOTAL;\r
-    _Py_ForgetReference((PyObject *)unicode);\r
-    PyObject_Del(unicode);\r
-    return NULL;\r
-}\r
-\r
-static\r
-void unicode_dealloc(register PyUnicodeObject *unicode)\r
-{\r
-    if (PyUnicode_CheckExact(unicode) &&\r
-        numfree < PyUnicode_MAXFREELIST) {\r
-        /* Keep-Alive optimization */\r
-        if (unicode->length >= KEEPALIVE_SIZE_LIMIT) {\r
-            PyObject_DEL(unicode->str);\r
-            unicode->str = NULL;\r
-            unicode->length = 0;\r
-        }\r
-        if (unicode->defenc) {\r
-            Py_CLEAR(unicode->defenc);\r
-        }\r
-        /* Add to free list */\r
-        *(PyUnicodeObject **)unicode = free_list;\r
-        free_list = unicode;\r
-        numfree++;\r
-    }\r
-    else {\r
-        PyObject_DEL(unicode->str);\r
-        Py_XDECREF(unicode->defenc);\r
-        Py_TYPE(unicode)->tp_free((PyObject *)unicode);\r
-    }\r
-}\r
-\r
-static\r
-int _PyUnicode_Resize(PyUnicodeObject **unicode, Py_ssize_t length)\r
-{\r
-    register PyUnicodeObject *v;\r
-\r
-    /* Argument checks */\r
-    if (unicode == NULL) {\r
-        PyErr_BadInternalCall();\r
-        return -1;\r
-    }\r
-    v = *unicode;\r
-    if (v == NULL || !PyUnicode_Check(v) || Py_REFCNT(v) != 1 || length < 0) {\r
-        PyErr_BadInternalCall();\r
-        return -1;\r
-    }\r
-\r
-    /* Resizing unicode_empty and single character objects is not\r
-       possible since these are being shared. We simply return a fresh\r
-       copy with the same Unicode content. */\r
-    if (v->length != length &&\r
-        (v == unicode_empty || v->length == 1)) {\r
-        PyUnicodeObject *w = _PyUnicode_New(length);\r
-        if (w == NULL)\r
-            return -1;\r
-        Py_UNICODE_COPY(w->str, v->str,\r
-                        length < v->length ? length : v->length);\r
-        Py_DECREF(*unicode);\r
-        *unicode = w;\r
-        return 0;\r
-    }\r
-\r
-    /* Note that we don't have to modify *unicode for unshared Unicode\r
-       objects, since we can modify them in-place. */\r
-    return unicode_resize(v, length);\r
-}\r
-\r
-int PyUnicode_Resize(PyObject **unicode, Py_ssize_t length)\r
-{\r
-    return _PyUnicode_Resize((PyUnicodeObject **)unicode, length);\r
-}\r
-\r
-PyObject *PyUnicode_FromUnicode(const Py_UNICODE *u,\r
-                                Py_ssize_t size)\r
-{\r
-    PyUnicodeObject *unicode;\r
-\r
-    /* If the Unicode data is known at construction time, we can apply\r
-       some optimizations which share commonly used objects. */\r
-    if (u != NULL) {\r
-\r
-        /* Optimization for empty strings */\r
-        if (size == 0)\r
-            _Py_RETURN_UNICODE_EMPTY();\r
-\r
-        /* Single character Unicode objects in the Latin-1 range are\r
-           shared when using this constructor */\r
-        if (size == 1 && *u < 256) {\r
-            unicode = unicode_latin1[*u];\r
-            if (!unicode) {\r
-                unicode = _PyUnicode_New(1);\r
-                if (!unicode)\r
-                    return NULL;\r
-                unicode->str[0] = *u;\r
-                unicode_latin1[*u] = unicode;\r
-            }\r
-            Py_INCREF(unicode);\r
-            return (PyObject *)unicode;\r
-        }\r
-    }\r
-\r
-    unicode = _PyUnicode_New(size);\r
-    if (!unicode)\r
-        return NULL;\r
-\r
-    /* Copy the Unicode data into the new object */\r
-    if (u != NULL)\r
-        Py_UNICODE_COPY(unicode->str, u, size);\r
-\r
-    return (PyObject *)unicode;\r
-}\r
-\r
-PyObject *PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size)\r
-{\r
-    PyUnicodeObject *unicode;\r
-\r
-    if (size < 0) {\r
-        PyErr_SetString(PyExc_SystemError,\r
-                        "Negative size passed to PyUnicode_FromStringAndSize");\r
-        return NULL;\r
-    }\r
-\r
-    /* If the Unicode data is known at construction time, we can apply\r
-       some optimizations which share commonly used objects.\r
-       Also, this means the input must be UTF-8, so fall back to the\r
-       UTF-8 decoder at the end. */\r
-    if (u != NULL) {\r
-\r
-        /* Optimization for empty strings */\r
-        if (size == 0)\r
-            _Py_RETURN_UNICODE_EMPTY();\r
-\r
-        /* Single characters are shared when using this constructor.\r
-           Restrict to ASCII, since the input must be UTF-8. */\r
-        if (size == 1 && Py_CHARMASK(*u) < 128) {\r
-            unicode = unicode_latin1[Py_CHARMASK(*u)];\r
-            if (!unicode) {\r
-                unicode = _PyUnicode_New(1);\r
-                if (!unicode)\r
-                    return NULL;\r
-                unicode->str[0] = Py_CHARMASK(*u);\r
-                unicode_latin1[Py_CHARMASK(*u)] = unicode;\r
-            }\r
-            Py_INCREF(unicode);\r
-            return (PyObject *)unicode;\r
-        }\r
-\r
-        return PyUnicode_DecodeUTF8(u, size, NULL);\r
-    }\r
-\r
-    unicode = _PyUnicode_New(size);\r
-    if (!unicode)\r
-        return NULL;\r
-\r
-    return (PyObject *)unicode;\r
-}\r
-\r
-PyObject *PyUnicode_FromString(const char *u)\r
-{\r
-    size_t size = strlen(u);\r
-    if (size > PY_SSIZE_T_MAX) {\r
-        PyErr_SetString(PyExc_OverflowError, "input too long");\r
-        return NULL;\r
-    }\r
-\r
-    return PyUnicode_FromStringAndSize(u, size);\r
-}\r
-\r
-/* _Py_UNICODE_NEXT is a private macro used to retrieve the character pointed\r
- * by 'ptr', possibly combining surrogate pairs on narrow builds.\r
- * 'ptr' and 'end' must be Py_UNICODE*, with 'ptr' pointing at the character\r
- * that should be returned and 'end' pointing to the end of the buffer.\r
- * ('end' is used on narrow builds to detect a lone surrogate at the\r
- * end of the buffer that should be returned unchanged.)\r
- * The ptr and end arguments should be side-effect free and ptr must an lvalue.\r
- * The type of the returned char is always Py_UCS4.\r
- *\r
- * Note: the macro advances ptr to next char, so it might have side-effects\r
- *       (especially if used with other macros).\r
- */\r
-\r
-/* helper macros used by _Py_UNICODE_NEXT */\r
-#define _Py_UNICODE_IS_HIGH_SURROGATE(ch) (0xD800 <= ch && ch <= 0xDBFF)\r
-#define _Py_UNICODE_IS_LOW_SURROGATE(ch) (0xDC00 <= ch && ch <= 0xDFFF)\r
-/* Join two surrogate characters and return a single Py_UCS4 value. */\r
-#define _Py_UNICODE_JOIN_SURROGATES(high, low)  \\r
-    (((((Py_UCS4)(high) & 0x03FF) << 10) |      \\r
-      ((Py_UCS4)(low) & 0x03FF)) + 0x10000)\r
-\r
-#ifdef Py_UNICODE_WIDE\r
-#define _Py_UNICODE_NEXT(ptr, end) *(ptr)++\r
-#else\r
-#define _Py_UNICODE_NEXT(ptr, end)                                      \\r
-     (((_Py_UNICODE_IS_HIGH_SURROGATE(*(ptr)) && (ptr) < (end)) &&      \\r
-        _Py_UNICODE_IS_LOW_SURROGATE((ptr)[1])) ?                       \\r
-       ((ptr) += 2,_Py_UNICODE_JOIN_SURROGATES((ptr)[-2], (ptr)[-1])) : \\r
-       (Py_UCS4)*(ptr)++)\r
-#endif\r
-\r
-#ifdef HAVE_WCHAR_H\r
-\r
-#if (Py_UNICODE_SIZE == 2) && defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4)\r
-# define CONVERT_WCHAR_TO_SURROGATES\r
-#endif\r
-\r
-#ifdef CONVERT_WCHAR_TO_SURROGATES\r
-\r
-/* Here sizeof(wchar_t) is 4 but Py_UNICODE_SIZE == 2, so we need\r
-   to convert from UTF32 to UTF16. */\r
-\r
-PyObject *PyUnicode_FromWideChar(register const wchar_t *w,\r
-                                 Py_ssize_t size)\r
-{\r
-    PyUnicodeObject *unicode;\r
-    register Py_ssize_t i;\r
-    Py_ssize_t alloc;\r
-    const wchar_t *orig_w;\r
-\r
-    if (w == NULL) {\r
-        PyErr_BadInternalCall();\r
-        return NULL;\r
-    }\r
-\r
-    alloc = size;\r
-    orig_w = w;\r
-    for (i = size; i > 0; i--) {\r
-        if (*w > 0xFFFF)\r
-            alloc++;\r
-        w++;\r
-    }\r
-    w = orig_w;\r
-    unicode = _PyUnicode_New(alloc);\r
-    if (!unicode)\r
-        return NULL;\r
-\r
-    /* Copy the wchar_t data into the new object */\r
-    {\r
-        register Py_UNICODE *u;\r
-        u = PyUnicode_AS_UNICODE(unicode);\r
-        for (i = size; i > 0; i--) {\r
-            if (*w > 0xFFFF) {\r
-                wchar_t ordinal = *w++;\r
-                ordinal -= 0x10000;\r
-                *u++ = 0xD800 | (ordinal >> 10);\r
-                *u++ = 0xDC00 | (ordinal & 0x3FF);\r
-            }\r
-            else\r
-                *u++ = *w++;\r
-        }\r
-    }\r
-    return (PyObject *)unicode;\r
-}\r
-\r
-#else\r
-\r
-PyObject *PyUnicode_FromWideChar(register const wchar_t *w,\r
-                                 Py_ssize_t size)\r
-{\r
-    PyUnicodeObject *unicode;\r
-\r
-    if (w == NULL) {\r
-        PyErr_BadInternalCall();\r
-        return NULL;\r
-    }\r
-\r
-    unicode = _PyUnicode_New(size);\r
-    if (!unicode)\r
-        return NULL;\r
-\r
-    /* Copy the wchar_t data into the new object */\r
-#ifdef HAVE_USABLE_WCHAR_T\r
-    memcpy(unicode->str, w, size * sizeof(wchar_t));\r
-#else\r
-    {\r
-        register Py_UNICODE *u;\r
-        register Py_ssize_t i;\r
-        u = PyUnicode_AS_UNICODE(unicode);\r
-        for (i = size; i > 0; i--)\r
-            *u++ = *w++;\r
-    }\r
-#endif\r
-\r
-    return (PyObject *)unicode;\r
-}\r
-\r
-#endif /* CONVERT_WCHAR_TO_SURROGATES */\r
-\r
-#undef CONVERT_WCHAR_TO_SURROGATES\r
-\r
-static void\r
-makefmt(char *fmt, int longflag, int size_tflag, int zeropad, int width, int precision, char c)\r
-{\r
-    *fmt++ = '%';\r
-    if (width) {\r
-        if (zeropad)\r
-            *fmt++ = '0';\r
-        fmt += sprintf(fmt, "%d", width);\r
-    }\r
-    if (precision)\r
-        fmt += sprintf(fmt, ".%d", precision);\r
-    if (longflag)\r
-        *fmt++ = 'l';\r
-    else if (size_tflag) {\r
-        char *f = PY_FORMAT_SIZE_T;\r
-        while (*f)\r
-            *fmt++ = *f++;\r
-    }\r
-    *fmt++ = c;\r
-    *fmt = '\0';\r
-}\r
-\r
-#define appendstring(string) \\r
-    do { \\r
-        for (copy = string;*copy; copy++) { \\r
-            *s++ = (unsigned char)*copy; \\r
-        } \\r
-    } while (0)\r
-\r
-PyObject *\r
-PyUnicode_FromFormatV(const char *format, va_list vargs)\r
-{\r
-    va_list count;\r
-    Py_ssize_t callcount = 0;\r
-    PyObject **callresults = NULL;\r
-    PyObject **callresult = NULL;\r
-    Py_ssize_t n = 0;\r
-    int width = 0;\r
-    int precision = 0;\r
-    int zeropad;\r
-    const char* f;\r
-    Py_UNICODE *s;\r
-    PyObject *string;\r
-    /* used by sprintf */\r
-    char buffer[21];\r
-    /* use abuffer instead of buffer, if we need more space\r
-     * (which can happen if there's a format specifier with width). */\r
-    char *abuffer = NULL;\r
-    char *realbuffer;\r
-    Py_ssize_t abuffersize = 0;\r
-    char fmt[60]; /* should be enough for %0width.precisionld */\r
-    const char *copy;\r
-\r
-#ifdef VA_LIST_IS_ARRAY\r
-    Py_MEMCPY(count, vargs, sizeof(va_list));\r
-#else\r
-#ifdef  __va_copy\r
-    __va_copy(count, vargs);\r
-#else\r
-    count = vargs;\r
-#endif\r
-#endif\r
-     /* step 1: count the number of %S/%R/%s format specifications\r
-      * (we call PyObject_Str()/PyObject_Repr()/PyUnicode_DecodeUTF8() for these\r
-      * objects once during step 3 and put the result in an array) */\r
-    for (f = format; *f; f++) {\r
-         if (*f == '%') {\r
-             f++;\r
-             while (*f && *f != '%' && !isalpha((unsigned)*f))\r
-                 f++;\r
-             if (!*f)\r
-                 break;\r
-             if (*f == 's' || *f=='S' || *f=='R')\r
-                 ++callcount;\r
-         }\r
-    }\r
-    /* step 2: allocate memory for the results of\r
-     * PyObject_Str()/PyObject_Repr()/PyUnicode_DecodeUTF8() calls */\r
-    if (callcount) {\r
-        callresults = PyObject_Malloc(sizeof(PyObject *)*callcount);\r
-        if (!callresults) {\r
-            PyErr_NoMemory();\r
-            return NULL;\r
-        }\r
-        callresult = callresults;\r
-    }\r
-    /* step 3: figure out how large a buffer we need */\r
-    for (f = format; *f; f++) {\r
-        if (*f == '%') {\r
-            const char* p = f++;\r
-            width = 0;\r
-            while (isdigit((unsigned)*f))\r
-                width = (width*10) + *f++ - '0';\r
-            precision = 0;\r
-            if (*f == '.') {\r
-                f++;\r
-                while (isdigit((unsigned)*f))\r
-                    precision = (precision*10) + *f++ - '0';\r
-            }\r
-\r
-            /* skip the 'l' or 'z' in {%ld, %zd, %lu, %zu} since\r
-             * they don't affect the amount of space we reserve.\r
-             */\r
-            if ((*f == 'l' || *f == 'z') &&\r
-                (f[1] == 'd' || f[1] == 'u'))\r
-                ++f;\r
-\r
-            switch (*f) {\r
-            case 'c':\r
-            {\r
-                int ordinal = va_arg(count, int);\r
-#ifdef Py_UNICODE_WIDE\r
-                if (ordinal < 0 || ordinal > 0x10ffff) {\r
-                    PyErr_SetString(PyExc_OverflowError,\r
-                                    "%c arg not in range(0x110000) "\r
-                                    "(wide Python build)");\r
-                    goto fail;\r
-                }\r
-#else\r
-                if (ordinal < 0 || ordinal > 0xffff) {\r
-                    PyErr_SetString(PyExc_OverflowError,\r
-                                    "%c arg not in range(0x10000) "\r
-                                    "(narrow Python build)");\r
-                    goto fail;\r
-                }\r
-#endif\r
-                /* fall through... */\r
-            }\r
-            case '%':\r
-                n++;\r
-                break;\r
-            case 'd': case 'u': case 'i': case 'x':\r
-                (void) va_arg(count, int);\r
-                if (width < precision)\r
-                    width = precision;\r
-                /* 20 bytes is enough to hold a 64-bit\r
-                   integer.  Decimal takes the most space.\r
-                   This isn't enough for octal.\r
-                   If a width is specified we need more\r
-                   (which we allocate later). */\r
-                if (width < 20)\r
-                    width = 20;\r
-                n += width;\r
-                if (abuffersize < width)\r
-                    abuffersize = width;\r
-                break;\r
-            case 's':\r
-            {\r
-                /* UTF-8 */\r
-                const char *s = va_arg(count, const char*);\r
-                PyObject *str = PyUnicode_DecodeUTF8(s, strlen(s), "replace");\r
-                if (!str)\r
-                    goto fail;\r
-                n += PyUnicode_GET_SIZE(str);\r
-                /* Remember the str and switch to the next slot */\r
-                *callresult++ = str;\r
-                break;\r
-            }\r
-            case 'U':\r
-            {\r
-                PyObject *obj = va_arg(count, PyObject *);\r
-                assert(obj && PyUnicode_Check(obj));\r
-                n += PyUnicode_GET_SIZE(obj);\r
-                break;\r
-            }\r
-            case 'V':\r
-            {\r
-                PyObject *obj = va_arg(count, PyObject *);\r
-                const char *str = va_arg(count, const char *);\r
-                assert(obj || str);\r
-                assert(!obj || PyUnicode_Check(obj));\r
-                if (obj)\r
-                    n += PyUnicode_GET_SIZE(obj);\r
-                else\r
-                    n += strlen(str);\r
-                break;\r
-            }\r
-            case 'S':\r
-            {\r
-                PyObject *obj = va_arg(count, PyObject *);\r
-                PyObject *str;\r
-                assert(obj);\r
-                str = PyObject_Str(obj);\r
-                if (!str)\r
-                    goto fail;\r
-                n += PyString_GET_SIZE(str);\r
-                /* Remember the str and switch to the next slot */\r
-                *callresult++ = str;\r
-                break;\r
-            }\r
-            case 'R':\r
-            {\r
-                PyObject *obj = va_arg(count, PyObject *);\r
-                PyObject *repr;\r
-                assert(obj);\r
-                repr = PyObject_Repr(obj);\r
-                if (!repr)\r
-                    goto fail;\r
-                n += PyUnicode_GET_SIZE(repr);\r
-                /* Remember the repr and switch to the next slot */\r
-                *callresult++ = repr;\r
-                break;\r
-            }\r
-            case 'p':\r
-                (void) va_arg(count, int);\r
-                /* maximum 64-bit pointer representation:\r
-                 * 0xffffffffffffffff\r
-                 * so 19 characters is enough.\r
-                 * XXX I count 18 -- what's the extra for?\r
-                 */\r
-                n += 19;\r
-                break;\r
-            default:\r
-                /* if we stumble upon an unknown\r
-                   formatting code, copy the rest of\r
-                   the format string to the output\r
-                   string. (we cannot just skip the\r
-                   code, since there's no way to know\r
-                   what's in the argument list) */\r
-                n += strlen(p);\r
-                goto expand;\r
-            }\r
-        } else\r
-            n++;\r
-    }\r
-  expand:\r
-    if (abuffersize > 20) {\r
-        /* add 1 for sprintf's trailing null byte */\r
-        abuffer = PyObject_Malloc(abuffersize + 1);\r
-        if (!abuffer) {\r
-            PyErr_NoMemory();\r
-            goto fail;\r
-        }\r
-        realbuffer = abuffer;\r
-    }\r
-    else\r
-        realbuffer = buffer;\r
-    /* step 4: fill the buffer */\r
-    /* Since we've analyzed how much space we need for the worst case,\r
-       we don't have to resize the string.\r
-       There can be no errors beyond this point. */\r
-    string = PyUnicode_FromUnicode(NULL, n);\r
-    if (!string)\r
-        goto fail;\r
-\r
-    s = PyUnicode_AS_UNICODE(string);\r
-    callresult = callresults;\r
-\r
-    for (f = format; *f; f++) {\r
-        if (*f == '%') {\r
-            const char* p = f++;\r
-            int longflag = 0;\r
-            int size_tflag = 0;\r
-            zeropad = (*f == '0');\r
-            /* parse the width.precision part */\r
-            width = 0;\r
-            while (isdigit((unsigned)*f))\r
-                width = (width*10) + *f++ - '0';\r
-            precision = 0;\r
-            if (*f == '.') {\r
-                f++;\r
-                while (isdigit((unsigned)*f))\r
-                    precision = (precision*10) + *f++ - '0';\r
-            }\r
-            /* handle the long flag, but only for %ld and %lu.\r
-               others can be added when necessary. */\r
-            if (*f == 'l' && (f[1] == 'd' || f[1] == 'u')) {\r
-                longflag = 1;\r
-                ++f;\r
-            }\r
-            /* handle the size_t flag. */\r
-            if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) {\r
-                size_tflag = 1;\r
-                ++f;\r
-            }\r
-\r
-            switch (*f) {\r
-            case 'c':\r
-                *s++ = va_arg(vargs, int);\r
-                break;\r
-            case 'd':\r
-                makefmt(fmt, longflag, size_tflag, zeropad, width, precision, 'd');\r
-                if (longflag)\r
-                    sprintf(realbuffer, fmt, va_arg(vargs, long));\r
-                else if (size_tflag)\r
-                    sprintf(realbuffer, fmt, va_arg(vargs, Py_ssize_t));\r
-                else\r
-                    sprintf(realbuffer, fmt, va_arg(vargs, int));\r
-                appendstring(realbuffer);\r
-                break;\r
-            case 'u':\r
-                makefmt(fmt, longflag, size_tflag, zeropad, width, precision, 'u');\r
-                if (longflag)\r
-                    sprintf(realbuffer, fmt, va_arg(vargs, unsigned long));\r
-                else if (size_tflag)\r
-                    sprintf(realbuffer, fmt, va_arg(vargs, size_t));\r
-                else\r
-                    sprintf(realbuffer, fmt, va_arg(vargs, unsigned int));\r
-                appendstring(realbuffer);\r
-                break;\r
-            case 'i':\r
-                makefmt(fmt, 0, 0, zeropad, width, precision, 'i');\r
-                sprintf(realbuffer, fmt, va_arg(vargs, int));\r
-                appendstring(realbuffer);\r
-                break;\r
-            case 'x':\r
-                makefmt(fmt, 0, 0, zeropad, width, precision, 'x');\r
-                sprintf(realbuffer, fmt, va_arg(vargs, int));\r
-                appendstring(realbuffer);\r
-                break;\r
-            case 's':\r
-            {\r
-                /* unused, since we already have the result */\r
-                (void) va_arg(vargs, char *);\r
-                Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(*callresult),\r
-                                PyUnicode_GET_SIZE(*callresult));\r
-                s += PyUnicode_GET_SIZE(*callresult);\r
-                /* We're done with the unicode()/repr() => forget it */\r
-                Py_DECREF(*callresult);\r
-                /* switch to next unicode()/repr() result */\r
-                ++callresult;\r
-                break;\r
-            }\r
-            case 'U':\r
-            {\r
-                PyObject *obj = va_arg(vargs, PyObject *);\r
-                Py_ssize_t size = PyUnicode_GET_SIZE(obj);\r
-                Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size);\r
-                s += size;\r
-                break;\r
-            }\r
-            case 'V':\r
-            {\r
-                PyObject *obj = va_arg(vargs, PyObject *);\r
-                const char *str = va_arg(vargs, const char *);\r
-                if (obj) {\r
-                    Py_ssize_t size = PyUnicode_GET_SIZE(obj);\r
-                    Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size);\r
-                    s += size;\r
-                } else {\r
-                    appendstring(str);\r
-                }\r
-                break;\r
-            }\r
-            case 'S':\r
-            case 'R':\r
-            {\r
-                const char *str = PyString_AS_STRING(*callresult);\r
-                /* unused, since we already have the result */\r
-                (void) va_arg(vargs, PyObject *);\r
-                appendstring(str);\r
-                /* We're done with the unicode()/repr() => forget it */\r
-                Py_DECREF(*callresult);\r
-                /* switch to next unicode()/repr() result */\r
-                ++callresult;\r
-                break;\r
-            }\r
-            case 'p':\r
-                sprintf(buffer, "%p", va_arg(vargs, void*));\r
-                /* %p is ill-defined:  ensure leading 0x. */\r
-                if (buffer[1] == 'X')\r
-                    buffer[1] = 'x';\r
-                else if (buffer[1] != 'x') {\r
-                    memmove(buffer+2, buffer, strlen(buffer)+1);\r
-                    buffer[0] = '0';\r
-                    buffer[1] = 'x';\r
-                }\r
-                appendstring(buffer);\r
-                break;\r
-            case '%':\r
-                *s++ = '%';\r
-                break;\r
-            default:\r
-                appendstring(p);\r
-                goto end;\r
-            }\r
-        } else\r
-            *s++ = *f;\r
-    }\r
-\r
-  end:\r
-    if (callresults)\r
-        PyObject_Free(callresults);\r
-    if (abuffer)\r
-        PyObject_Free(abuffer);\r
-    PyUnicode_Resize(&string, s - PyUnicode_AS_UNICODE(string));\r
-    return string;\r
-  fail:\r
-    if (callresults) {\r
-        PyObject **callresult2 = callresults;\r
-        while (callresult2 < callresult) {\r
-            Py_DECREF(*callresult2);\r
-            ++callresult2;\r
-        }\r
-        PyObject_Free(callresults);\r
-    }\r
-    if (abuffer)\r
-        PyObject_Free(abuffer);\r
-    return NULL;\r
-}\r
-\r
-#undef appendstring\r
-\r
-PyObject *\r
-PyUnicode_FromFormat(const char *format, ...)\r
-{\r
-    PyObject* ret;\r
-    va_list vargs;\r
-\r
-#ifdef HAVE_STDARG_PROTOTYPES\r
-    va_start(vargs, format);\r
-#else\r
-    va_start(vargs);\r
-#endif\r
-    ret = PyUnicode_FromFormatV(format, vargs);\r
-    va_end(vargs);\r
-    return ret;\r
-}\r
-\r
-Py_ssize_t PyUnicode_AsWideChar(PyUnicodeObject *unicode,\r
-                                wchar_t *w,\r
-                                Py_ssize_t size)\r
-{\r
-    if (unicode == NULL) {\r
-        PyErr_BadInternalCall();\r
-        return -1;\r
-    }\r
-\r
-    /* If possible, try to copy the 0-termination as well */\r
-    if (size > PyUnicode_GET_SIZE(unicode))\r
-        size = PyUnicode_GET_SIZE(unicode) + 1;\r
-\r
-#ifdef HAVE_USABLE_WCHAR_T\r
-    memcpy(w, unicode->str, size * sizeof(wchar_t));\r
-#else\r
-    {\r
-        register Py_UNICODE *u;\r
-        register Py_ssize_t i;\r
-        u = PyUnicode_AS_UNICODE(unicode);\r
-        for (i = size; i > 0; i--)\r
-            *w++ = *u++;\r
-    }\r
-#endif\r
-\r
-    if (size > PyUnicode_GET_SIZE(unicode))\r
-        return PyUnicode_GET_SIZE(unicode);\r
-    else\r
-        return size;\r
-}\r
-\r
-#endif\r
-\r
-PyObject *PyUnicode_FromOrdinal(int ordinal)\r
-{\r
-    Py_UNICODE s[1];\r
-\r
-#ifdef Py_UNICODE_WIDE\r
-    if (ordinal < 0 || ordinal > 0x10ffff) {\r
-        PyErr_SetString(PyExc_ValueError,\r
-                        "unichr() arg not in range(0x110000) "\r
-                        "(wide Python build)");\r
-        return NULL;\r
-    }\r
-#else\r
-    if (ordinal < 0 || ordinal > 0xffff) {\r
-        PyErr_SetString(PyExc_ValueError,\r
-                        "unichr() arg not in range(0x10000) "\r
-                        "(narrow Python build)");\r
-        return NULL;\r
-    }\r
-#endif\r
-\r
-    s[0] = (Py_UNICODE)ordinal;\r
-    return PyUnicode_FromUnicode(s, 1);\r
-}\r
-\r
-PyObject *PyUnicode_FromObject(register PyObject *obj)\r
-{\r
-    /* XXX Perhaps we should make this API an alias of\r
-       PyObject_Unicode() instead ?! */\r
-    if (PyUnicode_CheckExact(obj)) {\r
-        Py_INCREF(obj);\r
-        return obj;\r
-    }\r
-    if (PyUnicode_Check(obj)) {\r
-        /* For a Unicode subtype that's not a Unicode object,\r
-           return a true Unicode object with the same data. */\r
-        return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj),\r
-                                     PyUnicode_GET_SIZE(obj));\r
-    }\r
-    return PyUnicode_FromEncodedObject(obj, NULL, "strict");\r
-}\r
-\r
-PyObject *PyUnicode_FromEncodedObject(register PyObject *obj,\r
-                                      const char *encoding,\r
-                                      const char *errors)\r
-{\r
-    const char *s = NULL;\r
-    Py_ssize_t len;\r
-    PyObject *v;\r
-\r
-    if (obj == NULL) {\r
-        PyErr_BadInternalCall();\r
-        return NULL;\r
-    }\r
-\r
-#if 0\r
-    /* For b/w compatibility we also accept Unicode objects provided\r
-       that no encodings is given and then redirect to\r
-       PyObject_Unicode() which then applies the additional logic for\r
-       Unicode subclasses.\r
-\r
-       NOTE: This API should really only be used for object which\r
-       represent *encoded* Unicode !\r
-\r
-    */\r
-    if (PyUnicode_Check(obj)) {\r
-        if (encoding) {\r
-            PyErr_SetString(PyExc_TypeError,\r
-                            "decoding Unicode is not supported");\r
-            return NULL;\r
-        }\r
-        return PyObject_Unicode(obj);\r
-    }\r
-#else\r
-    if (PyUnicode_Check(obj)) {\r
-        PyErr_SetString(PyExc_TypeError,\r
-                        "decoding Unicode is not supported");\r
-        return NULL;\r
-    }\r
-#endif\r
-\r
-    /* Coerce object */\r
-    if (PyString_Check(obj)) {\r
-        s = PyString_AS_STRING(obj);\r
-        len = PyString_GET_SIZE(obj);\r
-    }\r
-    else if (PyByteArray_Check(obj)) {\r
-        /* Python 2.x specific */\r
-        PyErr_Format(PyExc_TypeError,\r
-                     "decoding bytearray is not supported");\r
-        return NULL;\r
-    }\r
-    else if (PyObject_AsCharBuffer(obj, &s, &len)) {\r
-        /* Overwrite the error message with something more useful in\r
-           case of a TypeError. */\r
-        if (PyErr_ExceptionMatches(PyExc_TypeError))\r
-            PyErr_Format(PyExc_TypeError,\r
-                         "coercing to Unicode: need string or buffer, "\r
-                         "%.80s found",\r
-                         Py_TYPE(obj)->tp_name);\r
-        goto onError;\r
-    }\r
-\r
-    /* Convert to Unicode */\r
-    if (len == 0)\r
-        _Py_RETURN_UNICODE_EMPTY();\r
-\r
-    v = PyUnicode_Decode(s, len, encoding, errors);\r
-    return v;\r
-\r
-  onError:\r
-    return NULL;\r
-}\r
-\r
-PyObject *PyUnicode_Decode(const char *s,\r
-                           Py_ssize_t size,\r
-                           const char *encoding,\r
-                           const char *errors)\r
-{\r
-    PyObject *buffer = NULL, *unicode;\r
-\r
-    if (encoding == NULL)\r
-        encoding = PyUnicode_GetDefaultEncoding();\r
-\r
-    /* Shortcuts for common default encodings */\r
-    if (strcmp(encoding, "utf-8") == 0)\r
-        return PyUnicode_DecodeUTF8(s, size, errors);\r
-    else if (strcmp(encoding, "latin-1") == 0)\r
-        return PyUnicode_DecodeLatin1(s, size, errors);\r
-#if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T)\r
-    else if (strcmp(encoding, "mbcs") == 0)\r
-        return PyUnicode_DecodeMBCS(s, size, errors);\r
-#endif\r
-    else if (strcmp(encoding, "ascii") == 0)\r
-        return PyUnicode_DecodeASCII(s, size, errors);\r
-\r
-    /* Decode via the codec registry */\r
-    buffer = PyBuffer_FromMemory((void *)s, size);\r
-    if (buffer == NULL)\r
-        goto onError;\r
-    unicode = PyCodec_Decode(buffer, encoding, errors);\r
-    if (unicode == NULL)\r
-        goto onError;\r
-    if (!PyUnicode_Check(unicode)) {\r
-        PyErr_Format(PyExc_TypeError,\r
-                     "decoder did not return an unicode object (type=%.400s)",\r
-                     Py_TYPE(unicode)->tp_name);\r
-        Py_DECREF(unicode);\r
-        goto onError;\r
-    }\r
-    Py_DECREF(buffer);\r
-    return unicode;\r
-\r
-  onError:\r
-    Py_XDECREF(buffer);\r
-    return NULL;\r
-}\r
-\r
-PyObject *PyUnicode_AsDecodedObject(PyObject *unicode,\r
-                                    const char *encoding,\r
-                                    const char *errors)\r
-{\r
-    PyObject *v;\r
-\r
-    if (!PyUnicode_Check(unicode)) {\r
-        PyErr_BadArgument();\r
-        goto onError;\r
-    }\r
-\r
-    if (encoding == NULL)\r
-        encoding = PyUnicode_GetDefaultEncoding();\r
-\r
-    /* Decode via the codec registry */\r
-    v = PyCodec_Decode(unicode, encoding, errors);\r
-    if (v == NULL)\r
-        goto onError;\r
-    return v;\r
-\r
-  onError:\r
-    return NULL;\r
-}\r
-\r
-PyObject *PyUnicode_Encode(const Py_UNICODE *s,\r
-                           Py_ssize_t size,\r
-                           const char *encoding,\r
-                           const char *errors)\r
-{\r
-    PyObject *v, *unicode;\r
-\r
-    unicode = PyUnicode_FromUnicode(s, size);\r
-    if (unicode == NULL)\r
-        return NULL;\r
-    v = PyUnicode_AsEncodedString(unicode, encoding, errors);\r
-    Py_DECREF(unicode);\r
-    return v;\r
-}\r
-\r
-PyObject *PyUnicode_AsEncodedObject(PyObject *unicode,\r
-                                    const char *encoding,\r
-                                    const char *errors)\r
-{\r
-    PyObject *v;\r
-\r
-    if (!PyUnicode_Check(unicode)) {\r
-        PyErr_BadArgument();\r
-        goto onError;\r
-    }\r
-\r
-    if (encoding == NULL)\r
-        encoding = PyUnicode_GetDefaultEncoding();\r
-\r
-    /* Encode via the codec registry */\r
-    v = PyCodec_Encode(unicode, encoding, errors);\r
-    if (v == NULL)\r
-        goto onError;\r
-    return v;\r
-\r
-  onError:\r
-    return NULL;\r
-}\r
-\r
-PyObject *PyUnicode_AsEncodedString(PyObject *unicode,\r
-                                    const char *encoding,\r
-                                    const char *errors)\r
-{\r
-    PyObject *v;\r
-\r
-    if (!PyUnicode_Check(unicode)) {\r
-        PyErr_BadArgument();\r
-        goto onError;\r
-    }\r
-\r
-    if (encoding == NULL)\r
-        encoding = PyUnicode_GetDefaultEncoding();\r
-\r
-    /* Shortcuts for common default encodings */\r
-    if (errors == NULL) {\r
-        if (strcmp(encoding, "utf-8") == 0)\r
-            return PyUnicode_AsUTF8String(unicode);\r
-        else if (strcmp(encoding, "latin-1") == 0)\r
-            return PyUnicode_AsLatin1String(unicode);\r
-#if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T)\r
-        else if (strcmp(encoding, "mbcs") == 0)\r
-            return PyUnicode_AsMBCSString(unicode);\r
-#endif\r
-        else if (strcmp(encoding, "ascii") == 0)\r
-            return PyUnicode_AsASCIIString(unicode);\r
-    }\r
-\r
-    /* Encode via the codec registry */\r
-    v = PyCodec_Encode(unicode, encoding, errors);\r
-    if (v == NULL)\r
-        goto onError;\r
-    if (!PyString_Check(v)) {\r
-        PyErr_Format(PyExc_TypeError,\r
-                     "encoder did not return a string object (type=%.400s)",\r
-                     Py_TYPE(v)->tp_name);\r
-        Py_DECREF(v);\r
-        goto onError;\r
-    }\r
-    return v;\r
-\r
-  onError:\r
-    return NULL;\r
-}\r
-\r
-PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode,\r
-                                            const char *errors)\r
-{\r
-    PyObject *v = ((PyUnicodeObject *)unicode)->defenc;\r
-\r
-    if (v)\r
-        return v;\r
-    v = PyUnicode_AsEncodedString(unicode, NULL, errors);\r
-    if (v && errors == NULL)\r
-        ((PyUnicodeObject *)unicode)->defenc = v;\r
-    return v;\r
-}\r
-\r
-Py_UNICODE *PyUnicode_AsUnicode(PyObject *unicode)\r
-{\r
-    if (!PyUnicode_Check(unicode)) {\r
-        PyErr_BadArgument();\r
-        goto onError;\r
-    }\r
-    return PyUnicode_AS_UNICODE(unicode);\r
-\r
-  onError:\r
-    return NULL;\r
-}\r
-\r
-Py_ssize_t PyUnicode_GetSize(PyObject *unicode)\r
-{\r
-    if (!PyUnicode_Check(unicode)) {\r
-        PyErr_BadArgument();\r
-        goto onError;\r
-    }\r
-    return PyUnicode_GET_SIZE(unicode);\r
-\r
-  onError:\r
-    return -1;\r
-}\r
-\r
-const char *PyUnicode_GetDefaultEncoding(void)\r
-{\r
-    return unicode_default_encoding;\r
-}\r
-\r
-int PyUnicode_SetDefaultEncoding(const char *encoding)\r
-{\r
-    PyObject *v;\r
-\r
-    /* Make sure the encoding is valid. As side effect, this also\r
-       loads the encoding into the codec registry cache. */\r
-    v = _PyCodec_Lookup(encoding);\r
-    if (v == NULL)\r
-        goto onError;\r
-    Py_DECREF(v);\r
-    strncpy(unicode_default_encoding,\r
-            encoding,\r
-            sizeof(unicode_default_encoding) - 1);\r
-    return 0;\r
-\r
-  onError:\r
-    return -1;\r
-}\r
-\r
-/* error handling callback helper:\r
-   build arguments, call the callback and check the arguments,\r
-   if no exception occurred, copy the replacement to the output\r
-   and adjust various state variables.\r
-   return 0 on success, -1 on error\r
-*/\r
-\r
-static\r
-int unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler,\r
-                                     const char *encoding, const char *reason,\r
-                                     const char *input, Py_ssize_t insize, Py_ssize_t *startinpos,\r
-                                     Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr,\r
-                                     PyUnicodeObject **output, Py_ssize_t *outpos, Py_UNICODE **outptr)\r
-{\r
-    static char *argparse = "O!n;decoding error handler must return (unicode, int) tuple";\r
-\r
-    PyObject *restuple = NULL;\r
-    PyObject *repunicode = NULL;\r
-    Py_ssize_t outsize = PyUnicode_GET_SIZE(*output);\r
-    Py_ssize_t requiredsize;\r
-    Py_ssize_t newpos;\r
-    Py_UNICODE *repptr;\r
-    Py_ssize_t repsize;\r
-    int res = -1;\r
-\r
-    if (*errorHandler == NULL) {\r
-        *errorHandler = PyCodec_LookupError(errors);\r
-        if (*errorHandler == NULL)\r
-            goto onError;\r
-    }\r
-\r
-    if (*exceptionObject == NULL) {\r
-        *exceptionObject = PyUnicodeDecodeError_Create(\r
-            encoding, input, insize, *startinpos, *endinpos, reason);\r
-        if (*exceptionObject == NULL)\r
-            goto onError;\r
-    }\r
-    else {\r
-        if (PyUnicodeDecodeError_SetStart(*exceptionObject, *startinpos))\r
-            goto onError;\r
-        if (PyUnicodeDecodeError_SetEnd(*exceptionObject, *endinpos))\r
-            goto onError;\r
-        if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason))\r
-            goto onError;\r
-    }\r
-\r
-    restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL);\r
-    if (restuple == NULL)\r
-        goto onError;\r
-    if (!PyTuple_Check(restuple)) {\r
-        PyErr_SetString(PyExc_TypeError, &argparse[4]);\r
-        goto onError;\r
-    }\r
-    if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos))\r
-        goto onError;\r
-    if (newpos<0)\r
-        newpos = insize+newpos;\r
-    if (newpos<0 || newpos>insize) {\r
-        PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos);\r
-        goto onError;\r
-    }\r
-\r
-    /* need more space? (at least enough for what we\r
-       have+the replacement+the rest of the string (starting\r
-       at the new input position), so we won't have to check space\r
-       when there are no errors in the rest of the string) */\r
-    repptr = PyUnicode_AS_UNICODE(repunicode);\r
-    repsize = PyUnicode_GET_SIZE(repunicode);\r
-    requiredsize = *outpos;\r
-    if (requiredsize > PY_SSIZE_T_MAX - repsize)\r
-        goto overflow;\r
-    requiredsize += repsize;\r
-    if (requiredsize > PY_SSIZE_T_MAX - (insize - newpos))\r
-        goto overflow;\r
-    requiredsize += insize - newpos;\r
-    if (requiredsize > outsize) {\r
-        if (outsize <= PY_SSIZE_T_MAX/2 && requiredsize < 2*outsize)\r
-            requiredsize = 2*outsize;\r
-        if (_PyUnicode_Resize(output, requiredsize) < 0)\r
-            goto onError;\r
-        *outptr = PyUnicode_AS_UNICODE(*output) + *outpos;\r
-    }\r
-    *endinpos = newpos;\r
-    *inptr = input + newpos;\r
-    Py_UNICODE_COPY(*outptr, repptr, repsize);\r
-    *outptr += repsize;\r
-    *outpos += repsize;\r
-    /* we made it! */\r
-    res = 0;\r
-\r
-  onError:\r
-    Py_XDECREF(restuple);\r
-    return res;\r
-\r
-  overflow:\r
-    PyErr_SetString(PyExc_OverflowError,\r
-                    "decoded result is too long for a Python string");\r
-    goto onError;\r
-}\r
-\r
-/* --- UTF-7 Codec -------------------------------------------------------- */\r
-\r
-/* See RFC2152 for details.  We encode conservatively and decode liberally. */\r
-\r
-/* Three simple macros defining base-64. */\r
-\r
-/* Is c a base-64 character? */\r
-\r
-#define IS_BASE64(c) \\r
-    (isalnum(c) || (c) == '+' || (c) == '/')\r
-\r
-/* given that c is a base-64 character, what is its base-64 value? */\r
-\r
-#define FROM_BASE64(c)                                                  \\r
-    (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' :                           \\r
-     ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 :                      \\r
-     ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 :                      \\r
-     (c) == '+' ? 62 : 63)\r
-\r
-/* What is the base-64 character of the bottom 6 bits of n? */\r
-\r
-#define TO_BASE64(n)  \\r
-    ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f])\r
-\r
-/* DECODE_DIRECT: this byte encountered in a UTF-7 string should be\r
- * decoded as itself.  We are permissive on decoding; the only ASCII\r
- * byte not decoding to itself is the + which begins a base64\r
- * string. */\r
-\r
-#define DECODE_DIRECT(c)                                \\r
-    ((c) <= 127 && (c) != '+')\r
-\r
-/* The UTF-7 encoder treats ASCII characters differently according to\r
- * whether they are Set D, Set O, Whitespace, or special (i.e. none of\r
- * the above).  See RFC2152.  This array identifies these different\r
- * sets:\r
- * 0 : "Set D"\r
- *     alphanumeric and '(),-./:?\r
- * 1 : "Set O"\r
- *     !"#$%&*;<=>@[]^_`{|}\r
- * 2 : "whitespace"\r
- *     ht nl cr sp\r
- * 3 : special (must be base64 encoded)\r
- *     everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127)\r
- */\r
-\r
-static\r
-char utf7_category[128] = {\r
-/* nul soh stx etx eot enq ack bel bs  ht  nl  vt  np  cr  so  si  */\r
-    3,  3,  3,  3,  3,  3,  3,  3,  3,  2,  2,  3,  3,  2,  3,  3,\r
-/* dle dc1 dc2 dc3 dc4 nak syn etb can em  sub esc fs  gs  rs  us  */\r
-    3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,\r
-/* sp   !   "   #   $   %   &   '   (   )   *   +   ,   -   .   /  */\r
-    2,  1,  1,  1,  1,  1,  1,  0,  0,  0,  1,  3,  0,  0,  0,  0,\r
-/*  0   1   2   3   4   5   6   7   8   9   :   ;   <   =   >   ?  */\r
-    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  1,  1,  1,  1,  0,\r
-/*  @   A   B   C   D   E   F   G   H   I   J   K   L   M   N   O  */\r
-    1,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,\r
-/*  P   Q   R   S   T   U   V   W   X   Y   Z   [   \   ]   ^   _  */\r
-    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  1,  3,  1,  1,  1,\r
-/*  `   a   b   c   d   e   f   g   h   i   j   k   l   m   n   o  */\r
-    1,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,\r
-/*  p   q   r   s   t   u   v   w   x   y   z   {   |   }   ~  del */\r
-    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  1,  1,  1,  3,  3,\r
-};\r
-\r
-/* ENCODE_DIRECT: this character should be encoded as itself.  The\r
- * answer depends on whether we are encoding set O as itself, and also\r
- * on whether we are encoding whitespace as itself.  RFC2152 makes it\r
- * clear that the answers to these questions vary between\r
- * applications, so this code needs to be flexible.  */\r
-\r
-#define ENCODE_DIRECT(c, directO, directWS)             \\r
-    ((c) < 128 && (c) > 0 &&                            \\r
-     ((utf7_category[(c)] == 0) ||                      \\r
-      (directWS && (utf7_category[(c)] == 2)) ||        \\r
-      (directO && (utf7_category[(c)] == 1))))\r
-\r
-PyObject *PyUnicode_DecodeUTF7(const char *s,\r
-                               Py_ssize_t size,\r
-                               const char *errors)\r
-{\r
-    return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL);\r
-}\r
-\r
-/* The decoder.  The only state we preserve is our read position,\r
- * i.e. how many characters we have consumed.  So if we end in the\r
- * middle of a shift sequence we have to back off the read position\r
- * and the output to the beginning of the sequence, otherwise we lose\r
- * all the shift state (seen bits, number of bits seen, high\r
- * surrogate). */\r
-\r
-PyObject *PyUnicode_DecodeUTF7Stateful(const char *s,\r
-                                       Py_ssize_t size,\r
-                                       const char *errors,\r
-                                       Py_ssize_t *consumed)\r
-{\r
-    const char *starts = s;\r
-    Py_ssize_t startinpos;\r
-    Py_ssize_t endinpos;\r
-    Py_ssize_t outpos;\r
-    const char *e;\r
-    PyUnicodeObject *unicode;\r
-    Py_UNICODE *p;\r
-    const char *errmsg = "";\r
-    int inShift = 0;\r
-    Py_UNICODE *shiftOutStart;\r
-    unsigned int base64bits = 0;\r
-    unsigned long base64buffer = 0;\r
-    Py_UNICODE surrogate = 0;\r
-    PyObject *errorHandler = NULL;\r
-    PyObject *exc = NULL;\r
-\r
-    unicode = _PyUnicode_New(size);\r
-    if (!unicode)\r
-        return NULL;\r
-    if (size == 0) {\r
-        if (consumed)\r
-            *consumed = 0;\r
-        return (PyObject *)unicode;\r
-    }\r
-\r
-    p = unicode->str;\r
-    shiftOutStart = p;\r
-    e = s + size;\r
-\r
-    while (s < e) {\r
-        Py_UNICODE ch = (unsigned char) *s;\r
-\r
-        if (inShift) { /* in a base-64 section */\r
-            if (IS_BASE64(ch)) { /* consume a base-64 character */\r
-                base64buffer = (base64buffer << 6) | FROM_BASE64(ch);\r
-                base64bits += 6;\r
-                s++;\r
-                if (base64bits >= 16) {\r
-                    /* we have enough bits for a UTF-16 value */\r
-                    Py_UNICODE outCh = (Py_UNICODE)\r
-                                       (base64buffer >> (base64bits-16));\r
-                    base64bits -= 16;\r
-                    base64buffer &= (1 << base64bits) - 1; /* clear high bits */\r
-                    assert(outCh <= 0xffff);\r
-                    if (surrogate) {\r
-                        /* expecting a second surrogate */\r
-                        if (outCh >= 0xDC00 && outCh <= 0xDFFF) {\r
-#ifdef Py_UNICODE_WIDE\r
-                            *p++ = (((surrogate & 0x3FF)<<10)\r
-                                    | (outCh & 0x3FF)) + 0x10000;\r
-#else\r
-                            *p++ = surrogate;\r
-                            *p++ = outCh;\r
-#endif\r
-                            surrogate = 0;\r
-                            continue;\r
-                        }\r
-                        else {\r
-                            *p++ = surrogate;\r
-                            surrogate = 0;\r
-                        }\r
-                    }\r
-                    if (outCh >= 0xD800 && outCh <= 0xDBFF) {\r
-                        /* first surrogate */\r
-                        surrogate = outCh;\r
-                    }\r
-                    else {\r
-                        *p++ = outCh;\r
-                    }\r
-                }\r
-            }\r
-            else { /* now leaving a base-64 section */\r
-                inShift = 0;\r
-                s++;\r
-                if (surrogate) {\r
-                    *p++ = surrogate;\r
-                    surrogate = 0;\r
-                }\r
-                if (base64bits > 0) { /* left-over bits */\r
-                    if (base64bits >= 6) {\r
-                        /* We've seen at least one base-64 character */\r
-                        errmsg = "partial character in shift sequence";\r
-                        goto utf7Error;\r
-                    }\r
-                    else {\r
-                        /* Some bits remain; they should be zero */\r
-                        if (base64buffer != 0) {\r
-                            errmsg = "non-zero padding bits in shift sequence";\r
-                            goto utf7Error;\r
-                        }\r
-                    }\r
-                }\r
-                if (ch != '-') {\r
-                    /* '-' is absorbed; other terminating\r
-                       characters are preserved */\r
-                    *p++ = ch;\r
-                }\r
-            }\r
-        }\r
-        else if ( ch == '+' ) {\r
-            startinpos = s-starts;\r
-            s++; /* consume '+' */\r
-            if (s < e && *s == '-') { /* '+-' encodes '+' */\r
-                s++;\r
-                *p++ = '+';\r
-            }\r
-            else { /* begin base64-encoded section */\r
-                inShift = 1;\r
-                shiftOutStart = p;\r
-                base64bits = 0;\r
-                base64buffer = 0;\r
-            }\r
-        }\r
-        else if (DECODE_DIRECT(ch)) { /* character decodes as itself */\r
-            *p++ = ch;\r
-            s++;\r
-        }\r
-        else {\r
-            startinpos = s-starts;\r
-            s++;\r
-            errmsg = "unexpected special character";\r
-            goto utf7Error;\r
-        }\r
-        continue;\r
-utf7Error:\r
-        outpos = p-PyUnicode_AS_UNICODE(unicode);\r
-        endinpos = s-starts;\r
-        if (unicode_decode_call_errorhandler(\r
-                errors, &errorHandler,\r
-                "utf7", errmsg,\r
-                starts, size, &startinpos, &endinpos, &exc, &s,\r
-                &unicode, &outpos, &p))\r
-            goto onError;\r
-    }\r
-\r
-    /* end of string */\r
-\r
-    if (inShift && !consumed) { /* in shift sequence, no more to follow */\r
-        /* if we're in an inconsistent state, that's an error */\r
-        if (surrogate ||\r
-                (base64bits >= 6) ||\r
-                (base64bits > 0 && base64buffer != 0)) {\r
-            outpos = p-PyUnicode_AS_UNICODE(unicode);\r
-            endinpos = size;\r
-            if (unicode_decode_call_errorhandler(\r
-                    errors, &errorHandler,\r
-                    "utf7", "unterminated shift sequence",\r
-                    starts, size, &startinpos, &endinpos, &exc, &s,\r
-                    &unicode, &outpos, &p))\r
-                goto onError;\r
-        }\r
-    }\r
-\r
-    /* return state */\r
-    if (consumed) {\r
-        if (inShift) {\r
-            p = shiftOutStart; /* back off output */\r
-            *consumed = startinpos;\r
-        }\r
-        else {\r
-            *consumed = s-starts;\r
-        }\r
-    }\r
-\r
-    if (_PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0)\r
-        goto onError;\r
-\r
-    Py_XDECREF(errorHandler);\r
-    Py_XDECREF(exc);\r
-    return (PyObject *)unicode;\r
-\r
-  onError:\r
-    Py_XDECREF(errorHandler);\r
-    Py_XDECREF(exc);\r
-    Py_DECREF(unicode);\r
-    return NULL;\r
-}\r
-\r
-\r
-PyObject *PyUnicode_EncodeUTF7(const Py_UNICODE *s,\r
-                               Py_ssize_t size,\r
-                               int base64SetO,\r
-                               int base64WhiteSpace,\r
-                               const char *errors)\r
-{\r
-    PyObject *v;\r
-    /* It might be possible to tighten this worst case */\r
-    Py_ssize_t allocated = 8 * size;\r
-    int inShift = 0;\r
-    Py_ssize_t i = 0;\r
-    unsigned int base64bits = 0;\r
-    unsigned long base64buffer = 0;\r
-    char * out;\r
-    char * start;\r
-\r
-    if (allocated / 8 != size)\r
-        return PyErr_NoMemory();\r
-\r
-    if (size == 0)\r
-        return PyString_FromStringAndSize(NULL, 0);\r
-\r
-    v = PyString_FromStringAndSize(NULL, allocated);\r
-    if (v == NULL)\r
-        return NULL;\r
-\r
-    start = out = PyString_AS_STRING(v);\r
-    for (;i < size; ++i) {\r
-        Py_UNICODE ch = s[i];\r
-\r
-        if (inShift) {\r
-            if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) {\r
-                /* shifting out */\r
-                if (base64bits) { /* output remaining bits */\r
-                    *out++ = TO_BASE64(base64buffer << (6-base64bits));\r
-                    base64buffer = 0;\r
-                    base64bits = 0;\r
-                }\r
-                inShift = 0;\r
-                /* Characters not in the BASE64 set implicitly unshift the sequence\r
-                   so no '-' is required, except if the character is itself a '-' */\r
-                if (IS_BASE64(ch) || ch == '-') {\r
-                    *out++ = '-';\r
-                }\r
-                *out++ = (char) ch;\r
-            }\r
-            else {\r
-                goto encode_char;\r
-            }\r
-        }\r
-        else { /* not in a shift sequence */\r
-            if (ch == '+') {\r
-                *out++ = '+';\r
-                        *out++ = '-';\r
-            }\r
-            else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) {\r
-                *out++ = (char) ch;\r
-            }\r
-            else {\r
-                *out++ = '+';\r
-                inShift = 1;\r
-                goto encode_char;\r
-            }\r
-        }\r
-        continue;\r
-encode_char:\r
-#ifdef Py_UNICODE_WIDE\r
-        if (ch >= 0x10000) {\r
-            /* code first surrogate */\r
-            base64bits += 16;\r
-            base64buffer = (base64buffer << 16) | 0xd800 | ((ch-0x10000) >> 10);\r
-            while (base64bits >= 6) {\r
-                *out++ = TO_BASE64(base64buffer >> (base64bits-6));\r
-                base64bits -= 6;\r
-            }\r
-            /* prepare second surrogate */\r
-            ch =  0xDC00 | ((ch-0x10000) & 0x3FF);\r
-        }\r
-#endif\r
-        base64bits += 16;\r
-        base64buffer = (base64buffer << 16) | ch;\r
-        while (base64bits >= 6) {\r
-            *out++ = TO_BASE64(base64buffer >> (base64bits-6));\r
-            base64bits -= 6;\r
-        }\r
-    }\r
-    if (base64bits)\r
-        *out++= TO_BASE64(base64buffer << (6-base64bits) );\r
-    if (inShift)\r
-        *out++ = '-';\r
-\r
-    if (_PyString_Resize(&v, out - start))\r
-        return NULL;\r
-    return v;\r
-}\r
-\r
-#undef IS_BASE64\r
-#undef FROM_BASE64\r
-#undef TO_BASE64\r
-#undef DECODE_DIRECT\r
-#undef ENCODE_DIRECT\r
-\r
-/* --- UTF-8 Codec -------------------------------------------------------- */\r
-\r
-static\r
-char utf8_code_length[256] = {\r
-    /* Map UTF-8 encoded prefix byte to sequence length.  Zero means\r
-       illegal prefix.  See RFC 3629 for details */\r
-    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00-0F */\r
-    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\r
-    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\r
-    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\r
-    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\r
-    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\r
-    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\r
-    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 70-7F */\r
-    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 80-8F */\r
-    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\r
-    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\r
-    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0-BF */\r
-    0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* C0-C1 + C2-CF */\r
-    2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* D0-DF */\r
-    3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* E0-EF */\r
-    4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  /* F0-F4 + F5-FF */\r
-};\r
-\r
-PyObject *PyUnicode_DecodeUTF8(const char *s,\r
-                               Py_ssize_t size,\r
-                               const char *errors)\r
-{\r
-    return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL);\r
-}\r
-\r
-PyObject *PyUnicode_DecodeUTF8Stateful(const char *s,\r
-                                       Py_ssize_t size,\r
-                                       const char *errors,\r
-                                       Py_ssize_t *consumed)\r
-{\r
-    const char *starts = s;\r
-    int n;\r
-    int k;\r
-    Py_ssize_t startinpos;\r
-    Py_ssize_t endinpos;\r
-    Py_ssize_t outpos;\r
-    const char *e;\r
-    PyUnicodeObject *unicode;\r
-    Py_UNICODE *p;\r
-    const char *errmsg = "";\r
-    PyObject *errorHandler = NULL;\r
-    PyObject *exc = NULL;\r
-\r
-    /* Note: size will always be longer than the resulting Unicode\r
-       character count */\r
-    unicode = _PyUnicode_New(size);\r
-    if (!unicode)\r
-        return NULL;\r
-    if (size == 0) {\r
-        if (consumed)\r
-            *consumed = 0;\r
-        return (PyObject *)unicode;\r
-    }\r
-\r
-    /* Unpack UTF-8 encoded data */\r
-    p = unicode->str;\r
-    e = s + size;\r
-\r
-    while (s < e) {\r
-        Py_UCS4 ch = (unsigned char)*s;\r
-\r
-        if (ch < 0x80) {\r
-            *p++ = (Py_UNICODE)ch;\r
-            s++;\r
-            continue;\r
-        }\r
-\r
-        n = utf8_code_length[ch];\r
-\r
-        if (s + n > e) {\r
-            if (consumed)\r
-                break;\r
-            else {\r
-                errmsg = "unexpected end of data";\r
-                startinpos = s-starts;\r
-                endinpos = startinpos+1;\r
-                for (k=1; (k < size-startinpos) && ((s[k]&0xC0) == 0x80); k++)\r
-                    endinpos++;\r
-                goto utf8Error;\r
-            }\r
-        }\r
-\r
-        switch (n) {\r
-\r
-        case 0:\r
-            errmsg = "invalid start byte";\r
-            startinpos = s-starts;\r
-            endinpos = startinpos+1;\r
-            goto utf8Error;\r
-\r
-        case 1:\r
-            errmsg = "internal error";\r
-            startinpos = s-starts;\r
-            endinpos = startinpos+1;\r
-            goto utf8Error;\r
-\r
-        case 2:\r
-            if ((s[1] & 0xc0) != 0x80) {\r
-                errmsg = "invalid continuation byte";\r
-                startinpos = s-starts;\r
-                endinpos = startinpos + 1;\r
-                goto utf8Error;\r
-            }\r
-            ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f);\r
-            assert ((ch > 0x007F) && (ch <= 0x07FF));\r
-            *p++ = (Py_UNICODE)ch;\r
-            break;\r
-\r
-        case 3:\r
-            /* XXX: surrogates shouldn't be valid UTF-8!\r
-               see http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf\r
-               (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt\r
-               Uncomment the 2 lines below to make them invalid,\r
-               code points: d800-dfff; UTF-8: \xed\xa0\x80-\xed\xbf\xbf. */\r
-            if ((s[1] & 0xc0) != 0x80 ||\r
-                (s[2] & 0xc0) != 0x80 ||\r
-                ((unsigned char)s[0] == 0xE0 &&\r
-                 (unsigned char)s[1] < 0xA0)/* ||\r
-                ((unsigned char)s[0] == 0xED &&\r
-                 (unsigned char)s[1] > 0x9F)*/) {\r
-                errmsg = "invalid continuation byte";\r
-                startinpos = s-starts;\r
-                endinpos = startinpos + 1;\r
-\r
-                /* if s[1] first two bits are 1 and 0, then the invalid\r
-                   continuation byte is s[2], so increment endinpos by 1,\r
-                   if not, s[1] is invalid and endinpos doesn't need to\r
-                   be incremented. */\r
-                if ((s[1] & 0xC0) == 0x80)\r
-                    endinpos++;\r
-                goto utf8Error;\r
-            }\r
-            ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f);\r
-            assert ((ch > 0x07FF) && (ch <= 0xFFFF));\r
-            *p++ = (Py_UNICODE)ch;\r
-            break;\r
-\r
-        case 4:\r
-            if ((s[1] & 0xc0) != 0x80 ||\r
-                (s[2] & 0xc0) != 0x80 ||\r
-                (s[3] & 0xc0) != 0x80 ||\r
-                ((unsigned char)s[0] == 0xF0 &&\r
-                 (unsigned char)s[1] < 0x90) ||\r
-                ((unsigned char)s[0] == 0xF4 &&\r
-                 (unsigned char)s[1] > 0x8F)) {\r
-                errmsg = "invalid continuation byte";\r
-                startinpos = s-starts;\r
-                endinpos = startinpos + 1;\r
-                if ((s[1] & 0xC0) == 0x80) {\r
-                    endinpos++;\r
-                    if ((s[2] & 0xC0) == 0x80)\r
-                        endinpos++;\r
-                }\r
-                goto utf8Error;\r
-            }\r
-            ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) +\r
-                 ((s[2] & 0x3f) << 6) + (s[3] & 0x3f);\r
-            assert ((ch > 0xFFFF) && (ch <= 0x10ffff));\r
-\r
-#ifdef Py_UNICODE_WIDE\r
-            *p++ = (Py_UNICODE)ch;\r
-#else\r
-            /*  compute and append the two surrogates: */\r
-\r
-            /*  translate from 10000..10FFFF to 0..FFFF */\r
-            ch -= 0x10000;\r
-\r
-            /*  high surrogate = top 10 bits added to D800 */\r
-            *p++ = (Py_UNICODE)(0xD800 + (ch >> 10));\r
-\r
-            /*  low surrogate = bottom 10 bits added to DC00 */\r
-            *p++ = (Py_UNICODE)(0xDC00 + (ch & 0x03FF));\r
-#endif\r
-            break;\r
-        }\r
-        s += n;\r
-        continue;\r
-\r
-      utf8Error:\r
-        outpos = p-PyUnicode_AS_UNICODE(unicode);\r
-        if (unicode_decode_call_errorhandler(\r
-                errors, &errorHandler,\r
-                "utf8", errmsg,\r
-                starts, size, &startinpos, &endinpos, &exc, &s,\r
-                &unicode, &outpos, &p))\r
-            goto onError;\r
-    }\r
-    if (consumed)\r
-        *consumed = s-starts;\r
-\r
-    /* Adjust length */\r
-    if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0)\r
-        goto onError;\r
-\r
-    Py_XDECREF(errorHandler);\r
-    Py_XDECREF(exc);\r
-    return (PyObject *)unicode;\r
-\r
-  onError:\r
-    Py_XDECREF(errorHandler);\r
-    Py_XDECREF(exc);\r
-    Py_DECREF(unicode);\r
-    return NULL;\r
-}\r
-\r
-/* Allocation strategy:  if the string is short, convert into a stack buffer\r
-   and allocate exactly as much space needed at the end.  Else allocate the\r
-   maximum possible needed (4 result bytes per Unicode character), and return\r
-   the excess memory at the end.\r
-*/\r
-PyObject *\r
-PyUnicode_EncodeUTF8(const Py_UNICODE *s,\r
-                     Py_ssize_t size,\r
-                     const char *errors)\r
-{\r
-#define MAX_SHORT_UNICHARS 300  /* largest size we'll do on the stack */\r
-\r
-    Py_ssize_t i;           /* index into s of next input byte */\r
-    PyObject *v;        /* result string object */\r
-    char *p;            /* next free byte in output buffer */\r
-    Py_ssize_t nallocated;  /* number of result bytes allocated */\r
-    Py_ssize_t nneeded;        /* number of result bytes needed */\r
-    char stackbuf[MAX_SHORT_UNICHARS * 4];\r
-\r
-    assert(s != NULL);\r
-    assert(size >= 0);\r
-\r
-    if (size <= MAX_SHORT_UNICHARS) {\r
-        /* Write into the stack buffer; nallocated can't overflow.\r
-         * At the end, we'll allocate exactly as much heap space as it\r
-         * turns out we need.\r
-         */\r
-        nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int);\r
-        v = NULL;   /* will allocate after we're done */\r
-        p = stackbuf;\r
-    }\r
-    else {\r
-        /* Overallocate on the heap, and give the excess back at the end. */\r
-        nallocated = size * 4;\r
-        if (nallocated / 4 != size)  /* overflow! */\r
-            return PyErr_NoMemory();\r
-        v = PyString_FromStringAndSize(NULL, nallocated);\r
-        if (v == NULL)\r
-            return NULL;\r
-        p = PyString_AS_STRING(v);\r
-    }\r
-\r
-    for (i = 0; i < size;) {\r
-        Py_UCS4 ch = s[i++];\r
-\r
-        if (ch < 0x80)\r
-            /* Encode ASCII */\r
-            *p++ = (char) ch;\r
-\r
-        else if (ch < 0x0800) {\r
-            /* Encode Latin-1 */\r
-            *p++ = (char)(0xc0 | (ch >> 6));\r
-            *p++ = (char)(0x80 | (ch & 0x3f));\r
-        }\r
-        else {\r
-            /* Encode UCS2 Unicode ordinals */\r
-            if (ch < 0x10000) {\r
-                /* Special case: check for high surrogate */\r
-                if (0xD800 <= ch && ch <= 0xDBFF && i != size) {\r
-                    Py_UCS4 ch2 = s[i];\r
-                    /* Check for low surrogate and combine the two to\r
-                       form a UCS4 value */\r
-                    if (0xDC00 <= ch2 && ch2 <= 0xDFFF) {\r
-                        ch = ((ch - 0xD800) << 10 | (ch2 - 0xDC00)) + 0x10000;\r
-                        i++;\r
-                        goto encodeUCS4;\r
-                    }\r
-                    /* Fall through: handles isolated high surrogates */\r
-                }\r
-                *p++ = (char)(0xe0 | (ch >> 12));\r
-                *p++ = (char)(0x80 | ((ch >> 6) & 0x3f));\r
-                *p++ = (char)(0x80 | (ch & 0x3f));\r
-                continue;\r
-            }\r
-          encodeUCS4:\r
-            /* Encode UCS4 Unicode ordinals */\r
-            *p++ = (char)(0xf0 | (ch >> 18));\r
-            *p++ = (char)(0x80 | ((ch >> 12) & 0x3f));\r
-            *p++ = (char)(0x80 | ((ch >> 6) & 0x3f));\r
-            *p++ = (char)(0x80 | (ch & 0x3f));\r
-        }\r
-    }\r
-\r
-    if (v == NULL) {\r
-        /* This was stack allocated. */\r
-        nneeded = p - stackbuf;\r
-        assert(nneeded <= nallocated);\r
-        v = PyString_FromStringAndSize(stackbuf, nneeded);\r
-    }\r
-    else {\r
-        /* Cut back to size actually needed. */\r
-        nneeded = p - PyString_AS_STRING(v);\r
-        assert(nneeded <= nallocated);\r
-        if (_PyString_Resize(&v, nneeded))\r
-            return NULL;\r
-    }\r
-    return v;\r
-\r
-#undef MAX_SHORT_UNICHARS\r
-}\r
-\r
-PyObject *PyUnicode_AsUTF8String(PyObject *unicode)\r
-{\r
-    if (!PyUnicode_Check(unicode)) {\r
-        PyErr_BadArgument();\r
-        return NULL;\r
-    }\r
-    return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode),\r
-                                PyUnicode_GET_SIZE(unicode),\r
-                                NULL);\r
-}\r
-\r
-/* --- UTF-32 Codec ------------------------------------------------------- */\r
-\r
-PyObject *\r
-PyUnicode_DecodeUTF32(const char *s,\r
-                      Py_ssize_t size,\r
-                      const char *errors,\r
-                      int *byteorder)\r
-{\r
-    return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL);\r
-}\r
-\r
-PyObject *\r
-PyUnicode_DecodeUTF32Stateful(const char *s,\r
-                              Py_ssize_t size,\r
-                              const char *errors,\r
-                              int *byteorder,\r
-                              Py_ssize_t *consumed)\r
-{\r
-    const char *starts = s;\r
-    Py_ssize_t startinpos;\r
-    Py_ssize_t endinpos;\r
-    Py_ssize_t outpos;\r
-    PyUnicodeObject *unicode;\r
-    Py_UNICODE *p;\r
-#ifndef Py_UNICODE_WIDE\r
-    int pairs = 0;\r
-    const unsigned char *qq;\r
-#else\r
-    const int pairs = 0;\r
-#endif\r
-    const unsigned char *q, *e;\r
-    int bo = 0;       /* assume native ordering by default */\r
-    const char *errmsg = "";\r
-    /* Offsets from q for retrieving bytes in the right order. */\r
-#ifdef BYTEORDER_IS_LITTLE_ENDIAN\r
-    int iorder[] = {0, 1, 2, 3};\r
-#else\r
-    int iorder[] = {3, 2, 1, 0};\r
-#endif\r
-    PyObject *errorHandler = NULL;\r
-    PyObject *exc = NULL;\r
-\r
-    q = (unsigned char *)s;\r
-    e = q + size;\r
-\r
-    if (byteorder)\r
-        bo = *byteorder;\r
-\r
-    /* Check for BOM marks (U+FEFF) in the input and adjust current\r
-       byte order setting accordingly. In native mode, the leading BOM\r
-       mark is skipped, in all other modes, it is copied to the output\r
-       stream as-is (giving a ZWNBSP character). */\r
-    if (bo == 0) {\r
-        if (size >= 4) {\r
-            const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) |\r
-                (q[iorder[1]] << 8) | q[iorder[0]];\r
-#ifdef BYTEORDER_IS_LITTLE_ENDIAN\r
-            if (bom == 0x0000FEFF) {\r
-                q += 4;\r
-                bo = -1;\r
-            }\r
-            else if (bom == 0xFFFE0000) {\r
-                q += 4;\r
-                bo = 1;\r
-            }\r
-#else\r
-            if (bom == 0x0000FEFF) {\r
-                q += 4;\r
-                bo = 1;\r
-            }\r
-            else if (bom == 0xFFFE0000) {\r
-                q += 4;\r
-                bo = -1;\r
-            }\r
-#endif\r
-        }\r
-    }\r
-\r
-    if (bo == -1) {\r
-        /* force LE */\r
-        iorder[0] = 0;\r
-        iorder[1] = 1;\r
-        iorder[2] = 2;\r
-        iorder[3] = 3;\r
-    }\r
-    else if (bo == 1) {\r
-        /* force BE */\r
-        iorder[0] = 3;\r
-        iorder[1] = 2;\r
-        iorder[2] = 1;\r
-        iorder[3] = 0;\r
-    }\r
-\r
-    /* On narrow builds we split characters outside the BMP into two\r
-       code points => count how much extra space we need. */\r
-#ifndef Py_UNICODE_WIDE\r
-    for (qq = q; e - qq >= 4; qq += 4)\r
-        if (qq[iorder[2]] != 0 || qq[iorder[3]] != 0)\r
-            pairs++;\r
-#endif\r
-\r
-    /* This might be one to much, because of a BOM */\r
-    unicode = _PyUnicode_New((size+3)/4+pairs);\r
-    if (!unicode)\r
-        return NULL;\r
-    if (size == 0)\r
-        return (PyObject *)unicode;\r
-\r
-    /* Unpack UTF-32 encoded data */\r
-    p = unicode->str;\r
-\r
-    while (q < e) {\r
-        Py_UCS4 ch;\r
-        /* remaining bytes at the end? (size should be divisible by 4) */\r
-        if (e-q<4) {\r
-            if (consumed)\r
-                break;\r
-            errmsg = "truncated data";\r
-            startinpos = ((const char *)q)-starts;\r
-            endinpos = ((const char *)e)-starts;\r
-            goto utf32Error;\r
-            /* The remaining input chars are ignored if the callback\r
-               chooses to skip the input */\r
-        }\r
-        ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) |\r
-            (q[iorder[1]] << 8) | q[iorder[0]];\r
-\r
-        if (ch >= 0x110000)\r
-        {\r
-            errmsg = "code point not in range(0x110000)";\r
-            startinpos = ((const char *)q)-starts;\r
-            endinpos = startinpos+4;\r
-            goto utf32Error;\r
-        }\r
-#ifndef Py_UNICODE_WIDE\r
-        if (ch >= 0x10000)\r
-        {\r
-            *p++ = 0xD800 | ((ch-0x10000) >> 10);\r
-            *p++ = 0xDC00 | ((ch-0x10000) & 0x3FF);\r
-        }\r
-        else\r
-#endif\r
-            *p++ = ch;\r
-        q += 4;\r
-        continue;\r
-      utf32Error:\r
-        outpos = p-PyUnicode_AS_UNICODE(unicode);\r
-        if (unicode_decode_call_errorhandler(\r
-                errors, &errorHandler,\r
-                "utf32", errmsg,\r
-                starts, size, &startinpos, &endinpos, &exc, (const char **)&q,\r
-                &unicode, &outpos, &p))\r
-            goto onError;\r
-    }\r
-\r
-    if (byteorder)\r
-        *byteorder = bo;\r
-\r
-    if (consumed)\r
-        *consumed = (const char *)q-starts;\r
-\r
-    /* Adjust length */\r
-    if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0)\r
-        goto onError;\r
-\r
-    Py_XDECREF(errorHandler);\r
-    Py_XDECREF(exc);\r
-    return (PyObject *)unicode;\r
-\r
-  onError:\r
-    Py_DECREF(unicode);\r
-    Py_XDECREF(errorHandler);\r
-    Py_XDECREF(exc);\r
-    return NULL;\r
-}\r
-\r
-PyObject *\r
-PyUnicode_EncodeUTF32(const Py_UNICODE *s,\r
-                      Py_ssize_t size,\r
-                      const char *errors,\r
-                      int byteorder)\r
-{\r
-    PyObject *v;\r
-    unsigned char *p;\r
-    Py_ssize_t nsize, bytesize;\r
-#ifndef Py_UNICODE_WIDE\r
-    Py_ssize_t i, pairs;\r
-#else\r
-    const int pairs = 0;\r
-#endif\r
-    /* Offsets from p for storing byte pairs in the right order. */\r
-#ifdef BYTEORDER_IS_LITTLE_ENDIAN\r
-    int iorder[] = {0, 1, 2, 3};\r
-#else\r
-    int iorder[] = {3, 2, 1, 0};\r
-#endif\r
-\r
-#define STORECHAR(CH)                           \\r
-    do {                                        \\r
-        p[iorder[3]] = ((CH) >> 24) & 0xff;     \\r
-        p[iorder[2]] = ((CH) >> 16) & 0xff;     \\r
-        p[iorder[1]] = ((CH) >> 8) & 0xff;      \\r
-        p[iorder[0]] = (CH) & 0xff;             \\r
-        p += 4;                                 \\r
-    } while(0)\r
-\r
-    /* In narrow builds we can output surrogate pairs as one code point,\r
-       so we need less space. */\r
-#ifndef Py_UNICODE_WIDE\r
-    for (i = pairs = 0; i < size-1; i++)\r
-        if (0xD800 <= s[i] && s[i] <= 0xDBFF &&\r
-            0xDC00 <= s[i+1] && s[i+1] <= 0xDFFF)\r
-            pairs++;\r
-#endif\r
-    nsize = (size - pairs + (byteorder == 0));\r
-    bytesize = nsize * 4;\r
-    if (bytesize / 4 != nsize)\r
-        return PyErr_NoMemory();\r
-    v = PyString_FromStringAndSize(NULL, bytesize);\r
-    if (v == NULL)\r
-        return NULL;\r
-\r
-    p = (unsigned char *)PyString_AS_STRING(v);\r
-    if (byteorder == 0)\r
-        STORECHAR(0xFEFF);\r
-    if (size == 0)\r
-        return v;\r
-\r
-    if (byteorder == -1) {\r
-        /* force LE */\r
-        iorder[0] = 0;\r
-        iorder[1] = 1;\r
-        iorder[2] = 2;\r
-        iorder[3] = 3;\r
-    }\r
-    else if (byteorder == 1) {\r
-        /* force BE */\r
-        iorder[0] = 3;\r
-        iorder[1] = 2;\r
-        iorder[2] = 1;\r
-        iorder[3] = 0;\r
-    }\r
-\r
-    while (size-- > 0) {\r
-        Py_UCS4 ch = *s++;\r
-#ifndef Py_UNICODE_WIDE\r
-        if (0xD800 <= ch && ch <= 0xDBFF && size > 0) {\r
-            Py_UCS4 ch2 = *s;\r
-            if (0xDC00 <= ch2 && ch2 <= 0xDFFF) {\r
-                ch = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000;\r
-                s++;\r
-                size--;\r
-            }\r
-        }\r
-#endif\r
-        STORECHAR(ch);\r
-    }\r
-    return v;\r
-#undef STORECHAR\r
-}\r
-\r
-PyObject *PyUnicode_AsUTF32String(PyObject *unicode)\r
-{\r
-    if (!PyUnicode_Check(unicode)) {\r
-        PyErr_BadArgument();\r
-        return NULL;\r
-    }\r
-    return PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(unicode),\r
-                                 PyUnicode_GET_SIZE(unicode),\r
-                                 NULL,\r
-                                 0);\r
-}\r
-\r
-/* --- UTF-16 Codec ------------------------------------------------------- */\r
-\r
-PyObject *\r
-PyUnicode_DecodeUTF16(const char *s,\r
-                      Py_ssize_t size,\r
-                      const char *errors,\r
-                      int *byteorder)\r
-{\r
-    return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL);\r
-}\r
-\r
-PyObject *\r
-PyUnicode_DecodeUTF16Stateful(const char *s,\r
-                              Py_ssize_t size,\r
-                              const char *errors,\r
-                              int *byteorder,\r
-                              Py_ssize_t *consumed)\r
-{\r
-    const char *starts = s;\r
-    Py_ssize_t startinpos;\r
-    Py_ssize_t endinpos;\r
-    Py_ssize_t outpos;\r
-    PyUnicodeObject *unicode;\r
-    Py_UNICODE *p;\r
-    const unsigned char *q, *e;\r
-    int bo = 0;       /* assume native ordering by default */\r
-    const char *errmsg = "";\r
-    /* Offsets from q for retrieving byte pairs in the right order. */\r
-#ifdef BYTEORDER_IS_LITTLE_ENDIAN\r
-    int ihi = 1, ilo = 0;\r
-#else\r
-    int ihi = 0, ilo = 1;\r
-#endif\r
-    PyObject *errorHandler = NULL;\r
-    PyObject *exc = NULL;\r
-\r
-    /* Note: size will always be longer than the resulting Unicode\r
-       character count */\r
-    unicode = _PyUnicode_New(size);\r
-    if (!unicode)\r
-        return NULL;\r
-    if (size == 0)\r
-        return (PyObject *)unicode;\r
-\r
-    /* Unpack UTF-16 encoded data */\r
-    p = unicode->str;\r
-    q = (unsigned char *)s;\r
-    e = q + size;\r
-\r
-    if (byteorder)\r
-        bo = *byteorder;\r
-\r
-    /* Check for BOM marks (U+FEFF) in the input and adjust current\r
-       byte order setting accordingly. In native mode, the leading BOM\r
-       mark is skipped, in all other modes, it is copied to the output\r
-       stream as-is (giving a ZWNBSP character). */\r
-    if (bo == 0) {\r
-        if (size >= 2) {\r
-            const Py_UNICODE bom = (q[ihi] << 8) | q[ilo];\r
-#ifdef BYTEORDER_IS_LITTLE_ENDIAN\r
-            if (bom == 0xFEFF) {\r
-                q += 2;\r
-                bo = -1;\r
-            }\r
-            else if (bom == 0xFFFE) {\r
-                q += 2;\r
-                bo = 1;\r
-            }\r
-#else\r
-            if (bom == 0xFEFF) {\r
-                q += 2;\r
-                bo = 1;\r
-            }\r
-            else if (bom == 0xFFFE) {\r
-                q += 2;\r
-                bo = -1;\r
-            }\r
-#endif\r
-        }\r
-    }\r
-\r
-    if (bo == -1) {\r
-        /* force LE */\r
-        ihi = 1;\r
-        ilo = 0;\r
-    }\r
-    else if (bo == 1) {\r
-        /* force BE */\r
-        ihi = 0;\r
-        ilo = 1;\r
-    }\r
-\r
-    while (q < e) {\r
-        Py_UNICODE ch;\r
-        /* remaining bytes at the end? (size should be even) */\r
-        if (e-q<2) {\r
-            if (consumed)\r
-                break;\r
-            errmsg = "truncated data";\r
-            startinpos = ((const char *)q)-starts;\r
-            endinpos = ((const char *)e)-starts;\r
-            goto utf16Error;\r
-            /* The remaining input chars are ignored if the callback\r
-               chooses to skip the input */\r
-        }\r
-        ch = (q[ihi] << 8) | q[ilo];\r
-\r
-        q += 2;\r
-\r
-        if (ch < 0xD800 || ch > 0xDFFF) {\r
-            *p++ = ch;\r
-            continue;\r
-        }\r
-\r
-        /* UTF-16 code pair: */\r
-        if (e - q < 2) {\r
-            q -= 2;\r
-            if (consumed)\r
-                break;\r
-            errmsg = "unexpected end of data";\r
-            startinpos = ((const char *)q)-starts;\r
-            endinpos = ((const char *)e)-starts;\r
-            goto utf16Error;\r
-        }\r
-        if (0xD800 <= ch && ch <= 0xDBFF) {\r
-            Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo];\r
-            q += 2;\r
-            if (0xDC00 <= ch2 && ch2 <= 0xDFFF) {\r
-#ifndef Py_UNICODE_WIDE\r
-                *p++ = ch;\r
-                *p++ = ch2;\r
-#else\r
-                *p++ = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000;\r
-#endif\r
-                continue;\r
-            }\r
-            else {\r
-                errmsg = "illegal UTF-16 surrogate";\r
-                startinpos = (((const char *)q)-4)-starts;\r
-                endinpos = startinpos+2;\r
-                goto utf16Error;\r
-            }\r
-\r
-        }\r
-        errmsg = "illegal encoding";\r
-        startinpos = (((const char *)q)-2)-starts;\r
-        endinpos = startinpos+2;\r
-        /* Fall through to report the error */\r
-\r
-      utf16Error:\r
-        outpos = p-PyUnicode_AS_UNICODE(unicode);\r
-        if (unicode_decode_call_errorhandler(\r
-                errors, &errorHandler,\r
-                "utf16", errmsg,\r
-                starts, size, &startinpos, &endinpos, &exc, (const char **)&q,\r
-                &unicode, &outpos, &p))\r
-            goto onError;\r
-    }\r
-\r
-    if (byteorder)\r
-        *byteorder = bo;\r
-\r
-    if (consumed)\r
-        *consumed = (const char *)q-starts;\r
-\r
-    /* Adjust length */\r
-    if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0)\r
-        goto onError;\r
-\r
-    Py_XDECREF(errorHandler);\r
-    Py_XDECREF(exc);\r
-    return (PyObject *)unicode;\r
-\r
-  onError:\r
-    Py_DECREF(unicode);\r
-    Py_XDECREF(errorHandler);\r
-    Py_XDECREF(exc);\r
-    return NULL;\r
-}\r
-\r
-PyObject *\r
-PyUnicode_EncodeUTF16(const Py_UNICODE *s,\r
-                      Py_ssize_t size,\r
-                      const char *errors,\r
-                      int byteorder)\r
-{\r
-    PyObject *v;\r
-    unsigned char *p;\r
-    Py_ssize_t nsize, bytesize;\r
-#ifdef Py_UNICODE_WIDE\r
-    Py_ssize_t i, pairs;\r
-#else\r
-    const int pairs = 0;\r
-#endif\r
-    /* Offsets from p for storing byte pairs in the right order. */\r
-#ifdef BYTEORDER_IS_LITTLE_ENDIAN\r
-    int ihi = 1, ilo = 0;\r
-#else\r
-    int ihi = 0, ilo = 1;\r
-#endif\r
-\r
-#define STORECHAR(CH)                           \\r
-    do {                                        \\r
-        p[ihi] = ((CH) >> 8) & 0xff;            \\r
-        p[ilo] = (CH) & 0xff;                   \\r
-        p += 2;                                 \\r
-    } while(0)\r
-\r
-#ifdef Py_UNICODE_WIDE\r
-    for (i = pairs = 0; i < size; i++)\r
-        if (s[i] >= 0x10000)\r
-            pairs++;\r
-#endif\r
-    /* 2 * (size + pairs + (byteorder == 0)) */\r
-    if (size > PY_SSIZE_T_MAX ||\r
-        size > PY_SSIZE_T_MAX - pairs - (byteorder == 0))\r
-        return PyErr_NoMemory();\r
-    nsize = size + pairs + (byteorder == 0);\r
-    bytesize = nsize * 2;\r
-    if (bytesize / 2 != nsize)\r
-        return PyErr_NoMemory();\r
-    v = PyString_FromStringAndSize(NULL, bytesize);\r
-    if (v == NULL)\r
-        return NULL;\r
-\r
-    p = (unsigned char *)PyString_AS_STRING(v);\r
-    if (byteorder == 0)\r
-        STORECHAR(0xFEFF);\r
-    if (size == 0)\r
-        return v;\r
-\r
-    if (byteorder == -1) {\r
-        /* force LE */\r
-        ihi = 1;\r
-        ilo = 0;\r
-    }\r
-    else if (byteorder == 1) {\r
-        /* force BE */\r
-        ihi = 0;\r
-        ilo = 1;\r
-    }\r
-\r
-    while (size-- > 0) {\r
-        Py_UNICODE ch = *s++;\r
-        Py_UNICODE ch2 = 0;\r
-#ifdef Py_UNICODE_WIDE\r
-        if (ch >= 0x10000) {\r
-            ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF);\r
-            ch  = 0xD800 | ((ch-0x10000) >> 10);\r
-        }\r
-#endif\r
-        STORECHAR(ch);\r
-        if (ch2)\r
-            STORECHAR(ch2);\r
-    }\r
-    return v;\r
-#undef STORECHAR\r
-}\r
-\r
-PyObject *PyUnicode_AsUTF16String(PyObject *unicode)\r
-{\r
-    if (!PyUnicode_Check(unicode)) {\r
-        PyErr_BadArgument();\r
-        return NULL;\r
-    }\r
-    return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode),\r
-                                 PyUnicode_GET_SIZE(unicode),\r
-                                 NULL,\r
-                                 0);\r
-}\r
-\r
-/* --- Unicode Escape Codec ----------------------------------------------- */\r
-\r
-static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL;\r
-\r
-PyObject *PyUnicode_DecodeUnicodeEscape(const char *s,\r
-                                        Py_ssize_t size,\r
-                                        const char *errors)\r
-{\r
-    const char *starts = s;\r
-    Py_ssize_t startinpos;\r
-    Py_ssize_t endinpos;\r
-    Py_ssize_t outpos;\r
-    PyUnicodeObject *v;\r
-    Py_UNICODE *p;\r
-    const char *end;\r
-    char* message;\r
-    Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */\r
-    PyObject *errorHandler = NULL;\r
-    PyObject *exc = NULL;\r
-\r
-    /* Escaped strings will always be longer than the resulting\r
-       Unicode string, so we start with size here and then reduce the\r
-       length after conversion to the true value.\r
-       (but if the error callback returns a long replacement string\r
-       we'll have to allocate more space) */\r
-    v = _PyUnicode_New(size);\r
-    if (v == NULL)\r
-        goto onError;\r
-    if (size == 0)\r
-        return (PyObject *)v;\r
-\r
-    p = PyUnicode_AS_UNICODE(v);\r
-    end = s + size;\r
-\r
-    while (s < end) {\r
-        unsigned char c;\r
-        Py_UNICODE x;\r
-        int digits;\r
-\r
-        /* Non-escape characters are interpreted as Unicode ordinals */\r
-        if (*s != '\\') {\r
-            *p++ = (unsigned char) *s++;\r
-            continue;\r
-        }\r
-\r
-        startinpos = s-starts;\r
-        /* \ - Escapes */\r
-        s++;\r
-        c = *s++;\r
-        if (s > end)\r
-            c = '\0'; /* Invalid after \ */\r
-        switch (c) {\r
-\r
-            /* \x escapes */\r
-        case '\n': break;\r
-        case '\\': *p++ = '\\'; break;\r
-        case '\'': *p++ = '\''; break;\r
-        case '\"': *p++ = '\"'; break;\r
-        case 'b': *p++ = '\b'; break;\r
-        case 'f': *p++ = '\014'; break; /* FF */\r
-        case 't': *p++ = '\t'; break;\r
-        case 'n': *p++ = '\n'; break;\r
-        case 'r': *p++ = '\r'; break;\r
-        case 'v': *p++ = '\013'; break; /* VT */\r
-        case 'a': *p++ = '\007'; break; /* BEL, not classic C */\r
-\r
-            /* \OOO (octal) escapes */\r
-        case '0': case '1': case '2': case '3':\r
-        case '4': case '5': case '6': case '7':\r
-            x = s[-1] - '0';\r
-            if (s < end && '0' <= *s && *s <= '7') {\r
-                x = (x<<3) + *s++ - '0';\r
-                if (s < end && '0' <= *s && *s <= '7')\r
-                    x = (x<<3) + *s++ - '0';\r
-            }\r
-            *p++ = x;\r
-            break;\r
-\r
-            /* hex escapes */\r
-            /* \xXX */\r
-        case 'x':\r
-            digits = 2;\r
-            message = "truncated \\xXX escape";\r
-            goto hexescape;\r
-\r
-            /* \uXXXX */\r
-        case 'u':\r
-            digits = 4;\r
-            message = "truncated \\uXXXX escape";\r
-            goto hexescape;\r
-\r
-            /* \UXXXXXXXX */\r
-        case 'U':\r
-            digits = 8;\r
-            message = "truncated \\UXXXXXXXX escape";\r
-        hexescape:\r
-            chr = 0;\r
-            if (end - s < digits) {\r
-                /* count only hex digits */\r
-                for (; s < end; ++s) {\r
-                    c = (unsigned char)*s;\r
-                    if (!Py_ISXDIGIT(c))\r
-                        goto error;\r
-                }\r
-                goto error;\r
-            }\r
-            for (; digits--; ++s) {\r
-                c = (unsigned char)*s;\r
-                if (!Py_ISXDIGIT(c))\r
-                    goto error;\r
-                chr = (chr<<4) & ~0xF;\r
-                if (c >= '0' && c <= '9')\r
-                    chr += c - '0';\r
-                else if (c >= 'a' && c <= 'f')\r
-                    chr += 10 + c - 'a';\r
-                else\r
-                    chr += 10 + c - 'A';\r
-            }\r
-            if (chr == 0xffffffff && PyErr_Occurred())\r
-                /* _decoding_error will have already written into the\r
-                   target buffer. */\r
-                break;\r
-        store:\r
-            /* when we get here, chr is a 32-bit unicode character */\r
-            if (chr <= 0xffff)\r
-                /* UCS-2 character */\r
-                *p++ = (Py_UNICODE) chr;\r
-            else if (chr <= 0x10ffff) {\r
-                /* UCS-4 character. Either store directly, or as\r
-                   surrogate pair. */\r
-#ifdef Py_UNICODE_WIDE\r
-                *p++ = chr;\r
-#else\r
-                chr -= 0x10000L;\r
-                *p++ = 0xD800 + (Py_UNICODE) (chr >> 10);\r
-                *p++ = 0xDC00 + (Py_UNICODE) (chr & 0x03FF);\r
-#endif\r
-            } else {\r
-                message = "illegal Unicode character";\r
-                goto error;\r
-            }\r
-            break;\r
-\r
-            /* \N{name} */\r
-        case 'N':\r
-            message = "malformed \\N character escape";\r
-            if (ucnhash_CAPI == NULL) {\r
-                /* load the unicode data module */\r
-                ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import(PyUnicodeData_CAPSULE_NAME, 1);\r
-                if (ucnhash_CAPI == NULL)\r
-                    goto ucnhashError;\r
-            }\r
-            if (*s == '{') {\r
-                const char *start = s+1;\r
-                /* look for the closing brace */\r
-                while (*s != '}' && s < end)\r
-                    s++;\r
-                if (s > start && s < end && *s == '}') {\r
-                    /* found a name.  look it up in the unicode database */\r
-                    message = "unknown Unicode character name";\r
-                    s++;\r
-                    if (s - start - 1 <= INT_MAX &&\r
-                        ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), &chr))\r
-                        goto store;\r
-                }\r
-            }\r
-            goto error;\r
-\r
-        default:\r
-            if (s > end) {\r
-                message = "\\ at end of string";\r
-                s--;\r
-                goto error;\r
-            }\r
-            else {\r
-                *p++ = '\\';\r
-                *p++ = (unsigned char)s[-1];\r
-            }\r
-            break;\r
-        }\r
-        continue;\r
-\r
-      error:\r
-        endinpos = s-starts;\r
-        outpos = p-PyUnicode_AS_UNICODE(v);\r
-        if (unicode_decode_call_errorhandler(\r
-                errors, &errorHandler,\r
-                "unicodeescape", message,\r
-                starts, size, &startinpos, &endinpos, &exc, &s,\r
-                &v, &outpos, &p))\r
-            goto onError;\r
-        continue;\r
-    }\r
-    if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0)\r
-        goto onError;\r
-    Py_XDECREF(errorHandler);\r
-    Py_XDECREF(exc);\r
-    return (PyObject *)v;\r
-\r
-  ucnhashError:\r
-    PyErr_SetString(\r
-        PyExc_UnicodeError,\r
-        "\\N escapes not supported (can't load unicodedata module)"\r
-        );\r
-    Py_XDECREF(v);\r
-    Py_XDECREF(errorHandler);\r
-    Py_XDECREF(exc);\r
-    return NULL;\r
-\r
-  onError:\r
-    Py_XDECREF(v);\r
-    Py_XDECREF(errorHandler);\r
-    Py_XDECREF(exc);\r
-    return NULL;\r
-}\r
-\r
-/* Return a Unicode-Escape string version of the Unicode object.\r
-\r
-   If quotes is true, the string is enclosed in u"" or u'' quotes as\r
-   appropriate.\r
-\r
-*/\r
-\r
-Py_LOCAL_INLINE(const Py_UNICODE *) findchar(const Py_UNICODE *s,\r
-                                             Py_ssize_t size,\r
-                                             Py_UNICODE ch)\r
-{\r
-    /* like wcschr, but doesn't stop at NULL characters */\r
-\r
-    while (size-- > 0) {\r
-        if (*s == ch)\r
-            return s;\r
-        s++;\r
-    }\r
-\r
-    return NULL;\r
-}\r
-\r
-static\r
-PyObject *unicodeescape_string(const Py_UNICODE *s,\r
-                               Py_ssize_t size,\r
-                               int quotes)\r
-{\r
-    PyObject *repr;\r
-    char *p;\r
-\r
-    static const char *hexdigit = "0123456789abcdef";\r
-#ifdef Py_UNICODE_WIDE\r
-    const Py_ssize_t expandsize = 10;\r
-#else\r
-    const Py_ssize_t expandsize = 6;\r
-#endif\r
-\r
-    /* XXX(nnorwitz): rather than over-allocating, it would be\r
-       better to choose a different scheme.  Perhaps scan the\r
-       first N-chars of the string and allocate based on that size.\r
-    */\r
-    /* Initial allocation is based on the longest-possible unichr\r
-       escape.\r
-\r
-       In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source\r
-       unichr, so in this case it's the longest unichr escape. In\r
-       narrow (UTF-16) builds this is five chars per source unichr\r
-       since there are two unichrs in the surrogate pair, so in narrow\r
-       (UTF-16) builds it's not the longest unichr escape.\r
-\r
-       In wide or narrow builds '\uxxxx' is 6 chars per source unichr,\r
-       so in the narrow (UTF-16) build case it's the longest unichr\r
-       escape.\r
-    */\r
-\r
-    if (size > (PY_SSIZE_T_MAX - 2 - 1) / expandsize)\r
-        return PyErr_NoMemory();\r
-\r
-    repr = PyString_FromStringAndSize(NULL,\r
-                                      2\r
-                                      + expandsize*size\r
-                                      + 1);\r
-    if (repr == NULL)\r
-        return NULL;\r
-\r
-    p = PyString_AS_STRING(repr);\r
-\r
-    if (quotes) {\r
-        *p++ = 'u';\r
-        *p++ = (findchar(s, size, '\'') &&\r
-                !findchar(s, size, '"')) ? '"' : '\'';\r
-    }\r
-    while (size-- > 0) {\r
-        Py_UNICODE ch = *s++;\r
-\r
-        /* Escape quotes and backslashes */\r
-        if ((quotes &&\r
-             ch == (Py_UNICODE) PyString_AS_STRING(repr)[1]) || ch == '\\') {\r
-            *p++ = '\\';\r
-            *p++ = (char) ch;\r
-            continue;\r
-        }\r
-\r
-#ifdef Py_UNICODE_WIDE\r
-        /* Map 21-bit characters to '\U00xxxxxx' */\r
-        else if (ch >= 0x10000) {\r
-            *p++ = '\\';\r
-            *p++ = 'U';\r
-            *p++ = hexdigit[(ch >> 28) & 0x0000000F];\r
-            *p++ = hexdigit[(ch >> 24) & 0x0000000F];\r
-            *p++ = hexdigit[(ch >> 20) & 0x0000000F];\r
-            *p++ = hexdigit[(ch >> 16) & 0x0000000F];\r
-            *p++ = hexdigit[(ch >> 12) & 0x0000000F];\r
-            *p++ = hexdigit[(ch >> 8) & 0x0000000F];\r
-            *p++ = hexdigit[(ch >> 4) & 0x0000000F];\r
-            *p++ = hexdigit[ch & 0x0000000F];\r
-            continue;\r
-        }\r
-#else\r
-        /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */\r
-        else if (ch >= 0xD800 && ch < 0xDC00) {\r
-            Py_UNICODE ch2;\r
-            Py_UCS4 ucs;\r
-\r
-            ch2 = *s++;\r
-            size--;\r
-            if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) {\r
-                ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000;\r
-                *p++ = '\\';\r
-                *p++ = 'U';\r
-                *p++ = hexdigit[(ucs >> 28) & 0x0000000F];\r
-                *p++ = hexdigit[(ucs >> 24) & 0x0000000F];\r
-                *p++ = hexdigit[(ucs >> 20) & 0x0000000F];\r
-                *p++ = hexdigit[(ucs >> 16) & 0x0000000F];\r
-                *p++ = hexdigit[(ucs >> 12) & 0x0000000F];\r
-                *p++ = hexdigit[(ucs >> 8) & 0x0000000F];\r
-                *p++ = hexdigit[(ucs >> 4) & 0x0000000F];\r
-                *p++ = hexdigit[ucs & 0x0000000F];\r
-                continue;\r
-            }\r
-            /* Fall through: isolated surrogates are copied as-is */\r
-            s--;\r
-            size++;\r
-        }\r
-#endif\r
-\r
-        /* Map 16-bit characters to '\uxxxx' */\r
-        if (ch >= 256) {\r
-            *p++ = '\\';\r
-            *p++ = 'u';\r
-            *p++ = hexdigit[(ch >> 12) & 0x000F];\r
-            *p++ = hexdigit[(ch >> 8) & 0x000F];\r
-            *p++ = hexdigit[(ch >> 4) & 0x000F];\r
-            *p++ = hexdigit[ch & 0x000F];\r
-        }\r
-\r
-        /* Map special whitespace to '\t', \n', '\r' */\r
-        else if (ch == '\t') {\r
-            *p++ = '\\';\r
-            *p++ = 't';\r
-        }\r
-        else if (ch == '\n') {\r
-            *p++ = '\\';\r
-            *p++ = 'n';\r
-        }\r
-        else if (ch == '\r') {\r
-            *p++ = '\\';\r
-            *p++ = 'r';\r
-        }\r
-\r
-        /* Map non-printable US ASCII to '\xhh' */\r
-        else if (ch < ' ' || ch >= 0x7F) {\r
-            *p++ = '\\';\r
-            *p++ = 'x';\r
-            *p++ = hexdigit[(ch >> 4) & 0x000F];\r
-            *p++ = hexdigit[ch & 0x000F];\r
-        }\r
-\r
-        /* Copy everything else as-is */\r
-        else\r
-            *p++ = (char) ch;\r
-    }\r
-    if (quotes)\r
-        *p++ = PyString_AS_STRING(repr)[1];\r
-\r
-    *p = '\0';\r
-    if (_PyString_Resize(&repr, p - PyString_AS_STRING(repr)))\r
-        return NULL;\r
-    return repr;\r
-}\r
-\r
-PyObject *PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s,\r
-                                        Py_ssize_t size)\r
-{\r
-    return unicodeescape_string(s, size, 0);\r
-}\r
-\r
-PyObject *PyUnicode_AsUnicodeEscapeString(PyObject *unicode)\r
-{\r
-    if (!PyUnicode_Check(unicode)) {\r
-        PyErr_BadArgument();\r
-        return NULL;\r
-    }\r
-    return PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode),\r
-                                         PyUnicode_GET_SIZE(unicode));\r
-}\r
-\r
-/* --- Raw Unicode Escape Codec ------------------------------------------- */\r
-\r
-PyObject *PyUnicode_DecodeRawUnicodeEscape(const char *s,\r
-                                           Py_ssize_t size,\r
-                                           const char *errors)\r
-{\r
-    const char *starts = s;\r
-    Py_ssize_t startinpos;\r
-    Py_ssize_t endinpos;\r
-    Py_ssize_t outpos;\r
-    PyUnicodeObject *v;\r
-    Py_UNICODE *p;\r
-    const char *end;\r
-    const char *bs;\r
-    PyObject *errorHandler = NULL;\r
-    PyObject *exc = NULL;\r
-\r
-    /* Escaped strings will always be longer than the resulting\r
-       Unicode string, so we start with size here and then reduce the\r
-       length after conversion to the true value. (But decoding error\r
-       handler might have to resize the string) */\r
-    v = _PyUnicode_New(size);\r
-    if (v == NULL)\r
-        goto onError;\r
-    if (size == 0)\r
-        return (PyObject *)v;\r
-    p = PyUnicode_AS_UNICODE(v);\r
-    end = s + size;\r
-    while (s < end) {\r
-        unsigned char c;\r
-        Py_UCS4 x;\r
-        int i;\r
-        int count;\r
-\r
-        /* Non-escape characters are interpreted as Unicode ordinals */\r
-        if (*s != '\\') {\r
-            *p++ = (unsigned char)*s++;\r
-            continue;\r
-        }\r
-        startinpos = s-starts;\r
-\r
-        /* \u-escapes are only interpreted iff the number of leading\r
-           backslashes if odd */\r
-        bs = s;\r
-        for (;s < end;) {\r
-            if (*s != '\\')\r
-                break;\r
-            *p++ = (unsigned char)*s++;\r
-        }\r
-        if (((s - bs) & 1) == 0 ||\r
-            s >= end ||\r
-            (*s != 'u' && *s != 'U')) {\r
-            continue;\r
-        }\r
-        p--;\r
-        count = *s=='u' ? 4 : 8;\r
-        s++;\r
-\r
-        /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */\r
-        outpos = p-PyUnicode_AS_UNICODE(v);\r
-        for (x = 0, i = 0; i < count; ++i, ++s) {\r
-            c = (unsigned char)*s;\r
-            if (!isxdigit(c)) {\r
-                endinpos = s-starts;\r
-                if (unicode_decode_call_errorhandler(\r
-                        errors, &errorHandler,\r
-                        "rawunicodeescape", "truncated \\uXXXX",\r
-                        starts, size, &startinpos, &endinpos, &exc, &s,\r
-                        &v, &outpos, &p))\r
-                    goto onError;\r
-                goto nextByte;\r
-            }\r
-            x = (x<<4) & ~0xF;\r
-            if (c >= '0' && c <= '9')\r
-                x += c - '0';\r
-            else if (c >= 'a' && c <= 'f')\r
-                x += 10 + c - 'a';\r
-            else\r
-                x += 10 + c - 'A';\r
-        }\r
-        if (x <= 0xffff)\r
-            /* UCS-2 character */\r
-            *p++ = (Py_UNICODE) x;\r
-        else if (x <= 0x10ffff) {\r
-            /* UCS-4 character. Either store directly, or as\r
-               surrogate pair. */\r
-#ifdef Py_UNICODE_WIDE\r
-            *p++ = (Py_UNICODE) x;\r
-#else\r
-            x -= 0x10000L;\r
-            *p++ = 0xD800 + (Py_UNICODE) (x >> 10);\r
-            *p++ = 0xDC00 + (Py_UNICODE) (x & 0x03FF);\r
-#endif\r
-        } else {\r
-            endinpos = s-starts;\r
-            outpos = p-PyUnicode_AS_UNICODE(v);\r
-            if (unicode_decode_call_errorhandler(\r
-                    errors, &errorHandler,\r
-                    "rawunicodeescape", "\\Uxxxxxxxx out of range",\r
-                    starts, size, &startinpos, &endinpos, &exc, &s,\r
-                    &v, &outpos, &p))\r
-                goto onError;\r
-        }\r
-      nextByte:\r
-        ;\r
-    }\r
-    if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0)\r
-        goto onError;\r
-    Py_XDECREF(errorHandler);\r
-    Py_XDECREF(exc);\r
-    return (PyObject *)v;\r
-\r
-  onError:\r
-    Py_XDECREF(v);\r
-    Py_XDECREF(errorHandler);\r
-    Py_XDECREF(exc);\r
-    return NULL;\r
-}\r
-\r
-PyObject *PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s,\r
-                                           Py_ssize_t size)\r
-{\r
-    PyObject *repr;\r
-    char *p;\r
-    char *q;\r
-\r
-    static const char *hexdigit = "0123456789abcdef";\r
-#ifdef Py_UNICODE_WIDE\r
-    const Py_ssize_t expandsize = 10;\r
-#else\r
-    const Py_ssize_t expandsize = 6;\r
-#endif\r
-\r
-    if (size > PY_SSIZE_T_MAX / expandsize)\r
-        return PyErr_NoMemory();\r
-\r
-    repr = PyString_FromStringAndSize(NULL, expandsize * size);\r
-    if (repr == NULL)\r
-        return NULL;\r
-    if (size == 0)\r
-        return repr;\r
-\r
-    p = q = PyString_AS_STRING(repr);\r
-    while (size-- > 0) {\r
-        Py_UNICODE ch = *s++;\r
-#ifdef Py_UNICODE_WIDE\r
-        /* Map 32-bit characters to '\Uxxxxxxxx' */\r
-        if (ch >= 0x10000) {\r
-            *p++ = '\\';\r
-            *p++ = 'U';\r
-            *p++ = hexdigit[(ch >> 28) & 0xf];\r
-            *p++ = hexdigit[(ch >> 24) & 0xf];\r
-            *p++ = hexdigit[(ch >> 20) & 0xf];\r
-            *p++ = hexdigit[(ch >> 16) & 0xf];\r
-            *p++ = hexdigit[(ch >> 12) & 0xf];\r
-            *p++ = hexdigit[(ch >> 8) & 0xf];\r
-            *p++ = hexdigit[(ch >> 4) & 0xf];\r
-            *p++ = hexdigit[ch & 15];\r
-        }\r
-        else\r
-#else\r
-            /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */\r
-            if (ch >= 0xD800 && ch < 0xDC00) {\r
-                Py_UNICODE ch2;\r
-                Py_UCS4 ucs;\r
-\r
-                ch2 = *s++;\r
-                size--;\r
-                if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) {\r
-                    ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000;\r
-                    *p++ = '\\';\r
-                    *p++ = 'U';\r
-                    *p++ = hexdigit[(ucs >> 28) & 0xf];\r
-                    *p++ = hexdigit[(ucs >> 24) & 0xf];\r
-                    *p++ = hexdigit[(ucs >> 20) & 0xf];\r
-                    *p++ = hexdigit[(ucs >> 16) & 0xf];\r
-                    *p++ = hexdigit[(ucs >> 12) & 0xf];\r
-                    *p++ = hexdigit[(ucs >> 8) & 0xf];\r
-                    *p++ = hexdigit[(ucs >> 4) & 0xf];\r
-                    *p++ = hexdigit[ucs & 0xf];\r
-                    continue;\r
-                }\r
-                /* Fall through: isolated surrogates are copied as-is */\r
-                s--;\r
-                size++;\r
-            }\r
-#endif\r
-        /* Map 16-bit characters to '\uxxxx' */\r
-        if (ch >= 256) {\r
-            *p++ = '\\';\r
-            *p++ = 'u';\r
-            *p++ = hexdigit[(ch >> 12) & 0xf];\r
-            *p++ = hexdigit[(ch >> 8) & 0xf];\r
-            *p++ = hexdigit[(ch >> 4) & 0xf];\r
-            *p++ = hexdigit[ch & 15];\r
-        }\r
-        /* Copy everything else as-is */\r
-        else\r
-            *p++ = (char) ch;\r
-    }\r
-    *p = '\0';\r
-    if (_PyString_Resize(&repr, p - q))\r
-        return NULL;\r
-    return repr;\r
-}\r
-\r
-PyObject *PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode)\r
-{\r
-    if (!PyUnicode_Check(unicode)) {\r
-        PyErr_BadArgument();\r
-        return NULL;\r
-    }\r
-    return PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode),\r
-                                            PyUnicode_GET_SIZE(unicode));\r
-}\r
-\r
-/* --- Unicode Internal Codec ------------------------------------------- */\r
-\r
-PyObject *_PyUnicode_DecodeUnicodeInternal(const char *s,\r
-                                           Py_ssize_t size,\r
-                                           const char *errors)\r
-{\r
-    const char *starts = s;\r
-    Py_ssize_t startinpos;\r
-    Py_ssize_t endinpos;\r
-    Py_ssize_t outpos;\r
-    PyUnicodeObject *v;\r
-    Py_UNICODE *p;\r
-    const char *end;\r
-    const char *reason;\r
-    PyObject *errorHandler = NULL;\r
-    PyObject *exc = NULL;\r
-\r
-#ifdef Py_UNICODE_WIDE\r
-    Py_UNICODE unimax = PyUnicode_GetMax();\r
-#endif\r
-\r
-    /* XXX overflow detection missing */\r
-    v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE);\r
-    if (v == NULL)\r
-        goto onError;\r
-    if (PyUnicode_GetSize((PyObject *)v) == 0)\r
-        return (PyObject *)v;\r
-    p = PyUnicode_AS_UNICODE(v);\r
-    end = s + size;\r
-\r
-    while (s < end) {\r
-        if (end-s < Py_UNICODE_SIZE) {\r
-            endinpos = end-starts;\r
-            reason = "truncated input";\r
-            goto error;\r
-        }\r
-        memcpy(p, s, sizeof(Py_UNICODE));\r
-#ifdef Py_UNICODE_WIDE\r
-        /* We have to sanity check the raw data, otherwise doom looms for\r
-           some malformed UCS-4 data. */\r
-        if (*p > unimax || *p < 0) {\r
-            endinpos = s - starts + Py_UNICODE_SIZE;\r
-            reason = "illegal code point (> 0x10FFFF)";\r
-            goto error;\r
-        }\r
-#endif\r
-        p++;\r
-        s += Py_UNICODE_SIZE;\r
-        continue;\r
-\r
-  error:\r
-        startinpos = s - starts;\r
-        outpos = p - PyUnicode_AS_UNICODE(v);\r
-        if (unicode_decode_call_errorhandler(\r
-                errors, &errorHandler,\r
-                "unicode_internal", reason,\r
-                starts, size, &startinpos, &endinpos, &exc, &s,\r
-                &v, &outpos, &p)) {\r
-            goto onError;\r
-        }\r
-    }\r
-\r
-    if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0)\r
-        goto onError;\r
-    Py_XDECREF(errorHandler);\r
-    Py_XDECREF(exc);\r
-    return (PyObject *)v;\r
-\r
-  onError:\r
-    Py_XDECREF(v);\r
-    Py_XDECREF(errorHandler);\r
-    Py_XDECREF(exc);\r
-    return NULL;\r
-}\r
-\r
-/* --- Latin-1 Codec ------------------------------------------------------ */\r
-\r
-PyObject *PyUnicode_DecodeLatin1(const char *s,\r
-                                 Py_ssize_t size,\r
-                                 const char *errors)\r
-{\r
-    PyUnicodeObject *v;\r
-    Py_UNICODE *p;\r
-\r
-    /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */\r
-    if (size == 1) {\r
-        Py_UNICODE r = *(unsigned char*)s;\r
-        return PyUnicode_FromUnicode(&r, 1);\r
-    }\r
-\r
-    v = _PyUnicode_New(size);\r
-    if (v == NULL)\r
-        goto onError;\r
-    if (size == 0)\r
-        return (PyObject *)v;\r
-    p = PyUnicode_AS_UNICODE(v);\r
-    while (size-- > 0)\r
-        *p++ = (unsigned char)*s++;\r
-    return (PyObject *)v;\r
-\r
-  onError:\r
-    Py_XDECREF(v);\r
-    return NULL;\r
-}\r
-\r
-/* create or adjust a UnicodeEncodeError */\r
-static void make_encode_exception(PyObject **exceptionObject,\r
-                                  const char *encoding,\r
-                                  const Py_UNICODE *unicode, Py_ssize_t size,\r
-                                  Py_ssize_t startpos, Py_ssize_t endpos,\r
-                                  const char *reason)\r
-{\r
-    if (*exceptionObject == NULL) {\r
-        *exceptionObject = PyUnicodeEncodeError_Create(\r
-            encoding, unicode, size, startpos, endpos, reason);\r
-    }\r
-    else {\r
-        if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos))\r
-            goto onError;\r
-        if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos))\r
-            goto onError;\r
-        if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason))\r
-            goto onError;\r
-        return;\r
-      onError:\r
-        Py_CLEAR(*exceptionObject);\r
-    }\r
-}\r
-\r
-/* raises a UnicodeEncodeError */\r
-static void raise_encode_exception(PyObject **exceptionObject,\r
-                                   const char *encoding,\r
-                                   const Py_UNICODE *unicode, Py_ssize_t size,\r
-                                   Py_ssize_t startpos, Py_ssize_t endpos,\r
-                                   const char *reason)\r
-{\r
-    make_encode_exception(exceptionObject,\r
-                          encoding, unicode, size, startpos, endpos, reason);\r
-    if (*exceptionObject != NULL)\r
-        PyCodec_StrictErrors(*exceptionObject);\r
-}\r
-\r
-/* error handling callback helper:\r
-   build arguments, call the callback and check the arguments,\r
-   put the result into newpos and return the replacement string, which\r
-   has to be freed by the caller */\r
-static PyObject *unicode_encode_call_errorhandler(const char *errors,\r
-                                                  PyObject **errorHandler,\r
-                                                  const char *encoding, const char *reason,\r
-                                                  const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject,\r
-                                                  Py_ssize_t startpos, Py_ssize_t endpos,\r
-                                                  Py_ssize_t *newpos)\r
-{\r
-    static char *argparse = "O!n;encoding error handler must return (unicode, int) tuple";\r
-\r
-    PyObject *restuple;\r
-    PyObject *resunicode;\r
-\r
-    if (*errorHandler == NULL) {\r
-        *errorHandler = PyCodec_LookupError(errors);\r
-        if (*errorHandler == NULL)\r
-            return NULL;\r
-    }\r
-\r
-    make_encode_exception(exceptionObject,\r
-                          encoding, unicode, size, startpos, endpos, reason);\r
-    if (*exceptionObject == NULL)\r
-        return NULL;\r
-\r
-    restuple = PyObject_CallFunctionObjArgs(\r
-        *errorHandler, *exceptionObject, NULL);\r
-    if (restuple == NULL)\r
-        return NULL;\r
-    if (!PyTuple_Check(restuple)) {\r
-        PyErr_SetString(PyExc_TypeError, &argparse[4]);\r
-        Py_DECREF(restuple);\r
-        return NULL;\r
-    }\r
-    if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type,\r
-                          &resunicode, newpos)) {\r
-        Py_DECREF(restuple);\r
-        return NULL;\r
-    }\r
-    if (*newpos<0)\r
-        *newpos = size+*newpos;\r
-    if (*newpos<0 || *newpos>size) {\r
-        PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos);\r
-        Py_DECREF(restuple);\r
-        return NULL;\r
-    }\r
-    Py_INCREF(resunicode);\r
-    Py_DECREF(restuple);\r
-    return resunicode;\r
-}\r
-\r
-static PyObject *unicode_encode_ucs1(const Py_UNICODE *p,\r
-                                     Py_ssize_t size,\r
-                                     const char *errors,\r
-                                     int limit)\r
-{\r
-    /* output object */\r
-    PyObject *res;\r
-    /* pointers to the beginning and end+1 of input */\r
-    const Py_UNICODE *startp = p;\r
-    const Py_UNICODE *endp = p + size;\r
-    /* pointer to the beginning of the unencodable characters */\r
-    /* const Py_UNICODE *badp = NULL; */\r
-    /* pointer into the output */\r
-    char *str;\r
-    /* current output position */\r
-    Py_ssize_t respos = 0;\r
-    Py_ssize_t ressize;\r
-    const char *encoding = (limit == 256) ? "latin-1" : "ascii";\r
-    const char *reason = (limit == 256) ? "ordinal not in range(256)" : "ordinal not in range(128)";\r
-    PyObject *errorHandler = NULL;\r
-    PyObject *exc = NULL;\r
-    /* the following variable is used for caching string comparisons\r
-     * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */\r
-    int known_errorHandler = -1;\r
-\r
-    /* allocate enough for a simple encoding without\r
-       replacements, if we need more, we'll resize */\r
-    res = PyString_FromStringAndSize(NULL, size);\r
-    if (res == NULL)\r
-        goto onError;\r
-    if (size == 0)\r
-        return res;\r
-    str = PyString_AS_STRING(res);\r
-    ressize = size;\r
-\r
-    while (p<endp) {\r
-        Py_UNICODE c = *p;\r
-\r
-        /* can we encode this? */\r
-        if (c<limit) {\r
-            /* no overflow check, because we know that the space is enough */\r
-            *str++ = (char)c;\r
-            ++p;\r
-        }\r
-        else {\r
-            Py_ssize_t unicodepos = p-startp;\r
-            Py_ssize_t requiredsize;\r
-            PyObject *repunicode;\r
-            Py_ssize_t repsize;\r
-            Py_ssize_t newpos;\r
-            Py_ssize_t respos;\r
-            Py_UNICODE *uni2;\r
-            /* startpos for collecting unencodable chars */\r
-            const Py_UNICODE *collstart = p;\r
-            const Py_UNICODE *collend = p;\r
-            /* find all unecodable characters */\r
-            while ((collend < endp) && ((*collend) >= limit))\r
-                ++collend;\r
-            /* cache callback name lookup (if not done yet, i.e. it's the first error) */\r
-            if (known_errorHandler==-1) {\r
-                if ((errors==NULL) || (!strcmp(errors, "strict")))\r
-                    known_errorHandler = 1;\r
-                else if (!strcmp(errors, "replace"))\r
-                    known_errorHandler = 2;\r
-                else if (!strcmp(errors, "ignore"))\r
-                    known_errorHandler = 3;\r
-                else if (!strcmp(errors, "xmlcharrefreplace"))\r
-                    known_errorHandler = 4;\r
-                else\r
-                    known_errorHandler = 0;\r
-            }\r
-            switch (known_errorHandler) {\r
-            case 1: /* strict */\r
-                raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason);\r
-                goto onError;\r
-            case 2: /* replace */\r
-                while (collstart++ < collend)\r
-                    *str++ = '?'; /* fall through */\r
-            case 3: /* ignore */\r
-                p = collend;\r
-                break;\r
-            case 4: /* xmlcharrefreplace */\r
-                respos = str - PyString_AS_STRING(res);\r
-                /* determine replacement size (temporarily (mis)uses p) */\r
-                requiredsize = respos;\r
-                for (p = collstart; p < collend;) {\r
-                    Py_UCS4 ch = _Py_UNICODE_NEXT(p, collend);\r
-                    Py_ssize_t incr;\r
-                    if (ch < 10)\r
-                        incr = 2+1+1;\r
-                    else if (ch < 100)\r
-                        incr = 2+2+1;\r
-                    else if (ch < 1000)\r
-                        incr = 2+3+1;\r
-                    else if (ch < 10000)\r
-                        incr = 2+4+1;\r
-                    else if (ch < 100000)\r
-                        incr = 2+5+1;\r
-                    else if (ch < 1000000)\r
-                        incr = 2+6+1;\r
-                    else\r
-                        incr = 2+7+1;\r
-                    if (requiredsize > PY_SSIZE_T_MAX - incr)\r
-                        goto overflow;\r
-                    requiredsize += incr;\r
-                }\r
-                if (requiredsize > PY_SSIZE_T_MAX - (endp - collend))\r
-                    goto overflow;\r
-                requiredsize += endp - collend;\r
-                if (requiredsize > ressize) {\r
-                    if (ressize <= PY_SSIZE_T_MAX/2 && requiredsize < 2*ressize)\r
-                        requiredsize = 2*ressize;\r
-                    if (_PyString_Resize(&res, requiredsize))\r
-                        goto onError;\r
-                    str = PyString_AS_STRING(res) + respos;\r
-                    ressize = requiredsize;\r
-                }\r
-                /* generate replacement (temporarily (mis)uses p) */\r
-                for (p = collstart; p < collend;) {\r
-                    Py_UCS4 ch = _Py_UNICODE_NEXT(p, collend);\r
-                    str += sprintf(str, "&#%d;", (int)ch);\r
-                }\r
-                p = collend;\r
-                break;\r
-            default:\r
-                repunicode = unicode_encode_call_errorhandler(errors, &errorHandler,\r
-                                                              encoding, reason, startp, size, &exc,\r
-                                                              collstart-startp, collend-startp, &newpos);\r
-                if (repunicode == NULL)\r
-                    goto onError;\r
-                /* need more space? (at least enough for what we have+the\r
-                   replacement+the rest of the string, so we won't have to\r
-                   check space for encodable characters) */\r
-                respos = str - PyString_AS_STRING(res);\r
-                repsize = PyUnicode_GET_SIZE(repunicode);\r
-                if (respos > PY_SSIZE_T_MAX - repsize)\r
-                    goto overflow;\r
-                requiredsize = respos + repsize;\r
-                if (requiredsize > PY_SSIZE_T_MAX - (endp - collend))\r
-                    goto overflow;\r
-                requiredsize += endp - collend;\r
-                if (requiredsize > ressize) {\r
-                    if (ressize <= PY_SSIZE_T_MAX/2 && requiredsize < 2*ressize)\r
-                        requiredsize = 2*ressize;\r
-                    if (_PyString_Resize(&res, requiredsize)) {\r
-                        Py_DECREF(repunicode);\r
-                        goto onError;\r
-                    }\r
-                    str = PyString_AS_STRING(res) + respos;\r
-                    ressize = requiredsize;\r
-                }\r
-                /* check if there is anything unencodable in the replacement\r
-                   and copy it to the output */\r
-                for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2, ++str) {\r
-                    c = *uni2;\r
-                    if (c >= limit) {\r
-                        raise_encode_exception(&exc, encoding, startp, size,\r
-                                               unicodepos, unicodepos+1, reason);\r
-                        Py_DECREF(repunicode);\r
-                        goto onError;\r
-                    }\r
-                    *str = (char)c;\r
-                }\r
-                p = startp + newpos;\r
-                Py_DECREF(repunicode);\r
-            }\r
-        }\r
-    }\r
-    /* Resize if we allocated to much */\r
-    respos = str - PyString_AS_STRING(res);\r
-    if (respos < ressize)\r
-        /* If this falls res will be NULL */\r
-        _PyString_Resize(&res, respos);\r
-    Py_XDECREF(errorHandler);\r
-    Py_XDECREF(exc);\r
-    return res;\r
-\r
-  overflow:\r
-    PyErr_SetString(PyExc_OverflowError,\r
-                    "encoded result is too long for a Python string");\r
-\r
-  onError:\r
-    Py_XDECREF(res);\r
-    Py_XDECREF(errorHandler);\r
-    Py_XDECREF(exc);\r
-    return NULL;\r
-}\r
-\r
-PyObject *PyUnicode_EncodeLatin1(const Py_UNICODE *p,\r
-                                 Py_ssize_t size,\r
-                                 const char *errors)\r
-{\r
-    return unicode_encode_ucs1(p, size, errors, 256);\r
-}\r
-\r
-PyObject *PyUnicode_AsLatin1String(PyObject *unicode)\r
-{\r
-    if (!PyUnicode_Check(unicode)) {\r
-        PyErr_BadArgument();\r
-        return NULL;\r
-    }\r
-    return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode),\r
-                                  PyUnicode_GET_SIZE(unicode),\r
-                                  NULL);\r
-}\r
-\r
-/* --- 7-bit ASCII Codec -------------------------------------------------- */\r
-\r
-PyObject *PyUnicode_DecodeASCII(const char *s,\r
-                                Py_ssize_t size,\r
-                                const char *errors)\r
-{\r
-    const char *starts = s;\r
-    PyUnicodeObject *v;\r
-    Py_UNICODE *p;\r
-    Py_ssize_t startinpos;\r
-    Py_ssize_t endinpos;\r
-    Py_ssize_t outpos;\r
-    const char *e;\r
-    PyObject *errorHandler = NULL;\r
-    PyObject *exc = NULL;\r
-\r
-    /* ASCII is equivalent to the first 128 ordinals in Unicode. */\r
-    if (size == 1 && *(unsigned char*)s < 128) {\r
-        Py_UNICODE r = *(unsigned char*)s;\r
-        return PyUnicode_FromUnicode(&r, 1);\r
-    }\r
-\r
-    v = _PyUnicode_New(size);\r
-    if (v == NULL)\r
-        goto onError;\r
-    if (size == 0)\r
-        return (PyObject *)v;\r
-    p = PyUnicode_AS_UNICODE(v);\r
-    e = s + size;\r
-    while (s < e) {\r
-        register unsigned char c = (unsigned char)*s;\r
-        if (c < 128) {\r
-            *p++ = c;\r
-            ++s;\r
-        }\r
-        else {\r
-            startinpos = s-starts;\r
-            endinpos = startinpos + 1;\r
-            outpos = p - (Py_UNICODE *)PyUnicode_AS_UNICODE(v);\r
-            if (unicode_decode_call_errorhandler(\r
-                    errors, &errorHandler,\r
-                    "ascii", "ordinal not in range(128)",\r
-                    starts, size, &startinpos, &endinpos, &exc, &s,\r
-                    &v, &outpos, &p))\r
-                goto onError;\r
-        }\r
-    }\r
-    if (p - PyUnicode_AS_UNICODE(v) < PyString_GET_SIZE(v))\r
-        if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0)\r
-            goto onError;\r
-    Py_XDECREF(errorHandler);\r
-    Py_XDECREF(exc);\r
-    return (PyObject *)v;\r
-\r
-  onError:\r
-    Py_XDECREF(v);\r
-    Py_XDECREF(errorHandler);\r
-    Py_XDECREF(exc);\r
-    return NULL;\r
-}\r
-\r
-PyObject *PyUnicode_EncodeASCII(const Py_UNICODE *p,\r
-                                Py_ssize_t size,\r
-                                const char *errors)\r
-{\r
-    return unicode_encode_ucs1(p, size, errors, 128);\r
-}\r
-\r
-PyObject *PyUnicode_AsASCIIString(PyObject *unicode)\r
-{\r
-    if (!PyUnicode_Check(unicode)) {\r
-        PyErr_BadArgument();\r
-        return NULL;\r
-    }\r
-    return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode),\r
-                                 PyUnicode_GET_SIZE(unicode),\r
-                                 NULL);\r
-}\r
-\r
-#if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T)\r
-\r
-/* --- MBCS codecs for Windows -------------------------------------------- */\r
-\r
-#if SIZEOF_INT < SIZEOF_SIZE_T\r
-#define NEED_RETRY\r
-#endif\r
-\r
-/* XXX This code is limited to "true" double-byte encodings, as\r
-   a) it assumes an incomplete character consists of a single byte, and\r
-   b) IsDBCSLeadByte (probably) does not work for non-DBCS multi-byte\r
-   encodings, see IsDBCSLeadByteEx documentation. */\r
-\r
-static int is_dbcs_lead_byte(const char *s, int offset)\r
-{\r
-    const char *curr = s + offset;\r
-\r
-    if (IsDBCSLeadByte(*curr)) {\r
-        const char *prev = CharPrev(s, curr);\r
-        return (prev == curr) || !IsDBCSLeadByte(*prev) || (curr - prev == 2);\r
-    }\r
-    return 0;\r
-}\r
-\r
-/*\r
- * Decode MBCS string into unicode object. If 'final' is set, converts\r
- * trailing lead-byte too. Returns consumed size if succeed, -1 otherwise.\r
- */\r
-static int decode_mbcs(PyUnicodeObject **v,\r
-                       const char *s, /* MBCS string */\r
-                       int size, /* sizeof MBCS string */\r
-                       int final)\r
-{\r
-    Py_UNICODE *p;\r
-    Py_ssize_t n = 0;\r
-    int usize = 0;\r
-\r
-    assert(size >= 0);\r
-\r
-    /* Skip trailing lead-byte unless 'final' is set */\r
-    if (!final && size >= 1 && is_dbcs_lead_byte(s, size - 1))\r
-        --size;\r
-\r
-    /* First get the size of the result */\r
-    if (size > 0) {\r
-        usize = MultiByteToWideChar(CP_ACP, 0, s, size, NULL, 0);\r
-        if (usize == 0) {\r
-            PyErr_SetFromWindowsErrWithFilename(0, NULL);\r
-            return -1;\r
-        }\r
-    }\r
-\r
-    if (*v == NULL) {\r
-        /* Create unicode object */\r
-        *v = _PyUnicode_New(usize);\r
-        if (*v == NULL)\r
-            return -1;\r
-    }\r
-    else {\r
-        /* Extend unicode object */\r
-        n = PyUnicode_GET_SIZE(*v);\r
-        if (_PyUnicode_Resize(v, n + usize) < 0)\r
-            return -1;\r
-    }\r
-\r
-    /* Do the conversion */\r
-    if (size > 0) {\r
-        p = PyUnicode_AS_UNICODE(*v) + n;\r
-        if (0 == MultiByteToWideChar(CP_ACP, 0, s, size, p, usize)) {\r
-            PyErr_SetFromWindowsErrWithFilename(0, NULL);\r
-            return -1;\r
-        }\r
-    }\r
-\r
-    return size;\r
-}\r
-\r
-PyObject *PyUnicode_DecodeMBCSStateful(const char *s,\r
-                                       Py_ssize_t size,\r
-                                       const char *errors,\r
-                                       Py_ssize_t *consumed)\r
-{\r
-    PyUnicodeObject *v = NULL;\r
-    int done;\r
-\r
-    if (consumed)\r
-        *consumed = 0;\r
-\r
-#ifdef NEED_RETRY\r
-  retry:\r
-    if (size > INT_MAX)\r
-        done = decode_mbcs(&v, s, INT_MAX, 0);\r
-    else\r
-#endif\r
-        done = decode_mbcs(&v, s, (int)size, !consumed);\r
-\r
-    if (done < 0) {\r
-        Py_XDECREF(v);\r
-        return NULL;\r
-    }\r
-\r
-    if (consumed)\r
-        *consumed += done;\r
-\r
-#ifdef NEED_RETRY\r
-    if (size > INT_MAX) {\r
-        s += done;\r
-        size -= done;\r
-        goto retry;\r
-    }\r
-#endif\r
-\r
-    return (PyObject *)v;\r
-}\r
-\r
-PyObject *PyUnicode_DecodeMBCS(const char *s,\r
-                               Py_ssize_t size,\r
-                               const char *errors)\r
-{\r
-    return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL);\r
-}\r
-\r
-/*\r
- * Convert unicode into string object (MBCS).\r
- * Returns 0 if succeed, -1 otherwise.\r
- */\r
-static int encode_mbcs(PyObject **repr,\r
-                       const Py_UNICODE *p, /* unicode */\r
-                       int size) /* size of unicode */\r
-{\r
-    int mbcssize = 0;\r
-    Py_ssize_t n = 0;\r
-\r
-    assert(size >= 0);\r
-\r
-    /* First get the size of the result */\r
-    if (size > 0) {\r
-        mbcssize = WideCharToMultiByte(CP_ACP, 0, p, size, NULL, 0, NULL, NULL);\r
-        if (mbcssize == 0) {\r
-            PyErr_SetFromWindowsErrWithFilename(0, NULL);\r
-            return -1;\r
-        }\r
-    }\r
-\r
-    if (*repr == NULL) {\r
-        /* Create string object */\r
-        *repr = PyString_FromStringAndSize(NULL, mbcssize);\r
-        if (*repr == NULL)\r
-            return -1;\r
-    }\r
-    else {\r
-        /* Extend string object */\r
-        n = PyString_Size(*repr);\r
-        if (_PyString_Resize(repr, n + mbcssize) < 0)\r
-            return -1;\r
-    }\r
-\r
-    /* Do the conversion */\r
-    if (size > 0) {\r
-        char *s = PyString_AS_STRING(*repr) + n;\r
-        if (0 == WideCharToMultiByte(CP_ACP, 0, p, size, s, mbcssize, NULL, NULL)) {\r
-            PyErr_SetFromWindowsErrWithFilename(0, NULL);\r
-            return -1;\r
-        }\r
-    }\r
-\r
-    return 0;\r
-}\r
-\r
-PyObject *PyUnicode_EncodeMBCS(const Py_UNICODE *p,\r
-                               Py_ssize_t size,\r
-                               const char *errors)\r
-{\r
-    PyObject *repr = NULL;\r
-    int ret;\r
-\r
-#ifdef NEED_RETRY\r
-  retry:\r
-    if (size > INT_MAX)\r
-        ret = encode_mbcs(&repr, p, INT_MAX);\r
-    else\r
-#endif\r
-        ret = encode_mbcs(&repr, p, (int)size);\r
-\r
-    if (ret < 0) {\r
-        Py_XDECREF(repr);\r
-        return NULL;\r
-    }\r
-\r
-#ifdef NEED_RETRY\r
-    if (size > INT_MAX) {\r
-        p += INT_MAX;\r
-        size -= INT_MAX;\r
-        goto retry;\r
-    }\r
-#endif\r
-\r
-    return repr;\r
-}\r
-\r
-PyObject *PyUnicode_AsMBCSString(PyObject *unicode)\r
-{\r
-    if (!PyUnicode_Check(unicode)) {\r
-        PyErr_BadArgument();\r
-        return NULL;\r
-    }\r
-    return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode),\r
-                                PyUnicode_GET_SIZE(unicode),\r
-                                NULL);\r
-}\r
-\r
-#undef NEED_RETRY\r
-\r
-#endif /* MS_WINDOWS */\r
-\r
-/* --- Character Mapping Codec -------------------------------------------- */\r
-\r
-PyObject *PyUnicode_DecodeCharmap(const char *s,\r
-                                  Py_ssize_t size,\r
-                                  PyObject *mapping,\r
-                                  const char *errors)\r
-{\r
-    const char *starts = s;\r
-    Py_ssize_t startinpos;\r
-    Py_ssize_t endinpos;\r
-    Py_ssize_t outpos;\r
-    const char *e;\r
-    PyUnicodeObject *v;\r
-    Py_UNICODE *p;\r
-    Py_ssize_t extrachars = 0;\r
-    PyObject *errorHandler = NULL;\r
-    PyObject *exc = NULL;\r
-    Py_UNICODE *mapstring = NULL;\r
-    Py_ssize_t maplen = 0;\r
-\r
-    /* Default to Latin-1 */\r
-    if (mapping == NULL)\r
-        return PyUnicode_DecodeLatin1(s, size, errors);\r
-\r
-    v = _PyUnicode_New(size);\r
-    if (v == NULL)\r
-        goto onError;\r
-    if (size == 0)\r
-        return (PyObject *)v;\r
-    p = PyUnicode_AS_UNICODE(v);\r
-    e = s + size;\r
-    if (PyUnicode_CheckExact(mapping)) {\r
-        mapstring = PyUnicode_AS_UNICODE(mapping);\r
-        maplen = PyUnicode_GET_SIZE(mapping);\r
-        while (s < e) {\r
-            unsigned char ch = *s;\r
-            Py_UNICODE x = 0xfffe; /* illegal value */\r
-\r
-            if (ch < maplen)\r
-                x = mapstring[ch];\r
-\r
-            if (x == 0xfffe) {\r
-                /* undefined mapping */\r
-                outpos = p-PyUnicode_AS_UNICODE(v);\r
-                startinpos = s-starts;\r
-                endinpos = startinpos+1;\r
-                if (unicode_decode_call_errorhandler(\r
-                        errors, &errorHandler,\r
-                        "charmap", "character maps to <undefined>",\r
-                        starts, size, &startinpos, &endinpos, &exc, &s,\r
-                        &v, &outpos, &p)) {\r
-                    goto onError;\r
-                }\r
-                continue;\r
-            }\r
-            *p++ = x;\r
-            ++s;\r
-        }\r
-    }\r
-    else {\r
-        while (s < e) {\r
-            unsigned char ch = *s;\r
-            PyObject *w, *x;\r
-\r
-            /* Get mapping (char ordinal -> integer, Unicode char or None) */\r
-            w = PyInt_FromLong((long)ch);\r
-            if (w == NULL)\r
-                goto onError;\r
-            x = PyObject_GetItem(mapping, w);\r
-            Py_DECREF(w);\r
-            if (x == NULL) {\r
-                if (PyErr_ExceptionMatches(PyExc_LookupError)) {\r
-                    /* No mapping found means: mapping is undefined. */\r
-                    PyErr_Clear();\r
-                    goto Undefined;\r
-                } else\r
-                    goto onError;\r
-            }\r
-\r
-            /* Apply mapping */\r
-            if (x == Py_None)\r
-                goto Undefined;\r
-            if (PyInt_Check(x)) {\r
-                long value = PyInt_AS_LONG(x);\r
-                if (value == 0xFFFE)\r
-                    goto Undefined;\r
-                if (value < 0 || value > 0x10FFFF) {\r
-                    PyErr_SetString(PyExc_TypeError,\r
-                                    "character mapping must be in range(0x110000)");\r
-                    Py_DECREF(x);\r
-                    goto onError;\r
-                }\r
-\r
-#ifndef Py_UNICODE_WIDE\r
-                if (value > 0xFFFF) {\r
-                    /* see the code for 1-n mapping below */\r
-                    if (extrachars < 2) {\r
-                        /* resize first */\r
-                        Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v);\r
-                        Py_ssize_t needed = 10 - extrachars;\r
-                        extrachars += needed;\r
-                        /* XXX overflow detection missing */\r
-                        if (_PyUnicode_Resize(&v,\r
-                                              PyUnicode_GET_SIZE(v) + needed) < 0) {\r
-                            Py_DECREF(x);\r
-                            goto onError;\r
-                        }\r
-                        p = PyUnicode_AS_UNICODE(v) + oldpos;\r
-                    }\r
-                    value -= 0x10000;\r
-                    *p++ = 0xD800 | (value >> 10);\r
-                    *p++ = 0xDC00 | (value & 0x3FF);\r
-                    extrachars -= 2;\r
-                }\r
-                else\r
-#endif\r
-                *p++ = (Py_UNICODE)value;\r
-            }\r
-            else if (PyUnicode_Check(x)) {\r
-                Py_ssize_t targetsize = PyUnicode_GET_SIZE(x);\r
-\r
-                if (targetsize == 1) {\r
-                    /* 1-1 mapping */\r
-                    Py_UNICODE value = *PyUnicode_AS_UNICODE(x);\r
-                    if (value == 0xFFFE)\r
-                        goto Undefined;\r
-                    *p++ = value;\r
-                }\r
-                else if (targetsize > 1) {\r
-                    /* 1-n mapping */\r
-                    if (targetsize > extrachars) {\r
-                        /* resize first */\r
-                        Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v);\r
-                        Py_ssize_t needed = (targetsize - extrachars) + \\r
-                            (targetsize << 2);\r
-                        extrachars += needed;\r
-                        /* XXX overflow detection missing */\r
-                        if (_PyUnicode_Resize(&v,\r
-                                              PyUnicode_GET_SIZE(v) + needed) < 0) {\r
-                            Py_DECREF(x);\r
-                            goto onError;\r
-                        }\r
-                        p = PyUnicode_AS_UNICODE(v) + oldpos;\r
-                    }\r
-                    Py_UNICODE_COPY(p,\r
-                                    PyUnicode_AS_UNICODE(x),\r
-                                    targetsize);\r
-                    p += targetsize;\r
-                    extrachars -= targetsize;\r
-                }\r
-                /* 1-0 mapping: skip the character */\r
-            }\r
-            else {\r
-                /* wrong return value */\r
-                PyErr_SetString(PyExc_TypeError,\r
-                                "character mapping must return integer, None or unicode");\r
-                Py_DECREF(x);\r
-                goto onError;\r
-            }\r
-            Py_DECREF(x);\r
-            ++s;\r
-            continue;\r
-Undefined:\r
-            /* undefined mapping */\r
-            Py_XDECREF(x);\r
-            outpos = p-PyUnicode_AS_UNICODE(v);\r
-            startinpos = s-starts;\r
-            endinpos = startinpos+1;\r
-            if (unicode_decode_call_errorhandler(\r
-                    errors, &errorHandler,\r
-                    "charmap", "character maps to <undefined>",\r
-                    starts, size, &startinpos, &endinpos, &exc, &s,\r
-                    &v, &outpos, &p)) {\r
-                goto onError;\r
-            }\r
-        }\r
-    }\r
-    if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v))\r
-        if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0)\r
-            goto onError;\r
-    Py_XDECREF(errorHandler);\r
-    Py_XDECREF(exc);\r
-    return (PyObject *)v;\r
-\r
-  onError:\r
-    Py_XDECREF(errorHandler);\r
-    Py_XDECREF(exc);\r
-    Py_XDECREF(v);\r
-    return NULL;\r
-}\r
-\r
-/* Charmap encoding: the lookup table */\r
-\r
-struct encoding_map{\r
-    PyObject_HEAD\r
-    unsigned char level1[32];\r
-    int count2, count3;\r
-    unsigned char level23[1];\r
-};\r
-\r
-static PyObject*\r
-encoding_map_size(PyObject *obj, PyObject* args)\r
-{\r
-    struct encoding_map *map = (struct encoding_map*)obj;\r
-    return PyInt_FromLong(sizeof(*map) - 1 + 16*map->count2 +\r
-                          128*map->count3);\r
-}\r
-\r
-static PyMethodDef encoding_map_methods[] = {\r
-    {"size", encoding_map_size, METH_NOARGS,\r
-     PyDoc_STR("Return the size (in bytes) of this object") },\r
-    { 0 }\r
-};\r
-\r
-static void\r
-encoding_map_dealloc(PyObject* o)\r
-{\r
-    PyObject_FREE(o);\r
-}\r
-\r
-static PyTypeObject EncodingMapType = {\r
-    PyVarObject_HEAD_INIT(NULL, 0)\r
-    "EncodingMap",          /*tp_name*/\r
-    sizeof(struct encoding_map),   /*tp_basicsize*/\r
-    0,                      /*tp_itemsize*/\r
-    /* methods */\r
-    encoding_map_dealloc,   /*tp_dealloc*/\r
-    0,                      /*tp_print*/\r
-    0,                      /*tp_getattr*/\r
-    0,                      /*tp_setattr*/\r
-    0,                      /*tp_compare*/\r
-    0,                      /*tp_repr*/\r
-    0,                      /*tp_as_number*/\r
-    0,                      /*tp_as_sequence*/\r
-    0,                      /*tp_as_mapping*/\r
-    0,                      /*tp_hash*/\r
-    0,                      /*tp_call*/\r
-    0,                      /*tp_str*/\r
-    0,                      /*tp_getattro*/\r
-    0,                      /*tp_setattro*/\r
-    0,                      /*tp_as_buffer*/\r
-    Py_TPFLAGS_DEFAULT,     /*tp_flags*/\r
-    0,                      /*tp_doc*/\r
-    0,                      /*tp_traverse*/\r
-    0,                      /*tp_clear*/\r
-    0,                      /*tp_richcompare*/\r
-    0,                      /*tp_weaklistoffset*/\r
-    0,                      /*tp_iter*/\r
-    0,                      /*tp_iternext*/\r
-    encoding_map_methods,   /*tp_methods*/\r
-    0,                      /*tp_members*/\r
-    0,                      /*tp_getset*/\r
-    0,                      /*tp_base*/\r
-    0,                      /*tp_dict*/\r
-    0,                      /*tp_descr_get*/\r
-    0,                      /*tp_descr_set*/\r
-    0,                      /*tp_dictoffset*/\r
-    0,                      /*tp_init*/\r
-    0,                      /*tp_alloc*/\r
-    0,                      /*tp_new*/\r
-    0,                      /*tp_free*/\r
-    0,                      /*tp_is_gc*/\r
-};\r
-\r
-PyObject*\r
-PyUnicode_BuildEncodingMap(PyObject* string)\r
-{\r
-    Py_UNICODE *decode;\r
-    PyObject *result;\r
-    struct encoding_map *mresult;\r
-    int i;\r
-    int need_dict = 0;\r
-    unsigned char level1[32];\r
-    unsigned char level2[512];\r
-    unsigned char *mlevel1, *mlevel2, *mlevel3;\r
-    int count2 = 0, count3 = 0;\r
-\r
-    if (!PyUnicode_Check(string) || PyUnicode_GetSize(string) != 256) {\r
-        PyErr_BadArgument();\r
-        return NULL;\r
-    }\r
-    decode = PyUnicode_AS_UNICODE(string);\r
-    memset(level1, 0xFF, sizeof level1);\r
-    memset(level2, 0xFF, sizeof level2);\r
-\r
-    /* If there isn't a one-to-one mapping of NULL to \0,\r
-       or if there are non-BMP characters, we need to use\r
-       a mapping dictionary. */\r
-    if (decode[0] != 0)\r
-        need_dict = 1;\r
-    for (i = 1; i < 256; i++) {\r
-        int l1, l2;\r
-        if (decode[i] == 0\r
-#ifdef Py_UNICODE_WIDE\r
-            || decode[i] > 0xFFFF\r
-#endif\r
-            ) {\r
-            need_dict = 1;\r
-            break;\r
-        }\r
-        if (decode[i] == 0xFFFE)\r
-            /* unmapped character */\r
-            continue;\r
-        l1 = decode[i] >> 11;\r
-        l2 = decode[i] >> 7;\r
-        if (level1[l1] == 0xFF)\r
-            level1[l1] = count2++;\r
-        if (level2[l2] == 0xFF)\r
-            level2[l2] = count3++;\r
-    }\r
-\r
-    if (count2 >= 0xFF || count3 >= 0xFF)\r
-        need_dict = 1;\r
-\r
-    if (need_dict) {\r
-        PyObject *result = PyDict_New();\r
-        PyObject *key, *value;\r
-        if (!result)\r
-            return NULL;\r
-        for (i = 0; i < 256; i++) {\r
-            value = NULL;\r
-            key = PyInt_FromLong(decode[i]);\r
-            value = PyInt_FromLong(i);\r
-            if (!key || !value)\r
-                goto failed1;\r
-            if (PyDict_SetItem(result, key, value) == -1)\r
-                goto failed1;\r
-            Py_DECREF(key);\r
-            Py_DECREF(value);\r
-        }\r
-        return result;\r
-      failed1:\r
-        Py_XDECREF(key);\r
-        Py_XDECREF(value);\r
-        Py_DECREF(result);\r
-        return NULL;\r
-    }\r
-\r
-    /* Create a three-level trie */\r
-    result = PyObject_MALLOC(sizeof(struct encoding_map) +\r
-                             16*count2 + 128*count3 - 1);\r
-    if (!result)\r
-        return PyErr_NoMemory();\r
-    PyObject_Init(result, &EncodingMapType);\r
-    mresult = (struct encoding_map*)result;\r
-    mresult->count2 = count2;\r
-    mresult->count3 = count3;\r
-    mlevel1 = mresult->level1;\r
-    mlevel2 = mresult->level23;\r
-    mlevel3 = mresult->level23 + 16*count2;\r
-    memcpy(mlevel1, level1, 32);\r
-    memset(mlevel2, 0xFF, 16*count2);\r
-    memset(mlevel3, 0, 128*count3);\r
-    count3 = 0;\r
-    for (i = 1; i < 256; i++) {\r
-        int o1, o2, o3, i2, i3;\r
-        if (decode[i] == 0xFFFE)\r
-            /* unmapped character */\r
-            continue;\r
-        o1 = decode[i]>>11;\r
-        o2 = (decode[i]>>7) & 0xF;\r
-        i2 = 16*mlevel1[o1] + o2;\r
-        if (mlevel2[i2] == 0xFF)\r
-            mlevel2[i2] = count3++;\r
-        o3 = decode[i] & 0x7F;\r
-        i3 = 128*mlevel2[i2] + o3;\r
-        mlevel3[i3] = i;\r
-    }\r
-    return result;\r
-}\r
-\r
-static int\r
-encoding_map_lookup(Py_UNICODE c, PyObject *mapping)\r
-{\r
-    struct encoding_map *map = (struct encoding_map*)mapping;\r
-    int l1 = c>>11;\r
-    int l2 = (c>>7) & 0xF;\r
-    int l3 = c & 0x7F;\r
-    int i;\r
-\r
-#ifdef Py_UNICODE_WIDE\r
-    if (c > 0xFFFF) {\r
-        return -1;\r
-    }\r
-#endif\r
-    if (c == 0)\r
-        return 0;\r
-    /* level 1*/\r
-    i = map->level1[l1];\r
-    if (i == 0xFF) {\r
-        return -1;\r
-    }\r
-    /* level 2*/\r
-    i = map->level23[16*i+l2];\r
-    if (i == 0xFF) {\r
-        return -1;\r
-    }\r
-    /* level 3 */\r
-    i = map->level23[16*map->count2 + 128*i + l3];\r
-    if (i == 0) {\r
-        return -1;\r
-    }\r
-    return i;\r
-}\r
-\r
-/* Lookup the character ch in the mapping. If the character\r
-   can't be found, Py_None is returned (or NULL, if another\r
-   error occurred). */\r
-static PyObject *charmapencode_lookup(Py_UNICODE c, PyObject *mapping)\r
-{\r
-    PyObject *w = PyInt_FromLong((long)c);\r
-    PyObject *x;\r
-\r
-    if (w == NULL)\r
-        return NULL;\r
-    x = PyObject_GetItem(mapping, w);\r
-    Py_DECREF(w);\r
-    if (x == NULL) {\r
-        if (PyErr_ExceptionMatches(PyExc_LookupError)) {\r
-            /* No mapping found means: mapping is undefined. */\r
-            PyErr_Clear();\r
-            x = Py_None;\r
-            Py_INCREF(x);\r
-            return x;\r
-        } else\r
-            return NULL;\r
-    }\r
-    else if (x == Py_None)\r
-        return x;\r
-    else if (PyInt_Check(x)) {\r
-        long value = PyInt_AS_LONG(x);\r
-        if (value < 0 || value > 255) {\r
-            PyErr_SetString(PyExc_TypeError,\r
-                            "character mapping must be in range(256)");\r
-            Py_DECREF(x);\r
-            return NULL;\r
-        }\r
-        return x;\r
-    }\r
-    else if (PyString_Check(x))\r
-        return x;\r
-    else {\r
-        /* wrong return value */\r
-        PyErr_SetString(PyExc_TypeError,\r
-                        "character mapping must return integer, None or str");\r
-        Py_DECREF(x);\r
-        return NULL;\r
-    }\r
-}\r
-\r
-static int\r
-charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize)\r
-{\r
-    Py_ssize_t outsize = PyString_GET_SIZE(*outobj);\r
-    /* exponentially overallocate to minimize reallocations */\r
-    if (requiredsize < 2*outsize)\r
-        requiredsize = 2*outsize;\r
-    if (_PyString_Resize(outobj, requiredsize)) {\r
-        return 0;\r
-    }\r
-    return 1;\r
-}\r
-\r
-typedef enum charmapencode_result {\r
-    enc_SUCCESS, enc_FAILED, enc_EXCEPTION\r
-}charmapencode_result;\r
-/* lookup the character, put the result in the output string and adjust\r
-   various state variables. Reallocate the output string if not enough\r
-   space is available. Return a new reference to the object that\r
-   was put in the output buffer, or Py_None, if the mapping was undefined\r
-   (in which case no character was written) or NULL, if a\r
-   reallocation error occurred. The caller must decref the result */\r
-static\r
-charmapencode_result charmapencode_output(Py_UNICODE c, PyObject *mapping,\r
-                                          PyObject **outobj, Py_ssize_t *outpos)\r
-{\r
-    PyObject *rep;\r
-    char *outstart;\r
-    Py_ssize_t outsize = PyString_GET_SIZE(*outobj);\r
-\r
-    if (Py_TYPE(mapping) == &EncodingMapType) {\r
-        int res = encoding_map_lookup(c, mapping);\r
-        Py_ssize_t requiredsize = *outpos+1;\r
-        if (res == -1)\r
-            return enc_FAILED;\r
-        if (outsize<requiredsize)\r
-            if (!charmapencode_resize(outobj, outpos, requiredsize))\r
-                return enc_EXCEPTION;\r
-        outstart = PyString_AS_STRING(*outobj);\r
-        outstart[(*outpos)++] = (char)res;\r
-        return enc_SUCCESS;\r
-    }\r
-\r
-    rep = charmapencode_lookup(c, mapping);\r
-    if (rep==NULL)\r
-        return enc_EXCEPTION;\r
-    else if (rep==Py_None) {\r
-        Py_DECREF(rep);\r
-        return enc_FAILED;\r
-    } else {\r
-        if (PyInt_Check(rep)) {\r
-            Py_ssize_t requiredsize = *outpos+1;\r
-            if (outsize<requiredsize)\r
-                if (!charmapencode_resize(outobj, outpos, requiredsize)) {\r
-                    Py_DECREF(rep);\r
-                    return enc_EXCEPTION;\r
-                }\r
-            outstart = PyString_AS_STRING(*outobj);\r
-            outstart[(*outpos)++] = (char)PyInt_AS_LONG(rep);\r
-        }\r
-        else {\r
-            const char *repchars = PyString_AS_STRING(rep);\r
-            Py_ssize_t repsize = PyString_GET_SIZE(rep);\r
-            Py_ssize_t requiredsize = *outpos+repsize;\r
-            if (outsize<requiredsize)\r
-                if (!charmapencode_resize(outobj, outpos, requiredsize)) {\r
-                    Py_DECREF(rep);\r
-                    return enc_EXCEPTION;\r
-                }\r
-            outstart = PyString_AS_STRING(*outobj);\r
-            memcpy(outstart + *outpos, repchars, repsize);\r
-            *outpos += repsize;\r
-        }\r
-    }\r
-    Py_DECREF(rep);\r
-    return enc_SUCCESS;\r
-}\r
-\r
-/* handle an error in PyUnicode_EncodeCharmap\r
-   Return 0 on success, -1 on error */\r
-static\r
-int charmap_encoding_error(\r
-    const Py_UNICODE *p, Py_ssize_t size, Py_ssize_t *inpos, PyObject *mapping,\r
-    PyObject **exceptionObject,\r
-    int *known_errorHandler, PyObject **errorHandler, const char *errors,\r
-    PyObject **res, Py_ssize_t *respos)\r
-{\r
-    PyObject *repunicode = NULL; /* initialize to prevent gcc warning */\r
-    Py_ssize_t repsize;\r
-    Py_ssize_t newpos;\r
-    Py_UNICODE *uni2;\r
-    /* startpos for collecting unencodable chars */\r
-    Py_ssize_t collstartpos = *inpos;\r
-    Py_ssize_t collendpos = *inpos+1;\r
-    Py_ssize_t collpos;\r
-    char *encoding = "charmap";\r
-    char *reason = "character maps to <undefined>";\r
-    charmapencode_result x;\r
-\r
-    /* find all unencodable characters */\r
-    while (collendpos < size) {\r
-        PyObject *rep;\r
-        if (Py_TYPE(mapping) == &EncodingMapType) {\r
-            int res = encoding_map_lookup(p[collendpos], mapping);\r
-            if (res != -1)\r
-                break;\r
-            ++collendpos;\r
-            continue;\r
-        }\r
-\r
-        rep = charmapencode_lookup(p[collendpos], mapping);\r
-        if (rep==NULL)\r
-            return -1;\r
-        else if (rep!=Py_None) {\r
-            Py_DECREF(rep);\r
-            break;\r
-        }\r
-        Py_DECREF(rep);\r
-        ++collendpos;\r
-    }\r
-    /* cache callback name lookup\r
-     * (if not done yet, i.e. it's the first error) */\r
-    if (*known_errorHandler==-1) {\r
-        if ((errors==NULL) || (!strcmp(errors, "strict")))\r
-            *known_errorHandler = 1;\r
-        else if (!strcmp(errors, "replace"))\r
-            *known_errorHandler = 2;\r
-        else if (!strcmp(errors, "ignore"))\r
-            *known_errorHandler = 3;\r
-        else if (!strcmp(errors, "xmlcharrefreplace"))\r
-            *known_errorHandler = 4;\r
-        else\r
-            *known_errorHandler = 0;\r
-    }\r
-    switch (*known_errorHandler) {\r
-    case 1: /* strict */\r
-        raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason);\r
-        return -1;\r
-    case 2: /* replace */\r
-        for (collpos = collstartpos; collpos<collendpos; ++collpos) {\r
-            x = charmapencode_output('?', mapping, res, respos);\r
-            if (x==enc_EXCEPTION) {\r
-                return -1;\r
-            }\r
-            else if (x==enc_FAILED) {\r
-                raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason);\r
-                return -1;\r
-            }\r
-        }\r
-        /* fall through */\r
-    case 3: /* ignore */\r
-        *inpos = collendpos;\r
-        break;\r
-    case 4: /* xmlcharrefreplace */\r
-        /* generate replacement */\r
-        for (collpos = collstartpos; collpos < collendpos;) {\r
-            char buffer[2+29+1+1];\r
-            char *cp;\r
-            Py_UCS4 ch = p[collpos++];\r
-#ifndef Py_UNICODE_WIDE\r
-            if ((0xD800 <= ch && ch <= 0xDBFF) &&\r
-                (collpos < collendpos) &&\r
-                (0xDC00 <= p[collpos] && p[collpos] <= 0xDFFF)) {\r
-                ch = ((((ch & 0x03FF) << 10) |\r
-                       ((Py_UCS4)p[collpos++] & 0x03FF)) + 0x10000);\r
-            }\r
-#endif\r
-            sprintf(buffer, "&#%d;", (int)ch);\r
-            for (cp = buffer; *cp; ++cp) {\r
-                x = charmapencode_output(*cp, mapping, res, respos);\r
-                if (x==enc_EXCEPTION)\r
-                    return -1;\r
-                else if (x==enc_FAILED) {\r
-                    raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason);\r
-                    return -1;\r
-                }\r
-            }\r
-        }\r
-        *inpos = collendpos;\r
-        break;\r
-    default:\r
-        repunicode = unicode_encode_call_errorhandler(errors, errorHandler,\r
-                                                      encoding, reason, p, size, exceptionObject,\r
-                                                      collstartpos, collendpos, &newpos);\r
-        if (repunicode == NULL)\r
-            return -1;\r
-        /* generate replacement  */\r
-        repsize = PyUnicode_GET_SIZE(repunicode);\r
-        for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) {\r
-            x = charmapencode_output(*uni2, mapping, res, respos);\r
-            if (x==enc_EXCEPTION) {\r
-                return -1;\r
-            }\r
-            else if (x==enc_FAILED) {\r
-                Py_DECREF(repunicode);\r
-                raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason);\r
-                return -1;\r
-            }\r
-        }\r
-        *inpos = newpos;\r
-        Py_DECREF(repunicode);\r
-    }\r
-    return 0;\r
-}\r
-\r
-PyObject *PyUnicode_EncodeCharmap(const Py_UNICODE *p,\r
-                                  Py_ssize_t size,\r
-                                  PyObject *mapping,\r
-                                  const char *errors)\r
-{\r
-    /* output object */\r
-    PyObject *res = NULL;\r
-    /* current input position */\r
-    Py_ssize_t inpos = 0;\r
-    /* current output position */\r
-    Py_ssize_t respos = 0;\r
-    PyObject *errorHandler = NULL;\r
-    PyObject *exc = NULL;\r
-    /* the following variable is used for caching string comparisons\r
-     * -1=not initialized, 0=unknown, 1=strict, 2=replace,\r
-     * 3=ignore, 4=xmlcharrefreplace */\r
-    int known_errorHandler = -1;\r
-\r
-    /* Default to Latin-1 */\r
-    if (mapping == NULL)\r
-        return PyUnicode_EncodeLatin1(p, size, errors);\r
-\r
-    /* allocate enough for a simple encoding without\r
-       replacements, if we need more, we'll resize */\r
-    res = PyString_FromStringAndSize(NULL, size);\r
-    if (res == NULL)\r
-        goto onError;\r
-    if (size == 0)\r
-        return res;\r
-\r
-    while (inpos<size) {\r
-        /* try to encode it */\r
-        charmapencode_result x = charmapencode_output(p[inpos], mapping, &res, &respos);\r
-        if (x==enc_EXCEPTION) /* error */\r
-            goto onError;\r
-        if (x==enc_FAILED) { /* unencodable character */\r
-            if (charmap_encoding_error(p, size, &inpos, mapping,\r
-                                       &exc,\r
-                                       &known_errorHandler, &errorHandler, errors,\r
-                                       &res, &respos)) {\r
-                goto onError;\r
-            }\r
-        }\r
-        else\r
-            /* done with this character => adjust input position */\r
-            ++inpos;\r
-    }\r
-\r
-    /* Resize if we allocated to much */\r
-    if (respos<PyString_GET_SIZE(res)) {\r
-        if (_PyString_Resize(&res, respos))\r
-            goto onError;\r
-    }\r
-    Py_XDECREF(exc);\r
-    Py_XDECREF(errorHandler);\r
-    return res;\r
-\r
-  onError:\r
-    Py_XDECREF(res);\r
-    Py_XDECREF(exc);\r
-    Py_XDECREF(errorHandler);\r
-    return NULL;\r
-}\r
-\r
-PyObject *PyUnicode_AsCharmapString(PyObject *unicode,\r
-                                    PyObject *mapping)\r
-{\r
-    if (!PyUnicode_Check(unicode) || mapping == NULL) {\r
-        PyErr_BadArgument();\r
-        return NULL;\r
-    }\r
-    return PyUnicode_EncodeCharmap(PyUnicode_AS_UNICODE(unicode),\r
-                                   PyUnicode_GET_SIZE(unicode),\r
-                                   mapping,\r
-                                   NULL);\r
-}\r
-\r
-/* create or adjust a UnicodeTranslateError */\r
-static void make_translate_exception(PyObject **exceptionObject,\r
-                                     const Py_UNICODE *unicode, Py_ssize_t size,\r
-                                     Py_ssize_t startpos, Py_ssize_t endpos,\r
-                                     const char *reason)\r
-{\r
-    if (*exceptionObject == NULL) {\r
-        *exceptionObject = PyUnicodeTranslateError_Create(\r
-            unicode, size, startpos, endpos, reason);\r
-    }\r
-    else {\r
-        if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos))\r
-            goto onError;\r
-        if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos))\r
-            goto onError;\r
-        if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason))\r
-            goto onError;\r
-        return;\r
-      onError:\r
-        Py_CLEAR(*exceptionObject);\r
-    }\r
-}\r
-\r
-/* raises a UnicodeTranslateError */\r
-static void raise_translate_exception(PyObject **exceptionObject,\r
-                                      const Py_UNICODE *unicode, Py_ssize_t size,\r
-                                      Py_ssize_t startpos, Py_ssize_t endpos,\r
-                                      const char *reason)\r
-{\r
-    make_translate_exception(exceptionObject,\r
-                             unicode, size, startpos, endpos, reason);\r
-    if (*exceptionObject != NULL)\r
-        PyCodec_StrictErrors(*exceptionObject);\r
-}\r
-\r
-/* error handling callback helper:\r
-   build arguments, call the callback and check the arguments,\r
-   put the result into newpos and return the replacement string, which\r
-   has to be freed by the caller */\r
-static PyObject *unicode_translate_call_errorhandler(const char *errors,\r
-                                                     PyObject **errorHandler,\r
-                                                     const char *reason,\r
-                                                     const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject,\r
-                                                     Py_ssize_t startpos, Py_ssize_t endpos,\r
-                                                     Py_ssize_t *newpos)\r
-{\r
-    static char *argparse = "O!n;translating error handler must return (unicode, int) tuple";\r
-\r
-    Py_ssize_t i_newpos;\r
-    PyObject *restuple;\r
-    PyObject *resunicode;\r
-\r
-    if (*errorHandler == NULL) {\r
-        *errorHandler = PyCodec_LookupError(errors);\r
-        if (*errorHandler == NULL)\r
-            return NULL;\r
-    }\r
-\r
-    make_translate_exception(exceptionObject,\r
-                             unicode, size, startpos, endpos, reason);\r
-    if (*exceptionObject == NULL)\r
-        return NULL;\r
-\r
-    restuple = PyObject_CallFunctionObjArgs(\r
-        *errorHandler, *exceptionObject, NULL);\r
-    if (restuple == NULL)\r
-        return NULL;\r
-    if (!PyTuple_Check(restuple)) {\r
-        PyErr_SetString(PyExc_TypeError, &argparse[4]);\r
-        Py_DECREF(restuple);\r
-        return NULL;\r
-    }\r
-    if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type,\r
-                          &resunicode, &i_newpos)) {\r
-        Py_DECREF(restuple);\r
-        return NULL;\r
-    }\r
-    if (i_newpos<0)\r
-        *newpos = size+i_newpos;\r
-    else\r
-        *newpos = i_newpos;\r
-    if (*newpos<0 || *newpos>size) {\r
-        PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos);\r
-        Py_DECREF(restuple);\r
-        return NULL;\r
-    }\r
-    Py_INCREF(resunicode);\r
-    Py_DECREF(restuple);\r
-    return resunicode;\r
-}\r
-\r
-/* Lookup the character ch in the mapping and put the result in result,\r
-   which must be decrefed by the caller.\r
-   Return 0 on success, -1 on error */\r
-static\r
-int charmaptranslate_lookup(Py_UNICODE c, PyObject *mapping, PyObject **result)\r
-{\r
-    PyObject *w = PyInt_FromLong((long)c);\r
-    PyObject *x;\r
-\r
-    if (w == NULL)\r
-        return -1;\r
-    x = PyObject_GetItem(mapping, w);\r
-    Py_DECREF(w);\r
-    if (x == NULL) {\r
-        if (PyErr_ExceptionMatches(PyExc_LookupError)) {\r
-            /* No mapping found means: use 1:1 mapping. */\r
-            PyErr_Clear();\r
-            *result = NULL;\r
-            return 0;\r
-        } else\r
-            return -1;\r
-    }\r
-    else if (x == Py_None) {\r
-        *result = x;\r
-        return 0;\r
-    }\r
-    else if (PyInt_Check(x)) {\r
-        long value = PyInt_AS_LONG(x);\r
-        long max = PyUnicode_GetMax();\r
-        if (value < 0 || value > max) {\r
-            PyErr_Format(PyExc_TypeError,\r
-                         "character mapping must be in range(0x%lx)", max+1);\r
-            Py_DECREF(x);\r
-            return -1;\r
-        }\r
-        *result = x;\r
-        return 0;\r
-    }\r
-    else if (PyUnicode_Check(x)) {\r
-        *result = x;\r
-        return 0;\r
-    }\r
-    else {\r
-        /* wrong return value */\r
-        PyErr_SetString(PyExc_TypeError,\r
-                        "character mapping must return integer, None or unicode");\r
-        Py_DECREF(x);\r
-        return -1;\r
-    }\r
-}\r
-/* ensure that *outobj is at least requiredsize characters long,\r
-   if not reallocate and adjust various state variables.\r
-   Return 0 on success, -1 on error */\r
-static\r
-int charmaptranslate_makespace(PyObject **outobj, Py_UNICODE **outp,\r
-                               Py_ssize_t requiredsize)\r
-{\r
-    Py_ssize_t oldsize = PyUnicode_GET_SIZE(*outobj);\r
-    if (requiredsize > oldsize) {\r
-        /* remember old output position */\r
-        Py_ssize_t outpos = *outp-PyUnicode_AS_UNICODE(*outobj);\r
-        /* exponentially overallocate to minimize reallocations */\r
-        if (requiredsize < 2 * oldsize)\r
-            requiredsize = 2 * oldsize;\r
-        if (PyUnicode_Resize(outobj, requiredsize) < 0)\r
-            return -1;\r
-        *outp = PyUnicode_AS_UNICODE(*outobj) + outpos;\r
-    }\r
-    return 0;\r
-}\r
-/* lookup the character, put the result in the output string and adjust\r
-   various state variables. Return a new reference to the object that\r
-   was put in the output buffer in *result, or Py_None, if the mapping was\r
-   undefined (in which case no character was written).\r
-   The called must decref result.\r
-   Return 0 on success, -1 on error. */\r
-static\r
-int charmaptranslate_output(const Py_UNICODE *startinp, const Py_UNICODE *curinp,\r
-                            Py_ssize_t insize, PyObject *mapping, PyObject **outobj, Py_UNICODE **outp,\r
-                            PyObject **res)\r
-{\r
-    if (charmaptranslate_lookup(*curinp, mapping, res))\r
-        return -1;\r
-    if (*res==NULL) {\r
-        /* not found => default to 1:1 mapping */\r
-        *(*outp)++ = *curinp;\r
-    }\r
-    else if (*res==Py_None)\r
-        ;\r
-    else if (PyInt_Check(*res)) {\r
-        /* no overflow check, because we know that the space is enough */\r
-        *(*outp)++ = (Py_UNICODE)PyInt_AS_LONG(*res);\r
-    }\r
-    else if (PyUnicode_Check(*res)) {\r
-        Py_ssize_t repsize = PyUnicode_GET_SIZE(*res);\r
-        if (repsize==1) {\r
-            /* no overflow check, because we know that the space is enough */\r
-            *(*outp)++ = *PyUnicode_AS_UNICODE(*res);\r
-        }\r
-        else if (repsize!=0) {\r
-            /* more than one character */\r
-            Py_ssize_t requiredsize = (*outp-PyUnicode_AS_UNICODE(*outobj)) +\r
-                (insize - (curinp-startinp)) +\r
-                repsize - 1;\r
-            if (charmaptranslate_makespace(outobj, outp, requiredsize))\r
-                return -1;\r
-            memcpy(*outp, PyUnicode_AS_UNICODE(*res), sizeof(Py_UNICODE)*repsize);\r
-            *outp += repsize;\r
-        }\r
-    }\r
-    else\r
-        return -1;\r
-    return 0;\r
-}\r
-\r
-PyObject *PyUnicode_TranslateCharmap(const Py_UNICODE *p,\r
-                                     Py_ssize_t size,\r
-                                     PyObject *mapping,\r
-                                     const char *errors)\r
-{\r
-    /* output object */\r
-    PyObject *res = NULL;\r
-    /* pointers to the beginning and end+1 of input */\r
-    const Py_UNICODE *startp = p;\r
-    const Py_UNICODE *endp = p + size;\r
-    /* pointer into the output */\r
-    Py_UNICODE *str;\r
-    /* current output position */\r
-    Py_ssize_t respos = 0;\r
-    char *reason = "character maps to <undefined>";\r
-    PyObject *errorHandler = NULL;\r
-    PyObject *exc = NULL;\r
-    /* the following variable is used for caching string comparisons\r
-     * -1=not initialized, 0=unknown, 1=strict, 2=replace,\r
-     * 3=ignore, 4=xmlcharrefreplace */\r
-    int known_errorHandler = -1;\r
-\r
-    if (mapping == NULL) {\r
-        PyErr_BadArgument();\r
-        return NULL;\r
-    }\r
-\r
-    /* allocate enough for a simple 1:1 translation without\r
-       replacements, if we need more, we'll resize */\r
-    res = PyUnicode_FromUnicode(NULL, size);\r
-    if (res == NULL)\r
-        goto onError;\r
-    if (size == 0)\r
-        return res;\r
-    str = PyUnicode_AS_UNICODE(res);\r
-\r
-    while (p<endp) {\r
-        /* try to encode it */\r
-        PyObject *x = NULL;\r
-        if (charmaptranslate_output(startp, p, size, mapping, &res, &str, &x)) {\r
-            Py_XDECREF(x);\r
-            goto onError;\r
-        }\r
-        Py_XDECREF(x);\r
-        if (x!=Py_None) /* it worked => adjust input pointer */\r
-            ++p;\r
-        else { /* untranslatable character */\r
-            PyObject *repunicode = NULL; /* initialize to prevent gcc warning */\r
-            Py_ssize_t repsize;\r
-            Py_ssize_t newpos;\r
-            Py_UNICODE *uni2;\r
-            /* startpos for collecting untranslatable chars */\r
-            const Py_UNICODE *collstart = p;\r
-            const Py_UNICODE *collend = p+1;\r
-            const Py_UNICODE *coll;\r
-\r
-            /* find all untranslatable characters */\r
-            while (collend < endp) {\r
-                if (charmaptranslate_lookup(*collend, mapping, &x))\r
-                    goto onError;\r
-                Py_XDECREF(x);\r
-                if (x!=Py_None)\r
-                    break;\r
-                ++collend;\r
-            }\r
-            /* cache callback name lookup\r
-             * (if not done yet, i.e. it's the first error) */\r
-            if (known_errorHandler==-1) {\r
-                if ((errors==NULL) || (!strcmp(errors, "strict")))\r
-                    known_errorHandler = 1;\r
-                else if (!strcmp(errors, "replace"))\r
-                    known_errorHandler = 2;\r
-                else if (!strcmp(errors, "ignore"))\r
-                    known_errorHandler = 3;\r
-                else if (!strcmp(errors, "xmlcharrefreplace"))\r
-                    known_errorHandler = 4;\r
-                else\r
-                    known_errorHandler = 0;\r
-            }\r
-            switch (known_errorHandler) {\r
-            case 1: /* strict */\r
-                raise_translate_exception(&exc, startp, size, collstart-startp, collend-startp, reason);\r
-                goto onError;\r
-            case 2: /* replace */\r
-                /* No need to check for space, this is a 1:1 replacement */\r
-                for (coll = collstart; coll<collend; ++coll)\r
-                    *str++ = '?';\r
-                /* fall through */\r
-            case 3: /* ignore */\r
-                p = collend;\r
-                break;\r
-            case 4: /* xmlcharrefreplace */\r
-                /* generate replacement (temporarily (mis)uses p) */\r
-                for (p = collstart; p < collend;) {\r
-                    char buffer[2+29+1+1];\r
-                    char *cp;\r
-                    Py_UCS4 ch = _Py_UNICODE_NEXT(p, collend);\r
-                    sprintf(buffer, "&#%d;", (int)ch);\r
-                    if (charmaptranslate_makespace(&res, &str,\r
-                                                   (str-PyUnicode_AS_UNICODE(res))+strlen(buffer)+(endp-collend)))\r
-                        goto onError;\r
-                    for (cp = buffer; *cp; ++cp)\r
-                        *str++ = *cp;\r
-                }\r
-                p = collend;\r
-                break;\r
-            default:\r
-                repunicode = unicode_translate_call_errorhandler(errors, &errorHandler,\r
-                                                                 reason, startp, size, &exc,\r
-                                                                 collstart-startp, collend-startp, &newpos);\r
-                if (repunicode == NULL)\r
-                    goto onError;\r
-                /* generate replacement  */\r
-                repsize = PyUnicode_GET_SIZE(repunicode);\r
-                if (charmaptranslate_makespace(&res, &str,\r
-                                               (str-PyUnicode_AS_UNICODE(res))+repsize+(endp-collend))) {\r
-                    Py_DECREF(repunicode);\r
-                    goto onError;\r
-                }\r
-                for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2)\r
-                    *str++ = *uni2;\r
-                p = startp + newpos;\r
-                Py_DECREF(repunicode);\r
-            }\r
-        }\r
-    }\r
-    /* Resize if we allocated to much */\r
-    respos = str-PyUnicode_AS_UNICODE(res);\r
-    if (respos<PyUnicode_GET_SIZE(res)) {\r
-        if (PyUnicode_Resize(&res, respos) < 0)\r
-            goto onError;\r
-    }\r
-    Py_XDECREF(exc);\r
-    Py_XDECREF(errorHandler);\r
-    return res;\r
-\r
-  onError:\r
-    Py_XDECREF(res);\r
-    Py_XDECREF(exc);\r
-    Py_XDECREF(errorHandler);\r
-    return NULL;\r
-}\r
-\r
-PyObject *PyUnicode_Translate(PyObject *str,\r
-                              PyObject *mapping,\r
-                              const char *errors)\r
-{\r
-    PyObject *result;\r
-\r
-    str = PyUnicode_FromObject(str);\r
-    if (str == NULL)\r
-        goto onError;\r
-    result = PyUnicode_TranslateCharmap(PyUnicode_AS_UNICODE(str),\r
-                                        PyUnicode_GET_SIZE(str),\r
-                                        mapping,\r
-                                        errors);\r
-    Py_DECREF(str);\r
-    return result;\r
-\r
-  onError:\r
-    Py_XDECREF(str);\r
-    return NULL;\r
-}\r
-\r
-/* --- Decimal Encoder ---------------------------------------------------- */\r
-\r
-int PyUnicode_EncodeDecimal(Py_UNICODE *s,\r
-                            Py_ssize_t length,\r
-                            char *output,\r
-                            const char *errors)\r
-{\r
-    Py_UNICODE *p, *end;\r
-    PyObject *errorHandler = NULL;\r
-    PyObject *exc = NULL;\r
-    const char *encoding = "decimal";\r
-    const char *reason = "invalid decimal Unicode string";\r
-    /* the following variable is used for caching string comparisons\r
-     * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */\r
-    int known_errorHandler = -1;\r
-\r
-    if (output == NULL) {\r
-        PyErr_BadArgument();\r
-        return -1;\r
-    }\r
-\r
-    p = s;\r
-    end = s + length;\r
-    while (p < end) {\r
-        register Py_UNICODE ch = *p;\r
-        int decimal;\r
-        PyObject *repunicode;\r
-        Py_ssize_t repsize;\r
-        Py_ssize_t newpos;\r
-        Py_UNICODE *uni2;\r
-        Py_UNICODE *collstart;\r
-        Py_UNICODE *collend;\r
-\r
-        if (Py_UNICODE_ISSPACE(ch)) {\r
-            *output++ = ' ';\r
-            ++p;\r
-            continue;\r
-        }\r
-        decimal = Py_UNICODE_TODECIMAL(ch);\r
-        if (decimal >= 0) {\r
-            *output++ = '0' + decimal;\r
-            ++p;\r
-            continue;\r
-        }\r
-        if (0 < ch && ch < 256) {\r
-            *output++ = (char)ch;\r
-            ++p;\r
-            continue;\r
-        }\r
-        /* All other characters are considered unencodable */\r
-        collstart = p;\r
-        for (collend = p+1; collend < end; collend++) {\r
-            if ((0 < *collend && *collend < 256) ||\r
-                Py_UNICODE_ISSPACE(*collend) ||\r
-                0 <= Py_UNICODE_TODECIMAL(*collend))\r
-                break;\r
-        }\r
-        /* cache callback name lookup\r
-         * (if not done yet, i.e. it's the first error) */\r
-        if (known_errorHandler==-1) {\r
-            if ((errors==NULL) || (!strcmp(errors, "strict")))\r
-                known_errorHandler = 1;\r
-            else if (!strcmp(errors, "replace"))\r
-                known_errorHandler = 2;\r
-            else if (!strcmp(errors, "ignore"))\r
-                known_errorHandler = 3;\r
-            else if (!strcmp(errors, "xmlcharrefreplace"))\r
-                known_errorHandler = 4;\r
-            else\r
-                known_errorHandler = 0;\r
-        }\r
-        switch (known_errorHandler) {\r
-        case 1: /* strict */\r
-            raise_encode_exception(&exc, encoding, s, length, collstart-s, collend-s, reason);\r
-            goto onError;\r
-        case 2: /* replace */\r
-            for (p = collstart; p < collend; ++p)\r
-                *output++ = '?';\r
-            /* fall through */\r
-        case 3: /* ignore */\r
-            p = collend;\r
-            break;\r
-        case 4: /* xmlcharrefreplace */\r
-            /* generate replacement (temporarily (mis)uses p) */\r
-            for (p = collstart; p < collend;) {\r
-                Py_UCS4 ch = _Py_UNICODE_NEXT(p, collend);\r
-                output += sprintf(output, "&#%d;", ch);\r
-            }\r
-            p = collend;\r
-            break;\r
-        default:\r
-            repunicode = unicode_encode_call_errorhandler(errors, &errorHandler,\r
-                                                          encoding, reason, s, length, &exc,\r
-                                                          collstart-s, collend-s, &newpos);\r
-            if (repunicode == NULL)\r
-                goto onError;\r
-            /* generate replacement  */\r
-            repsize = PyUnicode_GET_SIZE(repunicode);\r
-            for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) {\r
-                Py_UNICODE ch = *uni2;\r
-                if (Py_UNICODE_ISSPACE(ch))\r
-                    *output++ = ' ';\r
-                else {\r
-                    decimal = Py_UNICODE_TODECIMAL(ch);\r
-                    if (decimal >= 0)\r
-                        *output++ = '0' + decimal;\r
-                    else if (0 < ch && ch < 256)\r
-                        *output++ = (char)ch;\r
-                    else {\r
-                        Py_DECREF(repunicode);\r
-                        raise_encode_exception(&exc, encoding,\r
-                                               s, length, collstart-s, collend-s, reason);\r
-                        goto onError;\r
-                    }\r
-                }\r
-            }\r
-            p = s + newpos;\r
-            Py_DECREF(repunicode);\r
-        }\r
-    }\r
-    /* 0-terminate the output string */\r
-    *output++ = '\0';\r
-    Py_XDECREF(exc);\r
-    Py_XDECREF(errorHandler);\r
-    return 0;\r
-\r
-  onError:\r
-    Py_XDECREF(exc);\r
-    Py_XDECREF(errorHandler);\r
-    return -1;\r
-}\r
-\r
-/* --- Helpers ------------------------------------------------------------ */\r
-\r
-#include "stringlib/unicodedefs.h"\r
-#include "stringlib/fastsearch.h"\r
-\r
-#include "stringlib/count.h"\r
-#include "stringlib/find.h"\r
-#include "stringlib/partition.h"\r
-#include "stringlib/split.h"\r
-\r
-/* helper macro to fixup start/end slice values */\r
-#define ADJUST_INDICES(start, end, len)         \\r
-    if (end > len)                              \\r
-        end = len;                              \\r
-    else if (end < 0) {                         \\r
-        end += len;                             \\r
-        if (end < 0)                            \\r
-            end = 0;                            \\r
-    }                                           \\r
-    if (start < 0) {                            \\r
-        start += len;                           \\r
-        if (start < 0)                          \\r
-            start = 0;                          \\r
-    }\r
-\r
-Py_ssize_t PyUnicode_Count(PyObject *str,\r
-                           PyObject *substr,\r
-                           Py_ssize_t start,\r
-                           Py_ssize_t end)\r
-{\r
-    Py_ssize_t result;\r
-    PyUnicodeObject* str_obj;\r
-    PyUnicodeObject* sub_obj;\r
-\r
-    str_obj = (PyUnicodeObject*) PyUnicode_FromObject(str);\r
-    if (!str_obj)\r
-        return -1;\r
-    sub_obj = (PyUnicodeObject*) PyUnicode_FromObject(substr);\r
-    if (!sub_obj) {\r
-        Py_DECREF(str_obj);\r
-        return -1;\r
-    }\r
-\r
-    ADJUST_INDICES(start, end, str_obj->length);\r
-    result = stringlib_count(\r
-        str_obj->str + start, end - start, sub_obj->str, sub_obj->length,\r
-        PY_SSIZE_T_MAX\r
-        );\r
-\r
-    Py_DECREF(sub_obj);\r
-    Py_DECREF(str_obj);\r
-\r
-    return result;\r
-}\r
-\r
-Py_ssize_t PyUnicode_Find(PyObject *str,\r
-                          PyObject *sub,\r
-                          Py_ssize_t start,\r
-                          Py_ssize_t end,\r
-                          int direction)\r
-{\r
-    Py_ssize_t result;\r
-\r
-    str = PyUnicode_FromObject(str);\r
-    if (!str)\r
-        return -2;\r
-    sub = PyUnicode_FromObject(sub);\r
-    if (!sub) {\r
-        Py_DECREF(str);\r
-        return -2;\r
-    }\r
-\r
-    if (direction > 0)\r
-        result = stringlib_find_slice(\r
-            PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str),\r
-            PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub),\r
-            start, end\r
-            );\r
-    else\r
-        result = stringlib_rfind_slice(\r
-            PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str),\r
-            PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub),\r
-            start, end\r
-            );\r
-\r
-    Py_DECREF(str);\r
-    Py_DECREF(sub);\r
-\r
-    return result;\r
-}\r
-\r
-static\r
-int tailmatch(PyUnicodeObject *self,\r
-              PyUnicodeObject *substring,\r
-              Py_ssize_t start,\r
-              Py_ssize_t end,\r
-              int direction)\r
-{\r
-    if (substring->length == 0)\r
-        return 1;\r
-\r
-    ADJUST_INDICES(start, end, self->length);\r
-    end -= substring->length;\r
-    if (end < start)\r
-        return 0;\r
-\r
-    if (direction > 0) {\r
-        if (Py_UNICODE_MATCH(self, end, substring))\r
-            return 1;\r
-    } else {\r
-        if (Py_UNICODE_MATCH(self, start, substring))\r
-            return 1;\r
-    }\r
-\r
-    return 0;\r
-}\r
-\r
-Py_ssize_t PyUnicode_Tailmatch(PyObject *str,\r
-                               PyObject *substr,\r
-                               Py_ssize_t start,\r
-                               Py_ssize_t end,\r
-                               int direction)\r
-{\r
-    Py_ssize_t result;\r
-\r
-    str = PyUnicode_FromObject(str);\r
-    if (str == NULL)\r
-        return -1;\r
-    substr = PyUnicode_FromObject(substr);\r
-    if (substr == NULL) {\r
-        Py_DECREF(str);\r
-        return -1;\r
-    }\r
-\r
-    result = tailmatch((PyUnicodeObject *)str,\r
-                       (PyUnicodeObject *)substr,\r
-                       start, end, direction);\r
-    Py_DECREF(str);\r
-    Py_DECREF(substr);\r
-    return result;\r
-}\r
-\r
-/* Apply fixfct filter to the Unicode object self and return a\r
-   reference to the modified object */\r
-\r
-static\r
-PyObject *fixup(PyUnicodeObject *self,\r
-                int (*fixfct)(PyUnicodeObject *s))\r
-{\r
-\r
-    PyUnicodeObject *u;\r
-\r
-    u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length);\r
-    if (u == NULL)\r
-        return NULL;\r
-\r
-    Py_UNICODE_COPY(u->str, self->str, self->length);\r
-\r
-    if (!fixfct(u) && PyUnicode_CheckExact(self)) {\r
-        /* fixfct should return TRUE if it modified the buffer. If\r
-           FALSE, return a reference to the original buffer instead\r
-           (to save space, not time) */\r
-        Py_INCREF(self);\r
-        Py_DECREF(u);\r
-        return (PyObject*) self;\r
-    }\r
-    return (PyObject*) u;\r
-}\r
-\r
-static\r
-int fixupper(PyUnicodeObject *self)\r
-{\r
-    Py_ssize_t len = self->length;\r
-    Py_UNICODE *s = self->str;\r
-    int status = 0;\r
-\r
-    while (len-- > 0) {\r
-        register Py_UNICODE ch;\r
-\r
-        ch = Py_UNICODE_TOUPPER(*s);\r
-        if (ch != *s) {\r
-            status = 1;\r
-            *s = ch;\r
-        }\r
-        s++;\r
-    }\r
-\r
-    return status;\r
-}\r
-\r
-static\r
-int fixlower(PyUnicodeObject *self)\r
-{\r
-    Py_ssize_t len = self->length;\r
-    Py_UNICODE *s = self->str;\r
-    int status = 0;\r
-\r
-    while (len-- > 0) {\r
-        register Py_UNICODE ch;\r
-\r
-        ch = Py_UNICODE_TOLOWER(*s);\r
-        if (ch != *s) {\r
-            status = 1;\r
-            *s = ch;\r
-        }\r
-        s++;\r
-    }\r
-\r
-    return status;\r
-}\r
-\r
-static\r
-int fixswapcase(PyUnicodeObject *self)\r
-{\r
-    Py_ssize_t len = self->length;\r
-    Py_UNICODE *s = self->str;\r
-    int status = 0;\r
-\r
-    while (len-- > 0) {\r
-        if (Py_UNICODE_ISUPPER(*s)) {\r
-            *s = Py_UNICODE_TOLOWER(*s);\r
-            status = 1;\r
-        } else if (Py_UNICODE_ISLOWER(*s)) {\r
-            *s = Py_UNICODE_TOUPPER(*s);\r
-            status = 1;\r
-        }\r
-        s++;\r
-    }\r
-\r
-    return status;\r
-}\r
-\r
-static\r
-int fixcapitalize(PyUnicodeObject *self)\r
-{\r
-    Py_ssize_t len = self->length;\r
-    Py_UNICODE *s = self->str;\r
-    int status = 0;\r
-\r
-    if (len == 0)\r
-        return 0;\r
-    if (!Py_UNICODE_ISUPPER(*s)) {\r
-        *s = Py_UNICODE_TOUPPER(*s);\r
-        status = 1;\r
-    }\r
-    s++;\r
-    while (--len > 0) {\r
-        if (!Py_UNICODE_ISLOWER(*s)) {\r
-            *s = Py_UNICODE_TOLOWER(*s);\r
-            status = 1;\r
-        }\r
-        s++;\r
-    }\r
-    return status;\r
-}\r
-\r
-static\r
-int fixtitle(PyUnicodeObject *self)\r
-{\r
-    register Py_UNICODE *p = PyUnicode_AS_UNICODE(self);\r
-    register Py_UNICODE *e;\r
-    int previous_is_cased;\r
-\r
-    /* Shortcut for single character strings */\r
-    if (PyUnicode_GET_SIZE(self) == 1) {\r
-        Py_UNICODE ch = Py_UNICODE_TOTITLE(*p);\r
-        if (*p != ch) {\r
-            *p = ch;\r
-            return 1;\r
-        }\r
-        else\r
-            return 0;\r
-    }\r
-\r
-    e = p + PyUnicode_GET_SIZE(self);\r
-    previous_is_cased = 0;\r
-    for (; p < e; p++) {\r
-        register const Py_UNICODE ch = *p;\r
-\r
-        if (previous_is_cased)\r
-            *p = Py_UNICODE_TOLOWER(ch);\r
-        else\r
-            *p = Py_UNICODE_TOTITLE(ch);\r
-\r
-        if (Py_UNICODE_ISLOWER(ch) ||\r
-            Py_UNICODE_ISUPPER(ch) ||\r
-            Py_UNICODE_ISTITLE(ch))\r
-            previous_is_cased = 1;\r
-        else\r
-            previous_is_cased = 0;\r
-    }\r
-    return 1;\r
-}\r
-\r
-PyObject *\r
-PyUnicode_Join(PyObject *separator, PyObject *seq)\r
-{\r
-    PyObject *internal_separator = NULL;\r
-    const Py_UNICODE blank = ' ';\r
-    const Py_UNICODE *sep = &blank;\r
-    Py_ssize_t seplen = 1;\r
-    PyUnicodeObject *res = NULL; /* the result */\r
-    Py_ssize_t res_alloc = 100;  /* # allocated bytes for string in res */\r
-    Py_ssize_t res_used;         /* # used bytes */\r
-    Py_UNICODE *res_p;       /* pointer to free byte in res's string area */\r
-    PyObject *fseq;          /* PySequence_Fast(seq) */\r
-    Py_ssize_t seqlen;              /* len(fseq) -- number of items in sequence */\r
-    PyObject *item;\r
-    Py_ssize_t i;\r
-\r
-    fseq = PySequence_Fast(seq, "can only join an iterable");\r
-    if (fseq == NULL) {\r
-        return NULL;\r
-    }\r
-\r
-    /* Grrrr.  A codec may be invoked to convert str objects to\r
-     * Unicode, and so it's possible to call back into Python code\r
-     * during PyUnicode_FromObject(), and so it's possible for a sick\r
-     * codec to change the size of fseq (if seq is a list).  Therefore\r
-     * we have to keep refetching the size -- can't assume seqlen\r
-     * is invariant.\r
-     */\r
-    seqlen = PySequence_Fast_GET_SIZE(fseq);\r
-    /* If empty sequence, return u"". */\r
-    if (seqlen == 0) {\r
-        res = _PyUnicode_New(0);  /* empty sequence; return u"" */\r
-        goto Done;\r
-    }\r
-    /* If singleton sequence with an exact Unicode, return that. */\r
-    if (seqlen == 1) {\r
-        item = PySequence_Fast_GET_ITEM(fseq, 0);\r
-        if (PyUnicode_CheckExact(item)) {\r
-            Py_INCREF(item);\r
-            res = (PyUnicodeObject *)item;\r
-            goto Done;\r
-        }\r
-    }\r
-\r
-    /* At least two items to join, or one that isn't exact Unicode. */\r
-    if (seqlen > 1) {\r
-        /* Set up sep and seplen -- they're needed. */\r
-        if (separator == NULL) {\r
-            sep = &blank;\r
-            seplen = 1;\r
-        }\r
-        else {\r
-            internal_separator = PyUnicode_FromObject(separator);\r
-            if (internal_separator == NULL)\r
-                goto onError;\r
-            sep = PyUnicode_AS_UNICODE(internal_separator);\r
-            seplen = PyUnicode_GET_SIZE(internal_separator);\r
-            /* In case PyUnicode_FromObject() mutated seq. */\r
-            seqlen = PySequence_Fast_GET_SIZE(fseq);\r
-        }\r
-    }\r
-\r
-    /* Get space. */\r
-    res = _PyUnicode_New(res_alloc);\r
-    if (res == NULL)\r
-        goto onError;\r
-    res_p = PyUnicode_AS_UNICODE(res);\r
-    res_used = 0;\r
-\r
-    for (i = 0; i < seqlen; ++i) {\r
-        Py_ssize_t itemlen;\r
-        Py_ssize_t new_res_used;\r
-\r
-        item = PySequence_Fast_GET_ITEM(fseq, i);\r
-        /* Convert item to Unicode. */\r
-        if (! PyUnicode_Check(item) && ! PyString_Check(item)) {\r
-            PyErr_Format(PyExc_TypeError,\r
-                         "sequence item %zd: expected string or Unicode,"\r
-                         " %.80s found",\r
-                         i, Py_TYPE(item)->tp_name);\r
-            goto onError;\r
-        }\r
-        item = PyUnicode_FromObject(item);\r
-        if (item == NULL)\r
-            goto onError;\r
-        /* We own a reference to item from here on. */\r
-\r
-        /* In case PyUnicode_FromObject() mutated seq. */\r
-        seqlen = PySequence_Fast_GET_SIZE(fseq);\r
-\r
-        /* Make sure we have enough space for the separator and the item. */\r
-        itemlen = PyUnicode_GET_SIZE(item);\r
-        new_res_used = res_used + itemlen;\r
-        if (new_res_used < 0)\r
-            goto Overflow;\r
-        if (i < seqlen - 1) {\r
-            new_res_used += seplen;\r
-            if (new_res_used < 0)\r
-                goto Overflow;\r
-        }\r
-        if (new_res_used > res_alloc) {\r
-            /* double allocated size until it's big enough */\r
-            do {\r
-                res_alloc += res_alloc;\r
-                if (res_alloc <= 0)\r
-                    goto Overflow;\r
-            } while (new_res_used > res_alloc);\r
-            if (_PyUnicode_Resize(&res, res_alloc) < 0) {\r
-                Py_DECREF(item);\r
-                goto onError;\r
-            }\r
-            res_p = PyUnicode_AS_UNICODE(res) + res_used;\r
-        }\r
-\r
-        /* Copy item, and maybe the separator. */\r
-        Py_UNICODE_COPY(res_p, PyUnicode_AS_UNICODE(item), itemlen);\r
-        res_p += itemlen;\r
-        if (i < seqlen - 1) {\r
-            Py_UNICODE_COPY(res_p, sep, seplen);\r
-            res_p += seplen;\r
-        }\r
-        Py_DECREF(item);\r
-        res_used = new_res_used;\r
-    }\r
-\r
-    /* Shrink res to match the used area; this probably can't fail,\r
-     * but it's cheap to check.\r
-     */\r
-    if (_PyUnicode_Resize(&res, res_used) < 0)\r
-        goto onError;\r
-\r
-  Done:\r
-    Py_XDECREF(internal_separator);\r
-    Py_DECREF(fseq);\r
-    return (PyObject *)res;\r
-\r
-  Overflow:\r
-    PyErr_SetString(PyExc_OverflowError,\r
-                    "join() result is too long for a Python string");\r
-    Py_DECREF(item);\r
-    /* fall through */\r
-\r
-  onError:\r
-    Py_XDECREF(internal_separator);\r
-    Py_DECREF(fseq);\r
-    Py_XDECREF(res);\r
-    return NULL;\r
-}\r
-\r
-static\r
-PyUnicodeObject *pad(PyUnicodeObject *self,\r
-                     Py_ssize_t left,\r
-                     Py_ssize_t right,\r
-                     Py_UNICODE fill)\r
-{\r
-    PyUnicodeObject *u;\r
-\r
-    if (left < 0)\r
-        left = 0;\r
-    if (right < 0)\r
-        right = 0;\r
-\r
-    if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) {\r
-        Py_INCREF(self);\r
-        return self;\r
-    }\r
-\r
-    if (left > PY_SSIZE_T_MAX - self->length ||\r
-        right > PY_SSIZE_T_MAX - (left + self->length)) {\r
-        PyErr_SetString(PyExc_OverflowError, "padded string is too long");\r
-        return NULL;\r
-    }\r
-    u = _PyUnicode_New(left + self->length + right);\r
-    if (u) {\r
-        if (left)\r
-            Py_UNICODE_FILL(u->str, fill, left);\r
-        Py_UNICODE_COPY(u->str + left, self->str, self->length);\r
-        if (right)\r
-            Py_UNICODE_FILL(u->str + left + self->length, fill, right);\r
-    }\r
-\r
-    return u;\r
-}\r
-\r
-PyObject *PyUnicode_Splitlines(PyObject *string, int keepends)\r
-{\r
-    PyObject *list;\r
-\r
-    string = PyUnicode_FromObject(string);\r
-    if (string == NULL)\r
-        return NULL;\r
-\r
-    list = stringlib_splitlines(\r
-        (PyObject*) string, PyUnicode_AS_UNICODE(string),\r
-        PyUnicode_GET_SIZE(string), keepends);\r
-\r
-    Py_DECREF(string);\r
-    return list;\r
-}\r
-\r
-static\r
-PyObject *split(PyUnicodeObject *self,\r
-                PyUnicodeObject *substring,\r
-                Py_ssize_t maxcount)\r
-{\r
-    if (maxcount < 0)\r
-        maxcount = PY_SSIZE_T_MAX;\r
-\r
-    if (substring == NULL)\r
-        return stringlib_split_whitespace(\r
-            (PyObject*) self,  self->str, self->length, maxcount\r
-            );\r
-\r
-    return stringlib_split(\r
-        (PyObject*) self,  self->str, self->length,\r
-        substring->str, substring->length,\r
-        maxcount\r
-        );\r
-}\r
-\r
-static\r
-PyObject *rsplit(PyUnicodeObject *self,\r
-                 PyUnicodeObject *substring,\r
-                 Py_ssize_t maxcount)\r
-{\r
-    if (maxcount < 0)\r
-        maxcount = PY_SSIZE_T_MAX;\r
-\r
-    if (substring == NULL)\r
-        return stringlib_rsplit_whitespace(\r
-            (PyObject*) self,  self->str, self->length, maxcount\r
-            );\r
-\r
-    return stringlib_rsplit(\r
-        (PyObject*) self,  self->str, self->length,\r
-        substring->str, substring->length,\r
-        maxcount\r
-        );\r
-}\r
-\r
-static\r
-PyObject *replace(PyUnicodeObject *self,\r
-                  PyUnicodeObject *str1,\r
-                  PyUnicodeObject *str2,\r
-                  Py_ssize_t maxcount)\r
-{\r
-    PyUnicodeObject *u;\r
-\r
-    if (maxcount < 0)\r
-        maxcount = PY_SSIZE_T_MAX;\r
-    else if (maxcount == 0 || self->length == 0)\r
-        goto nothing;\r
-\r
-    if (str1->length == str2->length) {\r
-        Py_ssize_t i;\r
-        /* same length */\r
-        if (str1->length == 0)\r
-            goto nothing;\r
-        if (str1->length == 1) {\r
-            /* replace characters */\r
-            Py_UNICODE u1, u2;\r
-            if (!findchar(self->str, self->length, str1->str[0]))\r
-                goto nothing;\r
-            u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length);\r
-            if (!u)\r
-                return NULL;\r
-            Py_UNICODE_COPY(u->str, self->str, self->length);\r
-            u1 = str1->str[0];\r
-            u2 = str2->str[0];\r
-            for (i = 0; i < u->length; i++)\r
-                if (u->str[i] == u1) {\r
-                    if (--maxcount < 0)\r
-                        break;\r
-                    u->str[i] = u2;\r
-                }\r
-        } else {\r
-            i = stringlib_find(\r
-                self->str, self->length, str1->str, str1->length, 0\r
-                );\r
-            if (i < 0)\r
-                goto nothing;\r
-            u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length);\r
-            if (!u)\r
-                return NULL;\r
-            Py_UNICODE_COPY(u->str, self->str, self->length);\r
-\r
-            /* change everything in-place, starting with this one */\r
-            Py_UNICODE_COPY(u->str+i, str2->str, str2->length);\r
-            i += str1->length;\r
-\r
-            while ( --maxcount > 0) {\r
-                i = stringlib_find(self->str+i, self->length-i,\r
-                                   str1->str, str1->length,\r
-                                   i);\r
-                if (i == -1)\r
-                    break;\r
-                Py_UNICODE_COPY(u->str+i, str2->str, str2->length);\r
-                i += str1->length;\r
-            }\r
-        }\r
-    } else {\r
-\r
-        Py_ssize_t n, i, j;\r
-        Py_ssize_t product, new_size, delta;\r
-        Py_UNICODE *p;\r
-\r
-        /* replace strings */\r
-        n = stringlib_count(self->str, self->length, str1->str, str1->length,\r
-                            maxcount);\r
-        if (n == 0)\r
-            goto nothing;\r
-        /* new_size = self->length + n * (str2->length - str1->length)); */\r
-        delta = (str2->length - str1->length);\r
-        if (delta == 0) {\r
-            new_size = self->length;\r
-        } else {\r
-            product = n * (str2->length - str1->length);\r
-            if ((product / (str2->length - str1->length)) != n) {\r
-                PyErr_SetString(PyExc_OverflowError,\r
-                                "replace string is too long");\r
-                return NULL;\r
-            }\r
-            new_size = self->length + product;\r
-            if (new_size < 0) {\r
-                PyErr_SetString(PyExc_OverflowError,\r
-                                "replace string is too long");\r
-                return NULL;\r
-            }\r
-        }\r
-        u = _PyUnicode_New(new_size);\r
-        if (!u)\r
-            return NULL;\r
-        i = 0;\r
-        p = u->str;\r
-        if (str1->length > 0) {\r
-            while (n-- > 0) {\r
-                /* look for next match */\r
-                j = stringlib_find(self->str+i, self->length-i,\r
-                                   str1->str, str1->length,\r
-                                   i);\r
-                if (j == -1)\r
-                    break;\r
-                else if (j > i) {\r
-                    /* copy unchanged part [i:j] */\r
-                    Py_UNICODE_COPY(p, self->str+i, j-i);\r
-                    p += j - i;\r
-                }\r
-                /* copy substitution string */\r
-                if (str2->length > 0) {\r
-                    Py_UNICODE_COPY(p, str2->str, str2->length);\r
-                    p += str2->length;\r
-                }\r
-                i = j + str1->length;\r
-            }\r
-            if (i < self->length)\r
-                /* copy tail [i:] */\r
-                Py_UNICODE_COPY(p, self->str+i, self->length-i);\r
-        } else {\r
-            /* interleave */\r
-            while (n > 0) {\r
-                Py_UNICODE_COPY(p, str2->str, str2->length);\r
-                p += str2->length;\r
-                if (--n <= 0)\r
-                    break;\r
-                *p++ = self->str[i++];\r
-            }\r
-            Py_UNICODE_COPY(p, self->str+i, self->length-i);\r
-        }\r
-    }\r
-    return (PyObject *) u;\r
-\r
-  nothing:\r
-    /* nothing to replace; return original string (when possible) */\r
-    if (PyUnicode_CheckExact(self)) {\r
-        Py_INCREF(self);\r
-        return (PyObject *) self;\r
-    }\r
-    return PyUnicode_FromUnicode(self->str, self->length);\r
-}\r
-\r
-/* --- Unicode Object Methods --------------------------------------------- */\r
-\r
-PyDoc_STRVAR(title__doc__,\r
-             "S.title() -> unicode\n\\r
-\n\\r
-Return a titlecased version of S, i.e. words start with title case\n\\r
-characters, all remaining cased characters have lower case.");\r
-\r
-static PyObject*\r
-unicode_title(PyUnicodeObject *self)\r
-{\r
-    return fixup(self, fixtitle);\r
-}\r
-\r
-PyDoc_STRVAR(capitalize__doc__,\r
-             "S.capitalize() -> unicode\n\\r
-\n\\r
-Return a capitalized version of S, i.e. make the first character\n\\r
-have upper case and the rest lower case.");\r
-\r
-static PyObject*\r
-unicode_capitalize(PyUnicodeObject *self)\r
-{\r
-    return fixup(self, fixcapitalize);\r
-}\r
-\r
-#if 0\r
-PyDoc_STRVAR(capwords__doc__,\r
-             "S.capwords() -> unicode\n\\r
-\n\\r
-Apply .capitalize() to all words in S and return the result with\n\\r
-normalized whitespace (all whitespace strings are replaced by ' ').");\r
-\r
-static PyObject*\r
-unicode_capwords(PyUnicodeObject *self)\r
-{\r
-    PyObject *list;\r
-    PyObject *item;\r
-    Py_ssize_t i;\r
-\r
-    /* Split into words */\r
-    list = split(self, NULL, -1);\r
-    if (!list)\r
-        return NULL;\r
-\r
-    /* Capitalize each word */\r
-    for (i = 0; i < PyList_GET_SIZE(list); i++) {\r
-        item = fixup((PyUnicodeObject *)PyList_GET_ITEM(list, i),\r
-                     fixcapitalize);\r
-        if (item == NULL)\r
-            goto onError;\r
-        Py_DECREF(PyList_GET_ITEM(list, i));\r
-        PyList_SET_ITEM(list, i, item);\r
-    }\r
-\r
-    /* Join the words to form a new string */\r
-    item = PyUnicode_Join(NULL, list);\r
-\r
-  onError:\r
-    Py_DECREF(list);\r
-    return (PyObject *)item;\r
-}\r
-#endif\r
-\r
-/* Argument converter.  Coerces to a single unicode character */\r
-\r
-static int\r
-convert_uc(PyObject *obj, void *addr)\r
-{\r
-    Py_UNICODE *fillcharloc = (Py_UNICODE *)addr;\r
-    PyObject *uniobj;\r
-    Py_UNICODE *unistr;\r
-\r
-    uniobj = PyUnicode_FromObject(obj);\r
-    if (uniobj == NULL) {\r
-        PyErr_SetString(PyExc_TypeError,\r
-                        "The fill character cannot be converted to Unicode");\r
-        return 0;\r
-    }\r
-    if (PyUnicode_GET_SIZE(uniobj) != 1) {\r
-        PyErr_SetString(PyExc_TypeError,\r
-                        "The fill character must be exactly one character long");\r
-        Py_DECREF(uniobj);\r
-        return 0;\r
-    }\r
-    unistr = PyUnicode_AS_UNICODE(uniobj);\r
-    *fillcharloc = unistr[0];\r
-    Py_DECREF(uniobj);\r
-    return 1;\r
-}\r
-\r
-PyDoc_STRVAR(center__doc__,\r
-             "S.center(width[, fillchar]) -> unicode\n\\r
-\n\\r
-Return S centered in a Unicode string of length width. Padding is\n\\r
-done using the specified fill character (default is a space)");\r
-\r
-static PyObject *\r
-unicode_center(PyUnicodeObject *self, PyObject *args)\r
-{\r
-    Py_ssize_t marg, left;\r
-    Py_ssize_t width;\r
-    Py_UNICODE fillchar = ' ';\r
-\r
-    if (!PyArg_ParseTuple(args, "n|O&:center", &width, convert_uc, &fillchar))\r
-        return NULL;\r
-\r
-    if (self->length >= width && PyUnicode_CheckExact(self)) {\r
-        Py_INCREF(self);\r
-        return (PyObject*) self;\r
-    }\r
-\r
-    marg = width - self->length;\r
-    left = marg / 2 + (marg & width & 1);\r
-\r
-    return (PyObject*) pad(self, left, marg - left, fillchar);\r
-}\r
-\r
-#if 0\r
-\r
-/* This code should go into some future Unicode collation support\r
-   module. The basic comparison should compare ordinals on a naive\r
-   basis (this is what Java does and thus Jython too). */\r
-\r
-/* speedy UTF-16 code point order comparison */\r
-/* gleaned from: */\r
-/* http://www-4.ibm.com/software/developer/library/utf16.html?dwzone=unicode */\r
-\r
-static short utf16Fixup[32] =\r
-{\r
-    0, 0, 0, 0, 0, 0, 0, 0,\r
-    0, 0, 0, 0, 0, 0, 0, 0,\r
-    0, 0, 0, 0, 0, 0, 0, 0,\r
-    0, 0, 0, 0x2000, -0x800, -0x800, -0x800, -0x800\r
-};\r
-\r
-static int\r
-unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2)\r
-{\r
-    Py_ssize_t len1, len2;\r
-\r
-    Py_UNICODE *s1 = str1->str;\r
-    Py_UNICODE *s2 = str2->str;\r
-\r
-    len1 = str1->length;\r
-    len2 = str2->length;\r
-\r
-    while (len1 > 0 && len2 > 0) {\r
-        Py_UNICODE c1, c2;\r
-\r
-        c1 = *s1++;\r
-        c2 = *s2++;\r
-\r
-        if (c1 > (1<<11) * 26)\r
-            c1 += utf16Fixup[c1>>11];\r
-        if (c2 > (1<<11) * 26)\r
-            c2 += utf16Fixup[c2>>11];\r
-        /* now c1 and c2 are in UTF-32-compatible order */\r
-\r
-        if (c1 != c2)\r
-            return (c1 < c2) ? -1 : 1;\r
-\r
-        len1--; len2--;\r
-    }\r
-\r
-    return (len1 < len2) ? -1 : (len1 != len2);\r
-}\r
-\r
-#else\r
-\r
-static int\r
-unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2)\r
-{\r
-    register Py_ssize_t len1, len2;\r
-\r
-    Py_UNICODE *s1 = str1->str;\r
-    Py_UNICODE *s2 = str2->str;\r
-\r
-    len1 = str1->length;\r
-    len2 = str2->length;\r
-\r
-    while (len1 > 0 && len2 > 0) {\r
-        Py_UNICODE c1, c2;\r
-\r
-        c1 = *s1++;\r
-        c2 = *s2++;\r
-\r
-        if (c1 != c2)\r
-            return (c1 < c2) ? -1 : 1;\r
-\r
-        len1--; len2--;\r
-    }\r
-\r
-    return (len1 < len2) ? -1 : (len1 != len2);\r
-}\r
-\r
-#endif\r
-\r
-int PyUnicode_Compare(PyObject *left,\r
-                      PyObject *right)\r
-{\r
-    PyUnicodeObject *u = NULL, *v = NULL;\r
-    int result;\r
-\r
-    /* Coerce the two arguments */\r
-    u = (PyUnicodeObject *)PyUnicode_FromObject(left);\r
-    if (u == NULL)\r
-        goto onError;\r
-    v = (PyUnicodeObject *)PyUnicode_FromObject(right);\r
-    if (v == NULL)\r
-        goto onError;\r
-\r
-    /* Shortcut for empty or interned objects */\r
-    if (v == u) {\r
-        Py_DECREF(u);\r
-        Py_DECREF(v);\r
-        return 0;\r
-    }\r
-\r
-    result = unicode_compare(u, v);\r
-\r
-    Py_DECREF(u);\r
-    Py_DECREF(v);\r
-    return result;\r
-\r
-  onError:\r
-    Py_XDECREF(u);\r
-    Py_XDECREF(v);\r
-    return -1;\r
-}\r
-\r
-PyObject *PyUnicode_RichCompare(PyObject *left,\r
-                                PyObject *right,\r
-                                int op)\r
-{\r
-    int result;\r
-\r
-    result = PyUnicode_Compare(left, right);\r
-    if (result == -1 && PyErr_Occurred())\r
-        goto onError;\r
-\r
-    /* Convert the return value to a Boolean */\r
-    switch (op) {\r
-    case Py_EQ:\r
-        result = (result == 0);\r
-        break;\r
-    case Py_NE:\r
-        result = (result != 0);\r
-        break;\r
-    case Py_LE:\r
-        result = (result <= 0);\r
-        break;\r
-    case Py_GE:\r
-        result = (result >= 0);\r
-        break;\r
-    case Py_LT:\r
-        result = (result == -1);\r
-        break;\r
-    case Py_GT:\r
-        result = (result == 1);\r
-        break;\r
-    }\r
-    return PyBool_FromLong(result);\r
-\r
-  onError:\r
-\r
-    /* Standard case\r
-\r
-       Type errors mean that PyUnicode_FromObject() could not convert\r
-       one of the arguments (usually the right hand side) to Unicode,\r
-       ie. we can't handle the comparison request. However, it is\r
-       possible that the other object knows a comparison method, which\r
-       is why we return Py_NotImplemented to give the other object a\r
-       chance.\r
-\r
-    */\r
-    if (PyErr_ExceptionMatches(PyExc_TypeError)) {\r
-        PyErr_Clear();\r
-        Py_INCREF(Py_NotImplemented);\r
-        return Py_NotImplemented;\r
-    }\r
-    if (op != Py_EQ && op != Py_NE)\r
-        return NULL;\r
-\r
-    /* Equality comparison.\r
-\r
-       This is a special case: we silence any PyExc_UnicodeDecodeError\r
-       and instead turn it into a PyErr_UnicodeWarning.\r
-\r
-    */\r
-    if (!PyErr_ExceptionMatches(PyExc_UnicodeDecodeError))\r
-        return NULL;\r
-    PyErr_Clear();\r
-    if (PyErr_Warn(PyExc_UnicodeWarning,\r
-                   (op == Py_EQ) ?\r
-                   "Unicode equal comparison "\r
-                   "failed to convert both arguments to Unicode - "\r
-                   "interpreting them as being unequal" :\r
-                   "Unicode unequal comparison "\r
-                   "failed to convert both arguments to Unicode - "\r
-                   "interpreting them as being unequal"\r
-            ) < 0)\r
-        return NULL;\r
-    result = (op == Py_NE);\r
-    return PyBool_FromLong(result);\r
-}\r
-\r
-int PyUnicode_Contains(PyObject *container,\r
-                       PyObject *element)\r
-{\r
-    PyObject *str, *sub;\r
-    int result;\r
-\r
-    /* Coerce the two arguments */\r
-    sub = PyUnicode_FromObject(element);\r
-    if (!sub) {\r
-        return -1;\r
-    }\r
-\r
-    str = PyUnicode_FromObject(container);\r
-    if (!str) {\r
-        Py_DECREF(sub);\r
-        return -1;\r
-    }\r
-\r
-    result = stringlib_contains_obj(str, sub);\r
-\r
-    Py_DECREF(str);\r
-    Py_DECREF(sub);\r
-\r
-    return result;\r
-}\r
-\r
-/* Concat to string or Unicode object giving a new Unicode object. */\r
-\r
-PyObject *PyUnicode_Concat(PyObject *left,\r
-                           PyObject *right)\r
-{\r
-    PyUnicodeObject *u = NULL, *v = NULL, *w;\r
-\r
-    /* Coerce the two arguments */\r
-    u = (PyUnicodeObject *)PyUnicode_FromObject(left);\r
-    if (u == NULL)\r
-        goto onError;\r
-    v = (PyUnicodeObject *)PyUnicode_FromObject(right);\r
-    if (v == NULL)\r
-        goto onError;\r
-\r
-    /* Shortcuts */\r
-    if (v == unicode_empty) {\r
-        Py_DECREF(v);\r
-        return (PyObject *)u;\r
-    }\r
-    if (u == unicode_empty) {\r
-        Py_DECREF(u);\r
-        return (PyObject *)v;\r
-    }\r
-\r
-    /* Concat the two Unicode strings */\r
-    w = _PyUnicode_New(u->length + v->length);\r
-    if (w == NULL)\r
-        goto onError;\r
-    Py_UNICODE_COPY(w->str, u->str, u->length);\r
-    Py_UNICODE_COPY(w->str + u->length, v->str, v->length);\r
-\r
-    Py_DECREF(u);\r
-    Py_DECREF(v);\r
-    return (PyObject *)w;\r
-\r
-  onError:\r
-    Py_XDECREF(u);\r
-    Py_XDECREF(v);\r
-    return NULL;\r
-}\r
-\r
-PyDoc_STRVAR(count__doc__,\r
-             "S.count(sub[, start[, end]]) -> int\n\\r
-\n\\r
-Return the number of non-overlapping occurrences of substring sub in\n\\r
-Unicode string S[start:end].  Optional arguments start and end are\n\\r
-interpreted as in slice notation.");\r
-\r
-static PyObject *\r
-unicode_count(PyUnicodeObject *self, PyObject *args)\r
-{\r
-    PyUnicodeObject *substring;\r
-    Py_ssize_t start = 0;\r
-    Py_ssize_t end = PY_SSIZE_T_MAX;\r
-    PyObject *result;\r
-\r
-    if (!stringlib_parse_args_finds_unicode("count", args, &substring,\r
-                                            &start, &end))\r
-        return NULL;\r
-\r
-    ADJUST_INDICES(start, end, self->length);\r
-    result = PyInt_FromSsize_t(\r
-        stringlib_count(self->str + start, end - start,\r
-                        substring->str, substring->length,\r
-                        PY_SSIZE_T_MAX)\r
-        );\r
-\r
-    Py_DECREF(substring);\r
-\r
-    return result;\r
-}\r
-\r
-PyDoc_STRVAR(encode__doc__,\r
-             "S.encode([encoding[,errors]]) -> string or unicode\n\\r
-\n\\r
-Encodes S using the codec registered for encoding. encoding defaults\n\\r
-to the default encoding. errors may be given to set a different error\n\\r
-handling scheme. Default is 'strict' meaning that encoding errors raise\n\\r
-a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\\r
-'xmlcharrefreplace' as well as any other name registered with\n\\r
-codecs.register_error that can handle UnicodeEncodeErrors.");\r
-\r
-static PyObject *\r
-unicode_encode(PyUnicodeObject *self, PyObject *args, PyObject *kwargs)\r
-{\r
-    static char *kwlist[] = {"encoding", "errors", 0};\r
-    char *encoding = NULL;\r
-    char *errors = NULL;\r
-    PyObject *v;\r
-\r
-    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode",\r
-                                     kwlist, &encoding, &errors))\r
-        return NULL;\r
-    v = PyUnicode_AsEncodedObject((PyObject *)self, encoding, errors);\r
-    if (v == NULL)\r
-        goto onError;\r
-    if (!PyString_Check(v) && !PyUnicode_Check(v)) {\r
-        PyErr_Format(PyExc_TypeError,\r
-                     "encoder did not return a string/unicode object "\r
-                     "(type=%.400s)",\r
-                     Py_TYPE(v)->tp_name);\r
-        Py_DECREF(v);\r
-        return NULL;\r
-    }\r
-    return v;\r
-\r
-  onError:\r
-    return NULL;\r
-}\r
-\r
-PyDoc_STRVAR(decode__doc__,\r
-             "S.decode([encoding[,errors]]) -> string or unicode\n\\r
-\n\\r
-Decodes S using the codec registered for encoding. encoding defaults\n\\r
-to the default encoding. errors may be given to set a different error\n\\r
-handling scheme. Default is 'strict' meaning that encoding errors raise\n\\r
-a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\n\\r
-as well as any other name registered with codecs.register_error that is\n\\r
-able to handle UnicodeDecodeErrors.");\r
-\r
-static PyObject *\r
-unicode_decode(PyUnicodeObject *self, PyObject *args, PyObject *kwargs)\r
-{\r
-    static char *kwlist[] = {"encoding", "errors", 0};\r
-    char *encoding = NULL;\r
-    char *errors = NULL;\r
-    PyObject *v;\r
-\r
-    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:decode",\r
-                                     kwlist, &encoding, &errors))\r
-        return NULL;\r
-    v = PyUnicode_AsDecodedObject((PyObject *)self, encoding, errors);\r
-    if (v == NULL)\r
-        goto onError;\r
-    if (!PyString_Check(v) && !PyUnicode_Check(v)) {\r
-        PyErr_Format(PyExc_TypeError,\r
-                     "decoder did not return a string/unicode object "\r
-                     "(type=%.400s)",\r
-                     Py_TYPE(v)->tp_name);\r
-        Py_DECREF(v);\r
-        return NULL;\r
-    }\r
-    return v;\r
-\r
-  onError:\r
-    return NULL;\r
-}\r
-\r
-PyDoc_STRVAR(expandtabs__doc__,\r
-             "S.expandtabs([tabsize]) -> unicode\n\\r
-\n\\r
-Return a copy of S where all tab characters are expanded using spaces.\n\\r
-If tabsize is not given, a tab size of 8 characters is assumed.");\r
-\r
-static PyObject*\r
-unicode_expandtabs(PyUnicodeObject *self, PyObject *args)\r
-{\r
-    Py_UNICODE *e;\r
-    Py_UNICODE *p;\r
-    Py_UNICODE *q;\r
-    Py_UNICODE *qe;\r
-    Py_ssize_t i, j, incr;\r
-    PyUnicodeObject *u;\r
-    int tabsize = 8;\r
-\r
-    if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize))\r
-        return NULL;\r
-\r
-    /* First pass: determine size of output string */\r
-    i = 0; /* chars up to and including most recent \n or \r */\r
-    j = 0; /* chars since most recent \n or \r (use in tab calculations) */\r
-    e = self->str + self->length; /* end of input */\r
-    for (p = self->str; p < e; p++)\r
-        if (*p == '\t') {\r
-            if (tabsize > 0) {\r
-                incr = tabsize - (j % tabsize); /* cannot overflow */\r
-                if (j > PY_SSIZE_T_MAX - incr)\r
-                    goto overflow1;\r
-                j += incr;\r
-            }\r
-        }\r
-        else {\r
-            if (j > PY_SSIZE_T_MAX - 1)\r
-                goto overflow1;\r
-            j++;\r
-            if (*p == '\n' || *p == '\r') {\r
-                if (i > PY_SSIZE_T_MAX - j)\r
-                    goto overflow1;\r
-                i += j;\r
-                j = 0;\r
-            }\r
-        }\r
-\r
-    if (i > PY_SSIZE_T_MAX - j)\r
-        goto overflow1;\r
-\r
-    /* Second pass: create output string and fill it */\r
-    u = _PyUnicode_New(i + j);\r
-    if (!u)\r
-        return NULL;\r
-\r
-    j = 0; /* same as in first pass */\r
-    q = u->str; /* next output char */\r
-    qe = u->str + u->length; /* end of output */\r
-\r
-    for (p = self->str; p < e; p++)\r
-        if (*p == '\t') {\r
-            if (tabsize > 0) {\r
-                i = tabsize - (j % tabsize);\r
-                j += i;\r
-                while (i--) {\r
-                    if (q >= qe)\r
-                        goto overflow2;\r
-                    *q++ = ' ';\r
-                }\r
-            }\r
-        }\r
-        else {\r
-            if (q >= qe)\r
-                goto overflow2;\r
-            *q++ = *p;\r
-            j++;\r
-            if (*p == '\n' || *p == '\r')\r
-                j = 0;\r
-        }\r
-\r
-    return (PyObject*) u;\r
-\r
-  overflow2:\r
-    Py_DECREF(u);\r
-  overflow1:\r
-    PyErr_SetString(PyExc_OverflowError, "new string is too long");\r
-    return NULL;\r
-}\r
-\r
-PyDoc_STRVAR(find__doc__,\r
-             "S.find(sub [,start [,end]]) -> int\n\\r
-\n\\r
-Return the lowest index in S where substring sub is found,\n\\r
-such that sub is contained within S[start:end].  Optional\n\\r
-arguments start and end are interpreted as in slice notation.\n\\r
-\n\\r
-Return -1 on failure.");\r
-\r
-static PyObject *\r
-unicode_find(PyUnicodeObject *self, PyObject *args)\r
-{\r
-    PyUnicodeObject *substring;\r
-    Py_ssize_t start;\r
-    Py_ssize_t end;\r
-    Py_ssize_t result;\r
-\r
-    if (!stringlib_parse_args_finds_unicode("find", args, &substring,\r
-                                            &start, &end))\r
-        return NULL;\r
-\r
-    result = stringlib_find_slice(\r
-        PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self),\r
-        PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring),\r
-        start, end\r
-        );\r
-\r
-    Py_DECREF(substring);\r
-\r
-    return PyInt_FromSsize_t(result);\r
-}\r
-\r
-static PyObject *\r
-unicode_getitem(PyUnicodeObject *self, Py_ssize_t index)\r
-{\r
-    if (index < 0 || index >= self->length) {\r
-        PyErr_SetString(PyExc_IndexError, "string index out of range");\r
-        return NULL;\r
-    }\r
-\r
-    return (PyObject*) PyUnicode_FromUnicode(&self->str[index], 1);\r
-}\r
-\r
-static long\r
-unicode_hash(PyUnicodeObject *self)\r
-{\r
-    /* Since Unicode objects compare equal to their ASCII string\r
-       counterparts, they should use the individual character values\r
-       as basis for their hash value.  This is needed to assure that\r
-       strings and Unicode objects behave in the same way as\r
-       dictionary keys. */\r
-\r
-    register Py_ssize_t len;\r
-    register Py_UNICODE *p;\r
-    register long x;\r
-\r
-#ifdef Py_DEBUG\r
-    assert(_Py_HashSecret_Initialized);\r
-#endif\r
-    if (self->hash != -1)\r
-        return self->hash;\r
-    len = PyUnicode_GET_SIZE(self);\r
-    /*\r
-      We make the hash of the empty string be 0, rather than using\r
-      (prefix ^ suffix), since this slightly obfuscates the hash secret\r
-    */\r
-    if (len == 0) {\r
-        self->hash = 0;\r
-        return 0;\r
-    }\r
-    p = PyUnicode_AS_UNICODE(self);\r
-    x = _Py_HashSecret.prefix;\r
-    x ^= *p << 7;\r
-    while (--len >= 0)\r
-        x = (1000003*x) ^ *p++;\r
-    x ^= PyUnicode_GET_SIZE(self);\r
-    x ^= _Py_HashSecret.suffix;\r
-    if (x == -1)\r
-        x = -2;\r
-    self->hash = x;\r
-    return x;\r
-}\r
-\r
-PyDoc_STRVAR(index__doc__,\r
-             "S.index(sub [,start [,end]]) -> int\n\\r
-\n\\r
-Like S.find() but raise ValueError when the substring is not found.");\r
-\r
-static PyObject *\r
-unicode_index(PyUnicodeObject *self, PyObject *args)\r
-{\r
-    Py_ssize_t result;\r
-    PyUnicodeObject *substring;\r
-    Py_ssize_t start;\r
-    Py_ssize_t end;\r
-\r
-    if (!stringlib_parse_args_finds_unicode("index", args, &substring,\r
-                                            &start, &end))\r
-        return NULL;\r
-\r
-    result = stringlib_find_slice(\r
-        PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self),\r
-        PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring),\r
-        start, end\r
-        );\r
-\r
-    Py_DECREF(substring);\r
-\r
-    if (result < 0) {\r
-        PyErr_SetString(PyExc_ValueError, "substring not found");\r
-        return NULL;\r
-    }\r
-\r
-    return PyInt_FromSsize_t(result);\r
-}\r
-\r
-PyDoc_STRVAR(islower__doc__,\r
-             "S.islower() -> bool\n\\r
-\n\\r
-Return True if all cased characters in S are lowercase and there is\n\\r
-at least one cased character in S, False otherwise.");\r
-\r
-static PyObject*\r
-unicode_islower(PyUnicodeObject *self)\r
-{\r
-    register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self);\r
-    register const Py_UNICODE *e;\r
-    int cased;\r
-\r
-    /* Shortcut for single character strings */\r
-    if (PyUnicode_GET_SIZE(self) == 1)\r
-        return PyBool_FromLong(Py_UNICODE_ISLOWER(*p));\r
-\r
-    /* Special case for empty strings */\r
-    if (PyUnicode_GET_SIZE(self) == 0)\r
-        return PyBool_FromLong(0);\r
-\r
-    e = p + PyUnicode_GET_SIZE(self);\r
-    cased = 0;\r
-    for (; p < e; p++) {\r
-        register const Py_UNICODE ch = *p;\r
-\r
-        if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch))\r
-            return PyBool_FromLong(0);\r
-        else if (!cased && Py_UNICODE_ISLOWER(ch))\r
-            cased = 1;\r
-    }\r
-    return PyBool_FromLong(cased);\r
-}\r
-\r
-PyDoc_STRVAR(isupper__doc__,\r
-             "S.isupper() -> bool\n\\r
-\n\\r
-Return True if all cased characters in S are uppercase and there is\n\\r
-at least one cased character in S, False otherwise.");\r
-\r
-static PyObject*\r
-unicode_isupper(PyUnicodeObject *self)\r
-{\r
-    register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self);\r
-    register const Py_UNICODE *e;\r
-    int cased;\r
-\r
-    /* Shortcut for single character strings */\r
-    if (PyUnicode_GET_SIZE(self) == 1)\r
-        return PyBool_FromLong(Py_UNICODE_ISUPPER(*p) != 0);\r
-\r
-    /* Special case for empty strings */\r
-    if (PyUnicode_GET_SIZE(self) == 0)\r
-        return PyBool_FromLong(0);\r
-\r
-    e = p + PyUnicode_GET_SIZE(self);\r
-    cased = 0;\r
-    for (; p < e; p++) {\r
-        register const Py_UNICODE ch = *p;\r
-\r
-        if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch))\r
-            return PyBool_FromLong(0);\r
-        else if (!cased && Py_UNICODE_ISUPPER(ch))\r
-            cased = 1;\r
-    }\r
-    return PyBool_FromLong(cased);\r
-}\r
-\r
-PyDoc_STRVAR(istitle__doc__,\r
-             "S.istitle() -> bool\n\\r
-\n\\r
-Return True if S is a titlecased string and there is at least one\n\\r
-character in S, i.e. upper- and titlecase characters may only\n\\r
-follow uncased characters and lowercase characters only cased ones.\n\\r
-Return False otherwise.");\r
-\r
-static PyObject*\r
-unicode_istitle(PyUnicodeObject *self)\r
-{\r
-    register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self);\r
-    register const Py_UNICODE *e;\r
-    int cased, previous_is_cased;\r
-\r
-    /* Shortcut for single character strings */\r
-    if (PyUnicode_GET_SIZE(self) == 1)\r
-        return PyBool_FromLong((Py_UNICODE_ISTITLE(*p) != 0) ||\r
-                               (Py_UNICODE_ISUPPER(*p) != 0));\r
-\r
-    /* Special case for empty strings */\r
-    if (PyUnicode_GET_SIZE(self) == 0)\r
-        return PyBool_FromLong(0);\r
-\r
-    e = p + PyUnicode_GET_SIZE(self);\r
-    cased = 0;\r
-    previous_is_cased = 0;\r
-    for (; p < e; p++) {\r
-        register const Py_UNICODE ch = *p;\r
-\r
-        if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) {\r
-            if (previous_is_cased)\r
-                return PyBool_FromLong(0);\r
-            previous_is_cased = 1;\r
-            cased = 1;\r
-        }\r
-        else if (Py_UNICODE_ISLOWER(ch)) {\r
-            if (!previous_is_cased)\r
-                return PyBool_FromLong(0);\r
-            previous_is_cased = 1;\r
-            cased = 1;\r
-        }\r
-        else\r
-            previous_is_cased = 0;\r
-    }\r
-    return PyBool_FromLong(cased);\r
-}\r
-\r
-PyDoc_STRVAR(isspace__doc__,\r
-             "S.isspace() -> bool\n\\r
-\n\\r
-Return True if all characters in S are whitespace\n\\r
-and there is at least one character in S, False otherwise.");\r
-\r
-static PyObject*\r
-unicode_isspace(PyUnicodeObject *self)\r
-{\r
-    register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self);\r
-    register const Py_UNICODE *e;\r
-\r
-    /* Shortcut for single character strings */\r
-    if (PyUnicode_GET_SIZE(self) == 1 &&\r
-        Py_UNICODE_ISSPACE(*p))\r
-        return PyBool_FromLong(1);\r
-\r
-    /* Special case for empty strings */\r
-    if (PyUnicode_GET_SIZE(self) == 0)\r
-        return PyBool_FromLong(0);\r
-\r
-    e = p + PyUnicode_GET_SIZE(self);\r
-    for (; p < e; p++) {\r
-        if (!Py_UNICODE_ISSPACE(*p))\r
-            return PyBool_FromLong(0);\r
-    }\r
-    return PyBool_FromLong(1);\r
-}\r
-\r
-PyDoc_STRVAR(isalpha__doc__,\r
-             "S.isalpha() -> bool\n\\r
-\n\\r
-Return True if all characters in S are alphabetic\n\\r
-and there is at least one character in S, False otherwise.");\r
-\r
-static PyObject*\r
-unicode_isalpha(PyUnicodeObject *self)\r
-{\r
-    register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self);\r
-    register const Py_UNICODE *e;\r
-\r
-    /* Shortcut for single character strings */\r
-    if (PyUnicode_GET_SIZE(self) == 1 &&\r
-        Py_UNICODE_ISALPHA(*p))\r
-        return PyBool_FromLong(1);\r
-\r
-    /* Special case for empty strings */\r
-    if (PyUnicode_GET_SIZE(self) == 0)\r
-        return PyBool_FromLong(0);\r
-\r
-    e = p + PyUnicode_GET_SIZE(self);\r
-    for (; p < e; p++) {\r
-        if (!Py_UNICODE_ISALPHA(*p))\r
-            return PyBool_FromLong(0);\r
-    }\r
-    return PyBool_FromLong(1);\r
-}\r
-\r
-PyDoc_STRVAR(isalnum__doc__,\r
-             "S.isalnum() -> bool\n\\r
-\n\\r
-Return True if all characters in S are alphanumeric\n\\r
-and there is at least one character in S, False otherwise.");\r
-\r
-static PyObject*\r
-unicode_isalnum(PyUnicodeObject *self)\r
-{\r
-    register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self);\r
-    register const Py_UNICODE *e;\r
-\r
-    /* Shortcut for single character strings */\r
-    if (PyUnicode_GET_SIZE(self) == 1 &&\r
-        Py_UNICODE_ISALNUM(*p))\r
-        return PyBool_FromLong(1);\r
-\r
-    /* Special case for empty strings */\r
-    if (PyUnicode_GET_SIZE(self) == 0)\r
-        return PyBool_FromLong(0);\r
-\r
-    e = p + PyUnicode_GET_SIZE(self);\r
-    for (; p < e; p++) {\r
-        if (!Py_UNICODE_ISALNUM(*p))\r
-            return PyBool_FromLong(0);\r
-    }\r
-    return PyBool_FromLong(1);\r
-}\r
-\r
-PyDoc_STRVAR(isdecimal__doc__,\r
-             "S.isdecimal() -> bool\n\\r
-\n\\r
-Return True if there are only decimal characters in S,\n\\r
-False otherwise.");\r
-\r
-static PyObject*\r
-unicode_isdecimal(PyUnicodeObject *self)\r
-{\r
-    register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self);\r
-    register const Py_UNICODE *e;\r
-\r
-    /* Shortcut for single character strings */\r
-    if (PyUnicode_GET_SIZE(self) == 1 &&\r
-        Py_UNICODE_ISDECIMAL(*p))\r
-        return PyBool_FromLong(1);\r
-\r
-    /* Special case for empty strings */\r
-    if (PyUnicode_GET_SIZE(self) == 0)\r
-        return PyBool_FromLong(0);\r
-\r
-    e = p + PyUnicode_GET_SIZE(self);\r
-    for (; p < e; p++) {\r
-        if (!Py_UNICODE_ISDECIMAL(*p))\r
-            return PyBool_FromLong(0);\r
-    }\r
-    return PyBool_FromLong(1);\r
-}\r
-\r
-PyDoc_STRVAR(isdigit__doc__,\r
-             "S.isdigit() -> bool\n\\r
-\n\\r
-Return True if all characters in S are digits\n\\r
-and there is at least one character in S, False otherwise.");\r
-\r
-static PyObject*\r
-unicode_isdigit(PyUnicodeObject *self)\r
-{\r
-    register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self);\r
-    register const Py_UNICODE *e;\r
-\r
-    /* Shortcut for single character strings */\r
-    if (PyUnicode_GET_SIZE(self) == 1 &&\r
-        Py_UNICODE_ISDIGIT(*p))\r
-        return PyBool_FromLong(1);\r
-\r
-    /* Special case for empty strings */\r
-    if (PyUnicode_GET_SIZE(self) == 0)\r
-        return PyBool_FromLong(0);\r
-\r
-    e = p + PyUnicode_GET_SIZE(self);\r
-    for (; p < e; p++) {\r
-        if (!Py_UNICODE_ISDIGIT(*p))\r
-            return PyBool_FromLong(0);\r
-    }\r
-    return PyBool_FromLong(1);\r
-}\r
-\r
-PyDoc_STRVAR(isnumeric__doc__,\r
-             "S.isnumeric() -> bool\n\\r
-\n\\r
-Return True if there are only numeric characters in S,\n\\r
-False otherwise.");\r
-\r
-static PyObject*\r
-unicode_isnumeric(PyUnicodeObject *self)\r
-{\r
-    register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self);\r
-    register const Py_UNICODE *e;\r
-\r
-    /* Shortcut for single character strings */\r
-    if (PyUnicode_GET_SIZE(self) == 1 &&\r
-        Py_UNICODE_ISNUMERIC(*p))\r
-        return PyBool_FromLong(1);\r
-\r
-    /* Special case for empty strings */\r
-    if (PyUnicode_GET_SIZE(self) == 0)\r
-        return PyBool_FromLong(0);\r
-\r
-    e = p + PyUnicode_GET_SIZE(self);\r
-    for (; p < e; p++) {\r
-        if (!Py_UNICODE_ISNUMERIC(*p))\r
-            return PyBool_FromLong(0);\r
-    }\r
-    return PyBool_FromLong(1);\r
-}\r
-\r
-PyDoc_STRVAR(join__doc__,\r
-             "S.join(iterable) -> unicode\n\\r
-\n\\r
-Return a string which is the concatenation of the strings in the\n\\r
-iterable.  The separator between elements is S.");\r
-\r
-static PyObject*\r
-unicode_join(PyObject *self, PyObject *data)\r
-{\r
-    return PyUnicode_Join(self, data);\r
-}\r
-\r
-static Py_ssize_t\r
-unicode_length(PyUnicodeObject *self)\r
-{\r
-    return self->length;\r
-}\r
-\r
-PyDoc_STRVAR(ljust__doc__,\r
-             "S.ljust(width[, fillchar]) -> int\n\\r
-\n\\r
-Return S left-justified in a Unicode string of length width. Padding is\n\\r
-done using the specified fill character (default is a space).");\r
-\r
-static PyObject *\r
-unicode_ljust(PyUnicodeObject *self, PyObject *args)\r
-{\r
-    Py_ssize_t width;\r
-    Py_UNICODE fillchar = ' ';\r
-\r
-    if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar))\r
-        return NULL;\r
-\r
-    if (self->length >= width && PyUnicode_CheckExact(self)) {\r
-        Py_INCREF(self);\r
-        return (PyObject*) self;\r
-    }\r
-\r
-    return (PyObject*) pad(self, 0, width - self->length, fillchar);\r
-}\r
-\r
-PyDoc_STRVAR(lower__doc__,\r
-             "S.lower() -> unicode\n\\r
-\n\\r
-Return a copy of the string S converted to lowercase.");\r
-\r
-static PyObject*\r
-unicode_lower(PyUnicodeObject *self)\r
-{\r
-    return fixup(self, fixlower);\r
-}\r
-\r
-#define LEFTSTRIP 0\r
-#define RIGHTSTRIP 1\r
-#define BOTHSTRIP 2\r
-\r
-/* Arrays indexed by above */\r
-static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"};\r
-\r
-#define STRIPNAME(i) (stripformat[i]+3)\r
-\r
-/* externally visible for str.strip(unicode) */\r
-PyObject *\r
-_PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj)\r
-{\r
-    Py_UNICODE *s = PyUnicode_AS_UNICODE(self);\r
-    Py_ssize_t len = PyUnicode_GET_SIZE(self);\r
-    Py_UNICODE *sep = PyUnicode_AS_UNICODE(sepobj);\r
-    Py_ssize_t seplen = PyUnicode_GET_SIZE(sepobj);\r
-    Py_ssize_t i, j;\r
-\r
-    BLOOM_MASK sepmask = make_bloom_mask(sep, seplen);\r
-\r
-    i = 0;\r
-    if (striptype != RIGHTSTRIP) {\r
-        while (i < len && BLOOM_MEMBER(sepmask, s[i], sep, seplen)) {\r
-            i++;\r
-        }\r
-    }\r
-\r
-    j = len;\r
-    if (striptype != LEFTSTRIP) {\r
-        do {\r
-            j--;\r
-        } while (j >= i && BLOOM_MEMBER(sepmask, s[j], sep, seplen));\r
-        j++;\r
-    }\r
-\r
-    if (i == 0 && j == len && PyUnicode_CheckExact(self)) {\r
-        Py_INCREF(self);\r
-        return (PyObject*)self;\r
-    }\r
-    else\r
-        return PyUnicode_FromUnicode(s+i, j-i);\r
-}\r
-\r
-\r
-static PyObject *\r
-do_strip(PyUnicodeObject *self, int striptype)\r
-{\r
-    Py_UNICODE *s = PyUnicode_AS_UNICODE(self);\r
-    Py_ssize_t len = PyUnicode_GET_SIZE(self), i, j;\r
-\r
-    i = 0;\r
-    if (striptype != RIGHTSTRIP) {\r
-        while (i < len && Py_UNICODE_ISSPACE(s[i])) {\r
-            i++;\r
-        }\r
-    }\r
-\r
-    j = len;\r
-    if (striptype != LEFTSTRIP) {\r
-        do {\r
-            j--;\r
-        } while (j >= i && Py_UNICODE_ISSPACE(s[j]));\r
-        j++;\r
-    }\r
-\r
-    if (i == 0 && j == len && PyUnicode_CheckExact(self)) {\r
-        Py_INCREF(self);\r
-        return (PyObject*)self;\r
-    }\r
-    else\r
-        return PyUnicode_FromUnicode(s+i, j-i);\r
-}\r
-\r
-\r
-static PyObject *\r
-do_argstrip(PyUnicodeObject *self, int striptype, PyObject *args)\r
-{\r
-    PyObject *sep = NULL;\r
-\r
-    if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep))\r
-        return NULL;\r
-\r
-    if (sep != NULL && sep != Py_None) {\r
-        if (PyUnicode_Check(sep))\r
-            return _PyUnicode_XStrip(self, striptype, sep);\r
-        else if (PyString_Check(sep)) {\r
-            PyObject *res;\r
-            sep = PyUnicode_FromObject(sep);\r
-            if (sep==NULL)\r
-                return NULL;\r
-            res = _PyUnicode_XStrip(self, striptype, sep);\r
-            Py_DECREF(sep);\r
-            return res;\r
-        }\r
-        else {\r
-            PyErr_Format(PyExc_TypeError,\r
-                         "%s arg must be None, unicode or str",\r
-                         STRIPNAME(striptype));\r
-            return NULL;\r
-        }\r
-    }\r
-\r
-    return do_strip(self, striptype);\r
-}\r
-\r
-\r
-PyDoc_STRVAR(strip__doc__,\r
-             "S.strip([chars]) -> unicode\n\\r
-\n\\r
-Return a copy of the string S with leading and trailing\n\\r
-whitespace removed.\n\\r
-If chars is given and not None, remove characters in chars instead.\n\\r
-If chars is a str, it will be converted to unicode before stripping");\r
-\r
-static PyObject *\r
-unicode_strip(PyUnicodeObject *self, PyObject *args)\r
-{\r
-    if (PyTuple_GET_SIZE(args) == 0)\r
-        return do_strip(self, BOTHSTRIP); /* Common case */\r
-    else\r
-        return do_argstrip(self, BOTHSTRIP, args);\r
-}\r
-\r
-\r
-PyDoc_STRVAR(lstrip__doc__,\r
-             "S.lstrip([chars]) -> unicode\n\\r
-\n\\r
-Return a copy of the string S with leading whitespace removed.\n\\r
-If chars is given and not None, remove characters in chars instead.\n\\r
-If chars is a str, it will be converted to unicode before stripping");\r
-\r
-static PyObject *\r
-unicode_lstrip(PyUnicodeObject *self, PyObject *args)\r
-{\r
-    if (PyTuple_GET_SIZE(args) == 0)\r
-        return do_strip(self, LEFTSTRIP); /* Common case */\r
-    else\r
-        return do_argstrip(self, LEFTSTRIP, args);\r
-}\r
-\r
-\r
-PyDoc_STRVAR(rstrip__doc__,\r
-             "S.rstrip([chars]) -> unicode\n\\r
-\n\\r
-Return a copy of the string S with trailing whitespace removed.\n\\r
-If chars is given and not None, remove characters in chars instead.\n\\r
-If chars is a str, it will be converted to unicode before stripping");\r
-\r
-static PyObject *\r
-unicode_rstrip(PyUnicodeObject *self, PyObject *args)\r
-{\r
-    if (PyTuple_GET_SIZE(args) == 0)\r
-        return do_strip(self, RIGHTSTRIP); /* Common case */\r
-    else\r
-        return do_argstrip(self, RIGHTSTRIP, args);\r
-}\r
-\r
-\r
-static PyObject*\r
-unicode_repeat(PyUnicodeObject *str, Py_ssize_t len)\r
-{\r
-    PyUnicodeObject *u;\r
-    Py_UNICODE *p;\r
-    Py_ssize_t nchars;\r
-    size_t nbytes;\r
-\r
-    if (len < 0)\r
-        len = 0;\r
-\r
-    if (len == 1 && PyUnicode_CheckExact(str)) {\r
-        /* no repeat, return original string */\r
-        Py_INCREF(str);\r
-        return (PyObject*) str;\r
-    }\r
-\r
-    /* ensure # of chars needed doesn't overflow int and # of bytes\r
-     * needed doesn't overflow size_t\r
-     */\r
-    nchars = len * str->length;\r
-    if (len && nchars / len != str->length) {\r
-        PyErr_SetString(PyExc_OverflowError,\r
-                        "repeated string is too long");\r
-        return NULL;\r
-    }\r
-    nbytes = (nchars + 1) * sizeof(Py_UNICODE);\r
-    if (nbytes / sizeof(Py_UNICODE) != (size_t)(nchars + 1)) {\r
-        PyErr_SetString(PyExc_OverflowError,\r
-                        "repeated string is too long");\r
-        return NULL;\r
-    }\r
-    u = _PyUnicode_New(nchars);\r
-    if (!u)\r
-        return NULL;\r
-\r
-    p = u->str;\r
-\r
-    if (str->length == 1 && len > 0) {\r
-        Py_UNICODE_FILL(p, str->str[0], len);\r
-    } else {\r
-        Py_ssize_t done = 0; /* number of characters copied this far */\r
-        if (done < nchars) {\r
-            Py_UNICODE_COPY(p, str->str, str->length);\r
-            done = str->length;\r
-        }\r
-        while (done < nchars) {\r
-            Py_ssize_t n = (done <= nchars-done) ? done : nchars-done;\r
-            Py_UNICODE_COPY(p+done, p, n);\r
-            done += n;\r
-        }\r
-    }\r
-\r
-    return (PyObject*) u;\r
-}\r
-\r
-PyObject *PyUnicode_Replace(PyObject *obj,\r
-                            PyObject *subobj,\r
-                            PyObject *replobj,\r
-                            Py_ssize_t maxcount)\r
-{\r
-    PyObject *self;\r
-    PyObject *str1;\r
-    PyObject *str2;\r
-    PyObject *result;\r
-\r
-    self = PyUnicode_FromObject(obj);\r
-    if (self == NULL)\r
-        return NULL;\r
-    str1 = PyUnicode_FromObject(subobj);\r
-    if (str1 == NULL) {\r
-        Py_DECREF(self);\r
-        return NULL;\r
-    }\r
-    str2 = PyUnicode_FromObject(replobj);\r
-    if (str2 == NULL) {\r
-        Py_DECREF(self);\r
-        Py_DECREF(str1);\r
-        return NULL;\r
-    }\r
-    result = replace((PyUnicodeObject *)self,\r
-                     (PyUnicodeObject *)str1,\r
-                     (PyUnicodeObject *)str2,\r
-                     maxcount);\r
-    Py_DECREF(self);\r
-    Py_DECREF(str1);\r
-    Py_DECREF(str2);\r
-    return result;\r
-}\r
-\r
-PyDoc_STRVAR(replace__doc__,\r
-             "S.replace(old, new[, count]) -> unicode\n\\r
-\n\\r
-Return a copy of S with all occurrences of substring\n\\r
-old replaced by new.  If the optional argument count is\n\\r
-given, only the first count occurrences are replaced.");\r
-\r
-static PyObject*\r
-unicode_replace(PyUnicodeObject *self, PyObject *args)\r
-{\r
-    PyUnicodeObject *str1;\r
-    PyUnicodeObject *str2;\r
-    Py_ssize_t maxcount = -1;\r
-    PyObject *result;\r
-\r
-    if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount))\r
-        return NULL;\r
-    str1 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str1);\r
-    if (str1 == NULL)\r
-        return NULL;\r
-    str2 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str2);\r
-    if (str2 == NULL) {\r
-        Py_DECREF(str1);\r
-        return NULL;\r
-    }\r
-\r
-    result = replace(self, str1, str2, maxcount);\r
-\r
-    Py_DECREF(str1);\r
-    Py_DECREF(str2);\r
-    return result;\r
-}\r
-\r
-static\r
-PyObject *unicode_repr(PyObject *unicode)\r
-{\r
-    return unicodeescape_string(PyUnicode_AS_UNICODE(unicode),\r
-                                PyUnicode_GET_SIZE(unicode),\r
-                                1);\r
-}\r
-\r
-PyDoc_STRVAR(rfind__doc__,\r
-             "S.rfind(sub [,start [,end]]) -> int\n\\r
-\n\\r
-Return the highest index in S where substring sub is found,\n\\r
-such that sub is contained within S[start:end].  Optional\n\\r
-arguments start and end are interpreted as in slice notation.\n\\r
-\n\\r
-Return -1 on failure.");\r
-\r
-static PyObject *\r
-unicode_rfind(PyUnicodeObject *self, PyObject *args)\r
-{\r
-    PyUnicodeObject *substring;\r
-    Py_ssize_t start;\r
-    Py_ssize_t end;\r
-    Py_ssize_t result;\r
-\r
-    if (!stringlib_parse_args_finds_unicode("rfind", args, &substring,\r
-                                            &start, &end))\r
-        return NULL;\r
-\r
-    result = stringlib_rfind_slice(\r
-        PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self),\r
-        PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring),\r
-        start, end\r
-        );\r
-\r
-    Py_DECREF(substring);\r
-\r
-    return PyInt_FromSsize_t(result);\r
-}\r
-\r
-PyDoc_STRVAR(rindex__doc__,\r
-             "S.rindex(sub [,start [,end]]) -> int\n\\r
-\n\\r
-Like S.rfind() but raise ValueError when the substring is not found.");\r
-\r
-static PyObject *\r
-unicode_rindex(PyUnicodeObject *self, PyObject *args)\r
-{\r
-    PyUnicodeObject *substring;\r
-    Py_ssize_t start;\r
-    Py_ssize_t end;\r
-    Py_ssize_t result;\r
-\r
-    if (!stringlib_parse_args_finds_unicode("rindex", args, &substring,\r
-                                            &start, &end))\r
-        return NULL;\r
-\r
-    result = stringlib_rfind_slice(\r
-        PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self),\r
-        PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring),\r
-        start, end\r
-        );\r
-\r
-    Py_DECREF(substring);\r
-\r
-    if (result < 0) {\r
-        PyErr_SetString(PyExc_ValueError, "substring not found");\r
-        return NULL;\r
-    }\r
-    return PyInt_FromSsize_t(result);\r
-}\r
-\r
-PyDoc_STRVAR(rjust__doc__,\r
-             "S.rjust(width[, fillchar]) -> unicode\n\\r
-\n\\r
-Return S right-justified in a Unicode string of length width. Padding is\n\\r
-done using the specified fill character (default is a space).");\r
-\r
-static PyObject *\r
-unicode_rjust(PyUnicodeObject *self, PyObject *args)\r
-{\r
-    Py_ssize_t width;\r
-    Py_UNICODE fillchar = ' ';\r
-\r
-    if (!PyArg_ParseTuple(args, "n|O&:rjust", &width, convert_uc, &fillchar))\r
-        return NULL;\r
-\r
-    if (self->length >= width && PyUnicode_CheckExact(self)) {\r
-        Py_INCREF(self);\r
-        return (PyObject*) self;\r
-    }\r
-\r
-    return (PyObject*) pad(self, width - self->length, 0, fillchar);\r
-}\r
-\r
-static PyObject*\r
-unicode_slice(PyUnicodeObject *self, Py_ssize_t start, Py_ssize_t end)\r
-{\r
-    /* standard clamping */\r
-    if (start < 0)\r
-        start = 0;\r
-    if (end < 0)\r
-        end = 0;\r
-    if (end > self->length)\r
-        end = self->length;\r
-    if (start == 0 && end == self->length && PyUnicode_CheckExact(self)) {\r
-        /* full slice, return original string */\r
-        Py_INCREF(self);\r
-        return (PyObject*) self;\r
-    }\r
-    if (start > end)\r
-        start = end;\r
-    /* copy slice */\r
-    return (PyObject*) PyUnicode_FromUnicode(self->str + start,\r
-                                             end - start);\r
-}\r
-\r
-PyObject *PyUnicode_Split(PyObject *s,\r
-                          PyObject *sep,\r
-                          Py_ssize_t maxsplit)\r
-{\r
-    PyObject *result;\r
-\r
-    s = PyUnicode_FromObject(s);\r
-    if (s == NULL)\r
-        return NULL;\r
-    if (sep != NULL) {\r
-        sep = PyUnicode_FromObject(sep);\r
-        if (sep == NULL) {\r
-            Py_DECREF(s);\r
-            return NULL;\r
-        }\r
-    }\r
-\r
-    result = split((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit);\r
-\r
-    Py_DECREF(s);\r
-    Py_XDECREF(sep);\r
-    return result;\r
-}\r
-\r
-PyDoc_STRVAR(split__doc__,\r
-             "S.split([sep [,maxsplit]]) -> list of strings\n\\r
-\n\\r
-Return a list of the words in S, using sep as the\n\\r
-delimiter string.  If maxsplit is given, at most maxsplit\n\\r
-splits are done. If sep is not specified or is None, any\n\\r
-whitespace string is a separator and empty strings are\n\\r
-removed from the result.");\r
-\r
-static PyObject*\r
-unicode_split(PyUnicodeObject *self, PyObject *args)\r
-{\r
-    PyObject *substring = Py_None;\r
-    Py_ssize_t maxcount = -1;\r
-\r
-    if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount))\r
-        return NULL;\r
-\r
-    if (substring == Py_None)\r
-        return split(self, NULL, maxcount);\r
-    else if (PyUnicode_Check(substring))\r
-        return split(self, (PyUnicodeObject *)substring, maxcount);\r
-    else\r
-        return PyUnicode_Split((PyObject *)self, substring, maxcount);\r
-}\r
-\r
-PyObject *\r
-PyUnicode_Partition(PyObject *str_in, PyObject *sep_in)\r
-{\r
-    PyObject* str_obj;\r
-    PyObject* sep_obj;\r
-    PyObject* out;\r
-\r
-    str_obj = PyUnicode_FromObject(str_in);\r
-    if (!str_obj)\r
-        return NULL;\r
-    sep_obj = PyUnicode_FromObject(sep_in);\r
-    if (!sep_obj) {\r
-        Py_DECREF(str_obj);\r
-        return NULL;\r
-    }\r
-\r
-    out = stringlib_partition(\r
-        str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj),\r
-        sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj)\r
-        );\r
-\r
-    Py_DECREF(sep_obj);\r
-    Py_DECREF(str_obj);\r
-\r
-    return out;\r
-}\r
-\r
-\r
-PyObject *\r
-PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in)\r
-{\r
-    PyObject* str_obj;\r
-    PyObject* sep_obj;\r
-    PyObject* out;\r
-\r
-    str_obj = PyUnicode_FromObject(str_in);\r
-    if (!str_obj)\r
-        return NULL;\r
-    sep_obj = PyUnicode_FromObject(sep_in);\r
-    if (!sep_obj) {\r
-        Py_DECREF(str_obj);\r
-        return NULL;\r
-    }\r
-\r
-    out = stringlib_rpartition(\r
-        str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj),\r
-        sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj)\r
-        );\r
-\r
-    Py_DECREF(sep_obj);\r
-    Py_DECREF(str_obj);\r
-\r
-    return out;\r
-}\r
-\r
-PyDoc_STRVAR(partition__doc__,\r
-             "S.partition(sep) -> (head, sep, tail)\n\\r
-\n\\r
-Search for the separator sep in S, and return the part before it,\n\\r
-the separator itself, and the part after it.  If the separator is not\n\\r
-found, return S and two empty strings.");\r
-\r
-static PyObject*\r
-unicode_partition(PyUnicodeObject *self, PyObject *separator)\r
-{\r
-    return PyUnicode_Partition((PyObject *)self, separator);\r
-}\r
-\r
-PyDoc_STRVAR(rpartition__doc__,\r
-             "S.rpartition(sep) -> (head, sep, tail)\n\\r
-\n\\r
-Search for the separator sep in S, starting at the end of S, and return\n\\r
-the part before it, the separator itself, and the part after it.  If the\n\\r
-separator is not found, return two empty strings and S.");\r
-\r
-static PyObject*\r
-unicode_rpartition(PyUnicodeObject *self, PyObject *separator)\r
-{\r
-    return PyUnicode_RPartition((PyObject *)self, separator);\r
-}\r
-\r
-PyObject *PyUnicode_RSplit(PyObject *s,\r
-                           PyObject *sep,\r
-                           Py_ssize_t maxsplit)\r
-{\r
-    PyObject *result;\r
-\r
-    s = PyUnicode_FromObject(s);\r
-    if (s == NULL)\r
-        return NULL;\r
-    if (sep != NULL) {\r
-        sep = PyUnicode_FromObject(sep);\r
-        if (sep == NULL) {\r
-            Py_DECREF(s);\r
-            return NULL;\r
-        }\r
-    }\r
-\r
-    result = rsplit((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit);\r
-\r
-    Py_DECREF(s);\r
-    Py_XDECREF(sep);\r
-    return result;\r
-}\r
-\r
-PyDoc_STRVAR(rsplit__doc__,\r
-             "S.rsplit([sep [,maxsplit]]) -> list of strings\n\\r
-\n\\r
-Return a list of the words in S, using sep as the\n\\r
-delimiter string, starting at the end of the string and\n\\r
-working to the front.  If maxsplit is given, at most maxsplit\n\\r
-splits are done. If sep is not specified, any whitespace string\n\\r
-is a separator.");\r
-\r
-static PyObject*\r
-unicode_rsplit(PyUnicodeObject *self, PyObject *args)\r
-{\r
-    PyObject *substring = Py_None;\r
-    Py_ssize_t maxcount = -1;\r
-\r
-    if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount))\r
-        return NULL;\r
-\r
-    if (substring == Py_None)\r
-        return rsplit(self, NULL, maxcount);\r
-    else if (PyUnicode_Check(substring))\r
-        return rsplit(self, (PyUnicodeObject *)substring, maxcount);\r
-    else\r
-        return PyUnicode_RSplit((PyObject *)self, substring, maxcount);\r
-}\r
-\r
-PyDoc_STRVAR(splitlines__doc__,\r
-             "S.splitlines(keepends=False) -> list of strings\n\\r
-\n\\r
-Return a list of the lines in S, breaking at line boundaries.\n\\r
-Line breaks are not included in the resulting list unless keepends\n\\r
-is given and true.");\r
-\r
-static PyObject*\r
-unicode_splitlines(PyUnicodeObject *self, PyObject *args)\r
-{\r
-    int keepends = 0;\r
-\r
-    if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends))\r
-        return NULL;\r
-\r
-    return PyUnicode_Splitlines((PyObject *)self, keepends);\r
-}\r
-\r
-static\r
-PyObject *unicode_str(PyUnicodeObject *self)\r
-{\r
-    return PyUnicode_AsEncodedString((PyObject *)self, NULL, NULL);\r
-}\r
-\r
-PyDoc_STRVAR(swapcase__doc__,\r
-             "S.swapcase() -> unicode\n\\r
-\n\\r
-Return a copy of S with uppercase characters converted to lowercase\n\\r
-and vice versa.");\r
-\r
-static PyObject*\r
-unicode_swapcase(PyUnicodeObject *self)\r
-{\r
-    return fixup(self, fixswapcase);\r
-}\r
-\r
-PyDoc_STRVAR(translate__doc__,\r
-             "S.translate(table) -> unicode\n\\r
-\n\\r
-Return a copy of the string S, where all characters have been mapped\n\\r
-through the given translation table, which must be a mapping of\n\\r
-Unicode ordinals to Unicode ordinals, Unicode strings or None.\n\\r
-Unmapped characters are left untouched. Characters mapped to None\n\\r
-are deleted.");\r
-\r
-static PyObject*\r
-unicode_translate(PyUnicodeObject *self, PyObject *table)\r
-{\r
-    return PyUnicode_TranslateCharmap(self->str,\r
-                                      self->length,\r
-                                      table,\r
-                                      "ignore");\r
-}\r
-\r
-PyDoc_STRVAR(upper__doc__,\r
-             "S.upper() -> unicode\n\\r
-\n\\r
-Return a copy of S converted to uppercase.");\r
-\r
-static PyObject*\r
-unicode_upper(PyUnicodeObject *self)\r
-{\r
-    return fixup(self, fixupper);\r
-}\r
-\r
-PyDoc_STRVAR(zfill__doc__,\r
-             "S.zfill(width) -> unicode\n\\r
-\n\\r
-Pad a numeric string S with zeros on the left, to fill a field\n\\r
-of the specified width. The string S is never truncated.");\r
-\r
-static PyObject *\r
-unicode_zfill(PyUnicodeObject *self, PyObject *args)\r
-{\r
-    Py_ssize_t fill;\r
-    PyUnicodeObject *u;\r
-\r
-    Py_ssize_t width;\r
-    if (!PyArg_ParseTuple(args, "n:zfill", &width))\r
-        return NULL;\r
-\r
-    if (self->length >= width) {\r
-        if (PyUnicode_CheckExact(self)) {\r
-            Py_INCREF(self);\r
-            return (PyObject*) self;\r
-        }\r
-        else\r
-            return PyUnicode_FromUnicode(\r
-                PyUnicode_AS_UNICODE(self),\r
-                PyUnicode_GET_SIZE(self)\r
-                );\r
-    }\r
-\r
-    fill = width - self->length;\r
-\r
-    u = pad(self, fill, 0, '0');\r
-\r
-    if (u == NULL)\r
-        return NULL;\r
-\r
-    if (u->str[fill] == '+' || u->str[fill] == '-') {\r
-        /* move sign to beginning of string */\r
-        u->str[0] = u->str[fill];\r
-        u->str[fill] = '0';\r
-    }\r
-\r
-    return (PyObject*) u;\r
-}\r
-\r
-#if 0\r
-static PyObject*\r
-free_listsize(PyUnicodeObject *self)\r
-{\r
-    return PyInt_FromLong(numfree);\r
-}\r
-#endif\r
-\r
-PyDoc_STRVAR(startswith__doc__,\r
-             "S.startswith(prefix[, start[, end]]) -> bool\n\\r
-\n\\r
-Return True if S starts with the specified prefix, False otherwise.\n\\r
-With optional start, test S beginning at that position.\n\\r
-With optional end, stop comparing S at that position.\n\\r
-prefix can also be a tuple of strings to try.");\r
-\r
-static PyObject *\r
-unicode_startswith(PyUnicodeObject *self,\r
-                   PyObject *args)\r
-{\r
-    PyObject *subobj;\r
-    PyUnicodeObject *substring;\r
-    Py_ssize_t start = 0;\r
-    Py_ssize_t end = PY_SSIZE_T_MAX;\r
-    int result;\r
-\r
-    if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end))\r
-        return NULL;\r
-    if (PyTuple_Check(subobj)) {\r
-        Py_ssize_t i;\r
-        for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) {\r
-            substring = (PyUnicodeObject *)PyUnicode_FromObject(\r
-                PyTuple_GET_ITEM(subobj, i));\r
-            if (substring == NULL)\r
-                return NULL;\r
-            result = tailmatch(self, substring, start, end, -1);\r
-            Py_DECREF(substring);\r
-            if (result) {\r
-                Py_RETURN_TRUE;\r
-            }\r
-        }\r
-        /* nothing matched */\r
-        Py_RETURN_FALSE;\r
-    }\r
-    substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj);\r
-    if (substring == NULL) {\r
-        if (PyErr_ExceptionMatches(PyExc_TypeError))\r
-            PyErr_Format(PyExc_TypeError, "startswith first arg must be str, "\r
-                         "unicode, or tuple, not %s", Py_TYPE(subobj)->tp_name);\r
-        return NULL;\r
-    }\r
-    result = tailmatch(self, substring, start, end, -1);\r
-    Py_DECREF(substring);\r
-    return PyBool_FromLong(result);\r
-}\r
-\r
-\r
-PyDoc_STRVAR(endswith__doc__,\r
-             "S.endswith(suffix[, start[, end]]) -> bool\n\\r
-\n\\r
-Return True if S ends with the specified suffix, False otherwise.\n\\r
-With optional start, test S beginning at that position.\n\\r
-With optional end, stop comparing S at that position.\n\\r
-suffix can also be a tuple of strings to try.");\r
-\r
-static PyObject *\r
-unicode_endswith(PyUnicodeObject *self,\r
-                 PyObject *args)\r
-{\r
-    PyObject *subobj;\r
-    PyUnicodeObject *substring;\r
-    Py_ssize_t start = 0;\r
-    Py_ssize_t end = PY_SSIZE_T_MAX;\r
-    int result;\r
-\r
-    if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end))\r
-        return NULL;\r
-    if (PyTuple_Check(subobj)) {\r
-        Py_ssize_t i;\r
-        for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) {\r
-            substring = (PyUnicodeObject *)PyUnicode_FromObject(\r
-                PyTuple_GET_ITEM(subobj, i));\r
-            if (substring == NULL)\r
-                return NULL;\r
-            result = tailmatch(self, substring, start, end, +1);\r
-            Py_DECREF(substring);\r
-            if (result) {\r
-                Py_RETURN_TRUE;\r
-            }\r
-        }\r
-        Py_RETURN_FALSE;\r
-    }\r
-    substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj);\r
-    if (substring == NULL) {\r
-        if (PyErr_ExceptionMatches(PyExc_TypeError))\r
-            PyErr_Format(PyExc_TypeError, "endswith first arg must be str, "\r
-                         "unicode, or tuple, not %s", Py_TYPE(subobj)->tp_name);\r
-        return NULL;\r
-    }\r
-    result = tailmatch(self, substring, start, end, +1);\r
-    Py_DECREF(substring);\r
-    return PyBool_FromLong(result);\r
-}\r
-\r
-\r
-/* Implements do_string_format, which is unicode because of stringlib */\r
-#include "stringlib/string_format.h"\r
-\r
-PyDoc_STRVAR(format__doc__,\r
-             "S.format(*args, **kwargs) -> unicode\n\\r
-\n\\r
-Return a formatted version of S, using substitutions from args and kwargs.\n\\r
-The substitutions are identified by braces ('{' and '}').");\r
-\r
-static PyObject *\r
-unicode__format__(PyObject *self, PyObject *args)\r
-{\r
-    PyObject *format_spec;\r
-    PyObject *result = NULL;\r
-    PyObject *tmp = NULL;\r
-\r
-    /* If 2.x, convert format_spec to the same type as value */\r
-    /* This is to allow things like u''.format('') */\r
-    if (!PyArg_ParseTuple(args, "O:__format__", &format_spec))\r
-        goto done;\r
-    if (!(PyBytes_Check(format_spec) || PyUnicode_Check(format_spec))) {\r
-        PyErr_Format(PyExc_TypeError, "__format__ arg must be str "\r
-                     "or unicode, not %s", Py_TYPE(format_spec)->tp_name);\r
-        goto done;\r
-    }\r
-    tmp = PyObject_Unicode(format_spec);\r
-    if (tmp == NULL)\r
-        goto done;\r
-    format_spec = tmp;\r
-\r
-    result = _PyUnicode_FormatAdvanced(self,\r
-                                       PyUnicode_AS_UNICODE(format_spec),\r
-                                       PyUnicode_GET_SIZE(format_spec));\r
-  done:\r
-    Py_XDECREF(tmp);\r
-    return result;\r
-}\r
-\r
-PyDoc_STRVAR(p_format__doc__,\r
-             "S.__format__(format_spec) -> unicode\n\\r
-\n\\r
-Return a formatted version of S as described by format_spec.");\r
-\r
-static PyObject *\r
-unicode__sizeof__(PyUnicodeObject *v)\r
-{\r
-    return PyInt_FromSsize_t(sizeof(PyUnicodeObject) +\r
-                             sizeof(Py_UNICODE) * (v->length + 1));\r
-}\r
-\r
-PyDoc_STRVAR(sizeof__doc__,\r
-             "S.__sizeof__() -> size of S in memory, in bytes\n\\r
-\n\\r
-");\r
-\r
-static PyObject *\r
-unicode_getnewargs(PyUnicodeObject *v)\r
-{\r
-    return Py_BuildValue("(u#)", v->str, v->length);\r
-}\r
-\r
-\r
-static PyMethodDef unicode_methods[] = {\r
-    {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__},\r
-    {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__},\r
-    {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__},\r
-    {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__},\r
-    {"join", (PyCFunction) unicode_join, METH_O, join__doc__},\r
-    {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__},\r
-    {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__},\r
-    {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__},\r
-    {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__},\r
-    {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__},\r
-    {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__},\r
-    {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__},\r
-    {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__},\r
-    {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__},\r
-    {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__},\r
-    {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__},\r
-    {"decode", (PyCFunction) unicode_decode, METH_VARARGS | METH_KEYWORDS, decode__doc__},\r
-/*  {"maketrans", (PyCFunction) unicode_maketrans, METH_VARARGS, maketrans__doc__}, */\r
-    {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__},\r
-    {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__},\r
-    {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__},\r
-    {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__},\r
-    {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__},\r
-    {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS, splitlines__doc__},\r
-    {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__},\r
-    {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__},\r
-    {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__},\r
-    {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__},\r
-    {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__},\r
-    {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__},\r
-    {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__},\r
-    {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__},\r
-    {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__},\r
-    {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__},\r
-    {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__},\r
-    {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__},\r
-    {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__},\r
-    {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__},\r
-    {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__},\r
-    {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__},\r
-    {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__},\r
-    {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__},\r
-    {"_formatter_field_name_split", (PyCFunction) formatter_field_name_split, METH_NOARGS},\r
-    {"_formatter_parser", (PyCFunction) formatter_parser, METH_NOARGS},\r
-    {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__},\r
-#if 0\r
-    {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__},\r
-#endif\r
-\r
-#if 0\r
-    /* This one is just used for debugging the implementation. */\r
-    {"freelistsize", (PyCFunction) free_listsize, METH_NOARGS},\r
-#endif\r
-\r
-    {"__getnewargs__",  (PyCFunction)unicode_getnewargs, METH_NOARGS},\r
-    {NULL, NULL}\r
-};\r
-\r
-static PyObject *\r
-unicode_mod(PyObject *v, PyObject *w)\r
-{\r
-    if (!PyUnicode_Check(v)) {\r
-        Py_INCREF(Py_NotImplemented);\r
-        return Py_NotImplemented;\r
-    }\r
-    return PyUnicode_Format(v, w);\r
-}\r
-\r
-static PyNumberMethods unicode_as_number = {\r
-    0,              /*nb_add*/\r
-    0,              /*nb_subtract*/\r
-    0,              /*nb_multiply*/\r
-    0,              /*nb_divide*/\r
-    unicode_mod,            /*nb_remainder*/\r
-};\r
-\r
-static PySequenceMethods unicode_as_sequence = {\r
-    (lenfunc) unicode_length,       /* sq_length */\r
-    PyUnicode_Concat,           /* sq_concat */\r
-    (ssizeargfunc) unicode_repeat,  /* sq_repeat */\r
-    (ssizeargfunc) unicode_getitem,     /* sq_item */\r
-    (ssizessizeargfunc) unicode_slice,  /* sq_slice */\r
-    0,                  /* sq_ass_item */\r
-    0,                  /* sq_ass_slice */\r
-    PyUnicode_Contains,         /* sq_contains */\r
-};\r
-\r
-static PyObject*\r
-unicode_subscript(PyUnicodeObject* self, PyObject* item)\r
-{\r
-    if (PyIndex_Check(item)) {\r
-        Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);\r
-        if (i == -1 && PyErr_Occurred())\r
-            return NULL;\r
-        if (i < 0)\r
-            i += PyUnicode_GET_SIZE(self);\r
-        return unicode_getitem(self, i);\r
-    } else if (PySlice_Check(item)) {\r
-        Py_ssize_t start, stop, step, slicelength, cur, i;\r
-        Py_UNICODE* source_buf;\r
-        Py_UNICODE* result_buf;\r
-        PyObject* result;\r
-\r
-        if (PySlice_GetIndicesEx((PySliceObject*)item, PyUnicode_GET_SIZE(self),\r
-                                 &start, &stop, &step, &slicelength) < 0) {\r
-            return NULL;\r
-        }\r
-\r
-        if (slicelength <= 0) {\r
-            return PyUnicode_FromUnicode(NULL, 0);\r
-        } else if (start == 0 && step == 1 && slicelength == self->length &&\r
-                   PyUnicode_CheckExact(self)) {\r
-            Py_INCREF(self);\r
-            return (PyObject *)self;\r
-        } else if (step == 1) {\r
-            return PyUnicode_FromUnicode(self->str + start, slicelength);\r
-        } else {\r
-            source_buf = PyUnicode_AS_UNICODE((PyObject*)self);\r
-            result_buf = (Py_UNICODE *)PyObject_MALLOC(slicelength*\r
-                                                       sizeof(Py_UNICODE));\r
-\r
-            if (result_buf == NULL)\r
-                return PyErr_NoMemory();\r
-\r
-            for (cur = start, i = 0; i < slicelength; cur += step, i++) {\r
-                result_buf[i] = source_buf[cur];\r
-            }\r
-\r
-            result = PyUnicode_FromUnicode(result_buf, slicelength);\r
-            PyObject_FREE(result_buf);\r
-            return result;\r
-        }\r
-    } else {\r
-        PyErr_SetString(PyExc_TypeError, "string indices must be integers");\r
-        return NULL;\r
-    }\r
-}\r
-\r
-static PyMappingMethods unicode_as_mapping = {\r
-    (lenfunc)unicode_length,        /* mp_length */\r
-    (binaryfunc)unicode_subscript,  /* mp_subscript */\r
-    (objobjargproc)0,           /* mp_ass_subscript */\r
-};\r
-\r
-static Py_ssize_t\r
-unicode_buffer_getreadbuf(PyUnicodeObject *self,\r
-                          Py_ssize_t index,\r
-                          const void **ptr)\r
-{\r
-    if (index != 0) {\r
-        PyErr_SetString(PyExc_SystemError,\r
-                        "accessing non-existent unicode segment");\r
-        return -1;\r
-    }\r
-    *ptr = (void *) self->str;\r
-    return PyUnicode_GET_DATA_SIZE(self);\r
-}\r
-\r
-static Py_ssize_t\r
-unicode_buffer_getwritebuf(PyUnicodeObject *self, Py_ssize_t index,\r
-                           const void **ptr)\r
-{\r
-    PyErr_SetString(PyExc_TypeError,\r
-                    "cannot use unicode as modifiable buffer");\r
-    return -1;\r
-}\r
-\r
-static int\r
-unicode_buffer_getsegcount(PyUnicodeObject *self,\r
-                           Py_ssize_t *lenp)\r
-{\r
-    if (lenp)\r
-        *lenp = PyUnicode_GET_DATA_SIZE(self);\r
-    return 1;\r
-}\r
-\r
-static Py_ssize_t\r
-unicode_buffer_getcharbuf(PyUnicodeObject *self,\r
-                          Py_ssize_t index,\r
-                          const void **ptr)\r
-{\r
-    PyObject *str;\r
-\r
-    if (index != 0) {\r
-        PyErr_SetString(PyExc_SystemError,\r
-                        "accessing non-existent unicode segment");\r
-        return -1;\r
-    }\r
-    str = _PyUnicode_AsDefaultEncodedString((PyObject *)self, NULL);\r
-    if (str == NULL)\r
-        return -1;\r
-    *ptr = (void *) PyString_AS_STRING(str);\r
-    return PyString_GET_SIZE(str);\r
-}\r
-\r
-/* Helpers for PyUnicode_Format() */\r
-\r
-static PyObject *\r
-getnextarg(PyObject *args, Py_ssize_t arglen, Py_ssize_t *p_argidx)\r
-{\r
-    Py_ssize_t argidx = *p_argidx;\r
-    if (argidx < arglen) {\r
-        (*p_argidx)++;\r
-        if (arglen < 0)\r
-            return args;\r
-        else\r
-            return PyTuple_GetItem(args, argidx);\r
-    }\r
-    PyErr_SetString(PyExc_TypeError,\r
-                    "not enough arguments for format string");\r
-    return NULL;\r
-}\r
-\r
-#define F_LJUST (1<<0)\r
-#define F_SIGN  (1<<1)\r
-#define F_BLANK (1<<2)\r
-#define F_ALT   (1<<3)\r
-#define F_ZERO  (1<<4)\r
-\r
-static Py_ssize_t\r
-strtounicode(Py_UNICODE *buffer, const char *charbuffer)\r
-{\r
-    register Py_ssize_t i;\r
-    Py_ssize_t len = strlen(charbuffer);\r
-    for (i = len - 1; i >= 0; i--)\r
-        buffer[i] = (Py_UNICODE) charbuffer[i];\r
-\r
-    return len;\r
-}\r
-\r
-static int\r
-longtounicode(Py_UNICODE *buffer, size_t len, const char *format, long x)\r
-{\r
-    Py_ssize_t result;\r
-\r
-    PyOS_snprintf((char *)buffer, len, format, x);\r
-    result = strtounicode(buffer, (char *)buffer);\r
-    return Py_SAFE_DOWNCAST(result, Py_ssize_t, int);\r
-}\r
-\r
-/* XXX To save some code duplication, formatfloat/long/int could have been\r
-   shared with stringobject.c, converting from 8-bit to Unicode after the\r
-   formatting is done. */\r
-\r
-/* Returns a new reference to a PyUnicode object, or NULL on failure. */\r
-\r
-static PyObject *\r
-formatfloat(PyObject *v, int flags, int prec, int type)\r
-{\r
-    char *p;\r
-    PyObject *result;\r
-    double x;\r
-\r
-    x = PyFloat_AsDouble(v);\r
-    if (x == -1.0 && PyErr_Occurred())\r
-        return NULL;\r
-\r
-    if (prec < 0)\r
-        prec = 6;\r
-\r
-    p = PyOS_double_to_string(x, type, prec,\r
-                              (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL);\r
-    if (p == NULL)\r
-        return NULL;\r
-    result = PyUnicode_FromStringAndSize(p, strlen(p));\r
-    PyMem_Free(p);\r
-    return result;\r
-}\r
-\r
-static PyObject*\r
-formatlong(PyObject *val, int flags, int prec, int type)\r
-{\r
-    char *buf;\r
-    int i, len;\r
-    PyObject *str; /* temporary string object. */\r
-    PyUnicodeObject *result;\r
-\r
-    str = _PyString_FormatLong(val, flags, prec, type, &buf, &len);\r
-    if (!str)\r
-        return NULL;\r
-    result = _PyUnicode_New(len);\r
-    if (!result) {\r
-        Py_DECREF(str);\r
-        return NULL;\r
-    }\r
-    for (i = 0; i < len; i++)\r
-        result->str[i] = buf[i];\r
-    result->str[len] = 0;\r
-    Py_DECREF(str);\r
-    return (PyObject*)result;\r
-}\r
-\r
-static int\r
-formatint(Py_UNICODE *buf,\r
-          size_t buflen,\r
-          int flags,\r
-          int prec,\r
-          int type,\r
-          PyObject *v)\r
-{\r
-    /* fmt = '%#.' + `prec` + 'l' + `type`\r
-     * worst case length = 3 + 19 (worst len of INT_MAX on 64-bit machine)\r
-     *                     + 1 + 1\r
-     *                   = 24\r
-     */\r
-    char fmt[64]; /* plenty big enough! */\r
-    char *sign;\r
-    long x;\r
-\r
-    x = PyInt_AsLong(v);\r
-    if (x == -1 && PyErr_Occurred())\r
-        return -1;\r
-    if (x < 0 && type == 'u') {\r
-        type = 'd';\r
-    }\r
-    if (x < 0 && (type == 'x' || type == 'X' || type == 'o'))\r
-        sign = "-";\r
-    else\r
-        sign = "";\r
-    if (prec < 0)\r
-        prec = 1;\r
-\r
-    /* buf = '+'/'-'/'' + '0'/'0x'/'' + '[0-9]'*max(prec, len(x in octal))\r
-     * worst case buf = '-0x' + [0-9]*prec, where prec >= 11\r
-     */\r
-    if (buflen <= 14 || buflen <= (size_t)3 + (size_t)prec) {\r
-        PyErr_SetString(PyExc_OverflowError,\r
-                        "formatted integer is too long (precision too large?)");\r
-        return -1;\r
-    }\r
-\r
-    if ((flags & F_ALT) &&\r
-        (type == 'x' || type == 'X')) {\r
-        /* When converting under %#x or %#X, there are a number\r
-         * of issues that cause pain:\r
-         * - when 0 is being converted, the C standard leaves off\r
-         *   the '0x' or '0X', which is inconsistent with other\r
-         *   %#x/%#X conversions and inconsistent with Python's\r
-         *   hex() function\r
-         * - there are platforms that violate the standard and\r
-         *   convert 0 with the '0x' or '0X'\r
-         *   (Metrowerks, Compaq Tru64)\r
-         * - there are platforms that give '0x' when converting\r
-         *   under %#X, but convert 0 in accordance with the\r
-         *   standard (OS/2 EMX)\r
-         *\r
-         * We can achieve the desired consistency by inserting our\r
-         * own '0x' or '0X' prefix, and substituting %x/%X in place\r
-         * of %#x/%#X.\r
-         *\r
-         * Note that this is the same approach as used in\r
-         * formatint() in stringobject.c\r
-         */\r
-        PyOS_snprintf(fmt, sizeof(fmt), "%s0%c%%.%dl%c",\r
-                      sign, type, prec, type);\r
-    }\r
-    else {\r
-        PyOS_snprintf(fmt, sizeof(fmt), "%s%%%s.%dl%c",\r
-                      sign, (flags&F_ALT) ? "#" : "",\r
-                      prec, type);\r
-    }\r
-    if (sign[0])\r
-        return longtounicode(buf, buflen, fmt, -x);\r
-    else\r
-        return longtounicode(buf, buflen, fmt, x);\r
-}\r
-\r
-static int\r
-formatchar(Py_UNICODE *buf,\r
-           size_t buflen,\r
-           PyObject *v)\r
-{\r
-    PyObject *unistr;\r
-    char *str;\r
-    /* presume that the buffer is at least 2 characters long */\r
-    if (PyUnicode_Check(v)) {\r
-        if (PyUnicode_GET_SIZE(v) != 1)\r
-            goto onError;\r
-        buf[0] = PyUnicode_AS_UNICODE(v)[0];\r
-    }\r
-\r
-    else if (PyString_Check(v)) {\r
-        if (PyString_GET_SIZE(v) != 1)\r
-            goto onError;\r
-        /* #7649: "u'%c' % char" should behave like "u'%s' % char" and fail\r
-           with a UnicodeDecodeError if 'char' is not decodable with the\r
-           default encoding (usually ASCII, but it might be something else) */\r
-        str = PyString_AS_STRING(v);\r
-        if ((unsigned char)str[0] > 0x7F) {\r
-            /* the char is not ASCII; try to decode the string using the\r
-               default encoding and return -1 to let the UnicodeDecodeError\r
-               be raised if the string can't be decoded */\r
-            unistr = PyUnicode_Decode(str, 1, NULL, "strict");\r
-            if (unistr == NULL)\r
-                return -1;\r
-            buf[0] = PyUnicode_AS_UNICODE(unistr)[0];\r
-            Py_DECREF(unistr);\r
-        }\r
-        else\r
-            buf[0] = (Py_UNICODE)str[0];\r
-    }\r
-\r
-    else {\r
-        /* Integer input truncated to a character */\r
-        long x;\r
-        x = PyInt_AsLong(v);\r
-        if (x == -1 && PyErr_Occurred())\r
-            goto onError;\r
-#ifdef Py_UNICODE_WIDE\r
-        if (x < 0 || x > 0x10ffff) {\r
-            PyErr_SetString(PyExc_OverflowError,\r
-                            "%c arg not in range(0x110000) "\r
-                            "(wide Python build)");\r
-            return -1;\r
-        }\r
-#else\r
-        if (x < 0 || x > 0xffff) {\r
-            PyErr_SetString(PyExc_OverflowError,\r
-                            "%c arg not in range(0x10000) "\r
-                            "(narrow Python build)");\r
-            return -1;\r
-        }\r
-#endif\r
-        buf[0] = (Py_UNICODE) x;\r
-    }\r
-    buf[1] = '\0';\r
-    return 1;\r
-\r
-  onError:\r
-    PyErr_SetString(PyExc_TypeError,\r
-                    "%c requires int or char");\r
-    return -1;\r
-}\r
-\r
-/* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...)\r
-\r
-   FORMATBUFLEN is the length of the buffer in which the ints &\r
-   chars are formatted. XXX This is a magic number. Each formatting\r
-   routine does bounds checking to ensure no overflow, but a better\r
-   solution may be to malloc a buffer of appropriate size for each\r
-   format. For now, the current solution is sufficient.\r
-*/\r
-#define FORMATBUFLEN (size_t)120\r
-\r
-PyObject *PyUnicode_Format(PyObject *format,\r
-                           PyObject *args)\r
-{\r
-    Py_UNICODE *fmt, *res;\r
-    Py_ssize_t fmtcnt, rescnt, reslen, arglen, argidx;\r
-    int args_owned = 0;\r
-    PyUnicodeObject *result = NULL;\r
-    PyObject *dict = NULL;\r
-    PyObject *uformat;\r
-\r
-    if (format == NULL || args == NULL) {\r
-        PyErr_BadInternalCall();\r
-        return NULL;\r
-    }\r
-    uformat = PyUnicode_FromObject(format);\r
-    if (uformat == NULL)\r
-        return NULL;\r
-    fmt = PyUnicode_AS_UNICODE(uformat);\r
-    fmtcnt = PyUnicode_GET_SIZE(uformat);\r
-\r
-    reslen = rescnt = fmtcnt + 100;\r
-    result = _PyUnicode_New(reslen);\r
-    if (result == NULL)\r
-        goto onError;\r
-    res = PyUnicode_AS_UNICODE(result);\r
-\r
-    if (PyTuple_Check(args)) {\r
-        arglen = PyTuple_Size(args);\r
-        argidx = 0;\r
-    }\r
-    else {\r
-        arglen = -1;\r
-        argidx = -2;\r
-    }\r
-    if (Py_TYPE(args)->tp_as_mapping && Py_TYPE(args)->tp_as_mapping->mp_subscript &&\r
-        !PyTuple_Check(args) && !PyObject_TypeCheck(args, &PyBaseString_Type))\r
-        dict = args;\r
-\r
-    while (--fmtcnt >= 0) {\r
-        if (*fmt != '%') {\r
-            if (--rescnt < 0) {\r
-                rescnt = fmtcnt + 100;\r
-                reslen += rescnt;\r
-                if (_PyUnicode_Resize(&result, reslen) < 0)\r
-                    goto onError;\r
-                res = PyUnicode_AS_UNICODE(result) + reslen - rescnt;\r
-                --rescnt;\r
-            }\r
-            *res++ = *fmt++;\r
-        }\r
-        else {\r
-            /* Got a format specifier */\r
-            int flags = 0;\r
-            Py_ssize_t width = -1;\r
-            int prec = -1;\r
-            Py_UNICODE c = '\0';\r
-            Py_UNICODE fill;\r
-            int isnumok;\r
-            PyObject *v = NULL;\r
-            PyObject *temp = NULL;\r
-            Py_UNICODE *pbuf;\r
-            Py_UNICODE sign;\r
-            Py_ssize_t len;\r
-            Py_UNICODE formatbuf[FORMATBUFLEN]; /* For format{int,char}() */\r
-\r
-            fmt++;\r
-            if (*fmt == '(') {\r
-                Py_UNICODE *keystart;\r
-                Py_ssize_t keylen;\r
-                PyObject *key;\r
-                int pcount = 1;\r
-\r
-                if (dict == NULL) {\r
-                    PyErr_SetString(PyExc_TypeError,\r
-                                    "format requires a mapping");\r
-                    goto onError;\r
-                }\r
-                ++fmt;\r
-                --fmtcnt;\r
-                keystart = fmt;\r
-                /* Skip over balanced parentheses */\r
-                while (pcount > 0 && --fmtcnt >= 0) {\r
-                    if (*fmt == ')')\r
-                        --pcount;\r
-                    else if (*fmt == '(')\r
-                        ++pcount;\r
-                    fmt++;\r
-                }\r
-                keylen = fmt - keystart - 1;\r
-                if (fmtcnt < 0 || pcount > 0) {\r
-                    PyErr_SetString(PyExc_ValueError,\r
-                                    "incomplete format key");\r
-                    goto onError;\r
-                }\r
-#if 0\r
-                /* keys are converted to strings using UTF-8 and\r
-                   then looked up since Python uses strings to hold\r
-                   variables names etc. in its namespaces and we\r
-                   wouldn't want to break common idioms. */\r
-                key = PyUnicode_EncodeUTF8(keystart,\r
-                                           keylen,\r
-                                           NULL);\r
-#else\r
-                key = PyUnicode_FromUnicode(keystart, keylen);\r
-#endif\r
-                if (key == NULL)\r
-                    goto onError;\r
-                if (args_owned) {\r
-                    Py_DECREF(args);\r
-                    args_owned = 0;\r
-                }\r
-                args = PyObject_GetItem(dict, key);\r
-                Py_DECREF(key);\r
-                if (args == NULL) {\r
-                    goto onError;\r
-                }\r
-                args_owned = 1;\r
-                arglen = -1;\r
-                argidx = -2;\r
-            }\r
-            while (--fmtcnt >= 0) {\r
-                switch (c = *fmt++) {\r
-                case '-': flags |= F_LJUST; continue;\r
-                case '+': flags |= F_SIGN; continue;\r
-                case ' ': flags |= F_BLANK; continue;\r
-                case '#': flags |= F_ALT; continue;\r
-                case '0': flags |= F_ZERO; continue;\r
-                }\r
-                break;\r
-            }\r
-            if (c == '*') {\r
-                v = getnextarg(args, arglen, &argidx);\r
-                if (v == NULL)\r
-                    goto onError;\r
-                if (!PyInt_Check(v)) {\r
-                    PyErr_SetString(PyExc_TypeError,\r
-                                    "* wants int");\r
-                    goto onError;\r
-                }\r
-                width = PyInt_AsSsize_t(v);\r
-                if (width == -1 && PyErr_Occurred())\r
-                    goto onError;\r
-                if (width < 0) {\r
-                    flags |= F_LJUST;\r
-                    width = -width;\r
-                }\r
-                if (--fmtcnt >= 0)\r
-                    c = *fmt++;\r
-            }\r
-            else if (c >= '0' && c <= '9') {\r
-                width = c - '0';\r
-                while (--fmtcnt >= 0) {\r
-                    c = *fmt++;\r
-                    if (c < '0' || c > '9')\r
-                        break;\r
-                    if (width > (PY_SSIZE_T_MAX - ((int)c - '0')) / 10) {\r
-                        PyErr_SetString(PyExc_ValueError,\r
-                                        "width too big");\r
-                        goto onError;\r
-                    }\r
-                    width = width*10 + (c - '0');\r
-                }\r
-            }\r
-            if (c == '.') {\r
-                prec = 0;\r
-                if (--fmtcnt >= 0)\r
-                    c = *fmt++;\r
-                if (c == '*') {\r
-                    v = getnextarg(args, arglen, &argidx);\r
-                    if (v == NULL)\r
-                        goto onError;\r
-                    if (!PyInt_Check(v)) {\r
-                        PyErr_SetString(PyExc_TypeError,\r
-                                        "* wants int");\r
-                        goto onError;\r
-                    }\r
-                    prec = _PyInt_AsInt(v);\r
-                    if (prec == -1 && PyErr_Occurred())\r
-                        goto onError;\r
-                    if (prec < 0)\r
-                        prec = 0;\r
-                    if (--fmtcnt >= 0)\r
-                        c = *fmt++;\r
-                }\r
-                else if (c >= '0' && c <= '9') {\r
-                    prec = c - '0';\r
-                    while (--fmtcnt >= 0) {\r
-                        c = *fmt++;\r
-                        if (c < '0' || c > '9')\r
-                            break;\r
-                        if (prec > (INT_MAX - ((int)c - '0')) / 10) {\r
-                            PyErr_SetString(PyExc_ValueError,\r
-                                            "prec too big");\r
-                            goto onError;\r
-                        }\r
-                        prec = prec*10 + (c - '0');\r
-                    }\r
-                }\r
-            } /* prec */\r
-            if (fmtcnt >= 0) {\r
-                if (c == 'h' || c == 'l' || c == 'L') {\r
-                    if (--fmtcnt >= 0)\r
-                        c = *fmt++;\r
-                }\r
-            }\r
-            if (fmtcnt < 0) {\r
-                PyErr_SetString(PyExc_ValueError,\r
-                                "incomplete format");\r
-                goto onError;\r
-            }\r
-            if (c != '%') {\r
-                v = getnextarg(args, arglen, &argidx);\r
-                if (v == NULL)\r
-                    goto onError;\r
-            }\r
-            sign = 0;\r
-            fill = ' ';\r
-            switch (c) {\r
-\r
-            case '%':\r
-                pbuf = formatbuf;\r
-                /* presume that buffer length is at least 1 */\r
-                pbuf[0] = '%';\r
-                len = 1;\r
-                break;\r
-\r
-            case 's':\r
-            case 'r':\r
-                if (PyUnicode_CheckExact(v) && c == 's') {\r
-                    temp = v;\r
-                    Py_INCREF(temp);\r
-                }\r
-                else {\r
-                    PyObject *unicode;\r
-                    if (c == 's')\r
-                        temp = PyObject_Unicode(v);\r
-                    else\r
-                        temp = PyObject_Repr(v);\r
-                    if (temp == NULL)\r
-                        goto onError;\r
-                    if (PyUnicode_Check(temp))\r
-                        /* nothing to do */;\r
-                    else if (PyString_Check(temp)) {\r
-                        /* convert to string to Unicode */\r
-                        unicode = PyUnicode_Decode(PyString_AS_STRING(temp),\r
-                                                   PyString_GET_SIZE(temp),\r
-                                                   NULL,\r
-                                                   "strict");\r
-                        Py_DECREF(temp);\r
-                        temp = unicode;\r
-                        if (temp == NULL)\r
-                            goto onError;\r
-                    }\r
-                    else {\r
-                        Py_DECREF(temp);\r
-                        PyErr_SetString(PyExc_TypeError,\r
-                                        "%s argument has non-string str()");\r
-                        goto onError;\r
-                    }\r
-                }\r
-                pbuf = PyUnicode_AS_UNICODE(temp);\r
-                len = PyUnicode_GET_SIZE(temp);\r
-                if (prec >= 0 && len > prec)\r
-                    len = prec;\r
-                break;\r
-\r
-            case 'i':\r
-            case 'd':\r
-            case 'u':\r
-            case 'o':\r
-            case 'x':\r
-            case 'X':\r
-                if (c == 'i')\r
-                    c = 'd';\r
-                isnumok = 0;\r
-                if (PyNumber_Check(v)) {\r
-                    PyObject *iobj=NULL;\r
-\r
-                    if (PyInt_Check(v) || (PyLong_Check(v))) {\r
-                        iobj = v;\r
-                        Py_INCREF(iobj);\r
-                    }\r
-                    else {\r
-                        iobj = PyNumber_Int(v);\r
-                        if (iobj==NULL) iobj = PyNumber_Long(v);\r
-                    }\r
-                    if (iobj!=NULL) {\r
-                        if (PyInt_Check(iobj)) {\r
-                            isnumok = 1;\r
-                            pbuf = formatbuf;\r
-                            len = formatint(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE),\r
-                                            flags, prec, c, iobj);\r
-                            Py_DECREF(iobj);\r
-                            if (len < 0)\r
-                                goto onError;\r
-                            sign = 1;\r
-                        }\r
-                        else if (PyLong_Check(iobj)) {\r
-                            isnumok = 1;\r
-                            temp = formatlong(iobj, flags, prec, c);\r
-                            Py_DECREF(iobj);\r
-                            if (!temp)\r
-                                goto onError;\r
-                            pbuf = PyUnicode_AS_UNICODE(temp);\r
-                            len = PyUnicode_GET_SIZE(temp);\r
-                            sign = 1;\r
-                        }\r
-                        else {\r
-                            Py_DECREF(iobj);\r
-                        }\r
-                    }\r
-                }\r
-                if (!isnumok) {\r
-                    PyErr_Format(PyExc_TypeError,\r
-                                 "%%%c format: a number is required, "\r
-                                 "not %.200s", (char)c, Py_TYPE(v)->tp_name);\r
-                    goto onError;\r
-                }\r
-                if (flags & F_ZERO)\r
-                    fill = '0';\r
-                break;\r
-\r
-            case 'e':\r
-            case 'E':\r
-            case 'f':\r
-            case 'F':\r
-            case 'g':\r
-            case 'G':\r
-                temp = formatfloat(v, flags, prec, c);\r
-                if (temp == NULL)\r
-                    goto onError;\r
-                pbuf = PyUnicode_AS_UNICODE(temp);\r
-                len = PyUnicode_GET_SIZE(temp);\r
-                sign = 1;\r
-                if (flags & F_ZERO)\r
-                    fill = '0';\r
-                break;\r
-\r
-            case 'c':\r
-                pbuf = formatbuf;\r
-                len = formatchar(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), v);\r
-                if (len < 0)\r
-                    goto onError;\r
-                break;\r
-\r
-            default:\r
-                PyErr_Format(PyExc_ValueError,\r
-                             "unsupported format character '%c' (0x%x) "\r
-                             "at index %zd",\r
-                             (31<=c && c<=126) ? (char)c : '?',\r
-                             (int)c,\r
-                             (Py_ssize_t)(fmt - 1 -\r
-                                          PyUnicode_AS_UNICODE(uformat)));\r
-                goto onError;\r
-            }\r
-            if (sign) {\r
-                if (*pbuf == '-' || *pbuf == '+') {\r
-                    sign = *pbuf++;\r
-                    len--;\r
-                }\r
-                else if (flags & F_SIGN)\r
-                    sign = '+';\r
-                else if (flags & F_BLANK)\r
-                    sign = ' ';\r
-                else\r
-                    sign = 0;\r
-            }\r
-            if (width < len)\r
-                width = len;\r
-            if (rescnt - (sign != 0) < width) {\r
-                reslen -= rescnt;\r
-                rescnt = width + fmtcnt + 100;\r
-                reslen += rescnt;\r
-                if (reslen < 0) {\r
-                    Py_XDECREF(temp);\r
-                    PyErr_NoMemory();\r
-                    goto onError;\r
-                }\r
-                if (_PyUnicode_Resize(&result, reslen) < 0) {\r
-                    Py_XDECREF(temp);\r
-                    goto onError;\r
-                }\r
-                res = PyUnicode_AS_UNICODE(result)\r
-                    + reslen - rescnt;\r
-            }\r
-            if (sign) {\r
-                if (fill != ' ')\r
-                    *res++ = sign;\r
-                rescnt--;\r
-                if (width > len)\r
-                    width--;\r
-            }\r
-            if ((flags & F_ALT) && (c == 'x' || c == 'X')) {\r
-                assert(pbuf[0] == '0');\r
-                assert(pbuf[1] == c);\r
-                if (fill != ' ') {\r
-                    *res++ = *pbuf++;\r
-                    *res++ = *pbuf++;\r
-                }\r
-                rescnt -= 2;\r
-                width -= 2;\r
-                if (width < 0)\r
-                    width = 0;\r
-                len -= 2;\r
-            }\r
-            if (width > len && !(flags & F_LJUST)) {\r
-                do {\r
-                    --rescnt;\r
-                    *res++ = fill;\r
-                } while (--width > len);\r
-            }\r
-            if (fill == ' ') {\r
-                if (sign)\r
-                    *res++ = sign;\r
-                if ((flags & F_ALT) && (c == 'x' || c == 'X')) {\r
-                    assert(pbuf[0] == '0');\r
-                    assert(pbuf[1] == c);\r
-                    *res++ = *pbuf++;\r
-                    *res++ = *pbuf++;\r
-                }\r
-            }\r
-            Py_UNICODE_COPY(res, pbuf, len);\r
-            res += len;\r
-            rescnt -= len;\r
-            while (--width >= len) {\r
-                --rescnt;\r
-                *res++ = ' ';\r
-            }\r
-            if (dict && (argidx < arglen) && c != '%') {\r
-                PyErr_SetString(PyExc_TypeError,\r
-                                "not all arguments converted during string formatting");\r
-                Py_XDECREF(temp);\r
-                goto onError;\r
-            }\r
-            Py_XDECREF(temp);\r
-        } /* '%' */\r
-    } /* until end */\r
-    if (argidx < arglen && !dict) {\r
-        PyErr_SetString(PyExc_TypeError,\r
-                        "not all arguments converted during string formatting");\r
-        goto onError;\r
-    }\r
-\r
-    if (_PyUnicode_Resize(&result, reslen - rescnt) < 0)\r
-        goto onError;\r
-    if (args_owned) {\r
-        Py_DECREF(args);\r
-    }\r
-    Py_DECREF(uformat);\r
-    return (PyObject *)result;\r
-\r
-  onError:\r
-    Py_XDECREF(result);\r
-    Py_DECREF(uformat);\r
-    if (args_owned) {\r
-        Py_DECREF(args);\r
-    }\r
-    return NULL;\r
-}\r
-\r
-static PyBufferProcs unicode_as_buffer = {\r
-    (readbufferproc) unicode_buffer_getreadbuf,\r
-    (writebufferproc) unicode_buffer_getwritebuf,\r
-    (segcountproc) unicode_buffer_getsegcount,\r
-    (charbufferproc) unicode_buffer_getcharbuf,\r
-};\r
-\r
-static PyObject *\r
-unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);\r
-\r
-static PyObject *\r
-unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds)\r
-{\r
-    PyObject *x = NULL;\r
-    static char *kwlist[] = {"string", "encoding", "errors", 0};\r
-    char *encoding = NULL;\r
-    char *errors = NULL;\r
-\r
-    if (type != &PyUnicode_Type)\r
-        return unicode_subtype_new(type, args, kwds);\r
-    if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:unicode",\r
-                                     kwlist, &x, &encoding, &errors))\r
-        return NULL;\r
-    if (x == NULL)\r
-        return (PyObject *)_PyUnicode_New(0);\r
-    if (encoding == NULL && errors == NULL)\r
-        return PyObject_Unicode(x);\r
-    else\r
-        return PyUnicode_FromEncodedObject(x, encoding, errors);\r
-}\r
-\r
-static PyObject *\r
-unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)\r
-{\r
-    PyUnicodeObject *tmp, *pnew;\r
-    Py_ssize_t n;\r
-\r
-    assert(PyType_IsSubtype(type, &PyUnicode_Type));\r
-    tmp = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds);\r
-    if (tmp == NULL)\r
-        return NULL;\r
-    assert(PyUnicode_Check(tmp));\r
-    pnew = (PyUnicodeObject *) type->tp_alloc(type, n = tmp->length);\r
-    if (pnew == NULL) {\r
-        Py_DECREF(tmp);\r
-        return NULL;\r
-    }\r
-    pnew->str = (Py_UNICODE*) PyObject_MALLOC(sizeof(Py_UNICODE) * (n+1));\r
-    if (pnew->str == NULL) {\r
-        _Py_ForgetReference((PyObject *)pnew);\r
-        PyObject_Del(pnew);\r
-        Py_DECREF(tmp);\r
-        return PyErr_NoMemory();\r
-    }\r
-    Py_UNICODE_COPY(pnew->str, tmp->str, n+1);\r
-    pnew->length = n;\r
-    pnew->hash = tmp->hash;\r
-    Py_DECREF(tmp);\r
-    return (PyObject *)pnew;\r
-}\r
-\r
-PyDoc_STRVAR(unicode_doc,\r
-             "unicode(object='') -> unicode object\n\\r
-unicode(string[, encoding[, errors]]) -> unicode object\n\\r
-\n\\r
-Create a new Unicode object from the given encoded string.\n\\r
-encoding defaults to the current default string encoding.\n\\r
-errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'.");\r
-\r
-PyTypeObject PyUnicode_Type = {\r
-    PyVarObject_HEAD_INIT(&PyType_Type, 0)\r
-    "unicode",              /* tp_name */\r
-    sizeof(PyUnicodeObject),        /* tp_size */\r
-    0,                  /* tp_itemsize */\r
-    /* Slots */\r
-    (destructor)unicode_dealloc,    /* tp_dealloc */\r
-    0,                  /* tp_print */\r
-    0,                  /* tp_getattr */\r
-    0,                  /* tp_setattr */\r
-    0,                  /* tp_compare */\r
-    unicode_repr,           /* tp_repr */\r
-    &unicode_as_number,         /* tp_as_number */\r
-    &unicode_as_sequence,       /* tp_as_sequence */\r
-    &unicode_as_mapping,        /* tp_as_mapping */\r
-    (hashfunc) unicode_hash,        /* tp_hash*/\r
-    0,                  /* tp_call*/\r
-    (reprfunc) unicode_str,     /* tp_str */\r
-    PyObject_GenericGetAttr,        /* tp_getattro */\r
-    0,                  /* tp_setattro */\r
-    &unicode_as_buffer,         /* tp_as_buffer */\r
-    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |\r
-    Py_TPFLAGS_BASETYPE | Py_TPFLAGS_UNICODE_SUBCLASS,  /* tp_flags */\r
-    unicode_doc,            /* tp_doc */\r
-    0,                  /* tp_traverse */\r
-    0,                  /* tp_clear */\r
-    PyUnicode_RichCompare,      /* tp_richcompare */\r
-    0,                  /* tp_weaklistoffset */\r
-    0,                  /* tp_iter */\r
-    0,                  /* tp_iternext */\r
-    unicode_methods,            /* tp_methods */\r
-    0,                  /* tp_members */\r
-    0,                  /* tp_getset */\r
-    &PyBaseString_Type,         /* tp_base */\r
-    0,                  /* tp_dict */\r
-    0,                  /* tp_descr_get */\r
-    0,                  /* tp_descr_set */\r
-    0,                  /* tp_dictoffset */\r
-    0,                  /* tp_init */\r
-    0,                  /* tp_alloc */\r
-    unicode_new,            /* tp_new */\r
-    PyObject_Del,           /* tp_free */\r
-};\r
-\r
-/* Initialize the Unicode implementation */\r
-\r
-void _PyUnicode_Init(void)\r
-{\r
-    /* XXX - move this array to unicodectype.c ? */\r
-    Py_UNICODE linebreak[] = {\r
-        0x000A, /* LINE FEED */\r
-        0x000D, /* CARRIAGE RETURN */\r
-        0x001C, /* FILE SEPARATOR */\r
-        0x001D, /* GROUP SEPARATOR */\r
-        0x001E, /* RECORD SEPARATOR */\r
-        0x0085, /* NEXT LINE */\r
-        0x2028, /* LINE SEPARATOR */\r
-        0x2029, /* PARAGRAPH SEPARATOR */\r
-    };\r
-\r
-    /* Init the implementation */\r
-    if (!unicode_empty) {\r
-        unicode_empty = _PyUnicode_New(0);\r
-        if (!unicode_empty)\r
-            return;\r
-    }\r
-\r
-    if (PyType_Ready(&PyUnicode_Type) < 0)\r
-        Py_FatalError("Can't initialize 'unicode'");\r
-\r
-    /* initialize the linebreak bloom filter */\r
-    bloom_linebreak = make_bloom_mask(\r
-        linebreak, sizeof(linebreak) / sizeof(linebreak[0])\r
-        );\r
-\r
-    PyType_Ready(&EncodingMapType);\r
-\r
-    if (PyType_Ready(&PyFieldNameIter_Type) < 0)\r
-        Py_FatalError("Can't initialize field name iterator type");\r
-\r
-    if (PyType_Ready(&PyFormatterIter_Type) < 0)\r
-        Py_FatalError("Can't initialize formatter iter type");\r
-}\r
-\r
-/* Finalize the Unicode implementation */\r
-\r
-int\r
-PyUnicode_ClearFreeList(void)\r
-{\r
-    int freelist_size = numfree;\r
-    PyUnicodeObject *u;\r
-\r
-    for (u = free_list; u != NULL;) {\r
-        PyUnicodeObject *v = u;\r
-        u = *(PyUnicodeObject **)u;\r
-        if (v->str)\r
-            PyObject_DEL(v->str);\r
-        Py_XDECREF(v->defenc);\r
-        PyObject_Del(v);\r
-        numfree--;\r
-    }\r
-    free_list = NULL;\r
-    assert(numfree == 0);\r
-    return freelist_size;\r
-}\r
-\r
-void\r
-_PyUnicode_Fini(void)\r
-{\r
-    int i;\r
-\r
-    Py_CLEAR(unicode_empty);\r
-\r
-    for (i = 0; i < 256; i++)\r
-        Py_CLEAR(unicode_latin1[i]);\r
-\r
-    (void)PyUnicode_ClearFreeList();\r
-}\r
-\r
-#ifdef __cplusplus\r
-}\r
-#endif\r