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

View file

@ -8,6 +8,7 @@
#include "../../std/algorithm.hpp" #include "../../std/algorithm.hpp"
#include "../../std/vector.hpp" #include "../../std/vector.hpp"
#include "../../std/array.hpp" #include "../../std/array.hpp"
#include "../../std/iterator.hpp"
/// @note Do not edit formatting here (SRC() test): /// @note Do not edit formatting here (SRC() test):
@ -86,7 +87,7 @@ TEST(Env, Tokenizer)
string out[] = { "aaa", "bbb", "ccc" }; string out[] = { "aaa", "bbb", "ccc" };
vector<string> v; 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())); EXPECT_TRUE(equal(out, out + ArraySize(out), v.begin()));
} }

View file

@ -4,3 +4,21 @@
using std::back_inserter; using std::back_inserter;
using std::distance; 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; continue;
entries.clear(); entries.clear();
str::Tokenize(str, "\t ", back_inserter(entries)); str::Tokenize(str, "\t ", MakeBackInserter(entries));
toDo(entries); toDo(entries);
} }