blob: fb420420df11e832931683b723c9e447ef0a2ce2 [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);
Yingdi Yu5e974202014-01-29 16:59:06 -080023
24 virtual ~RegexRepeatMatcher(){}
25
26 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 */
34 virtual void
35 compile();
36
Yingdi Yu5e974202014-01-29 16:59:06 -080037private:
38 bool
39 parseRepetition();
40
41 bool
42 recursiveMatch (int repeat,
43 const Name & name,
44 const int & offset,
45 const int &len);
46
47private:
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
70inline void
71RegexRepeatMatcher::compile()
72{
73 // _LOG_TRACE ("Enter RegexRepeatMatcher::compile");
74
75 shared_ptr<RegexMatcher> matcher;
76
77 if('(' == m_expr[0]){
78 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);
86
87 parseRepetition();
88
89 // _LOG_TRACE ("Exit RegexRepeatMatcher::compile");
90
91}
92
93inline bool
94RegexRepeatMatcher::parseRepetition()
95{
96 // _LOG_DEBUG ("Enter RegexRepeatMatcher::ParseRepetition()" << m_expr << " indicator: " << m_indicator);
97
98 int exprSize = m_expr.size();
99 int intMax = std::numeric_limits<int>::max();
100
101 if(exprSize == m_indicator){
102 m_repeatMin = 1;
103 m_repeatMax = 1;
104
105 return true;
106 }
107 else{
108 if(exprSize == (m_indicator + 1)){
109 if('?' == m_expr[m_indicator]){
110 m_repeatMin = 0;
111 m_repeatMax = 1;
112 return true;
113 }
114 if('+' == m_expr[m_indicator]){
115 m_repeatMin = 1;
116 m_repeatMax = intMax;
117 return true;
118 }
119 if('*' == m_expr[m_indicator]){
120 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
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());
135 }
136 else if(boost::regex_match(repeatStruct, boost::regex("\\{,[0-9]+\\}"))){
137 int separator = repeatStruct.find_first_of(',', 0);
138 min = 0;
139 max = atoi(repeatStruct.substr(separator + 1, rsSize - separator - 2).c_str());
140 }
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());
144 max = intMax;
145 }
146 else if(boost::regex_match(repeatStruct, boost::regex("\\{[0-9]+\\}"))){
147 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);
153
154 if(min > intMax || max > intMax || min > max)
155 throw RegexMatcher::Error(std::string("Error: RegexRepeatMatcher.ParseRepetition(): ")
156 + "Wrong number " + m_expr);
157
158 m_repeatMin = min;
159 m_repeatMax = max;
160
161 return true;
162 }
163 }
164 return false;
165}
166
167inline bool
168RegexRepeatMatcher::match(const Name & name, const int & offset, const int & len)
169{
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
188inline bool
189RegexRepeatMatcher::recursiveMatch(int repeat, const Name & name, const int & offset, const int & len)
190{
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 }
216
217 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