Changed str::Tokenizer semantics (pass functor instead of iterator).

This commit is contained in:
vng 2013-08-12 21:58:53 +03:00
parent 0868f48b9a
commit 50c3fe7cfa
4 changed files with 24 additions and 7 deletions

8
env/strings.hpp vendored
View file

@ -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())
@ -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;
}
}

View file

@ -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):
@ -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()));
}

View file

@ -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);
}

View file

@ -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);
}