diff options
author | Johannes Hofmann <Johannes.Hofmann@gmx.de> | 2009-07-11 13:31:40 +0200 |
---|---|---|
committer | Johannes Hofmann <Johannes.Hofmann@gmx.de> | 2009-07-11 13:31:40 +0200 |
commit | d8233fabcd83281e50d6a09f24aa60b60a451fb2 (patch) | |
tree | a50d6b8ca06a92f2f50d2586b16a7ef757d0bc0c /src/styleengine.cc | |
parent | 3d860ca045e0915f28f372c17ec267cb83905ee6 (diff) |
optimize splitStr()
Diffstat (limited to 'src/styleengine.cc')
-rw-r--r-- | src/styleengine.cc | 31 |
1 files changed, 18 insertions, 13 deletions
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 <sep> 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<char *> * -splitStr(const char *str, const char *sep) { - char *tok, *copy, *p; +static lout::misc::SimpleVector<char *> *splitStr (const char *str, char sep) { + const char *p1 = NULL; lout::misc::SimpleVector<char *> *list = - new lout::misc::SimpleVector<char *>(1); + new lout::misc::SimpleVector<char *> (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) { |