Skip to content

Commit

Permalink
Changed str::Tokenizer semantics (pass functor instead of iterator).
Browse files Browse the repository at this point in the history
  • Loading branch information
vng committed Aug 12, 2013
1 parent 0868f48 commit 50c3fe7
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
8 changes: 3 additions & 5 deletions env/strings.hpp
Expand Up @@ -18,8 +18,8 @@ void Trim(string & s);

string MakeNormalizeAndLowerUtf8(string const & s);

template <class IterT>
IterT Tokenize(string const & s, char const * delims, IterT out)
template <class ToDo>
void Tokenize(string const & s, char const * delims, ToDo toDo)
{
size_t i = 0;
while (i < s.size())
Expand All @@ -29,14 +29,12 @@ IterT Tokenize(string const & s, char const * delims, IterT out)
j = s.size();
if (j > i)
{
*out++ = s.substr(i, j-i);
toDo(s.substr(i, j-i));
i = j+1;
}
else
++i;
}

return out;
}

}
3 changes: 2 additions & 1 deletion env/tests/env_tests.cpp
Expand Up @@ -8,6 +8,7 @@
#include "../../std/algorithm.hpp"
#include "../../std/vector.hpp"
#include "../../std/array.hpp"
#include "../../std/iterator.hpp"


/// @note Do not edit formatting here (SRC() test):
Expand Down Expand Up @@ -86,7 +87,7 @@ TEST(Env, Tokenizer)
string out[] = { "aaa", "bbb", "ccc" };

vector<string> v;
str::Tokenize(in, " ,\t?", back_inserter(v));
str::Tokenize(in, " ,\t?", MakeBackInserter(v));

EXPECT_TRUE(equal(out, out + ArraySize(out), v.begin()));
}
18 changes: 18 additions & 0 deletions std/iterator.hpp
Expand Up @@ -4,3 +4,21 @@

using std::back_inserter;
using std::distance;

namespace impl
{
template <class ContT>
class BackInserterFn
{
ContT & m_cont;
public:
BackInserterFn(ContT & cont) : m_cont(cont) {}
template <class T> void operator() (T const & t) { m_cont.push_back(t); }
};
}

template <class ContT>
impl::BackInserterFn<ContT> MakeBackInserter(ContT & cont)
{
return impl::BackInserterFn<ContT>(cont);
}
2 changes: 1 addition & 1 deletion storage/storage_builder.cpp
Expand Up @@ -27,7 +27,7 @@ void ProcessEntriesFile(string const & path, ToDo & toDo)
continue;

entries.clear();
str::Tokenize(str, "\t ", back_inserter(entries));
str::Tokenize(str, "\t ", MakeBackInserter(entries));

toDo(entries);
}
Expand Down

0 comments on commit 50c3fe7

Please sign in to comment.