NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: 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:
87  virtual void
88  compile() = 0;
89 
90 private:
91  bool
92  recursiveMatch(size_t matcherNo, const Name& name, size_t offset, size_t len);
93 
94 
95 protected:
96  const std::string m_expr;
98  shared_ptr<RegexBackrefManager> m_backrefManager;
99  std::vector<shared_ptr<RegexMatcher> > m_matchers;
100  std::vector<name::Component> m_matchResult;
101 };
102 
103 inline std::ostream&
104 operator<<(std::ostream& os, const RegexMatcher& regex)
105 {
106  os << regex.getExpr();
107  return os;
108 }
109 
110 } // namespace ndn
111 
112 #include "regex-backref-manager.hpp"
113 
114 namespace ndn {
115 
116 inline
117 RegexMatcher::RegexMatcher(const std::string& expr,
118  const RegexExprType& type,
119  shared_ptr<RegexBackrefManager> backrefManager)
120  : m_expr(expr)
121  , m_type(type)
122  , m_backrefManager(backrefManager)
123 {
124  if (!static_cast<bool>(m_backrefManager))
125  m_backrefManager = make_shared<RegexBackrefManager>();
126 }
127 
128 inline
130 {
131 }
132 
133 inline bool
134 RegexMatcher::match(const Name& name, size_t offset, size_t len)
135 {
136  bool result = false;
137 
138  m_matchResult.clear();
139 
140  if (recursiveMatch(0, name, offset, len))
141  {
142  for (size_t i = offset; i < offset + len ; i++)
143  m_matchResult.push_back(name.get(i));
144  result = true;
145  }
146  else
147  {
148  result = false;
149  }
150 
151  return result;
152 }
153 
154 inline bool
155 RegexMatcher::recursiveMatch(size_t matcherNo, const Name& name, size_t offset, size_t len)
156 {
157  ssize_t tried = len;
158 
159  if (matcherNo >= m_matchers.size())
160  return (len == 0);
161 
162  shared_ptr<RegexMatcher> matcher = m_matchers[matcherNo];
163 
164  while (tried >= 0)
165  {
166  if (matcher->match(name, offset, tried) &&
167  recursiveMatch(matcherNo + 1, name, offset + tried, len - tried))
168  return true;
169  tried--;
170  }
171 
172  return false;
173 }
174 
175 
176 } // namespace ndn
177 
178 
179 #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)
const Component & get(ssize_t i) const
Get the component at the given index.
Definition: name.hpp:411
std::ostream & operator<<(std::ostream &os, const Data &data)
Definition: data.cpp:320
const RegexExprType m_type
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
const std::string & getExpr() const
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)