8 #ifndef NDN_UTIL_REGEX_REGEX_REPEAT_MATCHER_HPP
9 #define NDN_UTIL_REGEX_REGEX_REPEAT_MATCHER_HPP
11 #include "../../common.hpp"
13 #include <boost/regex.hpp>
22 RegexRepeatMatcher(
const std::string& expr, shared_ptr<RegexBackrefManager> backRefManager,
int indicator);
27 match(
const Name& name,
const int& offset,
const int& len);
42 recursiveMatch (
int repeat,
62 :
RegexMatcher (expr, EXPR_REPEAT_PATTERN, backrefManager),
63 m_indicator(indicator)
75 shared_ptr<RegexMatcher> matcher;
94 RegexRepeatMatcher::parseRepetition()
98 int exprSize =
m_expr.size();
99 int intMax = std::numeric_limits<int>::max();
101 if (exprSize == m_indicator){
108 if (exprSize == (m_indicator + 1)){
109 if (
'?' ==
m_expr[m_indicator]){
114 if (
'+' ==
m_expr[m_indicator]){
116 m_repeatMax = intMax;
119 if (
'*' ==
m_expr[m_indicator]){
121 m_repeatMax = intMax;
126 std::string repeatStruct =
m_expr.substr(m_indicator, exprSize - m_indicator);
127 int rsSize = repeatStruct.size();
131 if (boost::regex_match(repeatStruct, boost::regex(
"\\{[0-9]+,[0-9]+\\}"))){
132 int separator = repeatStruct.find_first_of(
',', 0);
133 min = atoi(repeatStruct.substr(1, separator - 1).c_str());
134 max = atoi(repeatStruct.substr(separator + 1, rsSize - separator - 2).c_str());
136 else if (boost::regex_match(repeatStruct, boost::regex(
"\\{,[0-9]+\\}"))){
137 int separator = repeatStruct.find_first_of(
',', 0);
139 max = atoi(repeatStruct.substr(separator + 1, rsSize - separator - 2).c_str());
141 else if (boost::regex_match(repeatStruct, boost::regex(
"\\{[0-9]+,\\}"))){
142 int separator = repeatStruct.find_first_of(
',', 0);
143 min = atoi(repeatStruct.substr(1, separator).c_str());
146 else if (boost::regex_match(repeatStruct, boost::regex(
"\\{[0-9]+\\}"))){
147 min = atoi(repeatStruct.substr(1, rsSize - 1).c_str());
151 throw RegexMatcher::Error(std::string(
"Error: RegexRepeatMatcher.ParseRepetition(): ")
152 +
"Unrecognized format "+
m_expr);
154 if (min > intMax || max > intMax || min > max)
155 throw RegexMatcher::Error(std::string(
"Error: RegexRepeatMatcher.ParseRepetition(): ")
156 +
"Wrong number " +
m_expr);
174 if (0 == m_repeatMin)
178 if (recursiveMatch(0, name, offset, len))
180 for (
int i = offset; i < offset + len; i++)
189 RegexRepeatMatcher::recursiveMatch(
int repeat,
const Name& name,
const int& offset,
const int& len)
199 if (0 < len && repeat >= m_repeatMax)
205 if (0 == len && repeat < m_repeatMin)
211 if (0 == len && repeat >= m_repeatMin)
221 if (matcher->match(name, offset, tried) and recursiveMatch(repeat + 1, name, offset + tried, len - tried))
233 #endif // NDN_UTIL_REGEX_REGEX_REPEAT_MATCHER_HPP
virtual bool match(const Name &name, const int &offset, const int &len)
virtual void compile()
Compile the regular expression to generate the more matchers when necessary.
shared_ptr< RegexBackrefManager > m_backrefManager
std::vector< name::Component > m_matchResult
A Name holds an array of Name::Component and represents an NDN name.
RegexRepeatMatcher(const std::string &expr, shared_ptr< RegexBackrefManager > backRefManager, int indicator)
std::vector< shared_ptr< RegexMatcher > > m_matcherList
virtual ~RegexRepeatMatcher()
const Component & get(ssize_t i) const
Get the component at the given index.