All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
regex-component-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_COMPONENT_MATCHER_HPP
9 #define NDN_UTIL_REGEX_REGEX_COMPONENT_MATCHER_HPP
10 
11 #include <boost/regex.hpp>
12 
13 #include "regex-matcher.hpp"
14 #include "regex-pseudo-matcher.hpp"
15 
16 
17 namespace ndn {
18 
20 {
21 public:
28  RegexComponentMatcher(const std::string& expr,
29  ptr_lib::shared_ptr<RegexBackrefManager> backRefManager,
30  bool exact = true);
31 
32  virtual ~RegexComponentMatcher() {};
33 
34  virtual bool
35  match(const Name & name, const int & offset, const int &len = 1);
36 
37 protected:
42  virtual void
43  compile();
44 
45 private:
46  bool m_exact;
47  boost::regex m_componentRegex;
48  std::vector<ptr_lib::shared_ptr<RegexPseudoMatcher> > m_pseudoMatcher;
49 
50 };
51 
52 
53 inline
55  ptr_lib::shared_ptr<RegexBackrefManager> backRefManager,
56  bool exact)
57  : RegexMatcher (expr, EXPR_COMPONENT, backRefManager),
58  m_exact(exact)
59 {
60  // _LOG_TRACE ("Enter RegexComponentMatcher Constructor: ");
61  compile();
62  // _LOG_TRACE ("Exit RegexComponentMatcher Constructor: ");
63 }
64 
65 inline void
67 {
68  // _LOG_TRACE ("Enter RegexComponentMatcher::compile");
69 
70  m_componentRegex = boost::regex (m_expr);
71 
72  m_pseudoMatcher.clear();
73  m_pseudoMatcher.push_back(ptr_lib::make_shared<RegexPseudoMatcher>());
74 
75  for (size_t i = 1; i < m_componentRegex.mark_count(); i++)
76  {
77  ptr_lib::shared_ptr<RegexPseudoMatcher> pMatcher = ptr_lib::make_shared<RegexPseudoMatcher>();
78  m_pseudoMatcher.push_back(pMatcher);
79  m_backrefManager->pushRef(ptr_lib::static_pointer_cast<RegexMatcher>(pMatcher));
80  }
81 
82 
83  // _LOG_TRACE ("Exit RegexComponentMatcher::compile");
84 }
85 
86 inline bool
87 RegexComponentMatcher::match (const Name & name, const int & offset, const int & len)
88 {
89  // _LOG_TRACE ("Enter RegexComponentMatcher::match ");
90 
91  m_matchResult.clear();
92 
93  if ("" == m_expr)
94  {
95  m_matchResult.push_back(name.get(offset));
96  return true;
97  }
98 
99  if (true == m_exact)
100  {
101  boost::smatch subResult;
102  std::string targetStr = name.get(offset).toEscapedString();
103  if (boost::regex_match(targetStr, subResult, m_componentRegex))
104  {
105  for (size_t i = 1; i < m_componentRegex.mark_count(); i++)
106  {
107  m_pseudoMatcher[i]->resetMatchResult();
108  m_pseudoMatcher[i]->setMatchResult(subResult[i]);
109  }
110  m_matchResult.push_back(name.get(offset));
111  return true;
112  }
113  }
114  else
115  {
116  throw RegexMatcher::Error("Non-exact component search is not supported yet!");
117  }
118 
119  return false;
120 }
121 
122 
123 } // namespace ndn
124 
125 #endif // NDN_UTIL_REGEX_REGEX_COMPONENT_MATCHER_HPP
void toEscapedString(std::ostream &result) const
Write the value to result, escaping characters according to the NDN URI Scheme.
RegexComponentMatcher(const std::string &expr, ptr_lib::shared_ptr< RegexBackrefManager > backRefManager, bool exact=true)
Create a RegexComponent matcher from expr.
shared_ptr< RegexBackrefManager > m_backrefManager
virtual void compile()
Compile the regular expression to generate the more matchers when necessary.
virtual bool match(const Name &name, const int &offset, const int &len=1)
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
const Component & get(ssize_t i) const
Get the component at the given index.
Definition: name.hpp:340