blob: 43cd1691f56037641f59a214c98d04f630988f0c [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 Afanasyev258ec2b2014-05-14 16:15:37 -070020#include <boost/lexical_cast.hpp>
21
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -080022namespace ndn {
Yingdi Yu5e974202014-01-29 16:59:06 -080023
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -080024RegexTopMatcher::RegexTopMatcher(const std::string& expr, const std::string& expand)
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -070025 : RegexMatcher(expr, EXPR_TOP)
26 , m_expand(expand)
27 , m_isSecondaryUsed(false)
Yingdi Yu5e974202014-01-29 16:59:06 -080028{
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -070029 m_primaryBackrefManager = make_shared<RegexBackrefManager>();
30 m_secondaryBackrefManager = make_shared<RegexBackrefManager>();
Yingdi Yu5e974202014-01-29 16:59:06 -080031 compile();
Yingdi Yu5e974202014-01-29 16:59:06 -080032}
33
34RegexTopMatcher::~RegexTopMatcher()
35{
Yingdi Yu5e974202014-01-29 16:59:06 -080036}
37
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070038void
Yingdi Yu5e974202014-01-29 16:59:06 -080039RegexTopMatcher::compile()
40{
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -080041 std::string errMsg = "Error: RegexTopMatcher.Compile(): ";
Yingdi Yu5e974202014-01-29 16:59:06 -080042
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -080043 std::string expr = m_expr;
Yingdi Yu5e974202014-01-29 16:59:06 -080044
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070045 if ('$' != expr[expr.size() - 1])
Yingdi Yu5e974202014-01-29 16:59:06 -080046 expr = expr + "<.*>*";
47 else
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -070048 expr = expr.substr(0, expr.size() - 1);
Yingdi Yu5e974202014-01-29 16:59:06 -080049
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -070050 if ('^' != expr[0]) {
51 m_secondaryMatcher = make_shared<RegexPatternListMatcher>(
52 "<.*>*" + expr,
Alexander Afanasyevf73f0632014-05-12 18:02:37 -070053 m_secondaryBackrefManager);
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -070054 }
55 else {
56 expr = expr.substr(1, expr.size() - 1);
57 }
Yingdi Yu5e974202014-01-29 16:59:06 -080058
Alexander Afanasyevf73f0632014-05-12 18:02:37 -070059 // On OSX 10.9, boost, and C++03 the following doesn't work without ndn::
60 // because the argument-dependent lookup prefers STL to boost
61 m_primaryMatcher = ndn::make_shared<RegexPatternListMatcher>(expr,
62 m_primaryBackrefManager);
Yingdi Yu5e974202014-01-29 16:59:06 -080063}
64
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070065bool
66RegexTopMatcher::match(const Name& name)
Yingdi Yu5e974202014-01-29 16:59:06 -080067{
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -070068 m_isSecondaryUsed = false;
Yingdi Yu5e974202014-01-29 16:59:06 -080069
70 m_matchResult.clear();
71
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070072 if (m_primaryMatcher->match(name, 0, name.size()))
Yingdi Yu5e974202014-01-29 16:59:06 -080073 {
74 m_matchResult = m_primaryMatcher->getMatchResult();
75 return true;
76 }
77 else
78 {
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -070079 if (static_cast<bool>(m_secondaryMatcher) && m_secondaryMatcher->match(name, 0, name.size()))
Yingdi Yu5e974202014-01-29 16:59:06 -080080 {
81 m_matchResult = m_secondaryMatcher->getMatchResult();
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -070082 m_isSecondaryUsed = true;
Yingdi Yu5e974202014-01-29 16:59:06 -080083 return true;
84 }
85 return false;
86 }
87}
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070088
89bool
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -070090RegexTopMatcher::match(const Name& name, size_t, size_t)
Yingdi Yu5e974202014-01-29 16:59:06 -080091{
92 return match(name);
93}
94
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070095Name
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -070096RegexTopMatcher::expand(const std::string& expandStr)
Yingdi Yu5e974202014-01-29 16:59:06 -080097{
Yingdi Yu5e974202014-01-29 16:59:06 -080098 Name result;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070099
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700100 shared_ptr<RegexBackrefManager> backrefManager =
101 (m_isSecondaryUsed ? m_secondaryBackrefManager : m_primaryBackrefManager);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700102
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700103 size_t backrefNo = backrefManager->size();
Yingdi Yu5e974202014-01-29 16:59:06 -0800104
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800105 std::string expand;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700106
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700107 if (!expandStr.empty())
Yingdi Yu5e974202014-01-29 16:59:06 -0800108 expand = expandStr;
109 else
110 expand = m_expand;
111
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700112 size_t offset = 0;
113 while (offset < expand.size())
Yingdi Yu5e974202014-01-29 16:59:06 -0800114 {
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800115 std::string item = getItemFromExpand(expand, offset);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700116 if (item[0] == '<')
Yingdi Yu5e974202014-01-29 16:59:06 -0800117 {
118 result.append(item.substr(1, item.size() - 2));
119 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700120 if (item[0] == '\\')
Yingdi Yu5e974202014-01-29 16:59:06 -0800121 {
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700122 size_t index = boost::lexical_cast<size_t>(item.substr(1, item.size() - 1));
Yingdi Yu5e974202014-01-29 16:59:06 -0800123
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700124 if (0 == index) {
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800125 std::vector<name::Component>::iterator it = m_matchResult.begin();
126 std::vector<name::Component>::iterator end = m_matchResult.end();
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700127 for (; it != end; it++)
128 result.append(*it);
Yingdi Yu5e974202014-01-29 16:59:06 -0800129 }
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700130 else if (index <= backrefNo)
Yingdi Yu5e974202014-01-29 16:59:06 -0800131 {
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -0700132 std::vector<name::Component>::const_iterator it =
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700133 backrefManager->getBackref(index - 1)->getMatchResult().begin();
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -0700134 std::vector<name::Component>::const_iterator end =
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700135 backrefManager->getBackref(index - 1)->getMatchResult().end();
136 for (; it != end; it++)
137 result.append(*it);
Yingdi Yu5e974202014-01-29 16:59:06 -0800138 }
139 else
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700140 throw RegexMatcher::Error("Exceed the range of back reference");
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700141 }
Yingdi Yu5e974202014-01-29 16:59:06 -0800142 }
143 return result;
144}
145
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800146std::string
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700147RegexTopMatcher::getItemFromExpand(const std::string& expand, size_t& offset)
Yingdi Yu5e974202014-01-29 16:59:06 -0800148{
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700149 size_t begin = offset;
Yingdi Yu5e974202014-01-29 16:59:06 -0800150
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700151 if (expand[offset] == '\\')
Yingdi Yu5e974202014-01-29 16:59:06 -0800152 {
153 offset++;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700154 if (offset >= expand.size())
Yingdi Yu5e974202014-01-29 16:59:06 -0800155 throw RegexMatcher::Error("wrong format of expand string!");
156
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700157 while (expand[offset] <= '9' and expand[offset] >= '0') {
Yingdi Yu5e974202014-01-29 16:59:06 -0800158 offset++;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700159 if (offset > expand.size())
Yingdi Yu5e974202014-01-29 16:59:06 -0800160 throw RegexMatcher::Error("wrong format of expand string!");
161 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700162 if (offset > begin + 1)
Yingdi Yu5e974202014-01-29 16:59:06 -0800163 return expand.substr(begin, offset - begin);
164 else
165 throw RegexMatcher::Error("wrong format of expand string!");
166 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700167 else if (expand[offset] == '<')
Yingdi Yu5e974202014-01-29 16:59:06 -0800168 {
169 offset++;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700170 if (offset >= expand.size())
Yingdi Yu5e974202014-01-29 16:59:06 -0800171 throw RegexMatcher::Error("wrong format of expand string!");
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700172
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700173 size_t left = 1;
174 size_t right = 0;
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700175 while (right < left)
Yingdi Yu5e974202014-01-29 16:59:06 -0800176 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700177 if (expand[offset] == '<')
Yingdi Yu5e974202014-01-29 16:59:06 -0800178 left++;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700179 if (expand[offset] == '>')
180 right++;
Yingdi Yu5e974202014-01-29 16:59:06 -0800181 offset++;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700182 if (offset >= expand.size())
Yingdi Yu5e974202014-01-29 16:59:06 -0800183 throw RegexMatcher::Error("wrong format of expand string!");
184 }
185 return expand.substr(begin, offset - begin);
186 }
187 else
188 throw RegexMatcher::Error("wrong format of expand string!");
189}
190
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800191shared_ptr<RegexTopMatcher>
Yingdi Yu5e974202014-01-29 16:59:06 -0800192RegexTopMatcher::fromName(const Name& name, bool hasAnchor)
193{
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800194 std::string regexStr("^");
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700195
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700196 for (Name::const_iterator it = name.begin(); it != name.end(); it++)
Yingdi Yu5e974202014-01-29 16:59:06 -0800197 {
198 regexStr.append("<");
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700199 regexStr.append(convertSpecialChar(it->toUri()));
Yingdi Yu5e974202014-01-29 16:59:06 -0800200 regexStr.append(">");
201 }
202
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700203 if (hasAnchor)
Yingdi Yu5e974202014-01-29 16:59:06 -0800204 regexStr.append("$");
205
Alexander Afanasyevf73f0632014-05-12 18:02:37 -0700206 // On OSX 10.9, boost, and C++03 the following doesn't work without ndn::
207 // because the argument-dependent lookup prefers STL to boost
208 return ndn::make_shared<RegexTopMatcher>(regexStr);
Yingdi Yu5e974202014-01-29 16:59:06 -0800209}
210
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800211std::string
212RegexTopMatcher::convertSpecialChar(const std::string& str)
Yingdi Yu5e974202014-01-29 16:59:06 -0800213{
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800214 std::string newStr;
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700215 for (size_t i = 0; i < str.size(); i++)
Yingdi Yu5e974202014-01-29 16:59:06 -0800216 {
217 char c = str[i];
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700218 switch (c)
Yingdi Yu5e974202014-01-29 16:59:06 -0800219 {
220 case '.':
221 case '[':
222 case '{':
223 case '}':
224 case '(':
225 case ')':
226 case '\\':
227 case '*':
228 case '+':
229 case '?':
230 case '|':
231 case '^':
232 case '$':
233 newStr.push_back('\\');
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700234 // Fallthrough
Yingdi Yu5e974202014-01-29 16:59:06 -0800235 default:
236 newStr.push_back(c);
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700237 break;
Yingdi Yu5e974202014-01-29 16:59:06 -0800238 }
239 }
240
241 return newStr;
242}
243
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700244} // namespace ndn