blob: e2317c7f66c9ecf664fab2229c2758598b45ec25 [file] [log] [blame]
Yingdi Yu5e974202014-01-29 16:59:06 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07003 * Copyright (c) 2013-2014, Regents of the University of California.
4 * All rights reserved.
5 *
6 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
7 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
8 *
9 * This file licensed under New BSD License. See COPYING for detailed information about
10 * ndn-cxx library copyright, permissions, and redistribution restrictions.
11 *
12 * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/>
Yingdi Yu5e974202014-01-29 16:59:06 -080013 */
14
Yingdi Yu5e974202014-01-29 16:59:06 -080015#include "regex-top-matcher.hpp"
16
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -080017#include "regex-backref-manager.hpp"
18#include "regex-pattern-list-matcher.hpp"
Yingdi Yu5e974202014-01-29 16:59:06 -080019
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -080020namespace ndn {
Yingdi Yu5e974202014-01-29 16:59:06 -080021
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -080022RegexTopMatcher::RegexTopMatcher(const std::string& expr, const std::string& expand)
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -070023 : RegexMatcher(expr, EXPR_TOP)
24 , m_expand(expand)
25 , m_isSecondaryUsed(false)
Yingdi Yu5e974202014-01-29 16:59:06 -080026{
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -070027 m_primaryBackrefManager = make_shared<RegexBackrefManager>();
28 m_secondaryBackrefManager = make_shared<RegexBackrefManager>();
Yingdi Yu5e974202014-01-29 16:59:06 -080029 compile();
Yingdi Yu5e974202014-01-29 16:59:06 -080030}
31
32RegexTopMatcher::~RegexTopMatcher()
33{
Yingdi Yu5e974202014-01-29 16:59:06 -080034}
35
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070036void
Yingdi Yu5e974202014-01-29 16:59:06 -080037RegexTopMatcher::compile()
38{
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -080039 std::string errMsg = "Error: RegexTopMatcher.Compile(): ";
Yingdi Yu5e974202014-01-29 16:59:06 -080040
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -080041 std::string expr = m_expr;
Yingdi Yu5e974202014-01-29 16:59:06 -080042
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070043 if ('$' != expr[expr.size() - 1])
Yingdi Yu5e974202014-01-29 16:59:06 -080044 expr = expr + "<.*>*";
45 else
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -070046 expr = expr.substr(0, expr.size() - 1);
Yingdi Yu5e974202014-01-29 16:59:06 -080047
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -070048 if ('^' != expr[0]) {
49 m_secondaryMatcher = make_shared<RegexPatternListMatcher>(
50 "<.*>*" + expr,
Alexander Afanasyevf73f0632014-05-12 18:02:37 -070051 m_secondaryBackrefManager);
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -070052 }
53 else {
54 expr = expr.substr(1, expr.size() - 1);
55 }
Yingdi Yu5e974202014-01-29 16:59:06 -080056
Alexander Afanasyevf73f0632014-05-12 18:02:37 -070057 // On OSX 10.9, boost, and C++03 the following doesn't work without ndn::
58 // because the argument-dependent lookup prefers STL to boost
59 m_primaryMatcher = ndn::make_shared<RegexPatternListMatcher>(expr,
60 m_primaryBackrefManager);
Yingdi Yu5e974202014-01-29 16:59:06 -080061}
62
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070063bool
64RegexTopMatcher::match(const Name& name)
Yingdi Yu5e974202014-01-29 16:59:06 -080065{
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -070066 m_isSecondaryUsed = false;
Yingdi Yu5e974202014-01-29 16:59:06 -080067
68 m_matchResult.clear();
69
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070070 if (m_primaryMatcher->match(name, 0, name.size()))
Yingdi Yu5e974202014-01-29 16:59:06 -080071 {
72 m_matchResult = m_primaryMatcher->getMatchResult();
73 return true;
74 }
75 else
76 {
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -070077 if (static_cast<bool>(m_secondaryMatcher) && m_secondaryMatcher->match(name, 0, name.size()))
Yingdi Yu5e974202014-01-29 16:59:06 -080078 {
79 m_matchResult = m_secondaryMatcher->getMatchResult();
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -070080 m_isSecondaryUsed = true;
Yingdi Yu5e974202014-01-29 16:59:06 -080081 return true;
82 }
83 return false;
84 }
85}
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070086
87bool
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -070088RegexTopMatcher::match(const Name& name, size_t, size_t)
Yingdi Yu5e974202014-01-29 16:59:06 -080089{
90 return match(name);
91}
92
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070093Name
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -070094RegexTopMatcher::expand(const std::string& expandStr)
Yingdi Yu5e974202014-01-29 16:59:06 -080095{
Yingdi Yu5e974202014-01-29 16:59:06 -080096 Name result;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070097
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -070098 shared_ptr<RegexBackrefManager> backrefManager =
99 (m_isSecondaryUsed ? m_secondaryBackrefManager : m_primaryBackrefManager);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700100
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700101 size_t backrefNo = backrefManager->size();
Yingdi Yu5e974202014-01-29 16:59:06 -0800102
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800103 std::string expand;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700104
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700105 if (!expandStr.empty())
Yingdi Yu5e974202014-01-29 16:59:06 -0800106 expand = expandStr;
107 else
108 expand = m_expand;
109
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700110 size_t offset = 0;
111 while (offset < expand.size())
Yingdi Yu5e974202014-01-29 16:59:06 -0800112 {
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800113 std::string item = getItemFromExpand(expand, offset);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700114 if (item[0] == '<')
Yingdi Yu5e974202014-01-29 16:59:06 -0800115 {
116 result.append(item.substr(1, item.size() - 2));
117 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700118 if (item[0] == '\\')
Yingdi Yu5e974202014-01-29 16:59:06 -0800119 {
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700120 size_t index = boost::lexical_cast<size_t>(item.substr(1, item.size() - 1));
Yingdi Yu5e974202014-01-29 16:59:06 -0800121
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700122 if (0 == index) {
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800123 std::vector<name::Component>::iterator it = m_matchResult.begin();
124 std::vector<name::Component>::iterator end = m_matchResult.end();
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700125 for (; it != end; it++)
126 result.append(*it);
Yingdi Yu5e974202014-01-29 16:59:06 -0800127 }
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700128 else if (index <= backrefNo)
Yingdi Yu5e974202014-01-29 16:59:06 -0800129 {
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -0700130 std::vector<name::Component>::const_iterator it =
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700131 backrefManager->getBackref(index - 1)->getMatchResult().begin();
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -0700132 std::vector<name::Component>::const_iterator end =
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700133 backrefManager->getBackref(index - 1)->getMatchResult().end();
134 for (; it != end; it++)
135 result.append(*it);
Yingdi Yu5e974202014-01-29 16:59:06 -0800136 }
137 else
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700138 throw RegexMatcher::Error("Exceed the range of back reference");
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700139 }
Yingdi Yu5e974202014-01-29 16:59:06 -0800140 }
141 return result;
142}
143
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800144std::string
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700145RegexTopMatcher::getItemFromExpand(const std::string& expand, size_t& offset)
Yingdi Yu5e974202014-01-29 16:59:06 -0800146{
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700147 size_t begin = offset;
Yingdi Yu5e974202014-01-29 16:59:06 -0800148
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700149 if (expand[offset] == '\\')
Yingdi Yu5e974202014-01-29 16:59:06 -0800150 {
151 offset++;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700152 if (offset >= expand.size())
Yingdi Yu5e974202014-01-29 16:59:06 -0800153 throw RegexMatcher::Error("wrong format of expand string!");
154
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700155 while (expand[offset] <= '9' and expand[offset] >= '0') {
Yingdi Yu5e974202014-01-29 16:59:06 -0800156 offset++;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700157 if (offset > expand.size())
Yingdi Yu5e974202014-01-29 16:59:06 -0800158 throw RegexMatcher::Error("wrong format of expand string!");
159 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700160 if (offset > begin + 1)
Yingdi Yu5e974202014-01-29 16:59:06 -0800161 return expand.substr(begin, offset - begin);
162 else
163 throw RegexMatcher::Error("wrong format of expand string!");
164 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700165 else if (expand[offset] == '<')
Yingdi Yu5e974202014-01-29 16:59:06 -0800166 {
167 offset++;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700168 if (offset >= expand.size())
Yingdi Yu5e974202014-01-29 16:59:06 -0800169 throw RegexMatcher::Error("wrong format of expand string!");
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700170
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700171 size_t left = 1;
172 size_t right = 0;
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700173 while (right < left)
Yingdi Yu5e974202014-01-29 16:59:06 -0800174 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700175 if (expand[offset] == '<')
Yingdi Yu5e974202014-01-29 16:59:06 -0800176 left++;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700177 if (expand[offset] == '>')
178 right++;
Yingdi Yu5e974202014-01-29 16:59:06 -0800179 offset++;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700180 if (offset >= expand.size())
Yingdi Yu5e974202014-01-29 16:59:06 -0800181 throw RegexMatcher::Error("wrong format of expand string!");
182 }
183 return expand.substr(begin, offset - begin);
184 }
185 else
186 throw RegexMatcher::Error("wrong format of expand string!");
187}
188
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800189shared_ptr<RegexTopMatcher>
Yingdi Yu5e974202014-01-29 16:59:06 -0800190RegexTopMatcher::fromName(const Name& name, bool hasAnchor)
191{
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800192 std::string regexStr("^");
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700193
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700194 for (Name::const_iterator it = name.begin(); it != name.end(); it++)
Yingdi Yu5e974202014-01-29 16:59:06 -0800195 {
196 regexStr.append("<");
197 regexStr.append(convertSpecialChar(it->toEscapedString()));
198 regexStr.append(">");
199 }
200
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700201 if (hasAnchor)
Yingdi Yu5e974202014-01-29 16:59:06 -0800202 regexStr.append("$");
203
Alexander Afanasyevf73f0632014-05-12 18:02:37 -0700204 // On OSX 10.9, boost, and C++03 the following doesn't work without ndn::
205 // because the argument-dependent lookup prefers STL to boost
206 return ndn::make_shared<RegexTopMatcher>(regexStr);
Yingdi Yu5e974202014-01-29 16:59:06 -0800207}
208
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800209std::string
210RegexTopMatcher::convertSpecialChar(const std::string& str)
Yingdi Yu5e974202014-01-29 16:59:06 -0800211{
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800212 std::string newStr;
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700213 for (size_t i = 0; i < str.size(); i++)
Yingdi Yu5e974202014-01-29 16:59:06 -0800214 {
215 char c = str[i];
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700216 switch (c)
Yingdi Yu5e974202014-01-29 16:59:06 -0800217 {
218 case '.':
219 case '[':
220 case '{':
221 case '}':
222 case '(':
223 case ')':
224 case '\\':
225 case '*':
226 case '+':
227 case '?':
228 case '|':
229 case '^':
230 case '$':
231 newStr.push_back('\\');
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700232 // Fallthrough
Yingdi Yu5e974202014-01-29 16:59:06 -0800233 default:
234 newStr.push_back(c);
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700235 break;
Yingdi Yu5e974202014-01-29 16:59:06 -0800236 }
237 }
238
239 return newStr;
240}
241
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700242} // namespace ndn