NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
regex-component-set-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_COMPONENT_SET_MATCHER_HPP
25 #define NDN_UTIL_REGEX_COMPONENT_SET_MATCHER_HPP
26 
27 #include "../../common.hpp"
28 
29 #include "regex-matcher.hpp"
31 
32 #include <set>
33 
34 namespace ndn {
35 
37 {
38 public:
44  RegexComponentSetMatcher(const std::string& expr, shared_ptr<RegexBackrefManager> backrefManager);
45 
46  virtual
48 
49  virtual bool
50  match(const Name& name, size_t offset, size_t len = 1);
51 
52 protected:
57  virtual void
58  compile();
59 
60 private:
61  size_t
62  extractComponent(size_t index);
63 
64  void
65  compileSingleComponent();
66 
67  void
68  compileMultipleComponents(size_t start, size_t lastIndex);
69 
70 private:
71  typedef std::set<shared_ptr<RegexComponentMatcher> > ComponentsSet;
72  ComponentsSet m_components;
73  bool m_isInclusion;
74 };
75 
76 
77 inline
79  shared_ptr<RegexBackrefManager> backrefManager)
80  : RegexMatcher(expr, EXPR_COMPONENT_SET, backrefManager)
81  , m_isInclusion(true)
82 {
83  compile();
84 }
85 
86 inline
88 {
89 }
90 
91 inline void
93 {
94  if (m_expr.size() < 2)
95  BOOST_THROW_EXCEPTION(RegexMatcher::Error("Regexp compile error (cannot parse " +
96  m_expr + ")"));
97 
98  switch (m_expr[0]) {
99  case '<':
100  return compileSingleComponent();
101  case '[':
102  {
103  size_t lastIndex = m_expr.size() - 1;
104  if (']' != m_expr[lastIndex])
105  BOOST_THROW_EXCEPTION(RegexMatcher::Error("Regexp compile error (no matching ']' in " +
106  m_expr + ")"));
107 
108  if ('^' == m_expr[1]) {
109  m_isInclusion = false;
110  compileMultipleComponents(2, lastIndex);
111  }
112  else
113  compileMultipleComponents(1, lastIndex);
114  break;
115  }
116  default:
117  BOOST_THROW_EXCEPTION(RegexMatcher::Error("Regexp compile error (cannot parse " +
118  m_expr + ")"));
119  }
120 }
121 
122 inline void
123 RegexComponentSetMatcher::compileSingleComponent()
124 {
125  size_t end = extractComponent(1);
126 
127  if (m_expr.size() != end)
128  {
129  BOOST_THROW_EXCEPTION(RegexMatcher::Error("Component expr error " + m_expr));
130  }
131  else
132  {
133  shared_ptr<RegexComponentMatcher> component =
134  make_shared<RegexComponentMatcher>(m_expr.substr(1, end - 2), m_backrefManager);
135 
136  m_components.insert(component);
137  }
138 }
139 
140 inline void
141 RegexComponentSetMatcher::compileMultipleComponents(size_t start, size_t lastIndex)
142 {
143  size_t index = start;
144  size_t tempIndex = start;
145 
146  while (index < lastIndex) {
147  if ('<' != m_expr[index])
148  BOOST_THROW_EXCEPTION(RegexMatcher::Error("Component expr error " + m_expr));
149 
150  tempIndex = index + 1;
151  index = extractComponent(tempIndex);
152 
153  shared_ptr<RegexComponentMatcher> component =
154  make_shared<RegexComponentMatcher>(m_expr.substr(tempIndex, index - tempIndex - 1),
156 
157  m_components.insert(component);
158  }
159 
160  if (index != lastIndex)
161  BOOST_THROW_EXCEPTION(RegexMatcher::Error("Not sufficient expr to parse " + m_expr));
162 }
163 
164 inline bool
165 RegexComponentSetMatcher::match(const Name& name, size_t offset, size_t len)
166 {
167  bool isMatched = false;
168 
169  /* componentset only matches one component */
170  if (len != 1)
171  {
172  return false;
173  }
174 
175  for (ComponentsSet::iterator it = m_components.begin();
176  it != m_components.end();
177  ++it)
178  {
179  if ((*it)->match(name, offset, len))
180  {
181  isMatched = true;
182  break;
183  }
184  }
185 
186  m_matchResult.clear();
187 
188  if (m_isInclusion ? isMatched : !isMatched)
189  {
190  m_matchResult.push_back(name.get(offset));
191  return true;
192  }
193  else
194  return false;
195 }
196 
197 inline size_t
198 RegexComponentSetMatcher::extractComponent(size_t index)
199 {
200  size_t lcount = 1;
201  size_t rcount = 0;
202 
203  while (lcount > rcount) {
204  switch (m_expr[index]) {
205  case '<':
206  lcount++;
207  break;
208 
209  case '>':
210  rcount++;
211  break;
212 
213  case 0:
214  BOOST_THROW_EXCEPTION(RegexMatcher::Error("Error: square brackets mismatch"));
215  break;
216  }
217  index++;
218 
219  }
220 
221  return index;
222 }
223 
224 } // namespace ndn
225 
226 #endif // NDN_UTIL_REGEX_COMPONENT_SET_MATCHER_HPP
Copyright (c) 2011-2015 Regents of the University of California.
Table::const_iterator iterator
Definition: cs-internal.hpp:41
shared_ptr< RegexBackrefManager > m_backrefManager
std::vector< name::Component > m_matchResult
Name abstraction to represent an absolute name.
Definition: name.hpp:46
RegexComponentSetMatcher(const std::string &expr, shared_ptr< RegexBackrefManager > backrefManager)
Create a RegexComponentSetMatcher matcher from expr.
virtual void compile()
Compile the regular expression to generate the more matchers when necessary.
const std::string m_expr
virtual bool match(const Name &name, size_t offset, size_t len=1)
const Component & get(ssize_t i) const
Get the component at the given index.
Definition: name.hpp:419