Merge pull request #952 from david-moran/develop

Named substring support
This commit is contained in:
Aleksandar Fabijanic
2015-09-28 08:45:12 -05:00
4 changed files with 42 additions and 1 deletions

View File

@@ -25,6 +25,7 @@
#include "Poco/Foundation.h" #include "Poco/Foundation.h"
#include <vector> #include <vector>
#include <map>
// //
@@ -91,8 +92,10 @@ public:
{ {
std::string::size_type offset; /// zero based offset (std::string::npos if subexpr does not match) std::string::size_type offset; /// zero based offset (std::string::npos if subexpr does not match)
std::string::size_type length; /// length of substring std::string::size_type length; /// length of substring
std::string name; /// name of group
}; };
typedef std::vector<Match> MatchVec; typedef std::vector<Match> MatchVec;
typedef std::map<int, std::string> GroupMap;
RegularExpression(const std::string& pattern, int options = 0, bool study = true); RegularExpression(const std::string& pattern, int options = 0, bool study = true);
/// Creates a regular expression and parses the given pattern. /// Creates a regular expression and parses the given pattern.
@@ -212,6 +215,8 @@ private:
pcre* _pcre; pcre* _pcre;
pcre_extra* _extra; pcre_extra* _extra;
GroupMap _groups;
static const int OVEC_SIZE; static const int OVEC_SIZE;
RegularExpression(); RegularExpression();

View File

@@ -24,7 +24,6 @@
#include "pcre.h" #include "pcre.h"
#endif #endif
namespace Poco { namespace Poco {
@@ -35,6 +34,10 @@ RegularExpression::RegularExpression(const std::string& pattern, int options, bo
{ {
const char* error; const char* error;
int offs; int offs;
unsigned nmcount;
unsigned nmentrysz;
unsigned char* nmtbl;
_pcre = pcre_compile(pattern.c_str(), options, &error, &offs, 0); _pcre = pcre_compile(pattern.c_str(), options, &error, &offs, 0);
if (!_pcre) if (!_pcre)
{ {
@@ -44,6 +47,18 @@ RegularExpression::RegularExpression(const std::string& pattern, int options, bo
} }
if (study) if (study)
_extra = pcre_study(_pcre, 0, &error); _extra = pcre_study(_pcre, 0, &error);
pcre_fullinfo(_pcre, _extra, PCRE_INFO_NAMECOUNT, &nmcount);
pcre_fullinfo(_pcre, _extra, PCRE_INFO_NAMEENTRYSIZE, &nmentrysz);
pcre_fullinfo(_pcre, _extra, PCRE_INFO_NAMETABLE, &nmtbl);
for (int i = 0; i < nmcount; i++)
{
unsigned char* group = nmtbl + 2 + (nmentrysz * i);
int n = pcre_get_stringnumber(_pcre, (char*) group);
_groups[n] = std::string((char*) group);
}
} }
@@ -116,8 +131,17 @@ int RegularExpression::match(const std::string& subject, std::string::size_type
for (int i = 0; i < rc; ++i) for (int i = 0; i < rc; ++i)
{ {
Match m; Match m;
GroupMap::const_iterator it;
m.offset = ovec[i*2] < 0 ? std::string::npos : ovec[i*2] ; m.offset = ovec[i*2] < 0 ? std::string::npos : ovec[i*2] ;
m.length = ovec[i*2 + 1] - m.offset; m.length = ovec[i*2 + 1] - m.offset;
it = _groups.find(i);
if (it != _groups.end())
{
m.name = (*it).second;
}
matches.push_back(m); matches.push_back(m);
} }
return rc; return rc;

View File

@@ -264,6 +264,16 @@ void RegularExpressionTest::testError()
} }
} }
void RegularExpressionTest::testGroup()
{
RegularExpression::MatchVec matches;
RegularExpression re("(?P<group1>[a-z]+) (?P<group2>[0-9]+)");
assert (re.match("abcd 1234", 0, matches) == 3);
assert (matches[0].name == "");
assert (matches[1].name == "group1");
assert (matches[2].name == "group2");
}
void RegularExpressionTest::setUp() void RegularExpressionTest::setUp()
{ {
@@ -294,6 +304,7 @@ CppUnit::Test* RegularExpressionTest::suite()
CppUnit_addTest(pSuite, RegularExpressionTest, testSubst3); CppUnit_addTest(pSuite, RegularExpressionTest, testSubst3);
CppUnit_addTest(pSuite, RegularExpressionTest, testSubst4); CppUnit_addTest(pSuite, RegularExpressionTest, testSubst4);
CppUnit_addTest(pSuite, RegularExpressionTest, testError); CppUnit_addTest(pSuite, RegularExpressionTest, testError);
CppUnit_addTest(pSuite, RegularExpressionTest, testGroup);
return pSuite; return pSuite;
} }

View File

@@ -41,6 +41,7 @@ public:
void testSubst3(); void testSubst3();
void testSubst4(); void testSubst4();
void testError(); void testError();
void testGroup();
void setUp(); void setUp();
void tearDown(); void tearDown();