NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
regex-top-matcher.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
24 #include "regex-top-matcher.hpp"
25 
28 
29 #include <boost/lexical_cast.hpp>
30 
31 namespace ndn {
32 
33 RegexTopMatcher::RegexTopMatcher(const std::string& expr, const std::string& expand)
34  : RegexMatcher(expr, EXPR_TOP)
35  , m_expand(expand)
36  , m_isSecondaryUsed(false)
37 {
38  m_primaryBackrefManager = make_shared<RegexBackrefManager>();
39  m_secondaryBackrefManager = make_shared<RegexBackrefManager>();
40  compile();
41 }
42 
44 {
45 }
46 
47 void
49 {
50  std::string errMsg = "Error: RegexTopMatcher.Compile(): ";
51 
52  std::string expr = m_expr;
53 
54  if ('$' != expr[expr.size() - 1])
55  expr = expr + "<.*>*";
56  else
57  expr = expr.substr(0, expr.size() - 1);
58 
59  if ('^' != expr[0]) {
60  m_secondaryMatcher = make_shared<RegexPatternListMatcher>(
61  "<.*>*" + expr,
62  m_secondaryBackrefManager);
63  }
64  else {
65  expr = expr.substr(1, expr.size() - 1);
66  }
67 
68  // On OSX 10.9, boost, and C++03 the following doesn't work without ndn::
69  // because the argument-dependent lookup prefers STL to boost
70  m_primaryMatcher = ndn::make_shared<RegexPatternListMatcher>(expr,
71  m_primaryBackrefManager);
72 }
73 
74 bool
76 {
77  m_isSecondaryUsed = false;
78 
79  m_matchResult.clear();
80 
81  if (m_primaryMatcher->match(name, 0, name.size()))
82  {
83  m_matchResult = m_primaryMatcher->getMatchResult();
84  return true;
85  }
86  else
87  {
88  if (static_cast<bool>(m_secondaryMatcher) && m_secondaryMatcher->match(name, 0, name.size()))
89  {
90  m_matchResult = m_secondaryMatcher->getMatchResult();
91  m_isSecondaryUsed = true;
92  return true;
93  }
94  return false;
95  }
96 }
97 
98 bool
99 RegexTopMatcher::match(const Name& name, size_t, size_t)
100 {
101  return match(name);
102 }
103 
104 Name
105 RegexTopMatcher::expand(const std::string& expandStr)
106 {
107  Name result;
108 
109  shared_ptr<RegexBackrefManager> backrefManager =
110  (m_isSecondaryUsed ? m_secondaryBackrefManager : m_primaryBackrefManager);
111 
112  size_t backrefNo = backrefManager->size();
113 
114  std::string expand;
115 
116  if (!expandStr.empty())
117  expand = expandStr;
118  else
119  expand = m_expand;
120 
121  size_t offset = 0;
122  while (offset < expand.size())
123  {
124  std::string item = getItemFromExpand(expand, offset);
125  if (item[0] == '<')
126  {
127  result.append(item.substr(1, item.size() - 2));
128  }
129  if (item[0] == '\\')
130  {
131  size_t index = boost::lexical_cast<size_t>(item.substr(1, item.size() - 1));
132 
133  if (0 == index) {
136  for (; it != end; it++)
137  result.append(*it);
138  }
139  else if (index <= backrefNo)
140  {
141  std::vector<name::Component>::const_iterator it =
142  backrefManager->getBackref(index - 1)->getMatchResult().begin();
143  std::vector<name::Component>::const_iterator end =
144  backrefManager->getBackref(index - 1)->getMatchResult().end();
145  for (; it != end; it++)
146  result.append(*it);
147  }
148  else
149  BOOST_THROW_EXCEPTION(RegexMatcher::Error("Exceed the range of back reference"));
150  }
151  }
152  return result;
153 }
154 
155 std::string
156 RegexTopMatcher::getItemFromExpand(const std::string& expand, size_t& offset)
157 {
158  size_t begin = offset;
159 
160  if (expand[offset] == '\\')
161  {
162  offset++;
163  if (offset >= expand.size())
164  BOOST_THROW_EXCEPTION(RegexMatcher::Error("wrong format of expand string!"));
165 
166  while (expand[offset] <= '9' and expand[offset] >= '0') {
167  offset++;
168  if (offset > expand.size())
169  BOOST_THROW_EXCEPTION(RegexMatcher::Error("wrong format of expand string!"));
170  }
171  if (offset > begin + 1)
172  return expand.substr(begin, offset - begin);
173  else
174  BOOST_THROW_EXCEPTION(RegexMatcher::Error("wrong format of expand string!"));
175  }
176  else if (expand[offset] == '<')
177  {
178  offset++;
179  if (offset >= expand.size())
180  BOOST_THROW_EXCEPTION(RegexMatcher::Error("wrong format of expand string!"));
181 
182  size_t left = 1;
183  size_t right = 0;
184  while (right < left)
185  {
186  if (expand[offset] == '<')
187  left++;
188  if (expand[offset] == '>')
189  right++;
190  offset++;
191  if (offset >= expand.size())
192  BOOST_THROW_EXCEPTION(RegexMatcher::Error("wrong format of expand string!"));
193  }
194  return expand.substr(begin, offset - begin);
195  }
196  else
197  BOOST_THROW_EXCEPTION(RegexMatcher::Error("wrong format of expand string!"));
198 }
199 
200 shared_ptr<RegexTopMatcher>
201 RegexTopMatcher::fromName(const Name& name, bool hasAnchor)
202 {
203  std::string regexStr("^");
204 
205  for (Name::const_iterator it = name.begin(); it != name.end(); it++)
206  {
207  regexStr.append("<");
208  regexStr.append(convertSpecialChar(it->toUri()));
209  regexStr.append(">");
210  }
211 
212  if (hasAnchor)
213  regexStr.append("$");
214 
215  // On OSX 10.9, boost, and C++03 the following doesn't work without ndn::
216  // because the argument-dependent lookup prefers STL to boost
217  return ndn::make_shared<RegexTopMatcher>(regexStr);
218 }
219 
220 std::string
221 RegexTopMatcher::convertSpecialChar(const std::string& str)
222 {
223  std::string newStr;
224  for (size_t i = 0; i < str.size(); i++)
225  {
226  char c = str[i];
227  switch (c)
228  {
229  case '.':
230  case '[':
231  case '{':
232  case '}':
233  case '(':
234  case ')':
235  case '\\':
236  case '*':
237  case '+':
238  case '?':
239  case '|':
240  case '^':
241  case '$':
242  newStr.push_back('\\');
243  // Fallthrough
244  default:
245  newStr.push_back(c);
246  break;
247  }
248  }
249 
250  return newStr;
251 }
252 
253 } // namespace ndn
Copyright (c) 2011-2015 Regents of the University of California.
static shared_ptr< RegexTopMatcher > fromName(const Name &name, bool hasAnchor=false)
const_iterator end() const
End iterator (const).
Definition: name.hpp:587
Table::const_iterator iterator
Definition: cs-internal.hpp:41
virtual void compile()
Compile the regular expression to generate the more matchers when necessary.
std::vector< name::Component > m_matchResult
Name abstraction to represent an absolute name.
Definition: name.hpp:46
Buffer::const_iterator end() const
Definition: block.cpp:486
const_iterator begin() const
Begin iterator (const).
Definition: name.hpp:576
size_t size() const
Get the number of components.
Definition: name.hpp:408
virtual Name expand(const std::string &expand="")
const std::string m_expr
Component holds a read-only name component value.
Name & append(const uint8_t *value, size_t valueLength)
Append a new component, copying from value of length valueLength.
Definition: name.hpp:148
bool match(const Name &name)
RegexTopMatcher(const std::string &expr, const std::string &expand="")