Back to index

python3.2  3.2.2
Classes | Defines | Enumerations | Functions | Variables
string_format.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  SubString
struct  AutoNumber
struct  FieldNameIterator
struct  MarkupIterator
struct  formatteriterobject
struct  fieldnameiterobject

Defines

#define PyLong_FromSsize_t   _PyLong_FromSsize_t
#define INITIAL_SIZE_INCREMENT   100
#define SIZE_MULTIPLIER   2
#define MAX_SIZE_INCREMENT   3200

Enumerations

enum  AutoNumberState { ANS_INIT, ANS_AUTO, ANS_MANUAL }

Functions

static PyObjectbuild_string (SubString *input, PyObject *args, PyObject *kwargs, int recursion_depth, AutoNumber *auto_number)
static void AutoNumber_Init (AutoNumber *auto_number)
 Py_LOCAL_INLINE (void) SubString_init(SubString *str
static int output_initialize (OutputString *output, Py_ssize_t size)
static int output_extend (OutputString *output, Py_ssize_t count)
static int output_data (OutputString *output, const STRINGLIB_CHAR *s, Py_ssize_t count)
static Py_ssize_t get_integer (const SubString *str)
static PyObjectgetattr (PyObject *obj, SubString *name)
static PyObjectgetitem_sequence (PyObject *obj, Py_ssize_t idx)
static PyObjectgetitem_idx (PyObject *obj, Py_ssize_t idx)
static PyObjectgetitem_str (PyObject *obj, SubString *name)
static int FieldNameIterator_init (FieldNameIterator *self, STRINGLIB_CHAR *ptr, Py_ssize_t len)
static int _FieldNameIterator_attr (FieldNameIterator *self, SubString *name)
static int _FieldNameIterator_item (FieldNameIterator *self, SubString *name)
static int FieldNameIterator_next (FieldNameIterator *self, int *is_attribute, Py_ssize_t *name_idx, SubString *name)
static int field_name_split (STRINGLIB_CHAR *ptr, Py_ssize_t len, SubString *first, Py_ssize_t *first_idx, FieldNameIterator *rest, AutoNumber *auto_number)
static PyObjectget_field_object (SubString *input, PyObject *args, PyObject *kwargs, AutoNumber *auto_number)
static int render_field (PyObject *fieldobj, SubString *format_spec, OutputString *output)
static int parse_field (SubString *str, SubString *field_name, SubString *format_spec, STRINGLIB_CHAR *conversion)
static int MarkupIterator_init (MarkupIterator *self, STRINGLIB_CHAR *ptr, Py_ssize_t len)
static int MarkupIterator_next (MarkupIterator *self, SubString *literal, int *field_present, SubString *field_name, SubString *format_spec, STRINGLIB_CHAR *conversion, int *format_spec_needs_expanding)
static PyObjectdo_conversion (PyObject *obj, STRINGLIB_CHAR conversion)
static int output_markup (SubString *field_name, SubString *format_spec, int format_spec_needs_expanding, STRINGLIB_CHAR conversion, OutputString *output, PyObject *args, PyObject *kwargs, int recursion_depth, AutoNumber *auto_number)
static int do_markup (SubString *input, PyObject *args, PyObject *kwargs, OutputString *output, int recursion_depth, AutoNumber *auto_number)
static PyObjectdo_string_format (PyObject *self, PyObject *args, PyObject *kwargs)
static PyObjectdo_string_format_map (PyObject *self, PyObject *obj)
static void formatteriter_dealloc (formatteriterobject *it)
static PyObjectformatteriter_next (formatteriterobject *it)
static PyObjectformatter_parser (PyObject *ignored, STRINGLIB_OBJECT *self)
static void fieldnameiter_dealloc (fieldnameiterobject *it)
static PyObjectfieldnameiter_next (fieldnameiterobject *it)
static PyObjectformatter_field_name_split (PyObject *ignored, STRINGLIB_OBJECT *self)

Variables

STRINGLIB_CHARp
static PyMethodDef formatteriter_methods []
static PyTypeObject PyFormatterIter_Type
static PyMethodDef fieldnameiter_methods []
static PyTypeObject PyFieldNameIter_Type

Class Documentation

struct SubString

Definition at line 28 of file string_format.h.

Class Members
STRINGLIB_CHAR * end
STRINGLIB_CHAR * ptr
struct AutoNumber

Definition at line 41 of file string_format.h.

Class Members
int an_field_number
AutoNumberState an_state
struct FieldNameIterator

Definition at line 279 of file string_format.h.

Collaboration diagram for FieldNameIterator:
Class Members
STRINGLIB_CHAR * ptr
SubString str
struct MarkupIterator

Definition at line 716 of file string_format.h.

Collaboration diagram for MarkupIterator:
Class Members
SubString str
struct formatteriterobject

Definition at line 1071 of file string_format.h.

Collaboration diagram for formatteriterobject:
Class Members
MarkupIterator it_markup
PyObject_HEAD STRINGLIB_OBJECT * str
struct fieldnameiterobject

Definition at line 1236 of file string_format.h.

Collaboration diagram for fieldnameiterobject:
Class Members
FieldNameIterator it_field
PyObject_HEAD STRINGLIB_OBJECT * str

Define Documentation

#define INITIAL_SIZE_INCREMENT   100

Definition at line 15 of file string_format.h.

#define MAX_SIZE_INCREMENT   3200

Definition at line 17 of file string_format.h.

#define PyLong_FromSsize_t   _PyLong_FromSsize_t

Definition at line 11 of file string_format.h.

#define SIZE_MULTIPLIER   2

Definition at line 16 of file string_format.h.


Enumeration Type Documentation

Enumerator:
ANS_INIT 
ANS_AUTO 
ANS_MANUAL 

Definition at line 34 of file string_format.h.

             {
    ANS_INIT,
    ANS_AUTO,
    ANS_MANUAL
} AutoNumberState;   /* Keep track if we're auto-numbering fields */

Function Documentation

static int _FieldNameIterator_attr ( FieldNameIterator self,
SubString name 
) [static]

Definition at line 300 of file string_format.h.

{
    STRINGLIB_CHAR c;

    name->ptr = self->ptr;

    /* return everything until '.' or '[' */
    while (self->ptr < self->str.end) {
        switch (c = *self->ptr++) {
        case '[':
        case '.':
            /* backup so that we this character will be seen next time */
            self->ptr--;
            break;
        default:
            continue;
        }
        break;
    }
    /* end of string is okay */
    name->end = self->ptr;
    return 1;
}

Here is the caller graph for this function:

static int _FieldNameIterator_item ( FieldNameIterator self,
SubString name 
) [static]

Definition at line 325 of file string_format.h.

{
    int bracket_seen = 0;
    STRINGLIB_CHAR c;

    name->ptr = self->ptr;

    /* return everything until ']' */
    while (self->ptr < self->str.end) {
        switch (c = *self->ptr++) {
        case ']':
            bracket_seen = 1;
            break;
        default:
            continue;
        }
        break;
    }
    /* make sure we ended with a ']' */
    if (!bracket_seen) {
        PyErr_SetString(PyExc_ValueError, "Missing ']' in format string");
        return 0;
    }

    /* end of string is okay */
    /* don't include the ']' */
    name->end = self->ptr-1;
    return 1;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void AutoNumber_Init ( AutoNumber auto_number) [static]

Definition at line 59 of file string_format.h.

{
    auto_number->an_state = ANS_INIT;
    auto_number->an_field_number = 0;
}

Here is the caller graph for this function:

static PyObject * build_string ( SubString input,
PyObject args,
PyObject kwargs,
int  recursion_depth,
AutoNumber auto_number 
) [static]

Definition at line 990 of file string_format.h.

{
    OutputString output;
    PyObject *result = NULL;
    Py_ssize_t count;

    output.obj = NULL; /* needed so cleanup code always works */

    /* check the recursion level */
    if (recursion_depth <= 0) {
        PyErr_SetString(PyExc_ValueError,
                        "Max string recursion exceeded");
        goto done;
    }

    /* initial size is the length of the format string, plus the size
       increment.  seems like a reasonable default */
    if (!output_initialize(&output,
                           input->end - input->ptr +
                           INITIAL_SIZE_INCREMENT))
        goto done;

    if (!do_markup(input, args, kwargs, &output, recursion_depth,
                   auto_number)) {
        goto done;
    }

    count = output.ptr - STRINGLIB_STR(output.obj);
    if (STRINGLIB_RESIZE(&output.obj, count) < 0) {
        goto done;
    }

    /* transfer ownership to result */
    result = output.obj;
    output.obj = NULL;

done:
    Py_XDECREF(output.obj);
    return result;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static PyObject* do_conversion ( PyObject obj,
STRINGLIB_CHAR  conversion 
) [static]

Definition at line 850 of file string_format.h.

{
    /* XXX in pre-3.0, do we need to convert this to unicode, since it
       might have returned a string? */
    switch (conversion) {
    case 'r':
        return PyObject_Repr(obj);
    case 's':
        return STRINGLIB_TOSTR(obj);
#if PY_VERSION_HEX >= 0x03000000
    case 'a':
        return STRINGLIB_TOASCII(obj);
#endif
    default:
        if (conversion > 32 && conversion < 127) {
                /* It's the ASCII subrange; casting to char is safe
                   (assuming the execution character set is an ASCII
                   superset). */
                PyErr_Format(PyExc_ValueError,
                     "Unknown conversion specifier %c",
                     (char)conversion);
        } else
                PyErr_Format(PyExc_ValueError,
                     "Unknown conversion specifier \\x%x",
                     (unsigned int)conversion);
        return NULL;
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int do_markup ( SubString input,
PyObject args,
PyObject kwargs,
OutputString *  output,
int  recursion_depth,
AutoNumber auto_number 
) [static]

Definition at line 956 of file string_format.h.

{
    MarkupIterator iter;
    int format_spec_needs_expanding;
    int result;
    int field_present;
    SubString literal;
    SubString field_name;
    SubString format_spec;
    STRINGLIB_CHAR conversion;

    MarkupIterator_init(&iter, input->ptr, input->end - input->ptr);
    while ((result = MarkupIterator_next(&iter, &literal, &field_present,
                                         &field_name, &format_spec,
                                         &conversion,
                                         &format_spec_needs_expanding)) == 2) {
        if (!output_data(output, literal.ptr, literal.end - literal.ptr))
            return 0;
        if (field_present)
            if (!output_markup(&field_name, &format_spec,
                               format_spec_needs_expanding, conversion, output,
                               args, kwargs, recursion_depth, auto_number))
                return 0;
    }
    return result;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static PyObject* do_string_format ( PyObject self,
PyObject args,
PyObject kwargs 
) [static]

Definition at line 1038 of file string_format.h.

{
    SubString input;

    /* PEP 3101 says only 2 levels, so that
       "{0:{1}}".format('abc', 's')            # works
       "{0:{1:{2}}}".format('abc', 's', '')    # fails
    */
    int recursion_depth = 2;

    AutoNumber auto_number;

    AutoNumber_Init(&auto_number);
    SubString_init(&input, STRINGLIB_STR(self), STRINGLIB_LEN(self));
    return build_string(&input, args, kwargs, recursion_depth, &auto_number);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static PyObject* do_string_format_map ( PyObject self,
PyObject obj 
) [static]

Definition at line 1056 of file string_format.h.

{
    return do_string_format(self, NULL, obj);
}

Here is the call graph for this function:

static int field_name_split ( STRINGLIB_CHAR ptr,
Py_ssize_t  len,
SubString first,
Py_ssize_t first_idx,
FieldNameIterator rest,
AutoNumber auto_number 
) [static]

Definition at line 403 of file string_format.h.

{
    STRINGLIB_CHAR c;
    STRINGLIB_CHAR *p = ptr;
    STRINGLIB_CHAR *end = ptr + len;
    int field_name_is_empty;
    int using_numeric_index;

    /* find the part up until the first '.' or '[' */
    while (p < end) {
        switch (c = *p++) {
        case '[':
        case '.':
            /* backup so that we this character is available to the
               "rest" iterator */
            p--;
            break;
        default:
            continue;
        }
        break;
    }

    /* set up the return values */
    SubString_init(first, ptr, p - ptr);
    FieldNameIterator_init(rest, p, end - p);

    /* see if "first" is an integer, in which case it's used as an index */
    *first_idx = get_integer(first);
    if (*first_idx == -1 && PyErr_Occurred())
        return 0;

    field_name_is_empty = first->ptr >= first->end;

    /* If the field name is omitted or if we have a numeric index
       specified, then we're doing numeric indexing into args. */
    using_numeric_index = field_name_is_empty || *first_idx != -1;

    /* We always get here exactly one time for each field we're
       processing. And we get here in field order (counting by left
       braces). So this is the perfect place to handle automatic field
       numbering if the field name is omitted. */

    /* Check if we need to do the auto-numbering. It's not needed if
       we're called from string.Format routines, because it's handled
       in that class by itself. */
    if (auto_number) {
        /* Initialize our auto numbering state if this is the first
           time we're either auto-numbering or manually numbering. */
        if (auto_number->an_state == ANS_INIT && using_numeric_index)
            auto_number->an_state = field_name_is_empty ?
                ANS_AUTO : ANS_MANUAL;

        /* Make sure our state is consistent with what we're doing
           this time through. Only check if we're using a numeric
           index. */
        if (using_numeric_index)
            if (autonumber_state_error(auto_number->an_state,
                                       field_name_is_empty))
                return 0;
        /* Zero length field means we want to do auto-numbering of the
           fields. */
        if (field_name_is_empty)
            *first_idx = (auto_number->an_field_number)++;
    }

    return 1;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void fieldnameiter_dealloc ( fieldnameiterobject it) [static]

Definition at line 1245 of file string_format.h.

{
    Py_XDECREF(it->str);
    PyObject_FREE(it);
}
static PyObject* fieldnameiter_next ( fieldnameiterobject it) [static]

Definition at line 1258 of file string_format.h.

{
    int result;
    int is_attr;
    Py_ssize_t idx;
    SubString name;

    result = FieldNameIterator_next(&it->it_field, &is_attr,
                                    &idx, &name);
    if (result == 0 || result == 1)
        /* if 0, error has already been set, if 1, iterator is empty */
        return NULL;
    else {
        PyObject* result = NULL;
        PyObject* is_attr_obj = NULL;
        PyObject* obj = NULL;

        is_attr_obj = PyBool_FromLong(is_attr);
        if (is_attr_obj == NULL)
            goto done;

        /* either an integer or a string */
        if (idx != -1)
            obj = PyLong_FromSsize_t(idx);
        else
            obj = SubString_new_object(&name);
        if (obj == NULL)
            goto done;

        /* return a tuple of values */
        result = PyTuple_Pack(2, is_attr_obj, obj);

    done:
        Py_XDECREF(is_attr_obj);
        Py_XDECREF(obj);
        return result;
    }
}

Here is the call graph for this function:

static int FieldNameIterator_init ( FieldNameIterator self,
STRINGLIB_CHAR ptr,
Py_ssize_t  len 
) [static]

Definition at line 291 of file string_format.h.

{
    SubString_init(&self->str, ptr, len);
    self->ptr = self->str.ptr;
    return 1;
}

Here is the caller graph for this function:

static int FieldNameIterator_next ( FieldNameIterator self,
int is_attribute,
Py_ssize_t name_idx,
SubString name 
) [static]

Definition at line 357 of file string_format.h.

{
    /* check at end of input */
    if (self->ptr >= self->str.end)
        return 1;

    switch (*self->ptr++) {
    case '.':
        *is_attribute = 1;
        if (_FieldNameIterator_attr(self, name) == 0)
            return 0;
        *name_idx = -1;
        break;
    case '[':
        *is_attribute = 0;
        if (_FieldNameIterator_item(self, name) == 0)
            return 0;
        *name_idx = get_integer(name);
        if (*name_idx == -1 && PyErr_Occurred())
            return 0;
        break;
    default:
        /* Invalid character follows ']' */
        PyErr_SetString(PyExc_ValueError, "Only '.' or '[' may "
                        "follow ']' in format field specifier");
        return 0;
    }

    /* empty string is an error */
    if (name->ptr == name->end) {
        PyErr_SetString(PyExc_ValueError, "Empty attribute in format string");
        return 0;
    }

    return 2;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static PyObject* formatter_field_name_split ( PyObject ignored,
STRINGLIB_OBJECT self 
) [static]

Definition at line 1341 of file string_format.h.

{
    SubString first;
    Py_ssize_t first_idx;
    fieldnameiterobject *it;

    PyObject *first_obj = NULL;
    PyObject *result = NULL;

    if (!PyUnicode_Check(self)) {
        PyErr_Format(PyExc_TypeError, "expected str, got %s", Py_TYPE(self)->tp_name);
        return NULL;
    }

    it = PyObject_New(fieldnameiterobject, &PyFieldNameIter_Type);
    if (it == NULL)
        return NULL;

    /* take ownership, give the object to the iterator.  this is
       just to keep the field_name alive */
    Py_INCREF(self);
    it->str = self;

    /* Pass in auto_number = NULL. We'll return an empty string for
       first_obj in that case. */
    if (!field_name_split(STRINGLIB_STR(self),
                          STRINGLIB_LEN(self),
                          &first, &first_idx, &it->it_field, NULL))
        goto done;

    /* first becomes an integer, if possible; else a string */
    if (first_idx != -1)
        first_obj = PyLong_FromSsize_t(first_idx);
    else
        /* convert "first" into a string object */
        first_obj = SubString_new_object(&first);
    if (first_obj == NULL)
        goto done;

    /* return a tuple of values */
    result = PyTuple_Pack(2, first_obj, it);

done:
    Py_XDECREF(it);
    Py_XDECREF(first_obj);
    return result;
}

Here is the call graph for this function:

static PyObject* formatter_parser ( PyObject ignored,
STRINGLIB_OBJECT self 
) [static]

Definition at line 1201 of file string_format.h.

{
    formatteriterobject *it;

    if (!PyUnicode_Check(self)) {
        PyErr_Format(PyExc_TypeError, "expected str, got %s", Py_TYPE(self)->tp_name);
        return NULL;
    }

    it = PyObject_New(formatteriterobject, &PyFormatterIter_Type);
    if (it == NULL)
        return NULL;

    /* take ownership, give the object to the iterator */
    Py_INCREF(self);
    it->str = self;

    /* initialize the contained MarkupIterator */
    MarkupIterator_init(&it->it_markup,
                        STRINGLIB_STR(self),
                        STRINGLIB_LEN(self));

    return (PyObject *)it;
}

Here is the call graph for this function:

static void formatteriter_dealloc ( formatteriterobject it) [static]

Definition at line 1080 of file string_format.h.

{
    Py_XDECREF(it->str);
    PyObject_FREE(it);
}
static PyObject* formatteriter_next ( formatteriterobject it) [static]

Definition at line 1095 of file string_format.h.

{
    SubString literal;
    SubString field_name;
    SubString format_spec;
    STRINGLIB_CHAR conversion;
    int format_spec_needs_expanding;
    int field_present;
    int result = MarkupIterator_next(&it->it_markup, &literal, &field_present,
                                     &field_name, &format_spec, &conversion,
                                     &format_spec_needs_expanding);

    /* all of the SubString objects point into it->str, so no
       memory management needs to be done on them */
    assert(0 <= result && result <= 2);
    if (result == 0 || result == 1)
        /* if 0, error has already been set, if 1, iterator is empty */
        return NULL;
    else {
        PyObject *literal_str = NULL;
        PyObject *field_name_str = NULL;
        PyObject *format_spec_str = NULL;
        PyObject *conversion_str = NULL;
        PyObject *tuple = NULL;

        literal_str = SubString_new_object(&literal);
        if (literal_str == NULL)
            goto done;

        field_name_str = SubString_new_object(&field_name);
        if (field_name_str == NULL)
            goto done;

        /* if field_name is non-zero length, return a string for
           format_spec (even if zero length), else return None */
        format_spec_str = (field_present ?
                           SubString_new_object_or_empty :
                           SubString_new_object)(&format_spec);
        if (format_spec_str == NULL)
            goto done;

        /* if the conversion is not specified, return a None,
           otherwise create a one length string with the conversion
           character */
        if (conversion == '\0') {
            conversion_str = Py_None;
            Py_INCREF(conversion_str);
        }
        else
            conversion_str = STRINGLIB_NEW(&conversion, 1);
        if (conversion_str == NULL)
            goto done;

        tuple = PyTuple_Pack(4, literal_str, field_name_str, format_spec_str,
                             conversion_str);
    done:
        Py_XDECREF(literal_str);
        Py_XDECREF(field_name_str);
        Py_XDECREF(format_spec_str);
        Py_XDECREF(conversion_str);
        return tuple;
    }
}

Here is the call graph for this function:

static PyObject* get_field_object ( SubString input,
PyObject args,
PyObject kwargs,
AutoNumber auto_number 
) [static]

Definition at line 481 of file string_format.h.

{
    PyObject *obj = NULL;
    int ok;
    int is_attribute;
    SubString name;
    SubString first;
    Py_ssize_t index;
    FieldNameIterator rest;

    if (!field_name_split(input->ptr, input->end - input->ptr, &first,
                          &index, &rest, auto_number)) {
        goto error;
    }

    if (index == -1) {
        /* look up in kwargs */
        PyObject *key = SubString_new_object(&first);
        if (key == NULL)
            goto error;

        /* Use PyObject_GetItem instead of PyDict_GetItem because this
           code is no longer just used with kwargs. It might be passed
           a non-dict when called through format_map. */
        if ((kwargs == NULL) || (obj = PyObject_GetItem(kwargs, key)) == NULL) {
            PyErr_SetObject(PyExc_KeyError, key);
            Py_DECREF(key);
            goto error;
        }
        Py_DECREF(key);
    }
    else {
        /* If args is NULL, we have a format string with a positional field
           with only kwargs to retrieve it from. This can only happen when
           used with format_map(), where positional arguments are not
           allowed. */
        if (args == NULL) {
            PyErr_SetString(PyExc_ValueError, "Format string contains "
                            "positional fields");
            goto error;
        }

        /* look up in args */
        obj = PySequence_GetItem(args, index);
        if (obj == NULL)
            goto error;
    }

    /* iterate over the rest of the field_name */
    while ((ok = FieldNameIterator_next(&rest, &is_attribute, &index,
                                        &name)) == 2) {
        PyObject *tmp;

        if (is_attribute)
            /* getattr lookup "." */
            tmp = getattr(obj, &name);
        else
            /* getitem lookup "[]" */
            if (index == -1)
                tmp = getitem_str(obj, &name);
            else
                if (PySequence_Check(obj))
                    tmp = getitem_sequence(obj, index);
                else
                    /* not a sequence */
                    tmp = getitem_idx(obj, index);
        if (tmp == NULL)
            goto error;

        /* assign to obj */
        Py_DECREF(obj);
        obj = tmp;
    }
    /* end of iterator, this is the non-error case */
    if (ok == 1)
        return obj;
error:
    Py_XDECREF(obj);
    return NULL;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static Py_ssize_t get_integer ( const SubString str) [static]

Definition at line 196 of file string_format.h.

{
    Py_ssize_t accumulator = 0;
    Py_ssize_t digitval;
    Py_ssize_t oldaccumulator;
    STRINGLIB_CHAR *p;

    /* empty string is an error */
    if (str->ptr >= str->end)
        return -1;

    for (p = str->ptr; p < str->end; p++) {
        digitval = STRINGLIB_TODECIMAL(*p);
        if (digitval < 0)
            return -1;
        /*
           This trick was copied from old Unicode format code.  It's cute,
           but would really suck on an old machine with a slow divide
           implementation.  Fortunately, in the normal case we do not
           expect too many digits.
        */
        oldaccumulator = accumulator;
        accumulator *= 10;
        if ((accumulator+10)/10 != oldaccumulator+1) {
            PyErr_Format(PyExc_ValueError,
                         "Too many decimal digits in format string");
            return -1;
        }
        accumulator += digitval;
    }
    return accumulator;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static PyObject* getattr ( PyObject obj,
SubString name 
) [static]

Definition at line 235 of file string_format.h.

{
    PyObject *newobj;
    PyObject *str = SubString_new_object(name);
    if (str == NULL)
        return NULL;
    newobj = PyObject_GetAttr(obj, str);
    Py_DECREF(str);
    return newobj;
}

Here is the call graph for this function:

static PyObject* getitem_idx ( PyObject obj,
Py_ssize_t  idx 
) [static]

Definition at line 255 of file string_format.h.

{
    PyObject *newobj;
    PyObject *idx_obj = PyLong_FromSsize_t(idx);
    if (idx_obj == NULL)
        return NULL;
    newobj = PyObject_GetItem(obj, idx_obj);
    Py_DECREF(idx_obj);
    return newobj;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static PyObject* getitem_sequence ( PyObject obj,
Py_ssize_t  idx 
) [static]

Definition at line 248 of file string_format.h.

{
    return PySequence_GetItem(obj, idx);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static PyObject* getitem_str ( PyObject obj,
SubString name 
) [static]

Definition at line 268 of file string_format.h.

{
    PyObject *newobj;
    PyObject *str = SubString_new_object(name);
    if (str == NULL)
        return NULL;
    newobj = PyObject_GetItem(obj, str);
    Py_DECREF(str);
    return newobj;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int MarkupIterator_init ( MarkupIterator self,
STRINGLIB_CHAR ptr,
Py_ssize_t  len 
) [static]

Definition at line 721 of file string_format.h.

{
    SubString_init(&self->str, ptr, len);
    return 1;
}

Here is the caller graph for this function:

static int MarkupIterator_next ( MarkupIterator self,
SubString literal,
int field_present,
SubString field_name,
SubString format_spec,
STRINGLIB_CHAR conversion,
int format_spec_needs_expanding 
) [static]

Definition at line 730 of file string_format.h.

{
    int at_end;
    STRINGLIB_CHAR c = 0;
    STRINGLIB_CHAR *start;
    int count;
    Py_ssize_t len;
    int markup_follows = 0;

    /* initialize all of the output variables */
    SubString_init(literal, NULL, 0);
    SubString_init(field_name, NULL, 0);
    SubString_init(format_spec, NULL, 0);
    *conversion = '\0';
    *format_spec_needs_expanding = 0;
    *field_present = 0;

    /* No more input, end of iterator.  This is the normal exit
       path. */
    if (self->str.ptr >= self->str.end)
        return 1;

    start = self->str.ptr;

    /* First read any literal text. Read until the end of string, an
       escaped '{' or '}', or an unescaped '{'.  In order to never
       allocate memory and so I can just pass pointers around, if
       there's an escaped '{' or '}' then we'll return the literal
       including the brace, but no format object.  The next time
       through, we'll return the rest of the literal, skipping past
       the second consecutive brace. */
    while (self->str.ptr < self->str.end) {
        switch (c = *(self->str.ptr++)) {
        case '{':
        case '}':
            markup_follows = 1;
            break;
        default:
            continue;
        }
        break;
    }

    at_end = self->str.ptr >= self->str.end;
    len = self->str.ptr - start;

    if ((c == '}') && (at_end || (c != *self->str.ptr))) {
        PyErr_SetString(PyExc_ValueError, "Single '}' encountered "
                        "in format string");
        return 0;
    }
    if (at_end && c == '{') {
        PyErr_SetString(PyExc_ValueError, "Single '{' encountered "
                        "in format string");
        return 0;
    }
    if (!at_end) {
        if (c == *self->str.ptr) {
            /* escaped } or {, skip it in the input.  there is no
               markup object following us, just this literal text */
            self->str.ptr++;
            markup_follows = 0;
        }
        else
            len--;
    }

    /* record the literal text */
    literal->ptr = start;
    literal->end = start + len;

    if (!markup_follows)
        return 2;

    /* this is markup, find the end of the string by counting nested
       braces.  note that this prohibits escaped braces, so that
       format_specs cannot have braces in them. */
    *field_present = 1;
    count = 1;

    start = self->str.ptr;

    /* we know we can't have a zero length string, so don't worry
       about that case */
    while (self->str.ptr < self->str.end) {
        switch (c = *(self->str.ptr++)) {
        case '{':
            /* the format spec needs to be recursively expanded.
               this is an optimization, and not strictly needed */
            *format_spec_needs_expanding = 1;
            count++;
            break;
        case '}':
            count--;
            if (count <= 0) {
                /* we're done.  parse and get out */
                SubString s;

                SubString_init(&s, start, self->str.ptr - 1 - start);
                if (parse_field(&s, field_name, format_spec, conversion) == 0)
                    return 0;

                /* success */
                return 2;
            }
            break;
        }
    }

    /* end of string while searching for matching '}' */
    PyErr_SetString(PyExc_ValueError, "unmatched '{' in format");
    return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int output_data ( OutputString *  output,
const STRINGLIB_CHAR s,
Py_ssize_t  count 
) [static]

Definition at line 182 of file string_format.h.

{
    if ((count > output->end - output->ptr) && !output_extend(output, count))
        return 0;
    memcpy(output->ptr, s, count * sizeof(STRINGLIB_CHAR));
    output->ptr += count;
    return 1;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int output_extend ( OutputString *  output,
Py_ssize_t  count 
) [static]

Definition at line 156 of file string_format.h.

{
    STRINGLIB_CHAR *startptr = STRINGLIB_STR(output->obj);
    Py_ssize_t curlen = output->ptr - startptr;
    Py_ssize_t maxlen = curlen + count + output->size_increment;

    if (STRINGLIB_RESIZE(&output->obj, maxlen) < 0)
        return 0;
    startptr = STRINGLIB_STR(output->obj);
    output->ptr = startptr + curlen;
    output->end = startptr + maxlen;
    if (output->size_increment < MAX_SIZE_INCREMENT)
        output->size_increment *= SIZE_MULTIPLIER;
    return 1;
}

Here is the caller graph for this function:

static int output_initialize ( OutputString *  output,
Py_ssize_t  size 
) [static]

Definition at line 136 of file string_format.h.

{
    output->obj = STRINGLIB_NEW(NULL, size);
    if (output->obj == NULL)
        return 0;

    output->ptr = STRINGLIB_STR(output->obj);
    output->end = STRINGLIB_LEN(output->obj) + output->ptr;
    output->size_increment = INITIAL_SIZE_INCREMENT;

    return 1;
}

Here is the caller graph for this function:

static int output_markup ( SubString field_name,
SubString format_spec,
int  format_spec_needs_expanding,
STRINGLIB_CHAR  conversion,
OutputString *  output,
PyObject args,
PyObject kwargs,
int  recursion_depth,
AutoNumber auto_number 
) [static]

Definition at line 893 of file string_format.h.

{
    PyObject *tmp = NULL;
    PyObject *fieldobj = NULL;
    SubString expanded_format_spec;
    SubString *actual_format_spec;
    int result = 0;

    /* convert field_name to an object */
    fieldobj = get_field_object(field_name, args, kwargs, auto_number);
    if (fieldobj == NULL)
        goto done;

    if (conversion != '\0') {
        tmp = do_conversion(fieldobj, conversion);
        if (tmp == NULL)
            goto done;

        /* do the assignment, transferring ownership: fieldobj = tmp */
        Py_DECREF(fieldobj);
        fieldobj = tmp;
        tmp = NULL;
    }

    /* if needed, recurively compute the format_spec */
    if (format_spec_needs_expanding) {
        tmp = build_string(format_spec, args, kwargs, recursion_depth-1,
                           auto_number);
        if (tmp == NULL)
            goto done;

        /* note that in the case we're expanding the format string,
           tmp must be kept around until after the call to
           render_field. */
        SubString_init(&expanded_format_spec,
                       STRINGLIB_STR(tmp), STRINGLIB_LEN(tmp));
        actual_format_spec = &expanded_format_spec;
    }
    else
        actual_format_spec = format_spec;

    if (render_field(fieldobj, actual_format_spec, output) == 0)
        goto done;

    result = 1;

done:
    Py_XDECREF(fieldobj);
    Py_XDECREF(tmp);

    return result;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int parse_field ( SubString str,
SubString field_name,
SubString format_spec,
STRINGLIB_CHAR conversion 
) [static]

Definition at line 642 of file string_format.h.

{
    /* Note this function works if the field name is zero length,
       which is good.  Zero length field names are handled later, in
       field_name_split. */

    STRINGLIB_CHAR c = 0;

    /* initialize these, as they may be empty */
    *conversion = '\0';
    SubString_init(format_spec, NULL, 0);

    /* Search for the field name.  it's terminated by the end of
       the string, or a ':' or '!' */
    field_name->ptr = str->ptr;
    while (str->ptr < str->end) {
        switch (c = *(str->ptr++)) {
        case ':':
        case '!':
            break;
        default:
            continue;
        }
        break;
    }

    if (c == '!' || c == ':') {
        /* we have a format specifier and/or a conversion */
        /* don't include the last character */
        field_name->end = str->ptr-1;

        /* the format specifier is the rest of the string */
        format_spec->ptr = str->ptr;
        format_spec->end = str->end;

        /* see if there's a conversion specifier */
        if (c == '!') {
            /* there must be another character present */
            if (format_spec->ptr >= format_spec->end) {
                PyErr_SetString(PyExc_ValueError,
                                "end of format while looking for conversion "
                                "specifier");
                return 0;
            }
            *conversion = *(format_spec->ptr++);

            /* if there is another character, it must be a colon */
            if (format_spec->ptr < format_spec->end) {
                c = *(format_spec->ptr++);
                if (c != ':') {
                    PyErr_SetString(PyExc_ValueError,
                                    "expected ':' after format specifier");
                    return 0;
                }
            }
        }
    }
    else
        /* end of string, there's no format_spec or conversion */
        field_name->end = str->ptr;

    return 1;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int render_field ( PyObject fieldobj,
SubString format_spec,
OutputString *  output 
) [static]

Definition at line 576 of file string_format.h.

{
    int ok = 0;
    PyObject *result = NULL;
    PyObject *format_spec_object = NULL;
    PyObject *(*formatter)(PyObject *, STRINGLIB_CHAR *, Py_ssize_t) = NULL;
    STRINGLIB_CHAR* format_spec_start = format_spec->ptr ?
            format_spec->ptr : NULL;
    Py_ssize_t format_spec_len = format_spec->ptr ?
            format_spec->end - format_spec->ptr : 0;

    /* If we know the type exactly, skip the lookup of __format__ and just
       call the formatter directly. */
    if (PyUnicode_CheckExact(fieldobj))
        formatter = _PyUnicode_FormatAdvanced;
    else if (PyLong_CheckExact(fieldobj))
        formatter =_PyLong_FormatAdvanced;
    else if (PyFloat_CheckExact(fieldobj))
        formatter = _PyFloat_FormatAdvanced;

    /* XXX: for 2.6, convert format_spec to the appropriate type
       (unicode, str) */

    if (formatter) {
        /* we know exactly which formatter will be called when __format__ is
           looked up, so call it directly, instead. */
        result = formatter(fieldobj, format_spec_start, format_spec_len);
    }
    else {
        /* We need to create an object out of the pointers we have, because
           __format__ takes a string/unicode object for format_spec. */
        format_spec_object = STRINGLIB_NEW(format_spec_start,
                                           format_spec_len);
        if (format_spec_object == NULL)
            goto done;

        result = PyObject_Format(fieldobj, format_spec_object);
    }
    if (result == NULL)
        goto done;

#if PY_VERSION_HEX >= 0x03000000
    assert(PyUnicode_Check(result));
#else
    assert(PyBytes_Check(result) || PyUnicode_Check(result));

    /* Convert result to our type.  We could be str, and result could
       be unicode */
    {
        PyObject *tmp = STRINGLIB_TOSTR(result);
        if (tmp == NULL)
            goto done;
        Py_DECREF(result);
        result = tmp;
    }
#endif

    ok = output_data(output,
                     STRINGLIB_STR(result), STRINGLIB_LEN(result));
done:
    Py_XDECREF(format_spec_object);
    Py_XDECREF(result);
    return ok;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

Initial value:
 {
    {NULL,              NULL}           
}

Definition at line 1297 of file string_format.h.

Initial value:
 {
    {NULL,              NULL}           
}

Definition at line 1159 of file string_format.h.

Definition at line 67 of file string_format.h.

Definition at line 1301 of file string_format.h.

Definition at line 1163 of file string_format.h.