Add support for named sub-expressions.

[SVN r52823]
This commit is contained in:
John Maddock
2009-05-07 09:46:51 +00:00
parent 30941e330d
commit 55d979060c
49 changed files with 1287 additions and 206 deletions

View File

@ -87,6 +87,10 @@ test-suite regex
../build//boost_regex
]
[ run named_subexpressions/named_subexpressions_test.cpp
../build//boost_regex
]
[ run unicode/unicode_iterator_test.cpp ../build//boost_regex ]
[ run static_mutex/static_mutex_test.cpp
../../thread/build//boost_thread ../build//boost_regex

View File

@ -0,0 +1,109 @@
/*
*
* Copyright (c) 2009
* John Maddock
*
* Use, modification and distribution are subject to the
* Boost Software License, Version 1.0. (See accompanying file
* LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*
*/
#include <boost/regex.hpp>
#include <boost/test/test_tools.hpp>
template <class charT>
void test_named_subexpressions(charT)
{
//
// Really this is just a test that the overloaded access functions work correctly:
//
static const charT e[] =
{
'(', '?', '\'', 'o', 'n', 'e', '\'', 'a', '+', ')', '(', '?', '<', 't', 'w', 'o', '>', 'b', '+', ')', '\0'
};
static const charT t[] =
{
'm', 'm', 'a', 'a', 'a', 'b', 'b', 'n', 'n', '\0'
};
static const charT one[] =
{
'o', 'n', 'e', '\0'
};
static const charT two[] =
{
't', 'w', 'o', '\0'
};
static const std::basic_string<charT> s_one(one);
static const std::basic_string<charT> s_two(two);
static const charT result1[] = { 'a', 'a', 'a', '\0' };
static const charT result2[] = { 'b', 'b', '\0' };
static const std::basic_string<charT> s_result1(result1);
static const std::basic_string<charT> s_result2(result2);
static const char* c_one = "one";
static const char* c_two = "two";
static const std::string cs_one(c_one);
static const std::string cs_two(c_two);
boost::basic_regex<charT> expression(e);
boost::match_results<const charT*> what;
if(regex_search(t, what, expression))
{
BOOST_CHECK(what.length(1) == 3);
BOOST_CHECK(what.length(one) == 3);
BOOST_CHECK(what.length(s_one) == 3);
BOOST_CHECK(what.length(c_one) == 3);
BOOST_CHECK(what.length(cs_one) == 3);
BOOST_CHECK(what.position(1) == 2);
BOOST_CHECK(what.position(one) == 2);
BOOST_CHECK(what.position(s_one) == 2);
BOOST_CHECK(what.position(c_one) == 2);
BOOST_CHECK(what.position(cs_one) == 2);
BOOST_CHECK(what.str(1) == s_result1);
BOOST_CHECK(what.str(one) == s_result1);
BOOST_CHECK(what.str(s_one) == s_result1);
BOOST_CHECK(what.str(c_one) == s_result1);
BOOST_CHECK(what.str(cs_one) == s_result1);
BOOST_CHECK(what[1] == s_result1);
BOOST_CHECK(what[one] == s_result1);
BOOST_CHECK(what[s_one] == s_result1);
BOOST_CHECK(what[c_one] == s_result1);
BOOST_CHECK(what[cs_one] == s_result1);
BOOST_CHECK(what.length(2) == 2);
BOOST_CHECK(what.length(two) == 2);
BOOST_CHECK(what.length(s_two) == 2);
BOOST_CHECK(what.length(c_two) == 2);
BOOST_CHECK(what.length(cs_two) == 2);
BOOST_CHECK(what.position(2) == 5);
BOOST_CHECK(what.position(two) == 5);
BOOST_CHECK(what.position(s_two) == 5);
BOOST_CHECK(what.position(c_two) == 5);
BOOST_CHECK(what.position(cs_two) == 5);
BOOST_CHECK(what.str(2) == s_result2);
BOOST_CHECK(what.str(two) == s_result2);
BOOST_CHECK(what.str(s_two) == s_result2);
BOOST_CHECK(what.str(c_two) == s_result2);
BOOST_CHECK(what.str(cs_two) == s_result2);
BOOST_CHECK(what[2] == s_result2);
BOOST_CHECK(what[two] == s_result2);
BOOST_CHECK(what[s_two] == s_result2);
BOOST_CHECK(what[c_two] == s_result2);
BOOST_CHECK(what[cs_two] == s_result2);
}
else
{
BOOST_ERROR("Expected match not found");
}
}
int test_main( int , char* [] )
{
test_named_subexpressions(char(0));
test_named_subexpressions(wchar_t(0));
return 0;
}
#include <boost/test/included/test_exec_monitor.hpp>

View File

@ -90,5 +90,18 @@ void test_backrefs()
TEST_REGEX_SEARCH("a(b*)c\\g{-1}d", perl, "abbcbbbd", match_default, make_array(-2, -2));
TEST_REGEX_SEARCH("^(.)\\g{-1}", perl, "abc", match_default, make_array(-2, -2));
TEST_REGEX_SEARCH("a([bc])\\g{-1}d", perl, "abcdabbd", match_default, make_array(4, 8, 5, 6, -2, -2));
// And again but with named subexpressions:
TEST_REGEX_SEARCH("a(?<foo>(?<bar>(?<bb>(?<aa>b*))))c\\g{foo}d", perl, "abbcbbd", match_default, make_array(0, 7, 1, 3, 1, 3, 1, 3, 1, 3, -2, -2));
TEST_REGEX_SEARCH("a(?<foo>(?<bar>(?<bb>(?<aa>b*))))c\\g{foo}d", perl, "abbcbd", match_default, make_array(-2, -2));
TEST_REGEX_SEARCH("a(?<foo>(?<bar>(?<bb>(?<aa>b*))))c\\g{foo}d", perl, "abbcbbbd", match_default, make_array(-2, -2));
TEST_REGEX_SEARCH("^(?<foo>.)\\g{foo}", perl, "abc", match_default, make_array(-2, -2));
TEST_REGEX_SEARCH("a(?<foo>[bc])\\g{foo}d", perl, "abcdabbd", match_default, make_array(4, 8, 5, 6, -2, -2));
TEST_REGEX_SEARCH("a(?'foo'(?'bar'(?'bb'(?'aa'b*))))c\\g{foo}d", perl, "abbcbbd", match_default, make_array(0, 7, 1, 3, 1, 3, 1, 3, 1, 3, -2, -2));
TEST_REGEX_SEARCH("a(?'foo'(?'bar'(?'bb'(?'aa'b*))))c\\g{foo}d", perl, "abbcbd", match_default, make_array(-2, -2));
TEST_REGEX_SEARCH("a(?'foo'(?'bar'(?'bb'(?'aa'b*))))c\\g{foo}d", perl, "abbcbbbd", match_default, make_array(-2, -2));
TEST_REGEX_SEARCH("^(?'foo'.)\\g{foo}", perl, "abc", match_default, make_array(-2, -2));
TEST_REGEX_SEARCH("a(?'foo'[bc])\\g{foo}d", perl, "abcdabbd", match_default, make_array(4, 8, 5, 6, -2, -2));
}

View File

@ -107,7 +107,7 @@ void test_deprecated(const char&, const test_regex_search_tag&)
int i = 0;
while(results[2*i] != -2)
{
if(max_subs > i)
if((int)max_subs > i)
{
if(results[2*i] != matches[i].rm_so)
{
@ -231,7 +231,7 @@ void test_deprecated(const wchar_t&, const test_regex_search_tag&)
int i = 0;
while(results[2*i] != -2)
{
if(max_subs > i)
if((int)max_subs > i)
{
if(results[2*i] != matches[i].rm_so)
{

View File

@ -156,7 +156,7 @@ void test_assertion_escapes()
TEST_REGEX_SEARCH("\\R", perl, "foo\rbar", match_default, make_array(3, 4, -2, -2));
TEST_REGEX_SEARCH("\\R", perl, "foo\r\nbar", match_default, make_array(3, 5, -2, -2));
// see if \u works:
const wchar_t* w = "\u2028";
const wchar_t* w = L"\u2028";
if(*w == 0x2028u)
{
TEST_REGEX_SEARCH_W(L"\\R", perl, L"foo\u2028bar", match_default, make_array(3, 4, -2, -2));

View File

@ -126,5 +126,53 @@ void test_replace()
TEST_REGEX_REPLACE("(a+)", perl, "...aaa,,,", match_default, "/${10}/", "...//,,,");
TEST_REGEX_REPLACE("((((((((((a+))))))))))", perl, "...aaa,,,", match_default, "/${10}/", ".../aaa/,,,");
TEST_REGEX_REPLACE("(a+)", perl, "...aaa,,,", match_default, "/${1}0/", ".../aaa0/,,,");
// New Perl style operators:
TEST_REGEX_REPLACE("a+", perl, "...aaa,,,", match_default|format_no_copy, "$MATCH", "aaa");
TEST_REGEX_REPLACE("a+", perl, "...aaa,,,", match_default|format_no_copy, "${MATCH}", "aaa");
TEST_REGEX_REPLACE("a+", perl, "...aaa,,,", match_default|format_no_copy, "${^MATCH}", "aaa");
TEST_REGEX_REPLACE("a+", perl, "...aaa,,,", match_default|format_no_copy, "$MATC", "$MATC");
TEST_REGEX_REPLACE("a+", perl, "...aaa,,,", match_default|format_no_copy, "${MATCH", "${MATCH");
TEST_REGEX_REPLACE("a+", perl, "...aaa,,,", match_default|format_no_copy, "$PREMATCH", "...");
TEST_REGEX_REPLACE("a+", perl, "...aaa,,,", match_default|format_no_copy, "${PREMATCH}", "...");
TEST_REGEX_REPLACE("a+", perl, "...aaa,,,", match_default|format_no_copy, "${^PREMATCH}", "...");
TEST_REGEX_REPLACE("a+", perl, "...aaa,,,", match_default|format_no_copy, "$PREMATC", "$PREMATC");
TEST_REGEX_REPLACE("a+", perl, "...aaa,,,", match_default|format_no_copy, "${PREMATCH", "${PREMATCH");
TEST_REGEX_REPLACE("a+", perl, "...aaa,,,", match_default|format_no_copy, "$POSTMATCH", ",,,");
TEST_REGEX_REPLACE("a+", perl, "...aaa,,,", match_default|format_no_copy, "${POSTMATCH}", ",,,");
TEST_REGEX_REPLACE("a+", perl, "...aaa,,,", match_default|format_no_copy, "${^POSTMATCH}", ",,,");
TEST_REGEX_REPLACE("a+", perl, "...aaa,,,", match_default|format_no_copy, "$POSTMATC", "$POSTMATC");
TEST_REGEX_REPLACE("a+", perl, "...aaa,,,", match_default|format_no_copy, "${POSTMATCH", "${POSTMATCH");
TEST_REGEX_REPLACE("a+", perl, "...aaa,,,", match_default|format_no_copy, "$LAST_PAREN_MATCH", "");
TEST_REGEX_REPLACE("a+", perl, "...aaa,,,", match_default|format_no_copy, "$LAST_PAREN_MATC", "$LAST_PAREN_MATC");
TEST_REGEX_REPLACE("(a+)", perl, "...aaa,,,", match_default|format_no_copy, "$LAST_PAREN_MATCH", "aaa");
TEST_REGEX_REPLACE("(a+)(b+)", perl, "...aaabb,,,", match_default|format_no_copy, "$LAST_PAREN_MATCH", "bb");
TEST_REGEX_REPLACE("a+", perl, "...aaa,,,", match_default|format_no_copy, "$+", "");
TEST_REGEX_REPLACE("a+", perl, "...aaa,,,", match_default|format_no_copy, "$+foo", "foo");
TEST_REGEX_REPLACE("(a+)", perl, "...aaa,,,", match_default|format_no_copy, "$+", "aaa");
TEST_REGEX_REPLACE("(a+)(b+)", perl, "...aaabb,,,", match_default|format_no_copy, "$+foo", "bbfoo");
TEST_REGEX_REPLACE("(a+)(b+)", perl, "...aaabb,,,", match_default|format_no_copy, "$+{", "bb{");
TEST_REGEX_REPLACE("(a+)(b+)", perl, "...aaabb,,,", match_default|format_no_copy, "$+{foo", "bb{foo");
TEST_REGEX_REPLACE("a+", perl, "...aaa,,,", match_default|format_no_copy, "$LAST_SUBMATCH_RESULT", "");
TEST_REGEX_REPLACE("a+", perl, "...aaa,,,", match_default|format_no_copy, "$LAST_SUBMATCH_RESUL", "$LAST_SUBMATCH_RESUL");
TEST_REGEX_REPLACE("(a+)", perl, "...aaa,,,", match_default|format_no_copy, "$LAST_SUBMATCH_RESULT", "aaa");
TEST_REGEX_REPLACE("(a+)(b+)", perl, "...aaabb,,,", match_default|format_no_copy, "$LAST_SUBMATCH_RESULT", "bb");
TEST_REGEX_REPLACE("(a+)|(b+)", perl, "...aaa,,,", match_default|format_no_copy, "$LAST_SUBMATCH_RESULT", "aaa");
TEST_REGEX_REPLACE("a+", perl, "...aaa,,,", match_default|format_no_copy, "$^N", "");
TEST_REGEX_REPLACE("(a+)", perl, "...aaa,,,", match_default|format_no_copy, "$^N", "aaa");
TEST_REGEX_REPLACE("(a+)(b+)", perl, "...aaabb,,,", match_default|format_no_copy, "$^N", "bb");
TEST_REGEX_REPLACE("(a+)|(b+)", perl, "...aaa,,,", match_default|format_no_copy, "$^N", "aaa");
TEST_REGEX_REPLACE("(?<one>a+)(?<two>b+)", perl, " ...aabb,,", match_default|format_no_copy, "$&", "aabb");
TEST_REGEX_REPLACE("(?<one>a+)(?<two>b+)", perl, " ...aabb,,", match_default|format_no_copy, "$1", "aa");
TEST_REGEX_REPLACE("(?<one>a+)(?<two>b+)", perl, " ...aabb,,", match_default|format_no_copy, "$2", "bb");
TEST_REGEX_REPLACE("(?<one>a+)(?<two>b+)", perl, " ...aabb,,", match_default|format_no_copy, "d$+{one}c", "daac");
TEST_REGEX_REPLACE("(?<one>a+)(?<two>b+)", perl, " ...aabb,,", match_default|format_no_copy, "c$+{two}d", "cbbd");
}