Added regexp::ForEachMatched.

This commit is contained in:
vng 2014-08-07 18:00:03 +03:00 committed by Alex Zolotarev
parent efdf60f543
commit 789d6fbf2b
2 changed files with 68 additions and 0 deletions

View file

@ -2,6 +2,8 @@
#include "../regexp.hpp"
#include "../../base/stl_add.hpp"
UNIT_TEST(RegExp_Or)
{
@ -20,3 +22,57 @@ UNIT_TEST(RegExp_Or)
TEST(!regexp::IsExist("Aruba.mwm.resume3", exp), ());
TEST(!regexp::IsExist("Aruba.mwm.resume.tmp", exp), ());
}
UNIT_TEST(RegExp_ForEachMatched)
{
regexp::RegExpT exp;
regexp::Create("-?\\d+\\.?\\d*, *-?\\d+\\.?\\d*", exp);
{
string const s = "6.66, 9.99";
vector<string> v;
regexp::ForEachMatched(s, exp, MakeBackInsertFunctor(v));
TEST_EQUAL(v.size(), 1, ());
TEST_EQUAL(v[0], s, ());
}
{
string const s1 = "6.66, 9.99";
string const s2 = "-5.55, -7.77";
vector<string> v;
regexp::ForEachMatched(s1 + " 180 , bfuewib 365@" + s2, exp, MakeBackInsertFunctor(v));
TEST_EQUAL(v.size(), 2, ());
TEST_EQUAL(v[0], s1, ());
TEST_EQUAL(v[1], s2, ());
}
{
string const s = "X6.66, 9.99";
vector<string> v;
regexp::ForEachMatched(s, exp, MakeBackInsertFunctor(v));
TEST_EQUAL(v.size(), 1, ());
TEST_EQUAL(v[0], string(s.begin() + 1, s.end()), ());
}
{
string const s = "6.66, 9.99X";
vector<string> v;
regexp::ForEachMatched(s, exp, MakeBackInsertFunctor(v));
TEST_EQUAL(v.size(), 1, ());
TEST_EQUAL(v[0], string(s.begin(), s.end() - 1), ());
}
{
string const s = "6.66X, 9.99";
vector<string> v;
regexp::ForEachMatched(s, exp, MakeBackInsertFunctor(v));
TEST_EQUAL(v.size(), 0, ());
}
{
string const s = "6.66, X9.99";
vector<string> v;
regexp::ForEachMatched(s, exp, MakeBackInsertFunctor(v));
TEST_EQUAL(v.size(), 0, ());
}
}

View file

@ -1,6 +1,8 @@
#pragma once
#include <boost/xpressive/xpressive.hpp>
#include <boost/xpressive/regex_token_iterator.hpp>
namespace regexp
{
@ -15,4 +17,14 @@ namespace regexp
{
return boost::xpressive::regex_search(str, regexp);
}
template <class FnT> void ForEachMatched(string const & str, RegExpT const & regexp, FnT fn)
{
typedef boost::xpressive::sregex_token_iterator IterT;
IterT i(str.begin(), str.end(), regexp);
IterT end;
for (; i != end; ++i)
fn(*i);
}
}