From 95e36d1145603b133be15806492ee8f79ac0ef5f Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Sun, 25 Mar 2012 16:54:13 -0400 Subject: [PATCH] Enhancement - Broke the indentTree function into two pieces - one that processes a list, the other that processes a single item. This makes it more efficient for use places that have only one element. --- src/util.cpp | 31 ++++++++++++++++--------------- src/util.h | 7 ++++++- test/util.t.cpp | 8 +++++++- 3 files changed, 29 insertions(+), 17 deletions(-) diff --git a/src/util.cpp b/src/util.cpp index 2e40c12de..d24f61c47 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -588,24 +588,25 @@ const std::vector indentTree ( std::vector modified; std::vector ::const_iterator i; for (i = values.begin (); i != values.end (); ++i) - { - // Count the delimiters in *i. - int count = 0; - std::string::size_type pos = 0; - while ((pos = i->find (delimiter, pos + 1)) != std::string::npos) - ++count; - - // Construct a prefix string which is a concatenated set of whitespace - // strings. - std::string prefix; - for (int indent = 0; indent < count; ++indent) - prefix += whitespace; - - modified.push_back (prefix + *i); - } + modified.push_back (indentProject (*i, whitespace, delimiter)); return modified; } //////////////////////////////////////////////////////////////////////////////// +const std::string indentProject ( + const std::string& project, + const std::string& whitespace /* = " " */, + char delimiter /* = '.' */) +{ + // Count the delimiters in *i. + std::string prefix = ""; + std::string::size_type pos = 0; + while ((pos = project.find (delimiter, pos + 1)) != std::string::npos) + prefix += whitespace; + + return prefix + project; +} + +//////////////////////////////////////////////////////////////////////////////// diff --git a/src/util.h b/src/util.h index 26b3f0a00..411b76ce2 100644 --- a/src/util.h +++ b/src/util.h @@ -77,7 +77,12 @@ const std::string escape (const std::string&, char); const std::vector indentTree ( const std::vector&, const std::string& whitespace = " ", - char delimiter='.'); + char delimiter = '.'); + +const std::string indentProject ( + const std::string&, + const std::string& whitespace = " ", + char delimiter = '.'); #endif //////////////////////////////////////////////////////////////////////////////// diff --git a/test/util.t.cpp b/test/util.t.cpp index a749700be..faa3116e9 100644 --- a/test/util.t.cpp +++ b/test/util.t.cpp @@ -35,7 +35,7 @@ Context context; //////////////////////////////////////////////////////////////////////////////// int main (int argc, char** argv) { - UnitTest t (36); + UnitTest t (40); // TODO bool confirm (const std::string&); // TODO int confirm3 (const std::string&); @@ -129,6 +129,12 @@ int main (int argc, char** argv) t.is (structured[3], " one.four", "indentTree 'one.four' -> ' one.four'"); t.is (structured[4], "two", "indentTree 'two' -> 'two'"); + // std::vector indentProject (const std::string&, const std::string whitespace=" ", char delimiter='.'); + t.is (indentProject (""), "", "indentProject '' -> ''"); + t.is (indentProject ("one"), "one", "indentProject 'one' -> 'one'"); + t.is (indentProject ("one.two"), " one.two", "indentProject 'one.two' -> ' one.two'"); + t.is (indentProject ("one.two.three"), " one.two.three", "indentProject 'one.two.three' -> ' one.two.three'"); + // TODO const std::string encode (const std::string& value); // TODO const std::string decode (const std::string& value);