NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: NDN, CCN, CCNx, content centric networks
API Documentation
regex-repeat-matcher.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
24 #ifndef NDN_UTIL_REGEX_REGEX_REPEAT_MATCHER_HPP
25 #define NDN_UTIL_REGEX_REGEX_REPEAT_MATCHER_HPP
26 
27 #include "../../common.hpp"
28 
29 #include <boost/regex.hpp>
30 
31 #include "regex-matcher.hpp"
32 
33 namespace ndn {
34 
36 {
37 public:
38  RegexRepeatMatcher(const std::string& expr,
39  shared_ptr<RegexBackrefManager> backrefManager,
40  size_t indicator);
41 
42  virtual
44  {
45  }
46 
47  virtual bool
48  match(const Name& name, size_t offset, size_t len);
49 
50 protected:
54  virtual void
55  compile();
56 
57 private:
58  bool
59  parseRepetition();
60 
61  bool
62  recursiveMatch(size_t repeat,
63  const Name& name,
64  size_t offset, size_t len);
65 
66 private:
67  size_t m_indicator;
68  size_t m_repeatMin;
69  size_t m_repeatMax;
70 };
71 
72 } // namespace ndn
73 
76 
77 namespace ndn {
78 
79 inline
81  shared_ptr<RegexBackrefManager> backrefManager,
82  size_t indicator)
83  : RegexMatcher(expr, EXPR_REPEAT_PATTERN, backrefManager)
84  , m_indicator(indicator)
85 {
86  compile();
87 }
88 
89 inline void
91 {
92  shared_ptr<RegexMatcher> matcher;
93 
94  if ('(' == m_expr[0]) {
95  matcher = make_shared<RegexBackrefMatcher>(m_expr.substr(0, m_indicator), m_backrefManager);
96  m_backrefManager->pushRef(matcher);
97  dynamic_pointer_cast<RegexBackrefMatcher>(matcher)->lateCompile();
98  }
99  else{
100  matcher = make_shared<RegexComponentSetMatcher>(m_expr.substr(0, m_indicator),
102  }
103  m_matchers.push_back(matcher);
104 
105  parseRepetition();
106 }
107 
108 inline bool
109 RegexRepeatMatcher::parseRepetition()
110 {
111  size_t exprSize = m_expr.size();
112  const size_t MAX_REPETITIONS = std::numeric_limits<size_t>::max();
113 
114  if (exprSize == m_indicator) {
115  m_repeatMin = 1;
116  m_repeatMax = 1;
117 
118  return true;
119  }
120  else {
121  if (exprSize == (m_indicator + 1)) {
122  if ('?' == m_expr[m_indicator]) {
123  m_repeatMin = 0;
124  m_repeatMax = 1;
125  return true;
126  }
127  if ('+' == m_expr[m_indicator]) {
128  m_repeatMin = 1;
129  m_repeatMax = MAX_REPETITIONS;
130  return true;
131  }
132  if ('*' == m_expr[m_indicator]) {
133  m_repeatMin = 0;
134  m_repeatMax = MAX_REPETITIONS;
135  return true;
136  }
137  }
138  else {
139  std::string repeatStruct = m_expr.substr(m_indicator, exprSize - m_indicator);
140  size_t rsSize = repeatStruct.size();
141  size_t min = 0;
142  size_t max = 0;
143 
144  if (boost::regex_match(repeatStruct, boost::regex("\\{[0-9]+,[0-9]+\\}"))) {
145  size_t separator = repeatStruct.find_first_of(',', 0);
146  min = atoi(repeatStruct.substr(1, separator - 1).c_str());
147  max = atoi(repeatStruct.substr(separator + 1, rsSize - separator - 2).c_str());
148  }
149  else if (boost::regex_match(repeatStruct, boost::regex("\\{,[0-9]+\\}"))) {
150  size_t separator = repeatStruct.find_first_of(',', 0);
151  min = 0;
152  max = atoi(repeatStruct.substr(separator + 1, rsSize - separator - 2).c_str());
153  }
154  else if (boost::regex_match(repeatStruct, boost::regex("\\{[0-9]+,\\}"))) {
155  size_t separator = repeatStruct.find_first_of(',', 0);
156  min = atoi(repeatStruct.substr(1, separator).c_str());
157  max = MAX_REPETITIONS;
158  }
159  else if (boost::regex_match(repeatStruct, boost::regex("\\{[0-9]+\\}"))) {
160  min = atoi(repeatStruct.substr(1, rsSize - 1).c_str());
161  max = min;
162  }
163  else
164  BOOST_THROW_EXCEPTION(RegexMatcher::Error(std::string("Error: RegexRepeatMatcher.ParseRepetition():")
165  + " Unrecognized format "+ m_expr));
166 
167  if (min > MAX_REPETITIONS || max > MAX_REPETITIONS || min > max)
168  BOOST_THROW_EXCEPTION(RegexMatcher::Error(std::string("Error: RegexRepeatMatcher.ParseRepetition():")
169  + " Wrong number " + m_expr));
170 
171  m_repeatMin = min;
172  m_repeatMax = max;
173 
174  return true;
175  }
176  }
177  return false;
178 }
179 
180 inline bool
181 RegexRepeatMatcher::match(const Name& name, size_t offset, size_t len)
182 {
183  m_matchResult.clear();
184 
185  if (0 == m_repeatMin)
186  if (0 == len)
187  return true;
188 
189  if (recursiveMatch(0, name, offset, len))
190  {
191  for (size_t i = offset; i < offset + len; i++)
192  m_matchResult.push_back(name.get(i));
193  return true;
194  }
195  else
196  return false;
197 }
198 
199 inline bool
200 RegexRepeatMatcher::recursiveMatch(size_t repeat, const Name& name, size_t offset, size_t len)
201 {
202  ssize_t tried = len;
203  shared_ptr<RegexMatcher> matcher = m_matchers[0];
204 
205  if (0 < len && repeat >= m_repeatMax)
206  {
207  return false;
208  }
209 
210  if (0 == len && repeat < m_repeatMin)
211  {
212  return false;
213  }
214 
215  if (0 == len && repeat >= m_repeatMin)
216  {
217  return true;
218  }
219 
220  while (tried >= 0)
221  {
222  if (matcher->match(name, offset, tried) &&
223  recursiveMatch(repeat + 1, name, offset + tried, len - tried))
224  return true;
225  tried--;
226  }
227 
228  return false;
229 }
230 
231 
232 } // namespace ndn
233 
234 #endif // NDN_UTIL_REGEX_REGEX_REPEAT_MATCHER_HPP
Copyright (c) 2011-2015 Regents of the University of California.
RegexRepeatMatcher(const std::string &expr, shared_ptr< RegexBackrefManager > backrefManager, size_t indicator)
virtual void compile()
Compile the regular expression to generate the more matchers when necessary.
shared_ptr< RegexBackrefManager > m_backrefManager
std::vector< shared_ptr< RegexMatcher > > m_matchers
std::vector< name::Component > m_matchResult
Represents an absolute name.
Definition: name.hpp:42
const std::string m_expr
virtual bool match(const Name &name, size_t offset, size_t len)
const Component & get(ssize_t i) const
Get the component at the given index.
Definition: name.hpp:164