NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
regex-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_MATCHER_H
25 #define NDN_UTIL_REGEX_REGEX_MATCHER_H
26 
27 #include "../../common.hpp"
28 #include "../../name.hpp"
29 
30 namespace ndn {
31 
32 class RegexBackrefManager;
33 
35 {
36 public:
37  class Error : public std::runtime_error
38  {
39  public:
40  explicit
41  Error(const std::string& what)
42  : std::runtime_error(what)
43  {
44  }
45  };
46 
55  };
56 
57  RegexMatcher(const std::string& expr,
58  const RegexExprType& type,
59  shared_ptr<RegexBackrefManager> backrefManager = shared_ptr<RegexBackrefManager>());
60 
61  virtual
62  ~RegexMatcher();
63 
64  virtual bool
65  match(const Name& name, size_t offset, size_t len);
66 
71  const std::vector<name::Component>&
73  {
74  return m_matchResult;
75  }
76 
77  const std::string&
78  getExpr() const
79  {
80  return m_expr;
81  }
82 
83 protected:
88  virtual void
89  compile() = 0;
90 
91 private:
92  bool
93  recursiveMatch(size_t matcherNo, const Name& name, size_t offset, size_t len);
94 
95 
96 protected:
97  const std::string m_expr;
99  shared_ptr<RegexBackrefManager> m_backrefManager;
100  std::vector<shared_ptr<RegexMatcher> > m_matchers;
101  std::vector<name::Component> m_matchResult;
102 };
103 
104 inline std::ostream&
105 operator<<(std::ostream& os, const RegexMatcher& regex)
106 {
107  os << regex.getExpr();
108  return os;
109 }
110 
111 } // namespace ndn
112 
113 #include "regex-backref-manager.hpp"
114 
115 namespace ndn {
116 
117 inline
118 RegexMatcher::RegexMatcher(const std::string& expr,
119  const RegexExprType& type,
120  shared_ptr<RegexBackrefManager> backrefManager)
121  : m_expr(expr)
122  , m_type(type)
123  , m_backrefManager(backrefManager)
124 {
125  if (!static_cast<bool>(m_backrefManager))
126  m_backrefManager = make_shared<RegexBackrefManager>();
127 }
128 
129 inline
131 {
132 }
133 
134 inline bool
135 RegexMatcher::match(const Name& name, size_t offset, size_t len)
136 {
137  bool result = false;
138 
139  m_matchResult.clear();
140 
141  if (recursiveMatch(0, name, offset, len))
142  {
143  for (size_t i = offset; i < offset + len ; i++)
144  m_matchResult.push_back(name.get(i));
145  result = true;
146  }
147  else
148  {
149  result = false;
150  }
151 
152  return result;
153 }
154 
155 inline bool
156 RegexMatcher::recursiveMatch(size_t matcherNo, const Name& name, size_t offset, size_t len)
157 {
158  ssize_t tried = len;
159 
160  if (matcherNo >= m_matchers.size())
161  return (len == 0);
162 
163  shared_ptr<RegexMatcher> matcher = m_matchers[matcherNo];
164 
165  while (tried >= 0)
166  {
167  if (matcher->match(name, offset, tried) &&
168  recursiveMatch(matcherNo + 1, name, offset + tried, len - tried))
169  return true;
170  tried--;
171  }
172 
173  return false;
174 }
175 
176 
177 } // namespace ndn
178 
179 
180 #endif // NDN_UTIL_REGEX_REGEX_MATCHER_H
virtual void compile()=0
Compile the regular expression to generate the more matchers when necessary.
Copyright (c) 2011-2015 Regents of the University of California.
Error(const std::string &what)
std::ostream & operator<<(std::ostream &os, const Data &data)
Definition: data.cpp:340
const RegexExprType m_type
const std::string & getExpr() const
STL namespace.
const std::vector< name::Component > & getMatchResult() const
get the matched name components
RegexMatcher(const std::string &expr, const RegexExprType &type, shared_ptr< RegexBackrefManager > backrefManager=shared_ptr< RegexBackrefManager >())
shared_ptr< RegexBackrefManager > m_backrefManager
std::vector< shared_ptr< RegexMatcher > > m_matchers
std::vector< name::Component > m_matchResult
Name abstraction to represent an absolute name.
Definition: name.hpp:46
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:419