diff options
author | Johan Dahlin <jdahlin@async.com.br> | 2008-10-30 20:42:30 +0000 |
---|---|---|
committer | Johan Dahlin <johan@src.gnome.org> | 2008-10-30 20:42:30 +0000 |
commit | 98791e4955ea3955fec9c020bdfd3eee771ed241 (patch) | |
tree | 3b0cf7c737a09c72cb5fc00d71e953c9bff5cc5a /giscanner/giscannermodule.c | |
parent | 64b2a7fde8ec7df89e2544affd99868a83342a52 (diff) | |
download | gobject-introspection-98791e4955ea3955fec9c020bdfd3eee771ed241.tar.gz |
Write a C version of the xml namespace formatter. Saves 15% of the time it
2008-10-30 Johan Dahlin <jdahlin@async.com.br>
* giscanner/giscannermodule.c (calc_attrs_length),
(pygi_collect_attributes):
* giscanner/xmlwriter.py:
Write a C version of the xml namespace formatter.
Saves 15% of the time it takes to create a gtk gir.
svn path=/trunk/; revision=846
Diffstat (limited to 'giscanner/giscannermodule.c')
-rw-r--r-- | giscanner/giscannermodule.c | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/giscanner/giscannermodule.c b/giscanner/giscannermodule.c index dee9fed7..2764652e 100644 --- a/giscanner/giscannermodule.c +++ b/giscanner/giscannermodule.c @@ -592,9 +592,101 @@ static const PyMethodDef _PyGISourceScanner_methods[] = { }; +static int calc_attrs_length(PyObject *attributes, int indent, + int self_indent) +{ + int attr_length = 0; + int i; + + if (indent == -1) + return -1; + + for (i = 0; i < PyList_Size (attributes); ++i) + { + PyObject *tuple; + char *attr, *value; + char *escaped; + + tuple = PyList_GetItem (attributes, i); + if (PyTuple_GetItem(tuple, 1) == Py_None) + continue; + + g_assert(PyArg_ParseTuple(tuple, "ss", &attr, &value)); + + escaped = g_markup_escape_text (value, -1); + attr_length += 2 + strlen(attr) + strlen(escaped) + 2; + g_free(escaped); + } + + return attr_length + indent + self_indent; +} + +static PyObject * +pygi_collect_attributes (PyObject *self, + PyObject *args) +{ + char *tag_name; + PyObject *attributes; + int indent, indent_len, i, j, self_indent; + char *indent_char; + gboolean first; + GString *attr_value; + + if (!PyArg_ParseTuple(args, "sOisi", + &tag_name, &attributes, + &self_indent, &indent_char, + &indent)) + return NULL; + + if (attributes == Py_None || !PyList_Size(attributes)) + return PyString_FromString(""); + + if (calc_attrs_length(attributes, indent, self_indent) > 79) + indent_len = self_indent + strlen(tag_name) + 1; + else + indent_len = 0; + + first = TRUE; + attr_value = g_string_new (""); + + for (i = 0; i < PyList_Size (attributes); ++i) + { + PyObject *tuple; + char *attr, *value, *escaped; + + tuple = PyList_GetItem (attributes, i); + g_assert(tuple != NULL); + g_assert(PyTuple_Size(tuple) == 2); + if (PyTuple_GetItem(tuple, 1) == Py_None) + continue; + + g_assert(PyArg_ParseTuple(tuple, "ss", &attr, &value)); + + if (indent_len && !first) + { + g_string_append_c (attr_value, '\n'); + for (j = 0; j < indent_len; j++) + g_string_append_c (attr_value, ' '); + } + g_string_append_c (attr_value, ' '); + g_string_append (attr_value, attr); + g_string_append_c (attr_value, '='); + g_string_append_c (attr_value, '\"'); + escaped = g_markup_escape_text (value, -1); + g_string_append (attr_value, escaped); + g_string_append_c (attr_value, '\"'); + if (first) + first = FALSE; + } + + return PyString_FromString (g_string_free (attr_value, FALSE)); +} + /* Module */ static const PyMethodDef pyscanner_functions[] = { + { "collect_attributes", + (PyCFunction) pygi_collect_attributes, METH_VARARGS }, { NULL, NULL, 0, NULL } }; |