Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 2 | /** |
Qiuhan Ding | 699725d | 2015-05-24 01:41:09 -0700 | [diff] [blame^] | 3 | * Copyright (c) 2013-2015 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. |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 20 | */ |
| 21 | |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 22 | #include "regex-top-matcher.hpp" |
| 23 | |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 24 | #include "regex-backref-manager.hpp" |
| 25 | #include "regex-pattern-list-matcher.hpp" |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 26 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 27 | #include <boost/lexical_cast.hpp> |
| 28 | |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 29 | namespace ndn { |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 30 | |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 31 | RegexTopMatcher::RegexTopMatcher(const std::string& expr, const std::string& expand) |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 32 | : RegexMatcher(expr, EXPR_TOP) |
| 33 | , m_expand(expand) |
| 34 | , m_isSecondaryUsed(false) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 35 | { |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 36 | m_primaryBackrefManager = make_shared<RegexBackrefManager>(); |
| 37 | m_secondaryBackrefManager = make_shared<RegexBackrefManager>(); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 38 | compile(); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | RegexTopMatcher::~RegexTopMatcher() |
| 42 | { |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 43 | } |
| 44 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 45 | void |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 46 | RegexTopMatcher::compile() |
| 47 | { |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 48 | std::string expr = m_expr; |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 49 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 50 | if ('$' != expr[expr.size() - 1]) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 51 | expr = expr + "<.*>*"; |
| 52 | else |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 53 | expr = expr.substr(0, expr.size() - 1); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 54 | |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 55 | if ('^' != expr[0]) { |
| 56 | m_secondaryMatcher = make_shared<RegexPatternListMatcher>( |
| 57 | "<.*>*" + expr, |
Alexander Afanasyev | f73f063 | 2014-05-12 18:02:37 -0700 | [diff] [blame] | 58 | m_secondaryBackrefManager); |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 59 | } |
| 60 | else { |
| 61 | expr = expr.substr(1, expr.size() - 1); |
| 62 | } |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 63 | |
Alexander Afanasyev | f73f063 | 2014-05-12 18:02:37 -0700 | [diff] [blame] | 64 | // On OSX 10.9, boost, and C++03 the following doesn't work without ndn:: |
| 65 | // because the argument-dependent lookup prefers STL to boost |
| 66 | m_primaryMatcher = ndn::make_shared<RegexPatternListMatcher>(expr, |
| 67 | m_primaryBackrefManager); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 68 | } |
| 69 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 70 | bool |
| 71 | RegexTopMatcher::match(const Name& name) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 72 | { |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 73 | m_isSecondaryUsed = false; |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 74 | |
| 75 | m_matchResult.clear(); |
| 76 | |
Qiuhan Ding | 699725d | 2015-05-24 01:41:09 -0700 | [diff] [blame^] | 77 | if (m_primaryMatcher->match(name, 0, name.size())) { |
| 78 | m_matchResult = m_primaryMatcher->getMatchResult(); |
| 79 | return true; |
| 80 | } |
| 81 | else { |
| 82 | if (static_cast<bool>(m_secondaryMatcher) && m_secondaryMatcher->match(name, 0, name.size())) { |
| 83 | m_matchResult = m_secondaryMatcher->getMatchResult(); |
| 84 | m_isSecondaryUsed = true; |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 85 | return true; |
| 86 | } |
Qiuhan Ding | 699725d | 2015-05-24 01:41:09 -0700 | [diff] [blame^] | 87 | return false; |
| 88 | } |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 89 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 90 | |
| 91 | bool |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 92 | RegexTopMatcher::match(const Name& name, size_t, size_t) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 93 | { |
| 94 | return match(name); |
| 95 | } |
| 96 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 97 | Name |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 98 | RegexTopMatcher::expand(const std::string& expandStr) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 99 | { |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 100 | Name result; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 101 | |
Qiuhan Ding | 699725d | 2015-05-24 01:41:09 -0700 | [diff] [blame^] | 102 | auto backrefManager = |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 103 | (m_isSecondaryUsed ? m_secondaryBackrefManager : m_primaryBackrefManager); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 104 | |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 105 | size_t backrefNo = backrefManager->size(); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 106 | |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 107 | std::string expand; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 108 | |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 109 | if (!expandStr.empty()) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 110 | expand = expandStr; |
| 111 | else |
| 112 | expand = m_expand; |
| 113 | |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 114 | size_t offset = 0; |
Qiuhan Ding | 699725d | 2015-05-24 01:41:09 -0700 | [diff] [blame^] | 115 | while (offset < expand.size()) { |
| 116 | std::string item = getItemFromExpand(expand, offset); |
| 117 | if (item[0] == '<') { |
| 118 | result.append(item.substr(1, item.size() - 2)); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 119 | } |
Qiuhan Ding | 699725d | 2015-05-24 01:41:09 -0700 | [diff] [blame^] | 120 | if (item[0] == '\\') { |
| 121 | size_t index = boost::lexical_cast<size_t>(item.substr(1, item.size() - 1)); |
| 122 | |
| 123 | if (0 == index) { |
| 124 | std::vector<name::Component>::iterator it = m_matchResult.begin(); |
| 125 | std::vector<name::Component>::iterator end = m_matchResult.end(); |
| 126 | for (; it != end; it++) |
| 127 | result.append(*it); |
| 128 | } |
| 129 | else if (index <= backrefNo) { |
| 130 | std::vector<name::Component>::const_iterator it = |
| 131 | backrefManager->getBackref(index - 1)->getMatchResult().begin(); |
| 132 | std::vector<name::Component>::const_iterator end = |
| 133 | backrefManager->getBackref(index - 1)->getMatchResult().end(); |
| 134 | for (; it != end; it++) |
| 135 | result.append(*it); |
| 136 | } |
| 137 | else |
| 138 | throw Error("Exceed the range of back reference"); |
| 139 | } |
| 140 | } |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 141 | return result; |
| 142 | } |
| 143 | |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 144 | std::string |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 145 | RegexTopMatcher::getItemFromExpand(const std::string& expand, size_t& offset) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 146 | { |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 147 | size_t begin = offset; |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 148 | |
Qiuhan Ding | 699725d | 2015-05-24 01:41:09 -0700 | [diff] [blame^] | 149 | if (expand[offset] == '\\') { |
| 150 | offset++; |
| 151 | if (offset >= expand.size()) |
| 152 | throw Error("wrong format of expand string!"); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 153 | |
Qiuhan Ding | 699725d | 2015-05-24 01:41:09 -0700 | [diff] [blame^] | 154 | while (expand[offset] <= '9' and expand[offset] >= '0') { |
| 155 | offset++; |
| 156 | if (offset > expand.size()) |
| 157 | throw Error("wrong format of expand string!"); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 158 | } |
Qiuhan Ding | 699725d | 2015-05-24 01:41:09 -0700 | [diff] [blame^] | 159 | if (offset > begin + 1) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 160 | return expand.substr(begin, offset - begin); |
Qiuhan Ding | 699725d | 2015-05-24 01:41:09 -0700 | [diff] [blame^] | 161 | else |
| 162 | throw Error("wrong format of expand string!"); |
| 163 | } |
| 164 | else if (expand[offset] == '<') { |
| 165 | offset++; |
| 166 | if (offset >= expand.size()) |
| 167 | throw Error("wrong format of expand string!"); |
| 168 | |
| 169 | size_t left = 1; |
| 170 | size_t right = 0; |
| 171 | while (right < left) { |
| 172 | if (expand[offset] == '<') |
| 173 | left++; |
| 174 | if (expand[offset] == '>') |
| 175 | right++; |
| 176 | offset++; |
| 177 | if (offset >= expand.size()) |
| 178 | throw Error("wrong format of expand string!"); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 179 | } |
Qiuhan Ding | 699725d | 2015-05-24 01:41:09 -0700 | [diff] [blame^] | 180 | return expand.substr(begin, offset - begin); |
| 181 | } |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 182 | else |
Qiuhan Ding | 699725d | 2015-05-24 01:41:09 -0700 | [diff] [blame^] | 183 | throw Error("wrong format of expand string!"); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 184 | } |
| 185 | |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 186 | shared_ptr<RegexTopMatcher> |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 187 | RegexTopMatcher::fromName(const Name& name, bool hasAnchor) |
| 188 | { |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 189 | std::string regexStr("^"); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 190 | |
Qiuhan Ding | 699725d | 2015-05-24 01:41:09 -0700 | [diff] [blame^] | 191 | for (const auto& comp : name) { |
| 192 | regexStr.append("<"); |
| 193 | regexStr.append(convertSpecialChar(comp.toUri())); |
| 194 | regexStr.append(">"); |
| 195 | } |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 196 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 197 | if (hasAnchor) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 198 | regexStr.append("$"); |
| 199 | |
Alexander Afanasyev | f73f063 | 2014-05-12 18:02:37 -0700 | [diff] [blame] | 200 | // On OSX 10.9, boost, and C++03 the following doesn't work without ndn:: |
| 201 | // because the argument-dependent lookup prefers STL to boost |
| 202 | return ndn::make_shared<RegexTopMatcher>(regexStr); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 203 | } |
| 204 | |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 205 | std::string |
| 206 | RegexTopMatcher::convertSpecialChar(const std::string& str) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 207 | { |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 208 | std::string newStr; |
Qiuhan Ding | 699725d | 2015-05-24 01:41:09 -0700 | [diff] [blame^] | 209 | for (size_t i = 0; i < str.size(); i++) { |
| 210 | char c = str[i]; |
| 211 | switch (c) |
| 212 | { |
| 213 | case '.': |
| 214 | case '[': |
| 215 | case '{': |
| 216 | case '}': |
| 217 | case '(': |
| 218 | case ')': |
| 219 | case '\\': |
| 220 | case '*': |
| 221 | case '+': |
| 222 | case '?': |
| 223 | case '|': |
| 224 | case '^': |
| 225 | case '$': |
| 226 | newStr.push_back('\\'); |
| 227 | // Fallthrough |
| 228 | default: |
| 229 | newStr.push_back(c); |
| 230 | break; |
| 231 | } |
| 232 | } |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 233 | |
| 234 | return newStr; |
| 235 | } |
| 236 | |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 237 | } // namespace ndn |