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 | |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 24 | #ifndef NDN_UTIL_REGEX_REGEX_COMPONENT_MATCHER_HPP |
| 25 | #define NDN_UTIL_REGEX_REGEX_COMPONENT_MATCHER_HPP |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 26 | |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 27 | #include "regex-matcher.hpp" |
| 28 | #include "regex-pseudo-matcher.hpp" |
| 29 | |
Davide Pesavento | 45ab9a9 | 2017-11-05 19:34:31 -0500 | [diff] [blame^] | 30 | #include <boost/regex.hpp> |
| 31 | |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 32 | namespace ndn { |
| 33 | |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 34 | class RegexComponentMatcher : public RegexMatcher |
| 35 | { |
| 36 | public: |
| 37 | /** |
| 38 | * @brief Create a RegexComponent matcher from expr |
| 39 | * @param expr The standard regular expression to match a component |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 40 | * @param backrefManager The back reference manager |
| 41 | * @param isExactMatch The flag to provide exact match |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 42 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 43 | RegexComponentMatcher(const std::string& expr, |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 44 | shared_ptr<RegexBackrefManager> backrefManager, |
| 45 | bool isExactMatch = true); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 46 | |
Davide Pesavento | 45ab9a9 | 2017-11-05 19:34:31 -0500 | [diff] [blame^] | 47 | bool |
| 48 | match(const Name& name, size_t offset, size_t len = 1) override; |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 49 | |
| 50 | protected: |
Davide Pesavento | 45ab9a9 | 2017-11-05 19:34:31 -0500 | [diff] [blame^] | 51 | void |
| 52 | compile() override; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 53 | |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 54 | private: |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 55 | bool m_isExactMatch; |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 56 | boost::regex m_componentRegex; |
Davide Pesavento | 45ab9a9 | 2017-11-05 19:34:31 -0500 | [diff] [blame^] | 57 | std::vector<shared_ptr<RegexPseudoMatcher>> m_pseudoMatchers; |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 58 | }; |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 59 | |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 60 | inline |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 61 | RegexComponentMatcher::RegexComponentMatcher(const std::string& expr, |
| 62 | shared_ptr<RegexBackrefManager> backrefManager, |
| 63 | bool isExactMatch) |
Davide Pesavento | 45ab9a9 | 2017-11-05 19:34:31 -0500 | [diff] [blame^] | 64 | : RegexMatcher(expr, EXPR_COMPONENT, std::move(backrefManager)) |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 65 | , m_isExactMatch(isExactMatch) |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 66 | { |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 67 | compile(); |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 68 | } |
| 69 | |
Alexander Afanasyev | 680e77a | 2014-08-14 22:39:17 -0700 | [diff] [blame] | 70 | // Re: http://www.boost.org/users/history/version_1_56_0.html |
| 71 | // |
| 72 | // Breaking change: corrected behavior of basic_regex<>::mark_count() to match existing |
| 73 | // documentation, basic_regex<>::subexpression(n) changed to match, see |
| 74 | // https://svn.boost.org/trac/boost/ticket/9227 |
Davide Pesavento | 45ab9a9 | 2017-11-05 19:34:31 -0500 | [diff] [blame^] | 75 | // |
| 76 | static constexpr size_t BOOST_REGEXP_MARK_COUNT_CORRECTION = |
Alexander Afanasyev | 680e77a | 2014-08-14 22:39:17 -0700 | [diff] [blame] | 77 | #if BOOST_VERSION < 105600 |
Davide Pesavento | 45ab9a9 | 2017-11-05 19:34:31 -0500 | [diff] [blame^] | 78 | 1; |
Alexander Afanasyev | 680e77a | 2014-08-14 22:39:17 -0700 | [diff] [blame] | 79 | #else |
Davide Pesavento | 45ab9a9 | 2017-11-05 19:34:31 -0500 | [diff] [blame^] | 80 | 0; |
Alexander Afanasyev | 680e77a | 2014-08-14 22:39:17 -0700 | [diff] [blame] | 81 | #endif |
| 82 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 83 | inline void |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 84 | RegexComponentMatcher::compile() |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 85 | { |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 86 | m_componentRegex = boost::regex(m_expr); |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 87 | |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 88 | m_pseudoMatchers.clear(); |
| 89 | m_pseudoMatchers.push_back(make_shared<RegexPseudoMatcher>()); |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 90 | |
Davide Pesavento | 45ab9a9 | 2017-11-05 19:34:31 -0500 | [diff] [blame^] | 91 | for (size_t i = 1; i <= m_componentRegex.mark_count() - BOOST_REGEXP_MARK_COUNT_CORRECTION; i++) { |
| 92 | m_pseudoMatchers.push_back(make_shared<RegexPseudoMatcher>()); |
| 93 | m_backrefManager->pushRef(m_pseudoMatchers.back()); |
| 94 | } |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | inline bool |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 98 | RegexComponentMatcher::match(const Name& name, size_t offset, size_t len) |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 99 | { |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 100 | m_matchResult.clear(); |
| 101 | |
Davide Pesavento | 45ab9a9 | 2017-11-05 19:34:31 -0500 | [diff] [blame^] | 102 | if (m_expr.empty()) { |
| 103 | m_matchResult.push_back(name.get(offset)); |
| 104 | return true; |
| 105 | } |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 106 | |
Davide Pesavento | 45ab9a9 | 2017-11-05 19:34:31 -0500 | [diff] [blame^] | 107 | if (!m_isExactMatch) |
| 108 | BOOST_THROW_EXCEPTION(Error("Non-exact component search is not supported yet")); |
| 109 | |
| 110 | boost::smatch subResult; |
| 111 | std::string targetStr = name.get(offset).toUri(); |
| 112 | if (boost::regex_match(targetStr, subResult, m_componentRegex)) { |
| 113 | for (size_t i = 1; i <= m_componentRegex.mark_count() - BOOST_REGEXP_MARK_COUNT_CORRECTION; i++) { |
| 114 | m_pseudoMatchers[i]->resetMatchResult(); |
| 115 | m_pseudoMatchers[i]->setMatchResult(subResult[i]); |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 116 | } |
Davide Pesavento | 45ab9a9 | 2017-11-05 19:34:31 -0500 | [diff] [blame^] | 117 | m_matchResult.push_back(name.get(offset)); |
| 118 | return true; |
| 119 | } |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 120 | |
| 121 | return false; |
| 122 | } |
| 123 | |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 124 | } // namespace ndn |
| 125 | |
| 126 | #endif // NDN_UTIL_REGEX_REGEX_COMPONENT_MATCHER_HPP |