NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
regex-component-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_COMPONENT_MATCHER_HPP
25 #define NDN_UTIL_REGEX_REGEX_COMPONENT_MATCHER_HPP
26 
27 #include <boost/regex.hpp>
28 
29 #include "regex-matcher.hpp"
30 #include "regex-pseudo-matcher.hpp"
31 
32 namespace ndn {
33 
35 {
36 public:
43  RegexComponentMatcher(const std::string& expr,
44  shared_ptr<RegexBackrefManager> backrefManager,
45  bool isExactMatch = true);
46 
47  virtual
49  {
50  };
51 
52  virtual bool
53  match(const Name& name, size_t offset, size_t len = 1);
54 
55 protected:
60  virtual void
61  compile();
62 
63 private:
64  bool m_isExactMatch;
65  boost::regex m_componentRegex;
66  std::vector<shared_ptr<RegexPseudoMatcher> > m_pseudoMatchers;
67 
68 };
69 
70 
71 inline
73  shared_ptr<RegexBackrefManager> backrefManager,
74  bool isExactMatch)
75  : RegexMatcher(expr, EXPR_COMPONENT, backrefManager)
76  , m_isExactMatch(isExactMatch)
77 {
78  compile();
79 }
80 
81 // Re: http://www.boost.org/users/history/version_1_56_0.html
82 //
83 // Breaking change: corrected behavior of basic_regex<>::mark_count() to match existing
84 // documentation, basic_regex<>::subexpression(n) changed to match, see
85 // https://svn.boost.org/trac/boost/ticket/9227
87 #if BOOST_VERSION < 105600
88  1;
89 #else
90  0;
91 #endif
92 
93 inline void
95 {
96  m_componentRegex = boost::regex(m_expr);
97 
98  m_pseudoMatchers.clear();
99  m_pseudoMatchers.push_back(make_shared<RegexPseudoMatcher>());
100 
101  for (size_t i = 1;
102  i <= m_componentRegex.mark_count() - BOOST_REGEXP_MARK_COUNT_CORRECTION; i++)
103  {
104  shared_ptr<RegexPseudoMatcher> pMatcher = make_shared<RegexPseudoMatcher>();
105  m_pseudoMatchers.push_back(pMatcher);
106  m_backrefManager->pushRef(static_pointer_cast<RegexMatcher>(pMatcher));
107  }
108 }
109 
110 inline bool
111 RegexComponentMatcher::match(const Name& name, size_t offset, size_t len)
112 {
113  m_matchResult.clear();
114 
115  if (m_expr.empty())
116  {
117  m_matchResult.push_back(name.get(offset));
118  return true;
119  }
120 
121  if (m_isExactMatch)
122  {
123  boost::smatch subResult;
124  std::string targetStr = name.get(offset).toUri();
125  if (boost::regex_match(targetStr, subResult, m_componentRegex))
126  {
127  for (size_t i = 1;
128  i <= m_componentRegex.mark_count() - BOOST_REGEXP_MARK_COUNT_CORRECTION; i++)
129  {
130  m_pseudoMatchers[i]->resetMatchResult();
131  m_pseudoMatchers[i]->setMatchResult(subResult[i]);
132  }
133  m_matchResult.push_back(name.get(offset));
134  return true;
135  }
136  }
137  else
138  {
139  BOOST_THROW_EXCEPTION(RegexMatcher::Error("Non-exact component search is not supported "
140  "yet"));
141  }
142 
143  return false;
144 }
145 
146 
147 } // namespace ndn
148 
149 #endif // NDN_UTIL_REGEX_REGEX_COMPONENT_MATCHER_HPP
Copyright (c) 2011-2015 Regents of the University of California.
virtual bool match(const Name &name, size_t offset, size_t len=1)
RegexComponentMatcher(const std::string &expr, shared_ptr< RegexBackrefManager > backrefManager, bool isExactMatch=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.
void toUri(std::ostream &os) const
Write *this to the output stream, escaping characters according to the NDN URI Scheme.
std::vector< name::Component > m_matchResult
Name abstraction to represent an absolute name.
Definition: name.hpp:46
const std::string m_expr
static const size_t BOOST_REGEXP_MARK_COUNT_CORRECTION
const Component & get(ssize_t i) const
Get the component at the given index.
Definition: name.hpp:419