Enhancement

- Added indentTree function that will provide the basis for a new 'projects'
  command, and potentially more.  Includes unit tests.  Based on a patch from
  Ralph Bean.
This commit is contained in:
Paul Beckingham 2011-12-27 15:32:51 -05:00
parent fb842cd5f5
commit 0001457612
5 changed files with 72 additions and 15 deletions

View file

@ -63,6 +63,7 @@ The following submitted code, packages or analysis, and deserve special thanks:
Barton Meeks Barton Meeks
Martin Klepsch Martin Klepsch
Wim Schuermann Wim Schuermann
Ralph Bean
Thanks to the following, who submitted detailed bug reports and excellent Thanks to the following, who submitted detailed bug reports and excellent
suggestions: suggestions:

View file

@ -13,19 +13,6 @@ New Code Needs
- text.cpp extractLines needs to be rewritten in a UTF8-aware and color-code - text.cpp extractLines needs to be rewritten in a UTF8-aware and color-code
sensitive manner. sensitive manner.
- Need a function to accept a list of projects, and return an indented list
that reflects the hierarchy. This will likely go into the new 'projects'
command. Something like:
Input - "one"
"one.two"
"one.two.three"
"one.four"
"two"
Output - "one"
" one.two"
" one.two.three"
" one.four"
"two"
- Need export_sql.yy script. Any language. This would have value as an - Need export_sql.yy script. Any language. This would have value as an
example, or template script serving as a starting-point for anyone who example, or template script serving as a starting-point for anyone who
needed this format. needed this format.
@ -145,6 +132,7 @@ Current Codebase Condition
--- ---
2011-12-23 Removed entry for tree-indentation function.
2011-10-16 Removed obsolete entries, added test suite description. 2011-10-16 Removed obsolete entries, added test suite description.
2011-10-16 Removed obsolete entries. 2011-10-16 Removed obsolete entries.
2011-08-21 Small changes, new work listed. 2011-08-21 Small changes, new work listed.

View file

@ -25,7 +25,6 @@
// //
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
#define L10N // Localization complete. #define L10N // Localization complete.
#include <iostream> #include <iostream>
@ -559,6 +558,54 @@ const std::string escape (const std::string& value, char c)
return modified; return modified;
} }
////////////////////////////////////////////////////////////////////////////////
// Accept a list of projects, and return an indented list
// that reflects the hierarchy.
//
// Input - "one"
// "one.two"
// "one.two.three"
// "one.four"
// "two"
// Output - "one"
// " one.two"
// " one.two.three"
// " one.four"
// "two"
//
// There are two optional arguments, 'whitespace', and 'delimiter',
//
// - whitespace is the string used to build the prefixes of indented items.
// - defaults to two spaces, " "
// - delimiter is the character used to split up projects into subprojects.
// - defaults to the period, '.'
//
const std::vector<std::string> indentTree (
const std::vector<std::string>& values,
const std::string& whitespace /* = " " */,
char delimiter/* = '.' */)
{
std::vector <std::string> modified;
std::vector <std::string>::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);
}
return modified;
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////

View file

@ -74,5 +74,10 @@ const std::string decode (const std::string&);
const std::string escape (const std::string&, char); const std::string escape (const std::string&, char);
const std::vector<std::string> indentTree (
const std::vector<std::string>&,
const std::string& whitespace = " ",
char delimiter='.');
#endif #endif
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////

View file

@ -35,7 +35,7 @@ Context context;
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
int main (int argc, char** argv) int main (int argc, char** argv)
{ {
UnitTest t (30); UnitTest t (36);
// TODO bool confirm (const std::string&); // TODO bool confirm (const std::string&);
// TODO int confirm3 (const std::string&); // TODO int confirm3 (const std::string&);
@ -113,6 +113,22 @@ int main (int argc, char** argv)
t.is (vleft[3], 4, "1,2,3,4 + 3,4,5 -> 1,2,3,4,5"); t.is (vleft[3], 4, "1,2,3,4 + 3,4,5 -> 1,2,3,4,5");
t.is (vleft[4], 5, "1,2,3,4 + 3,4,5 -> 1,2,3,4,5"); t.is (vleft[4], 5, "1,2,3,4 + 3,4,5 -> 1,2,3,4,5");
// std::vector<std::string> indentTree (const std::vector<std::string>&, const std::string whitespace=" ", char delimiter='.');
std::vector <std::string> flat;
flat.push_back ("one");
flat.push_back ("one.two");
flat.push_back ("one.two.three");
flat.push_back ("one.four");
flat.push_back ("two");
std::vector <std::string> structured = indentTree (flat, " ", '.');
t.is (structured.size (), (size_t) 5, "indentTree yields 5 strings");
t.is (structured[0], "one", "indentTree 'one' -> 'one'");
t.is (structured[1], " one.two", "indentTree 'one.two' -> ' one.two'");
t.is (structured[2], " one.two.three", "indentTree 'one.two.three' -> ' one.two.three'");
t.is (structured[3], " one.four", "indentTree 'one.four' -> ' one.four'");
t.is (structured[4], "two", "indentTree 'two' -> 'two'");
return 0; return 0;
} }