Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 45ab9a9 | 2017-11-05 19:34:31 -0500 | [diff] [blame^] | 2 | /* |
| 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 20 | * |
| 21 | * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/> |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 22 | */ |
| 23 | |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 24 | #include "regex-top-matcher.hpp" |
| 25 | |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 26 | #include "regex-backref-manager.hpp" |
| 27 | #include "regex-pattern-list-matcher.hpp" |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 28 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 29 | #include <boost/lexical_cast.hpp> |
| 30 | |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 31 | namespace ndn { |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 32 | |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 33 | RegexTopMatcher::RegexTopMatcher(const std::string& expr, const std::string& expand) |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 34 | : RegexMatcher(expr, EXPR_TOP) |
| 35 | , m_expand(expand) |
| 36 | , m_isSecondaryUsed(false) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 37 | { |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 38 | m_primaryBackrefManager = make_shared<RegexBackrefManager>(); |
| 39 | m_secondaryBackrefManager = make_shared<RegexBackrefManager>(); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 40 | compile(); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 41 | } |
| 42 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 43 | void |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 44 | RegexTopMatcher::compile() |
| 45 | { |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 46 | std::string expr = m_expr; |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 47 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 48 | if ('$' != expr[expr.size() - 1]) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 49 | expr = expr + "<.*>*"; |
| 50 | else |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 51 | expr = expr.substr(0, expr.size() - 1); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 52 | |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 53 | if ('^' != expr[0]) { |
Davide Pesavento | 45ab9a9 | 2017-11-05 19:34:31 -0500 | [diff] [blame^] | 54 | m_secondaryMatcher = make_shared<RegexPatternListMatcher>("<.*>*" + expr, |
| 55 | m_secondaryBackrefManager); |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 56 | } |
| 57 | else { |
| 58 | expr = expr.substr(1, expr.size() - 1); |
| 59 | } |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 60 | |
Davide Pesavento | 45ab9a9 | 2017-11-05 19:34:31 -0500 | [diff] [blame^] | 61 | m_primaryMatcher = make_shared<RegexPatternListMatcher>(expr, m_primaryBackrefManager); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 62 | } |
| 63 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 64 | bool |
| 65 | RegexTopMatcher::match(const Name& name) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 66 | { |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 67 | m_isSecondaryUsed = false; |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 68 | |
| 69 | m_matchResult.clear(); |
| 70 | |
Davide Pesavento | 45ab9a9 | 2017-11-05 19:34:31 -0500 | [diff] [blame^] | 71 | if (m_primaryMatcher->match(name, 0, name.size())) { |
| 72 | m_matchResult = m_primaryMatcher->getMatchResult(); |
| 73 | return true; |
| 74 | } |
| 75 | else { |
| 76 | if (m_secondaryMatcher != nullptr && m_secondaryMatcher->match(name, 0, name.size())) { |
| 77 | m_matchResult = m_secondaryMatcher->getMatchResult(); |
| 78 | m_isSecondaryUsed = true; |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 79 | return true; |
| 80 | } |
Davide Pesavento | 45ab9a9 | 2017-11-05 19:34:31 -0500 | [diff] [blame^] | 81 | return false; |
| 82 | } |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 83 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 84 | |
| 85 | bool |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 86 | RegexTopMatcher::match(const Name& name, size_t, size_t) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 87 | { |
| 88 | return match(name); |
| 89 | } |
| 90 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 91 | Name |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 92 | RegexTopMatcher::expand(const std::string& expandStr) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 93 | { |
Davide Pesavento | 45ab9a9 | 2017-11-05 19:34:31 -0500 | [diff] [blame^] | 94 | auto backrefManager = m_isSecondaryUsed ? m_secondaryBackrefManager : m_primaryBackrefManager; |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 95 | size_t backrefNo = backrefManager->size(); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 96 | |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 97 | std::string expand; |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 98 | if (!expandStr.empty()) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 99 | expand = expandStr; |
| 100 | else |
| 101 | expand = m_expand; |
| 102 | |
Davide Pesavento | 45ab9a9 | 2017-11-05 19:34:31 -0500 | [diff] [blame^] | 103 | Name result; |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 104 | size_t offset = 0; |
Davide Pesavento | 45ab9a9 | 2017-11-05 19:34:31 -0500 | [diff] [blame^] | 105 | while (offset < expand.size()) { |
| 106 | std::string item = getItemFromExpand(expand, offset); |
| 107 | if (item[0] == '<') { |
| 108 | result.append(item.substr(1, item.size() - 2)); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 109 | } |
Davide Pesavento | 45ab9a9 | 2017-11-05 19:34:31 -0500 | [diff] [blame^] | 110 | if (item[0] == '\\') { |
| 111 | size_t index = boost::lexical_cast<size_t>(item.substr(1, item.size() - 1)); |
| 112 | if (index == 0) { |
| 113 | for (const auto& i : m_matchResult) |
| 114 | result.append(i); |
| 115 | } |
| 116 | else if (index <= backrefNo) { |
| 117 | for (const auto& i : backrefManager->getBackref(index - 1)->getMatchResult()) |
| 118 | result.append(i); |
| 119 | } |
| 120 | else |
| 121 | BOOST_THROW_EXCEPTION(Error("Exceed the range of back reference")); |
| 122 | } |
| 123 | } |
| 124 | |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 125 | return result; |
| 126 | } |
| 127 | |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 128 | std::string |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 129 | RegexTopMatcher::getItemFromExpand(const std::string& expand, size_t& offset) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 130 | { |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 131 | size_t begin = offset; |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 132 | |
Davide Pesavento | 45ab9a9 | 2017-11-05 19:34:31 -0500 | [diff] [blame^] | 133 | if (expand[offset] == '\\') { |
| 134 | offset++; |
| 135 | if (offset >= expand.size()) |
| 136 | BOOST_THROW_EXCEPTION(Error("Wrong format of expand string")); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 137 | |
Davide Pesavento | 45ab9a9 | 2017-11-05 19:34:31 -0500 | [diff] [blame^] | 138 | while (expand[offset] <= '9' and expand[offset] >= '0') { |
| 139 | offset++; |
| 140 | if (offset > expand.size()) |
| 141 | BOOST_THROW_EXCEPTION(Error("Wrong format of expand string")); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 142 | } |
Davide Pesavento | 45ab9a9 | 2017-11-05 19:34:31 -0500 | [diff] [blame^] | 143 | if (offset > begin + 1) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 144 | return expand.substr(begin, offset - begin); |
Davide Pesavento | 45ab9a9 | 2017-11-05 19:34:31 -0500 | [diff] [blame^] | 145 | else |
| 146 | BOOST_THROW_EXCEPTION(Error("Wrong format of expand string")); |
| 147 | } |
| 148 | else if (expand[offset] == '<') { |
| 149 | offset++; |
| 150 | if (offset >= expand.size()) |
| 151 | BOOST_THROW_EXCEPTION(Error("Wrong format of expand string")); |
| 152 | |
| 153 | size_t left = 1; |
| 154 | size_t right = 0; |
| 155 | while (right < left) { |
| 156 | if (expand[offset] == '<') |
| 157 | left++; |
| 158 | if (expand[offset] == '>') |
| 159 | right++; |
| 160 | offset++; |
| 161 | if (offset >= expand.size()) |
| 162 | BOOST_THROW_EXCEPTION(Error("Wrong format of expand string")); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 163 | } |
Davide Pesavento | 45ab9a9 | 2017-11-05 19:34:31 -0500 | [diff] [blame^] | 164 | return expand.substr(begin, offset - begin); |
| 165 | } |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 166 | else |
Davide Pesavento | 45ab9a9 | 2017-11-05 19:34:31 -0500 | [diff] [blame^] | 167 | BOOST_THROW_EXCEPTION(Error("Wrong format of expand string")); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 168 | } |
| 169 | |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 170 | shared_ptr<RegexTopMatcher> |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 171 | RegexTopMatcher::fromName(const Name& name, bool hasAnchor) |
| 172 | { |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 173 | std::string regexStr("^"); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 174 | |
Davide Pesavento | 45ab9a9 | 2017-11-05 19:34:31 -0500 | [diff] [blame^] | 175 | for (auto it = name.begin(); it != name.end(); it++) { |
| 176 | regexStr.append("<"); |
| 177 | regexStr.append(convertSpecialChar(it->toUri())); |
| 178 | regexStr.append(">"); |
| 179 | } |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 180 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 181 | if (hasAnchor) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 182 | regexStr.append("$"); |
| 183 | |
Davide Pesavento | 45ab9a9 | 2017-11-05 19:34:31 -0500 | [diff] [blame^] | 184 | return make_shared<RegexTopMatcher>(regexStr); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 185 | } |
| 186 | |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 187 | std::string |
| 188 | RegexTopMatcher::convertSpecialChar(const std::string& str) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 189 | { |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 190 | std::string newStr; |
Davide Pesavento | 45ab9a9 | 2017-11-05 19:34:31 -0500 | [diff] [blame^] | 191 | |
| 192 | for (size_t i = 0; i < str.size(); i++) { |
| 193 | char c = str[i]; |
| 194 | switch (c) { |
| 195 | case '.': |
| 196 | case '[': |
| 197 | case '{': |
| 198 | case '}': |
| 199 | case '(': |
| 200 | case ')': |
| 201 | case '\\': |
| 202 | case '*': |
| 203 | case '+': |
| 204 | case '?': |
| 205 | case '|': |
| 206 | case '^': |
| 207 | case '$': |
| 208 | newStr.push_back('\\'); |
| 209 | NDN_CXX_FALLTHROUGH; |
| 210 | default: |
| 211 | newStr.push_back(c); |
| 212 | break; |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 213 | } |
Davide Pesavento | 45ab9a9 | 2017-11-05 19:34:31 -0500 | [diff] [blame^] | 214 | } |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 215 | |
| 216 | return newStr; |
| 217 | } |
| 218 | |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 219 | } // namespace ndn |