]> git.proxmox.com Git - mirror_edk2.git/blobdiff - AppPkg/Applications/Python/Python-2.7.10/Objects/stringlib/string_format.h
edk2: Remove AppPkg, StdLib, StdLibPrivateInternalFiles
[mirror_edk2.git] / AppPkg / Applications / Python / Python-2.7.10 / Objects / stringlib / string_format.h
diff --git a/AppPkg/Applications/Python/Python-2.7.10/Objects/stringlib/string_format.h b/AppPkg/Applications/Python/Python-2.7.10/Objects/stringlib/string_format.h
deleted file mode 100644 (file)
index 77392a9..0000000
+++ /dev/null
@@ -1,1361 +0,0 @@
-/*\r
-    string_format.h -- implementation of string.format().\r
-\r
-    It uses the Objects/stringlib conventions, so that it can be\r
-    compiled for both unicode and string objects.\r
-*/\r
-\r
-\r
-/* Defines for Python 2.6 compatibility */\r
-#if PY_VERSION_HEX < 0x03000000\r
-#define PyLong_FromSsize_t _PyLong_FromSsize_t\r
-#endif\r
-\r
-/* Defines for more efficiently reallocating the string buffer */\r
-#define INITIAL_SIZE_INCREMENT 100\r
-#define SIZE_MULTIPLIER 2\r
-#define MAX_SIZE_INCREMENT  3200\r
-\r
-\r
-/************************************************************************/\r
-/***********   Global data structures and forward declarations  *********/\r
-/************************************************************************/\r
-\r
-/*\r
-   A SubString consists of the characters between two string or\r
-   unicode pointers.\r
-*/\r
-typedef struct {\r
-    STRINGLIB_CHAR *ptr;\r
-    STRINGLIB_CHAR *end;\r
-} SubString;\r
-\r
-\r
-typedef enum {\r
-    ANS_INIT,\r
-    ANS_AUTO,\r
-    ANS_MANUAL\r
-} AutoNumberState;   /* Keep track if we're auto-numbering fields */\r
-\r
-/* Keeps track of our auto-numbering state, and which number field we're on */\r
-typedef struct {\r
-    AutoNumberState an_state;\r
-    int an_field_number;\r
-} AutoNumber;\r
-\r
-\r
-/* forward declaration for recursion */\r
-static PyObject *\r
-build_string(SubString *input, PyObject *args, PyObject *kwargs,\r
-             int recursion_depth, AutoNumber *auto_number);\r
-\r
-\r
-\r
-/************************************************************************/\r
-/**************************  Utility  functions  ************************/\r
-/************************************************************************/\r
-\r
-static void\r
-AutoNumber_Init(AutoNumber *auto_number)\r
-{\r
-    auto_number->an_state = ANS_INIT;\r
-    auto_number->an_field_number = 0;\r
-}\r
-\r
-/* fill in a SubString from a pointer and length */\r
-Py_LOCAL_INLINE(void)\r
-SubString_init(SubString *str, STRINGLIB_CHAR *p, Py_ssize_t len)\r
-{\r
-    str->ptr = p;\r
-    if (p == NULL)\r
-        str->end = NULL;\r
-    else\r
-        str->end = str->ptr + len;\r
-}\r
-\r
-/* return a new string.  if str->ptr is NULL, return None */\r
-Py_LOCAL_INLINE(PyObject *)\r
-SubString_new_object(SubString *str)\r
-{\r
-    if (str->ptr == NULL) {\r
-        Py_INCREF(Py_None);\r
-        return Py_None;\r
-    }\r
-    return STRINGLIB_NEW(str->ptr, str->end - str->ptr);\r
-}\r
-\r
-/* return a new string.  if str->ptr is NULL, return None */\r
-Py_LOCAL_INLINE(PyObject *)\r
-SubString_new_object_or_empty(SubString *str)\r
-{\r
-    if (str->ptr == NULL) {\r
-        return STRINGLIB_NEW(NULL, 0);\r
-    }\r
-    return STRINGLIB_NEW(str->ptr, str->end - str->ptr);\r
-}\r
-\r
-/* Return 1 if an error has been detected switching between automatic\r
-   field numbering and manual field specification, else return 0. Set\r
-   ValueError on error. */\r
-static int\r
-autonumber_state_error(AutoNumberState state, int field_name_is_empty)\r
-{\r
-    if (state == ANS_MANUAL) {\r
-        if (field_name_is_empty) {\r
-            PyErr_SetString(PyExc_ValueError, "cannot switch from "\r
-                            "manual field specification to "\r
-                            "automatic field numbering");\r
-            return 1;\r
-        }\r
-    }\r
-    else {\r
-        if (!field_name_is_empty) {\r
-            PyErr_SetString(PyExc_ValueError, "cannot switch from "\r
-                            "automatic field numbering to "\r
-                            "manual field specification");\r
-            return 1;\r
-        }\r
-    }\r
-    return 0;\r
-}\r
-\r
-\r
-/************************************************************************/\r
-/***********    Output string management functions       ****************/\r
-/************************************************************************/\r
-\r
-typedef struct {\r
-    STRINGLIB_CHAR *ptr;\r
-    STRINGLIB_CHAR *end;\r
-    PyObject *obj;\r
-    Py_ssize_t size_increment;\r
-} OutputString;\r
-\r
-/* initialize an OutputString object, reserving size characters */\r
-static int\r
-output_initialize(OutputString *output, Py_ssize_t size)\r
-{\r
-    output->obj = STRINGLIB_NEW(NULL, size);\r
-    if (output->obj == NULL)\r
-        return 0;\r
-\r
-    output->ptr = STRINGLIB_STR(output->obj);\r
-    output->end = STRINGLIB_LEN(output->obj) + output->ptr;\r
-    output->size_increment = INITIAL_SIZE_INCREMENT;\r
-\r
-    return 1;\r
-}\r
-\r
-/*\r
-    output_extend reallocates the output string buffer.\r
-    It returns a status:  0 for a failed reallocation,\r
-    1 for success.\r
-*/\r
-\r
-static int\r
-output_extend(OutputString *output, Py_ssize_t count)\r
-{\r
-    STRINGLIB_CHAR *startptr = STRINGLIB_STR(output->obj);\r
-    Py_ssize_t curlen = output->ptr - startptr;\r
-    Py_ssize_t maxlen = curlen + count + output->size_increment;\r
-\r
-    if (STRINGLIB_RESIZE(&output->obj, maxlen) < 0)\r
-        return 0;\r
-    startptr = STRINGLIB_STR(output->obj);\r
-    output->ptr = startptr + curlen;\r
-    output->end = startptr + maxlen;\r
-    if (output->size_increment < MAX_SIZE_INCREMENT)\r
-        output->size_increment *= SIZE_MULTIPLIER;\r
-    return 1;\r
-}\r
-\r
-/*\r
-    output_data dumps characters into our output string\r
-    buffer.\r
-\r
-    In some cases, it has to reallocate the string.\r
-\r
-    It returns a status:  0 for a failed reallocation,\r
-    1 for success.\r
-*/\r
-static int\r
-output_data(OutputString *output, const STRINGLIB_CHAR *s, Py_ssize_t count)\r
-{\r
-    if ((count > output->end - output->ptr) && !output_extend(output, count))\r
-        return 0;\r
-    memcpy(output->ptr, s, count * sizeof(STRINGLIB_CHAR));\r
-    output->ptr += count;\r
-    return 1;\r
-}\r
-\r
-/************************************************************************/\r
-/***********  Format string parsing -- integers and identifiers *********/\r
-/************************************************************************/\r
-\r
-static Py_ssize_t\r
-get_integer(const SubString *str)\r
-{\r
-    Py_ssize_t accumulator = 0;\r
-    Py_ssize_t digitval;\r
-    STRINGLIB_CHAR *p;\r
-\r
-    /* empty string is an error */\r
-    if (str->ptr >= str->end)\r
-        return -1;\r
-\r
-    for (p = str->ptr; p < str->end; p++) {\r
-        digitval = STRINGLIB_TODECIMAL(*p);\r
-        if (digitval < 0)\r
-            return -1;\r
-        /*\r
-           Detect possible overflow before it happens:\r
-\r
-              accumulator * 10 + digitval > PY_SSIZE_T_MAX if and only if\r
-              accumulator > (PY_SSIZE_T_MAX - digitval) / 10.\r
-        */\r
-        if (accumulator > (PY_SSIZE_T_MAX - digitval) / 10) {\r
-            PyErr_Format(PyExc_ValueError,\r
-                         "Too many decimal digits in format string");\r
-            return -1;\r
-        }\r
-        accumulator = accumulator * 10 + digitval;\r
-    }\r
-    return accumulator;\r
-}\r
-\r
-/************************************************************************/\r
-/******** Functions to get field objects and specification strings ******/\r
-/************************************************************************/\r
-\r
-/* do the equivalent of obj.name */\r
-static PyObject *\r
-getattr(PyObject *obj, SubString *name)\r
-{\r
-    PyObject *newobj;\r
-    PyObject *str = SubString_new_object(name);\r
-    if (str == NULL)\r
-        return NULL;\r
-    newobj = PyObject_GetAttr(obj, str);\r
-    Py_DECREF(str);\r
-    return newobj;\r
-}\r
-\r
-/* do the equivalent of obj[idx], where obj is a sequence */\r
-static PyObject *\r
-getitem_sequence(PyObject *obj, Py_ssize_t idx)\r
-{\r
-    return PySequence_GetItem(obj, idx);\r
-}\r
-\r
-/* do the equivalent of obj[idx], where obj is not a sequence */\r
-static PyObject *\r
-getitem_idx(PyObject *obj, Py_ssize_t idx)\r
-{\r
-    PyObject *newobj;\r
-    PyObject *idx_obj = PyLong_FromSsize_t(idx);\r
-    if (idx_obj == NULL)\r
-        return NULL;\r
-    newobj = PyObject_GetItem(obj, idx_obj);\r
-    Py_DECREF(idx_obj);\r
-    return newobj;\r
-}\r
-\r
-/* do the equivalent of obj[name] */\r
-static PyObject *\r
-getitem_str(PyObject *obj, SubString *name)\r
-{\r
-    PyObject *newobj;\r
-    PyObject *str = SubString_new_object(name);\r
-    if (str == NULL)\r
-        return NULL;\r
-    newobj = PyObject_GetItem(obj, str);\r
-    Py_DECREF(str);\r
-    return newobj;\r
-}\r
-\r
-typedef struct {\r
-    /* the entire string we're parsing.  we assume that someone else\r
-       is managing its lifetime, and that it will exist for the\r
-       lifetime of the iterator.  can be empty */\r
-    SubString str;\r
-\r
-    /* pointer to where we are inside field_name */\r
-    STRINGLIB_CHAR *ptr;\r
-} FieldNameIterator;\r
-\r
-\r
-static int\r
-FieldNameIterator_init(FieldNameIterator *self, STRINGLIB_CHAR *ptr,\r
-                       Py_ssize_t len)\r
-{\r
-    SubString_init(&self->str, ptr, len);\r
-    self->ptr = self->str.ptr;\r
-    return 1;\r
-}\r
-\r
-static int\r
-_FieldNameIterator_attr(FieldNameIterator *self, SubString *name)\r
-{\r
-    STRINGLIB_CHAR c;\r
-\r
-    name->ptr = self->ptr;\r
-\r
-    /* return everything until '.' or '[' */\r
-    while (self->ptr < self->str.end) {\r
-        switch (c = *self->ptr++) {\r
-        case '[':\r
-        case '.':\r
-            /* backup so that we this character will be seen next time */\r
-            self->ptr--;\r
-            break;\r
-        default:\r
-            continue;\r
-        }\r
-        break;\r
-    }\r
-    /* end of string is okay */\r
-    name->end = self->ptr;\r
-    return 1;\r
-}\r
-\r
-static int\r
-_FieldNameIterator_item(FieldNameIterator *self, SubString *name)\r
-{\r
-    int bracket_seen = 0;\r
-    STRINGLIB_CHAR c;\r
-\r
-    name->ptr = self->ptr;\r
-\r
-    /* return everything until ']' */\r
-    while (self->ptr < self->str.end) {\r
-        switch (c = *self->ptr++) {\r
-        case ']':\r
-            bracket_seen = 1;\r
-            break;\r
-        default:\r
-            continue;\r
-        }\r
-        break;\r
-    }\r
-    /* make sure we ended with a ']' */\r
-    if (!bracket_seen) {\r
-        PyErr_SetString(PyExc_ValueError, "Missing ']' in format string");\r
-        return 0;\r
-    }\r
-\r
-    /* end of string is okay */\r
-    /* don't include the ']' */\r
-    name->end = self->ptr-1;\r
-    return 1;\r
-}\r
-\r
-/* returns 0 on error, 1 on non-error termination, and 2 if it returns a value */\r
-static int\r
-FieldNameIterator_next(FieldNameIterator *self, int *is_attribute,\r
-                       Py_ssize_t *name_idx, SubString *name)\r
-{\r
-    /* check at end of input */\r
-    if (self->ptr >= self->str.end)\r
-        return 1;\r
-\r
-    switch (*self->ptr++) {\r
-    case '.':\r
-        *is_attribute = 1;\r
-        if (_FieldNameIterator_attr(self, name) == 0)\r
-            return 0;\r
-        *name_idx = -1;\r
-        break;\r
-    case '[':\r
-        *is_attribute = 0;\r
-        if (_FieldNameIterator_item(self, name) == 0)\r
-            return 0;\r
-        *name_idx = get_integer(name);\r
-        if (*name_idx == -1 && PyErr_Occurred())\r
-            return 0;\r
-        break;\r
-    default:\r
-        /* Invalid character follows ']' */\r
-        PyErr_SetString(PyExc_ValueError, "Only '.' or '[' may "\r
-                        "follow ']' in format field specifier");\r
-        return 0;\r
-    }\r
-\r
-    /* empty string is an error */\r
-    if (name->ptr == name->end) {\r
-        PyErr_SetString(PyExc_ValueError, "Empty attribute in format string");\r
-        return 0;\r
-    }\r
-\r
-    return 2;\r
-}\r
-\r
-\r
-/* input: field_name\r
-   output: 'first' points to the part before the first '[' or '.'\r
-           'first_idx' is -1 if 'first' is not an integer, otherwise\r
-                       it's the value of first converted to an integer\r
-           'rest' is an iterator to return the rest\r
-*/\r
-static int\r
-field_name_split(STRINGLIB_CHAR *ptr, Py_ssize_t len, SubString *first,\r
-                 Py_ssize_t *first_idx, FieldNameIterator *rest,\r
-                 AutoNumber *auto_number)\r
-{\r
-    STRINGLIB_CHAR c;\r
-    STRINGLIB_CHAR *p = ptr;\r
-    STRINGLIB_CHAR *end = ptr + len;\r
-    int field_name_is_empty;\r
-    int using_numeric_index;\r
-\r
-    /* find the part up until the first '.' or '[' */\r
-    while (p < end) {\r
-        switch (c = *p++) {\r
-        case '[':\r
-        case '.':\r
-            /* backup so that we this character is available to the\r
-               "rest" iterator */\r
-            p--;\r
-            break;\r
-        default:\r
-            continue;\r
-        }\r
-        break;\r
-    }\r
-\r
-    /* set up the return values */\r
-    SubString_init(first, ptr, p - ptr);\r
-    FieldNameIterator_init(rest, p, end - p);\r
-\r
-    /* see if "first" is an integer, in which case it's used as an index */\r
-    *first_idx = get_integer(first);\r
-    if (*first_idx == -1 && PyErr_Occurred())\r
-        return 0;\r
-\r
-    field_name_is_empty = first->ptr >= first->end;\r
-\r
-    /* If the field name is omitted or if we have a numeric index\r
-       specified, then we're doing numeric indexing into args. */\r
-    using_numeric_index = field_name_is_empty || *first_idx != -1;\r
-\r
-    /* We always get here exactly one time for each field we're\r
-       processing. And we get here in field order (counting by left\r
-       braces). So this is the perfect place to handle automatic field\r
-       numbering if the field name is omitted. */\r
-\r
-    /* Check if we need to do the auto-numbering. It's not needed if\r
-       we're called from string.Format routines, because it's handled\r
-       in that class by itself. */\r
-    if (auto_number) {\r
-        /* Initialize our auto numbering state if this is the first\r
-           time we're either auto-numbering or manually numbering. */\r
-        if (auto_number->an_state == ANS_INIT && using_numeric_index)\r
-            auto_number->an_state = field_name_is_empty ?\r
-                ANS_AUTO : ANS_MANUAL;\r
-\r
-        /* Make sure our state is consistent with what we're doing\r
-           this time through. Only check if we're using a numeric\r
-           index. */\r
-        if (using_numeric_index)\r
-            if (autonumber_state_error(auto_number->an_state,\r
-                                       field_name_is_empty))\r
-                return 0;\r
-        /* Zero length field means we want to do auto-numbering of the\r
-           fields. */\r
-        if (field_name_is_empty)\r
-            *first_idx = (auto_number->an_field_number)++;\r
-    }\r
-\r
-    return 1;\r
-}\r
-\r
-\r
-/*\r
-    get_field_object returns the object inside {}, before the\r
-    format_spec.  It handles getindex and getattr lookups and consumes\r
-    the entire input string.\r
-*/\r
-static PyObject *\r
-get_field_object(SubString *input, PyObject *args, PyObject *kwargs,\r
-                 AutoNumber *auto_number)\r
-{\r
-    PyObject *obj = NULL;\r
-    int ok;\r
-    int is_attribute;\r
-    SubString name;\r
-    SubString first;\r
-    Py_ssize_t index;\r
-    FieldNameIterator rest;\r
-\r
-    if (!field_name_split(input->ptr, input->end - input->ptr, &first,\r
-                          &index, &rest, auto_number)) {\r
-        goto error;\r
-    }\r
-\r
-    if (index == -1) {\r
-        /* look up in kwargs */\r
-        PyObject *key = SubString_new_object(&first);\r
-        if (key == NULL)\r
-            goto error;\r
-        if ((kwargs == NULL) || (obj = PyDict_GetItem(kwargs, key)) == NULL) {\r
-            PyErr_SetObject(PyExc_KeyError, key);\r
-            Py_DECREF(key);\r
-            goto error;\r
-        }\r
-        Py_DECREF(key);\r
-        Py_INCREF(obj);\r
-    }\r
-    else {\r
-        /* look up in args */\r
-        obj = PySequence_GetItem(args, index);\r
-        if (obj == NULL)\r
-            goto error;\r
-    }\r
-\r
-    /* iterate over the rest of the field_name */\r
-    while ((ok = FieldNameIterator_next(&rest, &is_attribute, &index,\r
-                                        &name)) == 2) {\r
-        PyObject *tmp;\r
-\r
-        if (is_attribute)\r
-            /* getattr lookup "." */\r
-            tmp = getattr(obj, &name);\r
-        else\r
-            /* getitem lookup "[]" */\r
-            if (index == -1)\r
-                tmp = getitem_str(obj, &name);\r
-            else\r
-                if (PySequence_Check(obj))\r
-                    tmp = getitem_sequence(obj, index);\r
-                else\r
-                    /* not a sequence */\r
-                    tmp = getitem_idx(obj, index);\r
-        if (tmp == NULL)\r
-            goto error;\r
-\r
-        /* assign to obj */\r
-        Py_DECREF(obj);\r
-        obj = tmp;\r
-    }\r
-    /* end of iterator, this is the non-error case */\r
-    if (ok == 1)\r
-        return obj;\r
-error:\r
-    Py_XDECREF(obj);\r
-    return NULL;\r
-}\r
-\r
-/************************************************************************/\r
-/*****************  Field rendering functions  **************************/\r
-/************************************************************************/\r
-\r
-/*\r
-    render_field() is the main function in this section.  It takes the\r
-    field object and field specification string generated by\r
-    get_field_and_spec, and renders the field into the output string.\r
-\r
-    render_field calls fieldobj.__format__(format_spec) method, and\r
-    appends to the output.\r
-*/\r
-static int\r
-render_field(PyObject *fieldobj, SubString *format_spec, OutputString *output)\r
-{\r
-    int ok = 0;\r
-    PyObject *result = NULL;\r
-    PyObject *format_spec_object = NULL;\r
-    PyObject *(*formatter)(PyObject *, STRINGLIB_CHAR *, Py_ssize_t) = NULL;\r
-    STRINGLIB_CHAR* format_spec_start = format_spec->ptr ?\r
-            format_spec->ptr : NULL;\r
-    Py_ssize_t format_spec_len = format_spec->ptr ?\r
-            format_spec->end - format_spec->ptr : 0;\r
-\r
-    /* If we know the type exactly, skip the lookup of __format__ and just\r
-       call the formatter directly. */\r
-#if STRINGLIB_IS_UNICODE\r
-    if (PyUnicode_CheckExact(fieldobj))\r
-        formatter = _PyUnicode_FormatAdvanced;\r
-    /* Unfortunately, there's a problem with checking for int, long,\r
-       and float here.  If we're being included as unicode, their\r
-       formatters expect string format_spec args.  For now, just skip\r
-       this optimization for unicode.  This could be fixed, but it's a\r
-       hassle. */\r
-#else\r
-    if (PyString_CheckExact(fieldobj))\r
-        formatter = _PyBytes_FormatAdvanced;\r
-    else if (PyInt_CheckExact(fieldobj))\r
-        formatter =_PyInt_FormatAdvanced;\r
-    else if (PyLong_CheckExact(fieldobj))\r
-        formatter =_PyLong_FormatAdvanced;\r
-    else if (PyFloat_CheckExact(fieldobj))\r
-        formatter = _PyFloat_FormatAdvanced;\r
-#endif\r
-\r
-    if (formatter) {\r
-        /* we know exactly which formatter will be called when __format__ is\r
-           looked up, so call it directly, instead. */\r
-        result = formatter(fieldobj, format_spec_start, format_spec_len);\r
-    }\r
-    else {\r
-        /* We need to create an object out of the pointers we have, because\r
-           __format__ takes a string/unicode object for format_spec. */\r
-        format_spec_object = STRINGLIB_NEW(format_spec_start,\r
-                                           format_spec_len);\r
-        if (format_spec_object == NULL)\r
-            goto done;\r
-\r
-        result = PyObject_Format(fieldobj, format_spec_object);\r
-    }\r
-    if (result == NULL)\r
-        goto done;\r
-\r
-#if PY_VERSION_HEX >= 0x03000000\r
-    assert(PyUnicode_Check(result));\r
-#else\r
-    assert(PyString_Check(result) || PyUnicode_Check(result));\r
-\r
-    /* Convert result to our type.  We could be str, and result could\r
-       be unicode */\r
-    {\r
-        PyObject *tmp = STRINGLIB_TOSTR(result);\r
-        if (tmp == NULL)\r
-            goto done;\r
-        Py_DECREF(result);\r
-        result = tmp;\r
-    }\r
-#endif\r
-\r
-    ok = output_data(output,\r
-                     STRINGLIB_STR(result), STRINGLIB_LEN(result));\r
-done:\r
-    Py_XDECREF(format_spec_object);\r
-    Py_XDECREF(result);\r
-    return ok;\r
-}\r
-\r
-static int\r
-parse_field(SubString *str, SubString *field_name, SubString *format_spec,\r
-            STRINGLIB_CHAR *conversion)\r
-{\r
-    /* Note this function works if the field name is zero length,\r
-       which is good.  Zero length field names are handled later, in\r
-       field_name_split. */\r
-\r
-    STRINGLIB_CHAR c = 0;\r
-\r
-    /* initialize these, as they may be empty */\r
-    *conversion = '\0';\r
-    SubString_init(format_spec, NULL, 0);\r
-\r
-    /* Search for the field name.  it's terminated by the end of\r
-       the string, or a ':' or '!' */\r
-    field_name->ptr = str->ptr;\r
-    while (str->ptr < str->end) {\r
-        switch (c = *(str->ptr++)) {\r
-        case ':':\r
-        case '!':\r
-            break;\r
-        default:\r
-            continue;\r
-        }\r
-        break;\r
-    }\r
-\r
-    if (c == '!' || c == ':') {\r
-        /* we have a format specifier and/or a conversion */\r
-        /* don't include the last character */\r
-        field_name->end = str->ptr-1;\r
-\r
-        /* the format specifier is the rest of the string */\r
-        format_spec->ptr = str->ptr;\r
-        format_spec->end = str->end;\r
-\r
-        /* see if there's a conversion specifier */\r
-        if (c == '!') {\r
-            /* there must be another character present */\r
-            if (format_spec->ptr >= format_spec->end) {\r
-                PyErr_SetString(PyExc_ValueError,\r
-                                "end of format while looking for conversion "\r
-                                "specifier");\r
-                return 0;\r
-            }\r
-            *conversion = *(format_spec->ptr++);\r
-\r
-            /* if there is another character, it must be a colon */\r
-            if (format_spec->ptr < format_spec->end) {\r
-                c = *(format_spec->ptr++);\r
-                if (c != ':') {\r
-                    PyErr_SetString(PyExc_ValueError,\r
-                                    "expected ':' after format specifier");\r
-                    return 0;\r
-                }\r
-            }\r
-        }\r
-    }\r
-    else\r
-        /* end of string, there's no format_spec or conversion */\r
-        field_name->end = str->ptr;\r
-\r
-    return 1;\r
-}\r
-\r
-/************************************************************************/\r
-/******* Output string allocation and escape-to-markup processing  ******/\r
-/************************************************************************/\r
-\r
-/* MarkupIterator breaks the string into pieces of either literal\r
-   text, or things inside {} that need to be marked up.  it is\r
-   designed to make it easy to wrap a Python iterator around it, for\r
-   use with the Formatter class */\r
-\r
-typedef struct {\r
-    SubString str;\r
-} MarkupIterator;\r
-\r
-static int\r
-MarkupIterator_init(MarkupIterator *self, STRINGLIB_CHAR *ptr, Py_ssize_t len)\r
-{\r
-    SubString_init(&self->str, ptr, len);\r
-    return 1;\r
-}\r
-\r
-/* returns 0 on error, 1 on non-error termination, and 2 if it got a\r
-   string (or something to be expanded) */\r
-static int\r
-MarkupIterator_next(MarkupIterator *self, SubString *literal,\r
-                    int *field_present, SubString *field_name,\r
-                    SubString *format_spec, STRINGLIB_CHAR *conversion,\r
-                    int *format_spec_needs_expanding)\r
-{\r
-    int at_end;\r
-    STRINGLIB_CHAR c = 0;\r
-    STRINGLIB_CHAR *start;\r
-    int count;\r
-    Py_ssize_t len;\r
-    int markup_follows = 0;\r
-\r
-    /* initialize all of the output variables */\r
-    SubString_init(literal, NULL, 0);\r
-    SubString_init(field_name, NULL, 0);\r
-    SubString_init(format_spec, NULL, 0);\r
-    *conversion = '\0';\r
-    *format_spec_needs_expanding = 0;\r
-    *field_present = 0;\r
-\r
-    /* No more input, end of iterator.  This is the normal exit\r
-       path. */\r
-    if (self->str.ptr >= self->str.end)\r
-        return 1;\r
-\r
-    start = self->str.ptr;\r
-\r
-    /* First read any literal text. Read until the end of string, an\r
-       escaped '{' or '}', or an unescaped '{'.  In order to never\r
-       allocate memory and so I can just pass pointers around, if\r
-       there's an escaped '{' or '}' then we'll return the literal\r
-       including the brace, but no format object.  The next time\r
-       through, we'll return the rest of the literal, skipping past\r
-       the second consecutive brace. */\r
-    while (self->str.ptr < self->str.end) {\r
-        switch (c = *(self->str.ptr++)) {\r
-        case '{':\r
-        case '}':\r
-            markup_follows = 1;\r
-            break;\r
-        default:\r
-            continue;\r
-        }\r
-        break;\r
-    }\r
-\r
-    at_end = self->str.ptr >= self->str.end;\r
-    len = self->str.ptr - start;\r
-\r
-    if ((c == '}') && (at_end || (c != *self->str.ptr))) {\r
-        PyErr_SetString(PyExc_ValueError, "Single '}' encountered "\r
-                        "in format string");\r
-        return 0;\r
-    }\r
-    if (at_end && c == '{') {\r
-        PyErr_SetString(PyExc_ValueError, "Single '{' encountered "\r
-                        "in format string");\r
-        return 0;\r
-    }\r
-    if (!at_end) {\r
-        if (c == *self->str.ptr) {\r
-            /* escaped } or {, skip it in the input.  there is no\r
-               markup object following us, just this literal text */\r
-            self->str.ptr++;\r
-            markup_follows = 0;\r
-        }\r
-        else\r
-            len--;\r
-    }\r
-\r
-    /* record the literal text */\r
-    literal->ptr = start;\r
-    literal->end = start + len;\r
-\r
-    if (!markup_follows)\r
-        return 2;\r
-\r
-    /* this is markup, find the end of the string by counting nested\r
-       braces.  note that this prohibits escaped braces, so that\r
-       format_specs cannot have braces in them. */\r
-    *field_present = 1;\r
-    count = 1;\r
-\r
-    start = self->str.ptr;\r
-\r
-    /* we know we can't have a zero length string, so don't worry\r
-       about that case */\r
-    while (self->str.ptr < self->str.end) {\r
-        switch (c = *(self->str.ptr++)) {\r
-        case '{':\r
-            /* the format spec needs to be recursively expanded.\r
-               this is an optimization, and not strictly needed */\r
-            *format_spec_needs_expanding = 1;\r
-            count++;\r
-            break;\r
-        case '}':\r
-            count--;\r
-            if (count <= 0) {\r
-                /* we're done.  parse and get out */\r
-                SubString s;\r
-\r
-                SubString_init(&s, start, self->str.ptr - 1 - start);\r
-                if (parse_field(&s, field_name, format_spec, conversion) == 0)\r
-                    return 0;\r
-\r
-                /* success */\r
-                return 2;\r
-            }\r
-            break;\r
-        }\r
-    }\r
-\r
-    /* end of string while searching for matching '}' */\r
-    PyErr_SetString(PyExc_ValueError, "unmatched '{' in format");\r
-    return 0;\r
-}\r
-\r
-\r
-/* do the !r or !s conversion on obj */\r
-static PyObject *\r
-do_conversion(PyObject *obj, STRINGLIB_CHAR conversion)\r
-{\r
-    /* XXX in pre-3.0, do we need to convert this to unicode, since it\r
-       might have returned a string? */\r
-    switch (conversion) {\r
-    case 'r':\r
-        return PyObject_Repr(obj);\r
-    case 's':\r
-        return STRINGLIB_TOSTR(obj);\r
-    default:\r
-        if (conversion > 32 && conversion < 127) {\r
-                /* It's the ASCII subrange; casting to char is safe\r
-                   (assuming the execution character set is an ASCII\r
-                   superset). */\r
-                PyErr_Format(PyExc_ValueError,\r
-                     "Unknown conversion specifier %c",\r
-                     (char)conversion);\r
-        } else\r
-                PyErr_Format(PyExc_ValueError,\r
-                     "Unknown conversion specifier \\x%x",\r
-                     (unsigned int)conversion);\r
-        return NULL;\r
-    }\r
-}\r
-\r
-/* given:\r
-\r
-   {field_name!conversion:format_spec}\r
-\r
-   compute the result and write it to output.\r
-   format_spec_needs_expanding is an optimization.  if it's false,\r
-   just output the string directly, otherwise recursively expand the\r
-   format_spec string.\r
-\r
-   field_name is allowed to be zero length, in which case we\r
-   are doing auto field numbering.\r
-*/\r
-\r
-static int\r
-output_markup(SubString *field_name, SubString *format_spec,\r
-              int format_spec_needs_expanding, STRINGLIB_CHAR conversion,\r
-              OutputString *output, PyObject *args, PyObject *kwargs,\r
-              int recursion_depth, AutoNumber *auto_number)\r
-{\r
-    PyObject *tmp = NULL;\r
-    PyObject *fieldobj = NULL;\r
-    SubString expanded_format_spec;\r
-    SubString *actual_format_spec;\r
-    int result = 0;\r
-\r
-    /* convert field_name to an object */\r
-    fieldobj = get_field_object(field_name, args, kwargs, auto_number);\r
-    if (fieldobj == NULL)\r
-        goto done;\r
-\r
-    if (conversion != '\0') {\r
-        tmp = do_conversion(fieldobj, conversion);\r
-        if (tmp == NULL)\r
-            goto done;\r
-\r
-        /* do the assignment, transferring ownership: fieldobj = tmp */\r
-        Py_DECREF(fieldobj);\r
-        fieldobj = tmp;\r
-        tmp = NULL;\r
-    }\r
-\r
-    /* if needed, recurively compute the format_spec */\r
-    if (format_spec_needs_expanding) {\r
-        tmp = build_string(format_spec, args, kwargs, recursion_depth-1,\r
-                           auto_number);\r
-        if (tmp == NULL)\r
-            goto done;\r
-\r
-        /* note that in the case we're expanding the format string,\r
-           tmp must be kept around until after the call to\r
-           render_field. */\r
-        SubString_init(&expanded_format_spec,\r
-                       STRINGLIB_STR(tmp), STRINGLIB_LEN(tmp));\r
-        actual_format_spec = &expanded_format_spec;\r
-    }\r
-    else\r
-        actual_format_spec = format_spec;\r
-\r
-    if (render_field(fieldobj, actual_format_spec, output) == 0)\r
-        goto done;\r
-\r
-    result = 1;\r
-\r
-done:\r
-    Py_XDECREF(fieldobj);\r
-    Py_XDECREF(tmp);\r
-\r
-    return result;\r
-}\r
-\r
-/*\r
-    do_markup is the top-level loop for the format() method.  It\r
-    searches through the format string for escapes to markup codes, and\r
-    calls other functions to move non-markup text to the output,\r
-    and to perform the markup to the output.\r
-*/\r
-static int\r
-do_markup(SubString *input, PyObject *args, PyObject *kwargs,\r
-          OutputString *output, int recursion_depth, AutoNumber *auto_number)\r
-{\r
-    MarkupIterator iter;\r
-    int format_spec_needs_expanding;\r
-    int result;\r
-    int field_present;\r
-    SubString literal;\r
-    SubString field_name;\r
-    SubString format_spec;\r
-    STRINGLIB_CHAR conversion;\r
-\r
-    MarkupIterator_init(&iter, input->ptr, input->end - input->ptr);\r
-    while ((result = MarkupIterator_next(&iter, &literal, &field_present,\r
-                                         &field_name, &format_spec,\r
-                                         &conversion,\r
-                                         &format_spec_needs_expanding)) == 2) {\r
-        if (!output_data(output, literal.ptr, literal.end - literal.ptr))\r
-            return 0;\r
-        if (field_present)\r
-            if (!output_markup(&field_name, &format_spec,\r
-                               format_spec_needs_expanding, conversion, output,\r
-                               args, kwargs, recursion_depth, auto_number))\r
-                return 0;\r
-    }\r
-    return result;\r
-}\r
-\r
-\r
-/*\r
-    build_string allocates the output string and then\r
-    calls do_markup to do the heavy lifting.\r
-*/\r
-static PyObject *\r
-build_string(SubString *input, PyObject *args, PyObject *kwargs,\r
-             int recursion_depth, AutoNumber *auto_number)\r
-{\r
-    OutputString output;\r
-    PyObject *result = NULL;\r
-    Py_ssize_t count;\r
-\r
-    output.obj = NULL; /* needed so cleanup code always works */\r
-\r
-    /* check the recursion level */\r
-    if (recursion_depth <= 0) {\r
-        PyErr_SetString(PyExc_ValueError,\r
-                        "Max string recursion exceeded");\r
-        goto done;\r
-    }\r
-\r
-    /* initial size is the length of the format string, plus the size\r
-       increment.  seems like a reasonable default */\r
-    if (!output_initialize(&output,\r
-                           input->end - input->ptr +\r
-                           INITIAL_SIZE_INCREMENT))\r
-        goto done;\r
-\r
-    if (!do_markup(input, args, kwargs, &output, recursion_depth,\r
-                   auto_number)) {\r
-        goto done;\r
-    }\r
-\r
-    count = output.ptr - STRINGLIB_STR(output.obj);\r
-    if (STRINGLIB_RESIZE(&output.obj, count) < 0) {\r
-        goto done;\r
-    }\r
-\r
-    /* transfer ownership to result */\r
-    result = output.obj;\r
-    output.obj = NULL;\r
-\r
-done:\r
-    Py_XDECREF(output.obj);\r
-    return result;\r
-}\r
-\r
-/************************************************************************/\r
-/*********** main routine ***********************************************/\r
-/************************************************************************/\r
-\r
-/* this is the main entry point */\r
-static PyObject *\r
-do_string_format(PyObject *self, PyObject *args, PyObject *kwargs)\r
-{\r
-    SubString input;\r
-\r
-    /* PEP 3101 says only 2 levels, so that\r
-       "{0:{1}}".format('abc', 's')            # works\r
-       "{0:{1:{2}}}".format('abc', 's', '')    # fails\r
-    */\r
-    int recursion_depth = 2;\r
-\r
-    AutoNumber auto_number;\r
-\r
-    AutoNumber_Init(&auto_number);\r
-    SubString_init(&input, STRINGLIB_STR(self), STRINGLIB_LEN(self));\r
-    return build_string(&input, args, kwargs, recursion_depth, &auto_number);\r
-}\r
-\r
-\r
-\r
-/************************************************************************/\r
-/*********** formatteriterator ******************************************/\r
-/************************************************************************/\r
-\r
-/* This is used to implement string.Formatter.vparse().  It exists so\r
-   Formatter can share code with the built in unicode.format() method.\r
-   It's really just a wrapper around MarkupIterator that is callable\r
-   from Python. */\r
-\r
-typedef struct {\r
-    PyObject_HEAD\r
-\r
-    STRINGLIB_OBJECT *str;\r
-\r
-    MarkupIterator it_markup;\r
-} formatteriterobject;\r
-\r
-static void\r
-formatteriter_dealloc(formatteriterobject *it)\r
-{\r
-    Py_XDECREF(it->str);\r
-    PyObject_FREE(it);\r
-}\r
-\r
-/* returns a tuple:\r
-   (literal, field_name, format_spec, conversion)\r
-\r
-   literal is any literal text to output.  might be zero length\r
-   field_name is the string before the ':'.  might be None\r
-   format_spec is the string after the ':'.  mibht be None\r
-   conversion is either None, or the string after the '!'\r
-*/\r
-static PyObject *\r
-formatteriter_next(formatteriterobject *it)\r
-{\r
-    SubString literal;\r
-    SubString field_name;\r
-    SubString format_spec;\r
-    STRINGLIB_CHAR conversion;\r
-    int format_spec_needs_expanding;\r
-    int field_present;\r
-    int result = MarkupIterator_next(&it->it_markup, &literal, &field_present,\r
-                                     &field_name, &format_spec, &conversion,\r
-                                     &format_spec_needs_expanding);\r
-\r
-    /* all of the SubString objects point into it->str, so no\r
-       memory management needs to be done on them */\r
-    assert(0 <= result && result <= 2);\r
-    if (result == 0 || result == 1)\r
-        /* if 0, error has already been set, if 1, iterator is empty */\r
-        return NULL;\r
-    else {\r
-        PyObject *literal_str = NULL;\r
-        PyObject *field_name_str = NULL;\r
-        PyObject *format_spec_str = NULL;\r
-        PyObject *conversion_str = NULL;\r
-        PyObject *tuple = NULL;\r
-\r
-        literal_str = SubString_new_object(&literal);\r
-        if (literal_str == NULL)\r
-            goto done;\r
-\r
-        field_name_str = SubString_new_object(&field_name);\r
-        if (field_name_str == NULL)\r
-            goto done;\r
-\r
-        /* if field_name is non-zero length, return a string for\r
-           format_spec (even if zero length), else return None */\r
-        format_spec_str = (field_present ?\r
-                           SubString_new_object_or_empty :\r
-                           SubString_new_object)(&format_spec);\r
-        if (format_spec_str == NULL)\r
-            goto done;\r
-\r
-        /* if the conversion is not specified, return a None,\r
-           otherwise create a one length string with the conversion\r
-           character */\r
-        if (conversion == '\0') {\r
-            conversion_str = Py_None;\r
-            Py_INCREF(conversion_str);\r
-        }\r
-        else\r
-            conversion_str = STRINGLIB_NEW(&conversion, 1);\r
-        if (conversion_str == NULL)\r
-            goto done;\r
-\r
-        tuple = PyTuple_Pack(4, literal_str, field_name_str, format_spec_str,\r
-                             conversion_str);\r
-    done:\r
-        Py_XDECREF(literal_str);\r
-        Py_XDECREF(field_name_str);\r
-        Py_XDECREF(format_spec_str);\r
-        Py_XDECREF(conversion_str);\r
-        return tuple;\r
-    }\r
-}\r
-\r
-static PyMethodDef formatteriter_methods[] = {\r
-    {NULL,              NULL}           /* sentinel */\r
-};\r
-\r
-static PyTypeObject PyFormatterIter_Type = {\r
-    PyVarObject_HEAD_INIT(&PyType_Type, 0)\r
-    "formatteriterator",                /* tp_name */\r
-    sizeof(formatteriterobject),        /* tp_basicsize */\r
-    0,                                  /* tp_itemsize */\r
-    /* methods */\r
-    (destructor)formatteriter_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
-    PyObject_GenericGetAttr,            /* 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
-    PyObject_SelfIter,                  /* tp_iter */\r
-    (iternextfunc)formatteriter_next,   /* tp_iternext */\r
-    formatteriter_methods,              /* tp_methods */\r
-    0,\r
-};\r
-\r
-/* unicode_formatter_parser is used to implement\r
-   string.Formatter.vformat.  it parses a string and returns tuples\r
-   describing the parsed elements.  It's a wrapper around\r
-   stringlib/string_format.h's MarkupIterator */\r
-static PyObject *\r
-formatter_parser(STRINGLIB_OBJECT *self)\r
-{\r
-    formatteriterobject *it;\r
-\r
-    it = PyObject_New(formatteriterobject, &PyFormatterIter_Type);\r
-    if (it == NULL)\r
-        return NULL;\r
-\r
-    /* take ownership, give the object to the iterator */\r
-    Py_INCREF(self);\r
-    it->str = self;\r
-\r
-    /* initialize the contained MarkupIterator */\r
-    MarkupIterator_init(&it->it_markup,\r
-                        STRINGLIB_STR(self),\r
-                        STRINGLIB_LEN(self));\r
-\r
-    return (PyObject *)it;\r
-}\r
-\r
-\r
-/************************************************************************/\r
-/*********** fieldnameiterator ******************************************/\r
-/************************************************************************/\r
-\r
-\r
-/* This is used to implement string.Formatter.vparse().  It parses the\r
-   field name into attribute and item values.  It's a Python-callable\r
-   wrapper around FieldNameIterator */\r
-\r
-typedef struct {\r
-    PyObject_HEAD\r
-\r
-    STRINGLIB_OBJECT *str;\r
-\r
-    FieldNameIterator it_field;\r
-} fieldnameiterobject;\r
-\r
-static void\r
-fieldnameiter_dealloc(fieldnameiterobject *it)\r
-{\r
-    Py_XDECREF(it->str);\r
-    PyObject_FREE(it);\r
-}\r
-\r
-/* returns a tuple:\r
-   (is_attr, value)\r
-   is_attr is true if we used attribute syntax (e.g., '.foo')\r
-              false if we used index syntax (e.g., '[foo]')\r
-   value is an integer or string\r
-*/\r
-static PyObject *\r
-fieldnameiter_next(fieldnameiterobject *it)\r
-{\r
-    int result;\r
-    int is_attr;\r
-    Py_ssize_t idx;\r
-    SubString name;\r
-\r
-    result = FieldNameIterator_next(&it->it_field, &is_attr,\r
-                                    &idx, &name);\r
-    if (result == 0 || result == 1)\r
-        /* if 0, error has already been set, if 1, iterator is empty */\r
-        return NULL;\r
-    else {\r
-        PyObject* result = NULL;\r
-        PyObject* is_attr_obj = NULL;\r
-        PyObject* obj = NULL;\r
-\r
-        is_attr_obj = PyBool_FromLong(is_attr);\r
-        if (is_attr_obj == NULL)\r
-            goto done;\r
-\r
-        /* either an integer or a string */\r
-        if (idx != -1)\r
-            obj = PyLong_FromSsize_t(idx);\r
-        else\r
-            obj = SubString_new_object(&name);\r
-        if (obj == NULL)\r
-            goto done;\r
-\r
-        /* return a tuple of values */\r
-        result = PyTuple_Pack(2, is_attr_obj, obj);\r
-\r
-    done:\r
-        Py_XDECREF(is_attr_obj);\r
-        Py_XDECREF(obj);\r
-        return result;\r
-    }\r
-}\r
-\r
-static PyMethodDef fieldnameiter_methods[] = {\r
-    {NULL,              NULL}           /* sentinel */\r
-};\r
-\r
-static PyTypeObject PyFieldNameIter_Type = {\r
-    PyVarObject_HEAD_INIT(&PyType_Type, 0)\r
-    "fieldnameiterator",                /* tp_name */\r
-    sizeof(fieldnameiterobject),        /* tp_basicsize */\r
-    0,                                  /* tp_itemsize */\r
-    /* methods */\r
-    (destructor)fieldnameiter_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
-    PyObject_GenericGetAttr,            /* 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
-    PyObject_SelfIter,                  /* tp_iter */\r
-    (iternextfunc)fieldnameiter_next,   /* tp_iternext */\r
-    fieldnameiter_methods,              /* tp_methods */\r
-    0};\r
-\r
-/* unicode_formatter_field_name_split is used to implement\r
-   string.Formatter.vformat.  it takes an PEP 3101 "field name", and\r
-   returns a tuple of (first, rest): "first", the part before the\r
-   first '.' or '['; and "rest", an iterator for the rest of the field\r
-   name.  it's a wrapper around stringlib/string_format.h's\r
-   field_name_split.  The iterator it returns is a\r
-   FieldNameIterator */\r
-static PyObject *\r
-formatter_field_name_split(STRINGLIB_OBJECT *self)\r
-{\r
-    SubString first;\r
-    Py_ssize_t first_idx;\r
-    fieldnameiterobject *it;\r
-\r
-    PyObject *first_obj = NULL;\r
-    PyObject *result = NULL;\r
-\r
-    it = PyObject_New(fieldnameiterobject, &PyFieldNameIter_Type);\r
-    if (it == NULL)\r
-        return NULL;\r
-\r
-    /* take ownership, give the object to the iterator.  this is\r
-       just to keep the field_name alive */\r
-    Py_INCREF(self);\r
-    it->str = self;\r
-\r
-    /* Pass in auto_number = NULL. We'll return an empty string for\r
-       first_obj in that case. */\r
-    if (!field_name_split(STRINGLIB_STR(self),\r
-                          STRINGLIB_LEN(self),\r
-                          &first, &first_idx, &it->it_field, NULL))\r
-        goto done;\r
-\r
-    /* first becomes an integer, if possible; else a string */\r
-    if (first_idx != -1)\r
-        first_obj = PyLong_FromSsize_t(first_idx);\r
-    else\r
-        /* convert "first" into a string object */\r
-        first_obj = SubString_new_object(&first);\r
-    if (first_obj == NULL)\r
-        goto done;\r
-\r
-    /* return a tuple of values */\r
-    result = PyTuple_Pack(2, first_obj, it);\r
-\r
-done:\r
-    Py_XDECREF(it);\r
-    Py_XDECREF(first_obj);\r
-    return result;\r
-}\r