Davide Pesavento | 19745dc | 2017-11-05 19:34:31 -0500 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
| 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 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. |
| 20 | * |
| 21 | * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/> |
| 22 | */ |
| 23 | |
| 24 | #include "regex-component-matcher.hpp" |
| 25 | #include "regex-pseudo-matcher.hpp" |
| 26 | |
| 27 | namespace ndn { |
| 28 | |
| 29 | // Re: http://www.boost.org/users/history/version_1_56_0.html |
| 30 | // |
| 31 | // Breaking change: corrected behavior of basic_regex<>::mark_count() to match existing |
| 32 | // documentation, basic_regex<>::subexpression(n) changed to match, see |
| 33 | // https://svn.boost.org/trac/boost/ticket/9227 |
| 34 | // |
| 35 | static constexpr size_t BOOST_REGEXP_MARK_COUNT_CORRECTION = |
| 36 | #if BOOST_VERSION < 105600 |
| 37 | 1; |
| 38 | #else |
| 39 | 0; |
| 40 | #endif |
| 41 | |
| 42 | RegexComponentMatcher::RegexComponentMatcher(const std::string& expr, |
| 43 | shared_ptr<RegexBackrefManager> backrefManager, |
| 44 | bool isExactMatch) |
| 45 | : RegexMatcher(expr, EXPR_COMPONENT, std::move(backrefManager)) |
| 46 | , m_isExactMatch(isExactMatch) |
| 47 | { |
| 48 | compile(); |
| 49 | } |
| 50 | |
| 51 | void |
| 52 | RegexComponentMatcher::compile() |
| 53 | { |
| 54 | m_componentRegex = boost::regex(m_expr); |
| 55 | |
| 56 | m_pseudoMatchers.clear(); |
| 57 | m_pseudoMatchers.push_back(make_shared<RegexPseudoMatcher>()); |
| 58 | |
| 59 | for (size_t i = 1; i <= m_componentRegex.mark_count() - BOOST_REGEXP_MARK_COUNT_CORRECTION; i++) { |
| 60 | m_pseudoMatchers.push_back(make_shared<RegexPseudoMatcher>()); |
| 61 | m_backrefManager->pushRef(m_pseudoMatchers.back()); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | bool |
| 66 | RegexComponentMatcher::match(const Name& name, size_t offset, size_t len) |
| 67 | { |
| 68 | m_matchResult.clear(); |
| 69 | |
| 70 | if (m_expr.empty()) { |
| 71 | m_matchResult.push_back(name.get(offset)); |
| 72 | return true; |
| 73 | } |
| 74 | |
| 75 | if (!m_isExactMatch) |
| 76 | BOOST_THROW_EXCEPTION(Error("Non-exact component search is not supported yet")); |
| 77 | |
| 78 | boost::smatch subResult; |
| 79 | std::string targetStr = name.get(offset).toUri(); |
| 80 | if (boost::regex_match(targetStr, subResult, m_componentRegex)) { |
| 81 | for (size_t i = 1; i <= m_componentRegex.mark_count() - BOOST_REGEXP_MARK_COUNT_CORRECTION; i++) { |
| 82 | m_pseudoMatchers[i]->resetMatchResult(); |
| 83 | m_pseudoMatchers[i]->setMatchResult(subResult[i]); |
| 84 | } |
| 85 | m_matchResult.push_back(name.get(offset)); |
| 86 | return true; |
| 87 | } |
| 88 | |
| 89 | return false; |
| 90 | } |
| 91 | |
| 92 | } // namespace ndn |