From d8233fabcd83281e50d6a09f24aa60b60a451fb2 Mon Sep 17 00:00:00 2001 From: Johannes Hofmann Date: Sat, 11 Jul 2009 13:31:40 +0200 Subject: optimize splitStr() --- src/styleengine.cc | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) (limited to 'src/styleengine.cc') diff --git a/src/styleengine.cc b/src/styleengine.cc index 51357dbe..8bf78635 100644 --- a/src/styleengine.cc +++ b/src/styleengine.cc @@ -98,29 +98,34 @@ void StyleEngine::setId (const char *id) { }; /** - * \brief split a string at chars and return a simple vector of strings - * \todo should be optimized to avoid malloc()/free() calls. + * \brief split a string at sep chars and return a SimpleVector of strings */ -static lout::misc::SimpleVector * -splitStr(const char *str, const char *sep) { - char *tok, *copy, *p; +static lout::misc::SimpleVector *splitStr (const char *str, char sep) { + const char *p1 = NULL; lout::misc::SimpleVector *list = - new lout::misc::SimpleVector(1); + new lout::misc::SimpleVector (1); + + for (;; str++) { + if (*str != '\0' && *str != sep) { + if (!p1) + p1 = str; + } else if (p1) { + list->increase (); + list->set (list->size () - 1, dStrndup (p1, str - p1)); + p1 = NULL; + } - p = copy = dStrdup (str); - while ((tok = dStrsep(&p, sep))) { - list->increase (); - list->set (list->size () - 1, dStrdup (tok)); + if (*str == '\0') + break; } - dFree (copy); - return list; + return list; } void StyleEngine::setClass (const char *klass) { Node *n = stack->getRef (stack->size () - 1); assert (n->klass == NULL); - n->klass = splitStr (klass, " "); + n->klass = splitStr (klass, ' '); }; void StyleEngine::setStyle (const char *style) { -- cgit v1.2.3