29 #include <boost/lexical_cast.hpp> 36 , m_isSecondaryUsed(false)
38 m_primaryBackrefManager = make_shared<RegexBackrefManager>();
39 m_secondaryBackrefManager = make_shared<RegexBackrefManager>();
48 if (
'$' != expr[expr.size() - 1])
49 expr = expr +
"<.*>*";
51 expr = expr.substr(0, expr.size() - 1);
54 m_secondaryMatcher = make_shared<RegexPatternListMatcher>(
"<.*>*" + expr,
55 m_secondaryBackrefManager);
58 expr = expr.substr(1, expr.size() - 1);
61 m_primaryMatcher = make_shared<RegexPatternListMatcher>(expr, m_primaryBackrefManager);
67 m_isSecondaryUsed =
false;
71 if (m_primaryMatcher->match(
name, 0,
name.size())) {
76 if (m_secondaryMatcher !=
nullptr && m_secondaryMatcher->match(
name, 0,
name.size())) {
78 m_isSecondaryUsed =
true;
94 auto backrefManager = m_isSecondaryUsed ? m_secondaryBackrefManager : m_primaryBackrefManager;
95 size_t backrefNo = backrefManager->size();
98 if (!expandStr.empty())
106 std::string item = getItemFromExpand(
expand, offset);
107 if (item[0] ==
'<') {
108 result.
append(item.substr(1, item.size() - 2));
110 if (item[0] ==
'\\') {
111 size_t index = boost::lexical_cast<size_t>(item.substr(1, item.size() - 1));
116 else if (index <= backrefNo) {
117 for (
const auto& i : backrefManager->getBackref(index - 1)->getMatchResult())
121 BOOST_THROW_EXCEPTION(
Error(
"Exceed the range of back reference"));
129 RegexTopMatcher::getItemFromExpand(
const std::string& expand,
size_t& offset)
131 size_t begin = offset;
133 if (
expand[offset] ==
'\\') {
136 BOOST_THROW_EXCEPTION(Error(
"Wrong format of expand string"));
138 while (
expand[offset] <=
'9' and
expand[offset] >=
'0') {
141 BOOST_THROW_EXCEPTION(Error(
"Wrong format of expand string"));
143 if (offset > begin + 1)
144 return expand.substr(begin, offset - begin);
146 BOOST_THROW_EXCEPTION(Error(
"Wrong format of expand string"));
148 else if (
expand[offset] ==
'<') {
151 BOOST_THROW_EXCEPTION(Error(
"Wrong format of expand string"));
155 while (right < left) {
156 if (
expand[offset] ==
'<')
158 if (
expand[offset] ==
'>')
162 BOOST_THROW_EXCEPTION(Error(
"Wrong format of expand string"));
164 return expand.substr(begin, offset - begin);
167 BOOST_THROW_EXCEPTION(Error(
"Wrong format of expand string"));
170 shared_ptr<RegexTopMatcher>
173 std::string regexStr(
"^");
175 for (
const auto& i :
name) {
176 regexStr.append(
"<");
177 regexStr.append(convertSpecialChar(i.toUri()));
178 regexStr.append(
">");
182 regexStr.append(
"$");
184 return make_shared<RegexTopMatcher>(regexStr);
188 RegexTopMatcher::convertSpecialChar(
const std::string& str)
192 for (
size_t i = 0; i < str.size(); i++) {
208 newStr.push_back(
'\\');
Copyright (c) 2011-2015 Regents of the University of California.
static shared_ptr< RegexTopMatcher > fromName(const Name &name, bool hasAnchor=false)
Name & append(const Component &component)
Append a component.
void compile() override
Compile the regular expression to generate the more matchers when necessary.
std::vector< name::Component > m_matchResult
Represents an absolute name.
size_t size() const
Get number of components.
#define NDN_CXX_FALLTHROUGH
virtual Name expand(const std::string &expand="")
bool match(const Name &name)
RegexTopMatcher(const std::string &expr, const std::string &expand="")