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 | /** |
Alexander Afanasyev | 73e3004 | 2015-09-17 17:09:51 -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. |
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 | |
| 27 | #include <boost/regex.hpp> |
| 28 | |
| 29 | #include "regex-matcher.hpp" |
| 30 | #include "regex-pseudo-matcher.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 | |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 47 | virtual |
| 48 | ~RegexComponentMatcher() |
| 49 | { |
Davide Pesavento | 18cf81b | 2015-09-12 23:36:43 +0200 | [diff] [blame^] | 50 | } |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 51 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 52 | virtual bool |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 53 | match(const Name& name, size_t offset, size_t len = 1); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 54 | |
| 55 | protected: |
| 56 | /** |
| 57 | * @brief Compile the regular expression to generate the more matchers when necessary |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 58 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 59 | virtual void |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 60 | compile(); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 61 | |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 62 | private: |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 63 | bool m_isExactMatch; |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 64 | boost::regex m_componentRegex; |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 65 | std::vector<shared_ptr<RegexPseudoMatcher> > m_pseudoMatchers; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 66 | |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 67 | }; |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 68 | |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 69 | |
| 70 | inline |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 71 | RegexComponentMatcher::RegexComponentMatcher(const std::string& expr, |
| 72 | shared_ptr<RegexBackrefManager> backrefManager, |
| 73 | bool isExactMatch) |
| 74 | : RegexMatcher(expr, EXPR_COMPONENT, backrefManager) |
| 75 | , m_isExactMatch(isExactMatch) |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 76 | { |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 77 | compile(); |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Alexander Afanasyev | 680e77a | 2014-08-14 22:39:17 -0700 | [diff] [blame] | 80 | // Re: http://www.boost.org/users/history/version_1_56_0.html |
| 81 | // |
| 82 | // Breaking change: corrected behavior of basic_regex<>::mark_count() to match existing |
| 83 | // documentation, basic_regex<>::subexpression(n) changed to match, see |
| 84 | // https://svn.boost.org/trac/boost/ticket/9227 |
| 85 | static const size_t BOOST_REGEXP_MARK_COUNT_CORRECTION = |
| 86 | #if BOOST_VERSION < 105600 |
| 87 | 1; |
| 88 | #else |
| 89 | 0; |
| 90 | #endif |
| 91 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 92 | inline void |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 93 | RegexComponentMatcher::compile() |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 94 | { |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 95 | m_componentRegex = boost::regex(m_expr); |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 96 | |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 97 | m_pseudoMatchers.clear(); |
| 98 | m_pseudoMatchers.push_back(make_shared<RegexPseudoMatcher>()); |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 99 | |
Alexander Afanasyev | 680e77a | 2014-08-14 22:39:17 -0700 | [diff] [blame] | 100 | for (size_t i = 1; |
| 101 | i <= m_componentRegex.mark_count() - BOOST_REGEXP_MARK_COUNT_CORRECTION; i++) |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 102 | { |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 103 | shared_ptr<RegexPseudoMatcher> pMatcher = make_shared<RegexPseudoMatcher>(); |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 104 | m_pseudoMatchers.push_back(pMatcher); |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 105 | m_backrefManager->pushRef(static_pointer_cast<RegexMatcher>(pMatcher)); |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 106 | } |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | inline bool |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 110 | RegexComponentMatcher::match(const Name& name, size_t offset, size_t len) |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 111 | { |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 112 | m_matchResult.clear(); |
| 113 | |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 114 | if (m_expr.empty()) |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 115 | { |
| 116 | m_matchResult.push_back(name.get(offset)); |
| 117 | return true; |
| 118 | } |
| 119 | |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 120 | if (m_isExactMatch) |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 121 | { |
| 122 | boost::smatch subResult; |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 123 | std::string targetStr = name.get(offset).toUri(); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 124 | if (boost::regex_match(targetStr, subResult, m_componentRegex)) |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 125 | { |
Alexander Afanasyev | 680e77a | 2014-08-14 22:39:17 -0700 | [diff] [blame] | 126 | for (size_t i = 1; |
| 127 | i <= m_componentRegex.mark_count() - BOOST_REGEXP_MARK_COUNT_CORRECTION; i++) |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 128 | { |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 129 | m_pseudoMatchers[i]->resetMatchResult(); |
| 130 | m_pseudoMatchers[i]->setMatchResult(subResult[i]); |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 131 | } |
| 132 | m_matchResult.push_back(name.get(offset)); |
| 133 | return true; |
| 134 | } |
| 135 | } |
| 136 | else |
| 137 | { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 138 | BOOST_THROW_EXCEPTION(RegexMatcher::Error("Non-exact component search is not supported " |
| 139 | "yet")); |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | return false; |
| 143 | } |
| 144 | |
| 145 | |
| 146 | } // namespace ndn |
| 147 | |
| 148 | #endif // NDN_UTIL_REGEX_REGEX_COMPONENT_MATCHER_HPP |