All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
regex-matcher.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
8 #ifndef NDN_UTIL_REGEX_REGEX_MATCHER_H
9 #define NDN_UTIL_REGEX_REGEX_MATCHER_H
10 
11 #include "../../common.hpp"
12 #include "../../name.hpp"
13 
14 namespace ndn {
15 
16 class RegexBackrefManager;
17 
19 {
20 public:
21  class Error : public std::runtime_error
22  {
23  public:
24  explicit
25  Error(const std::string& what)
26  : std::runtime_error(what)
27  {
28  }
29  };
30 
33 
35 
37 
41 
43  };
44 
45  RegexMatcher(const std::string& expr,
46  const RegexExprType& type,
47  shared_ptr<RegexBackrefManager> backrefManager = shared_ptr<RegexBackrefManager>());
48 
49  virtual
50  ~RegexMatcher();
51 
52  virtual bool
53  match(const Name& name, const int& offset, const int& len);
54 
59  const std::vector<name::Component>&
61  { return m_matchResult; }
62 
63  const std::string&
64  getExpr() const
65  { return m_expr; }
66 
67 protected:
72  virtual void
73  compile() = 0;
74 
75 private:
76  bool
77  recursiveMatch(size_t mId, const Name& name, size_t offset, size_t len);
78 
79 
80 protected:
81  const std::string m_expr;
83  shared_ptr<RegexBackrefManager> m_backrefManager;
84  std::vector<shared_ptr<RegexMatcher> > m_matcherList;
85  std::vector<name::Component> m_matchResult;
86 };
87 
88 } // namespace ndn
89 
91 
92 namespace ndn {
93 
94 inline
95 RegexMatcher::RegexMatcher(const std::string& expr,
96  const RegexExprType& type,
97  shared_ptr<RegexBackrefManager> backrefManager)
98  : m_expr(expr),
99  m_type(type),
100  m_backrefManager(backrefManager)
101 {
102  if (NULL == m_backrefManager)
103  m_backrefManager = make_shared<RegexBackrefManager>();
104 }
105 
106 inline
108 {
109 }
110 
111 inline bool
112 RegexMatcher::match (const Name& name, const int& offset, const int& len)
113 {
114  // _LOG_TRACE ("Enter RegexMatcher::match");
115  bool result = false;
116 
117  m_matchResult.clear();
118 
119  if (recursiveMatch(0, name, offset, len))
120  {
121  for(int i = offset; i < offset + len ; i++)
122  m_matchResult.push_back(name.get(i));
123  result = true;
124  }
125  else
126  {
127  result = false;
128  }
129 
130  // _LOG_TRACE ("Exit RegexMatcher::match");
131  return result;
132 }
133 
134 inline bool
135 RegexMatcher::recursiveMatch(size_t mId, const Name& name, size_t offset, size_t len)
136 {
137  // _LOG_TRACE ("Enter RegexMatcher::recursiveMatch");
138 
139  int tried = len;
140 
141  if (mId >= m_matcherList.size())
142  return (len != 0 ? false : true);
143 
144  shared_ptr<RegexMatcher> matcher = m_matcherList[mId];
145 
146  while(tried >= 0)
147  {
148  if (matcher->match(name, offset, tried) && recursiveMatch(mId + 1, name, offset + tried, len - tried))
149  return true;
150  tried--;
151  }
152 
153  return false;
154 }
155 
156 
157 } // namespace ndn
158 
159 
160 #endif // NDN_UTIL_REGEX_REGEX_MATCHER_H
virtual void compile()=0
Compile the regular expression to generate the more matchers when necessary.
Error(const std::string &what)
const RegexExprType m_type
const std::string & getExpr() const
const std::vector< name::Component > & getMatchResult() const
get the matched name components
virtual bool match(const Name &name, const int &offset, const int &len)
RegexMatcher(const std::string &expr, const RegexExprType &type, shared_ptr< RegexBackrefManager > backrefManager=shared_ptr< RegexBackrefManager >())
shared_ptr< RegexBackrefManager > m_backrefManager
std::vector< name::Component > m_matchResult
A Name holds an array of Name::Component and represents an NDN name.
Definition: name.hpp:26
const std::string m_expr
std::vector< shared_ptr< RegexMatcher > > m_matcherList
const Component & get(ssize_t i) const
Get the component at the given index.
Definition: name.hpp:340