blob: 5eefbd1aa6025900c6103367a286399b1eb3e61c [file] [log] [blame]
Yingdi Yu5e974202014-01-29 16:59:06 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
3 * Copyright (C) 2013 Regents of the University of California.
4 * @author: Yingdi Yu <yingdi@cs.ucla.edu>
5 * See COPYING for copyright and distribution information.
6 */
7
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -08008#ifndef NDN_UTIL_REGEX_REGEX_REPEAT_MATCHER_HPP
9#define NDN_UTIL_REGEX_REGEX_REPEAT_MATCHER_HPP
10
11#include "../../common.hpp"
12
13#include <boost/regex.hpp>
Yingdi Yu5e974202014-01-29 16:59:06 -080014
15#include "regex-matcher.hpp"
16
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -080017namespace ndn {
Yingdi Yu5e974202014-01-29 16:59:06 -080018
19class RegexRepeatMatcher : public RegexMatcher
20{
21public:
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -080022 RegexRepeatMatcher(const std::string& expr, shared_ptr<RegexBackrefManager> backRefManager, int indicator);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070023
Yingdi Yu5e974202014-01-29 16:59:06 -080024 virtual ~RegexRepeatMatcher(){}
25
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070026 virtual bool
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -080027 match(const Name& name, const int& offset, const int& len);
Yingdi Yu5e974202014-01-29 16:59:06 -080028
29protected:
30 /**
31 * @brief Compile the regular expression to generate the more matchers when necessary
32 * @returns true if compiling succeeds
33 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070034 virtual void
Yingdi Yu5e974202014-01-29 16:59:06 -080035 compile();
36
Yingdi Yu5e974202014-01-29 16:59:06 -080037private:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070038 bool
Yingdi Yu5e974202014-01-29 16:59:06 -080039 parseRepetition();
40
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070041 bool
Yingdi Yu5e974202014-01-29 16:59:06 -080042 recursiveMatch (int repeat,
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070043 const Name& name,
44 const int& offset,
45 const int&len);
46
Yingdi Yu5e974202014-01-29 16:59:06 -080047private:
48 int m_indicator;
49 int m_repeatMin;
50 int m_repeatMax;
51};
52
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -080053} // namespace ndn
Yingdi Yu5e974202014-01-29 16:59:06 -080054
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -080055#include "regex-backref-matcher.hpp"
56#include "regex-component-set-matcher.hpp"
57
58namespace ndn {
59
60inline
61RegexRepeatMatcher::RegexRepeatMatcher(const std::string& expr, shared_ptr<RegexBackrefManager> backrefManager, int indicator)
62 : RegexMatcher (expr, EXPR_REPEAT_PATTERN, backrefManager),
63 m_indicator(indicator)
64{
65 // _LOG_TRACE ("Enter RegexRepeatMatcher Constructor");
66 compile();
67 // _LOG_TRACE ("Exit RegexRepeatMatcher Constructor");
68}
69
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070070inline void
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -080071RegexRepeatMatcher::compile()
72{
73 // _LOG_TRACE ("Enter RegexRepeatMatcher::compile");
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070074
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -080075 shared_ptr<RegexMatcher> matcher;
76
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070077 if ('(' == m_expr[0]){
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -080078 matcher = make_shared<RegexBackrefMatcher>(m_expr.substr(0, m_indicator), m_backrefManager);
79 m_backrefManager->pushRef(matcher);
80 boost::dynamic_pointer_cast<RegexBackrefMatcher>(matcher)->lateCompile();
81 }
82 else{
83 matcher = make_shared<RegexComponentSetMatcher>(m_expr.substr(0, m_indicator), m_backrefManager);
84 }
85 m_matcherList.push_back(matcher);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070086
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -080087 parseRepetition();
88
89 // _LOG_TRACE ("Exit RegexRepeatMatcher::compile");
90
91}
92
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070093inline bool
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -080094RegexRepeatMatcher::parseRepetition()
95{
96 // _LOG_DEBUG ("Enter RegexRepeatMatcher::ParseRepetition()" << m_expr << " indicator: " << m_indicator);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070097
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -080098 int exprSize = m_expr.size();
99 int intMax = std::numeric_limits<int>::max();
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700100
101 if (exprSize == m_indicator){
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800102 m_repeatMin = 1;
103 m_repeatMax = 1;
104
105 return true;
106 }
107 else{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700108 if (exprSize == (m_indicator + 1)){
109 if ('?' == m_expr[m_indicator]){
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800110 m_repeatMin = 0;
111 m_repeatMax = 1;
112 return true;
113 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700114 if ('+' == m_expr[m_indicator]){
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800115 m_repeatMin = 1;
116 m_repeatMax = intMax;
117 return true;
118 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700119 if ('*' == m_expr[m_indicator]){
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800120 m_repeatMin = 0;
121 m_repeatMax = intMax;
122 return true;
123 }
124 }
125 else{
126 std::string repeatStruct = m_expr.substr(m_indicator, exprSize - m_indicator);
127 int rsSize = repeatStruct.size();
128 int min = 0;
129 int max = 0;
130
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700131 if (boost::regex_match(repeatStruct, boost::regex("\\{[0-9]+,[0-9]+\\}"))){
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800132 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());
135 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700136 else if (boost::regex_match(repeatStruct, boost::regex("\\{,[0-9]+\\}"))){
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800137 int separator = repeatStruct.find_first_of(',', 0);
138 min = 0;
139 max = atoi(repeatStruct.substr(separator + 1, rsSize - separator - 2).c_str());
140 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700141 else if (boost::regex_match(repeatStruct, boost::regex("\\{[0-9]+,\\}"))){
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800142 int separator = repeatStruct.find_first_of(',', 0);
143 min = atoi(repeatStruct.substr(1, separator).c_str());
144 max = intMax;
145 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700146 else if (boost::regex_match(repeatStruct, boost::regex("\\{[0-9]+\\}"))){
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800147 min = atoi(repeatStruct.substr(1, rsSize - 1).c_str());
148 max = min;
149 }
150 else
151 throw RegexMatcher::Error(std::string("Error: RegexRepeatMatcher.ParseRepetition(): ")
152 + "Unrecognized format "+ m_expr);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700153
154 if (min > intMax || max > intMax || min > max)
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800155 throw RegexMatcher::Error(std::string("Error: RegexRepeatMatcher.ParseRepetition(): ")
156 + "Wrong number " + m_expr);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700157
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800158 m_repeatMin = min;
159 m_repeatMax = max;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700160
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800161 return true;
162 }
163 }
164 return false;
165}
166
167inline bool
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700168RegexRepeatMatcher::match(const Name& name, const int& offset, const int& len)
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800169{
170 // _LOG_TRACE ("Enter RegexRepeatMatcher::match");
171
172 m_matchResult.clear();
173
174 if (0 == m_repeatMin)
175 if (0 == len)
176 return true;
177
178 if (recursiveMatch(0, name, offset, len))
179 {
180 for (int i = offset; i < offset + len; i++)
181 m_matchResult.push_back(name.get(i));
182 return true;
183 }
184 else
185 return false;
186}
187
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700188inline bool
189RegexRepeatMatcher::recursiveMatch(int repeat, const Name& name, const int& offset, const int& len)
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800190{
191 // _LOG_TRACE ("Enter RegexRepeatMatcher::recursiveMatch");
192
193 // _LOG_DEBUG ("repeat: " << repeat << " offset: " << offset << " len: " << len);
194 // _LOG_DEBUG ("m_repeatMin: " << m_repeatMin << " m_repeatMax: " << m_repeatMax);
195
196 int tried = len;
197 shared_ptr<RegexMatcher> matcher = m_matcherList[0];
198
199 if (0 < len && repeat >= m_repeatMax)
200 {
201 // _LOG_DEBUG("Match Fail: Reach m_repeatMax && More components");
202 return false;
203 }
204
205 if (0 == len && repeat < m_repeatMin)
206 {
207 // _LOG_DEBUG("Match Fail: No more components && have NOT reached m_repeatMin " << len << ", " << m_repeatMin);
208 return false;
209 }
210
211 if (0 == len && repeat >= m_repeatMin)
212 {
213 // _LOG_DEBUG("Match Succeed: No more components && reach m_repeatMin");
214 return true;
215 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700216
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800217 while(tried >= 0)
218 {
219 // _LOG_DEBUG("Attempt tried: " << tried);
220
221 if (matcher->match(name, offset, tried) and recursiveMatch(repeat + 1, name, offset + tried, len - tried))
222 return true;
223 // _LOG_DEBUG("Failed at tried: " << tried);
224 tried --;
225 }
226
227 return false;
228}
229
230
231} // namespace ndn
232
233#endif // NDN_UTIL_REGEX_REGEX_REPEAT_MATCHER_HPP