blob: dd2ac039710bc781bb487f1f46972815eac1dc3a [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,
51 cref(m_secondaryBackrefManager));
52 }
53 else {
54 expr = expr.substr(1, expr.size() - 1);
55 }
Yingdi Yu5e974202014-01-29 16:59:06 -080056
Alexander Afanasyevb67090a2014-04-29 22:31:01 -070057 m_primaryMatcher = make_shared<RegexPatternListMatcher>(func_lib::cref(expr),
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -070058 func_lib::cref(m_primaryBackrefManager));
Yingdi Yu5e974202014-01-29 16:59:06 -080059}
60
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070061bool
62RegexTopMatcher::match(const Name& name)
Yingdi Yu5e974202014-01-29 16:59:06 -080063{
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -070064 m_isSecondaryUsed = false;
Yingdi Yu5e974202014-01-29 16:59:06 -080065
66 m_matchResult.clear();
67
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070068 if (m_primaryMatcher->match(name, 0, name.size()))
Yingdi Yu5e974202014-01-29 16:59:06 -080069 {
70 m_matchResult = m_primaryMatcher->getMatchResult();
71 return true;
72 }
73 else
74 {
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -070075 if (static_cast<bool>(m_secondaryMatcher) && m_secondaryMatcher->match(name, 0, name.size()))
Yingdi Yu5e974202014-01-29 16:59:06 -080076 {
77 m_matchResult = m_secondaryMatcher->getMatchResult();
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -070078 m_isSecondaryUsed = true;
Yingdi Yu5e974202014-01-29 16:59:06 -080079 return true;
80 }
81 return false;
82 }
83}
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070084
85bool
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -070086RegexTopMatcher::match(const Name& name, size_t, size_t)
Yingdi Yu5e974202014-01-29 16:59:06 -080087{
88 return match(name);
89}
90
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070091Name
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -070092RegexTopMatcher::expand(const std::string& expandStr)
Yingdi Yu5e974202014-01-29 16:59:06 -080093{
Yingdi Yu5e974202014-01-29 16:59:06 -080094 Name result;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070095
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -070096 shared_ptr<RegexBackrefManager> backrefManager =
97 (m_isSecondaryUsed ? m_secondaryBackrefManager : m_primaryBackrefManager);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070098
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -070099 size_t backrefNo = backrefManager->size();
Yingdi Yu5e974202014-01-29 16:59:06 -0800100
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800101 std::string expand;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700102
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700103 if (!expandStr.empty())
Yingdi Yu5e974202014-01-29 16:59:06 -0800104 expand = expandStr;
105 else
106 expand = m_expand;
107
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700108 size_t offset = 0;
109 while (offset < expand.size())
Yingdi Yu5e974202014-01-29 16:59:06 -0800110 {
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800111 std::string item = getItemFromExpand(expand, offset);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700112 if (item[0] == '<')
Yingdi Yu5e974202014-01-29 16:59:06 -0800113 {
114 result.append(item.substr(1, item.size() - 2));
115 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700116 if (item[0] == '\\')
Yingdi Yu5e974202014-01-29 16:59:06 -0800117 {
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700118 size_t index = boost::lexical_cast<size_t>(item.substr(1, item.size() - 1));
Yingdi Yu5e974202014-01-29 16:59:06 -0800119
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700120 if (0 == index) {
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800121 std::vector<name::Component>::iterator it = m_matchResult.begin();
122 std::vector<name::Component>::iterator end = m_matchResult.end();
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700123 for (; it != end; it++)
124 result.append(*it);
Yingdi Yu5e974202014-01-29 16:59:06 -0800125 }
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700126 else if (index <= backrefNo)
Yingdi Yu5e974202014-01-29 16:59:06 -0800127 {
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -0700128 std::vector<name::Component>::const_iterator it =
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700129 backrefManager->getBackref(index - 1)->getMatchResult().begin();
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -0700130 std::vector<name::Component>::const_iterator end =
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700131 backrefManager->getBackref(index - 1)->getMatchResult().end();
132 for (; it != end; it++)
133 result.append(*it);
Yingdi Yu5e974202014-01-29 16:59:06 -0800134 }
135 else
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700136 throw RegexMatcher::Error("Exceed the range of back reference");
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700137 }
Yingdi Yu5e974202014-01-29 16:59:06 -0800138 }
139 return result;
140}
141
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800142std::string
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700143RegexTopMatcher::getItemFromExpand(const std::string& expand, size_t& offset)
Yingdi Yu5e974202014-01-29 16:59:06 -0800144{
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700145 size_t begin = offset;
Yingdi Yu5e974202014-01-29 16:59:06 -0800146
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700147 if (expand[offset] == '\\')
Yingdi Yu5e974202014-01-29 16:59:06 -0800148 {
149 offset++;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700150 if (offset >= expand.size())
Yingdi Yu5e974202014-01-29 16:59:06 -0800151 throw RegexMatcher::Error("wrong format of expand string!");
152
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700153 while (expand[offset] <= '9' and expand[offset] >= '0') {
Yingdi Yu5e974202014-01-29 16:59:06 -0800154 offset++;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700155 if (offset > expand.size())
Yingdi Yu5e974202014-01-29 16:59:06 -0800156 throw RegexMatcher::Error("wrong format of expand string!");
157 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700158 if (offset > begin + 1)
Yingdi Yu5e974202014-01-29 16:59:06 -0800159 return expand.substr(begin, offset - begin);
160 else
161 throw RegexMatcher::Error("wrong format of expand string!");
162 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700163 else if (expand[offset] == '<')
Yingdi Yu5e974202014-01-29 16:59:06 -0800164 {
165 offset++;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700166 if (offset >= expand.size())
Yingdi Yu5e974202014-01-29 16:59:06 -0800167 throw RegexMatcher::Error("wrong format of expand string!");
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700168
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700169 size_t left = 1;
170 size_t right = 0;
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700171 while (right < left)
Yingdi Yu5e974202014-01-29 16:59:06 -0800172 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700173 if (expand[offset] == '<')
Yingdi Yu5e974202014-01-29 16:59:06 -0800174 left++;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700175 if (expand[offset] == '>')
176 right++;
Yingdi Yu5e974202014-01-29 16:59:06 -0800177 offset++;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700178 if (offset >= expand.size())
Yingdi Yu5e974202014-01-29 16:59:06 -0800179 throw RegexMatcher::Error("wrong format of expand string!");
180 }
181 return expand.substr(begin, offset - begin);
182 }
183 else
184 throw RegexMatcher::Error("wrong format of expand string!");
185}
186
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800187shared_ptr<RegexTopMatcher>
Yingdi Yu5e974202014-01-29 16:59:06 -0800188RegexTopMatcher::fromName(const Name& name, bool hasAnchor)
189{
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800190 std::string regexStr("^");
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700191
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700192 for (Name::const_iterator it = name.begin(); it != name.end(); it++)
Yingdi Yu5e974202014-01-29 16:59:06 -0800193 {
194 regexStr.append("<");
195 regexStr.append(convertSpecialChar(it->toEscapedString()));
196 regexStr.append(">");
197 }
198
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700199 if (hasAnchor)
Yingdi Yu5e974202014-01-29 16:59:06 -0800200 regexStr.append("$");
201
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700202 // OSX 10.9 has problems with just cref
203 return make_shared<RegexTopMatcher>(func_lib::cref(regexStr));
Yingdi Yu5e974202014-01-29 16:59:06 -0800204}
205
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800206std::string
207RegexTopMatcher::convertSpecialChar(const std::string& str)
Yingdi Yu5e974202014-01-29 16:59:06 -0800208{
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800209 std::string newStr;
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700210 for (size_t i = 0; i < str.size(); i++)
Yingdi Yu5e974202014-01-29 16:59:06 -0800211 {
212 char c = str[i];
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700213 switch (c)
Yingdi Yu5e974202014-01-29 16:59:06 -0800214 {
215 case '.':
216 case '[':
217 case '{':
218 case '}':
219 case '(':
220 case ')':
221 case '\\':
222 case '*':
223 case '+':
224 case '?':
225 case '|':
226 case '^':
227 case '$':
228 newStr.push_back('\\');
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700229 // Fallthrough
Yingdi Yu5e974202014-01-29 16:59:06 -0800230 default:
231 newStr.push_back(c);
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700232 break;
Yingdi Yu5e974202014-01-29 16:59:06 -0800233 }
234 }
235
236 return newStr;
237}
238
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700239} // namespace ndn