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