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 | |
Davide Pesavento | 45ab9a9 | 2017-11-05 19:34:31 -0500 | [diff] [blame^] | 24 | #ifndef NDN_UTIL_REGEX_REGEX_COMPONENT_SET_MATCHER_HPP |
| 25 | #define NDN_UTIL_REGEX_REGEX_COMPONENT_SET_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-component-matcher.hpp" |
Davide Pesavento | 45ab9a9 | 2017-11-05 19:34:31 -0500 | [diff] [blame^] | 28 | #include "regex-matcher.hpp" |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 29 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 30 | #include <set> |
| 31 | |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 32 | namespace ndn { |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 33 | |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 34 | class RegexComponentSetMatcher : public RegexMatcher |
| 35 | { |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 36 | public: |
| 37 | /** |
| 38 | * @brief Create a RegexComponentSetMatcher 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 Shared pointer to back-reference manager |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 41 | */ |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 42 | RegexComponentSetMatcher(const std::string& expr, shared_ptr<RegexBackrefManager> backrefManager); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 43 | |
Davide Pesavento | 45ab9a9 | 2017-11-05 19:34:31 -0500 | [diff] [blame^] | 44 | bool |
| 45 | match(const Name& name, size_t offset, size_t len = 1) override; |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 46 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 47 | protected: |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 48 | /** |
| 49 | * @brief Compile the regular expression to generate the more matchers when necessary |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 50 | */ |
Davide Pesavento | 45ab9a9 | 2017-11-05 19:34:31 -0500 | [diff] [blame^] | 51 | void |
| 52 | compile() override; |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 53 | |
| 54 | private: |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 55 | void |
| 56 | compileSingleComponent(); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 57 | |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 58 | void |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 59 | compileMultipleComponents(size_t start, size_t lastIndex); |
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 | size_t |
| 62 | extractComponent(size_t index) const; |
| 63 | |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 64 | private: |
Davide Pesavento | 45ab9a9 | 2017-11-05 19:34:31 -0500 | [diff] [blame^] | 65 | std::set<shared_ptr<RegexComponentMatcher>> m_components; |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 66 | bool m_isInclusion; |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 67 | }; |
| 68 | |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 69 | inline |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 70 | RegexComponentSetMatcher::RegexComponentSetMatcher(const std::string& expr, |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 71 | shared_ptr<RegexBackrefManager> backrefManager) |
Davide Pesavento | 45ab9a9 | 2017-11-05 19:34:31 -0500 | [diff] [blame^] | 72 | : RegexMatcher(expr, EXPR_COMPONENT_SET, std::move(backrefManager)) |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 73 | , m_isInclusion(true) |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 74 | { |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 75 | compile(); |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 76 | } |
| 77 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 78 | inline void |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 79 | RegexComponentSetMatcher::compile() |
| 80 | { |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 81 | if (m_expr.size() < 2) |
Davide Pesavento | 45ab9a9 | 2017-11-05 19:34:31 -0500 | [diff] [blame^] | 82 | BOOST_THROW_EXCEPTION(Error("Regexp compile error (cannot parse " + m_expr + ")")); |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 83 | |
| 84 | switch (m_expr[0]) { |
Davide Pesavento | 45ab9a9 | 2017-11-05 19:34:31 -0500 | [diff] [blame^] | 85 | case '<': |
| 86 | return compileSingleComponent(); |
| 87 | case '[': { |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 88 | size_t lastIndex = m_expr.size() - 1; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 89 | if (']' != m_expr[lastIndex]) |
Davide Pesavento | 45ab9a9 | 2017-11-05 19:34:31 -0500 | [diff] [blame^] | 90 | BOOST_THROW_EXCEPTION(Error("Regexp compile error (no matching ']' in " + m_expr + ")")); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 91 | |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 92 | if ('^' == m_expr[1]) { |
| 93 | m_isInclusion = false; |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 94 | compileMultipleComponents(2, lastIndex); |
| 95 | } |
| 96 | else |
| 97 | compileMultipleComponents(1, lastIndex); |
| 98 | break; |
| 99 | } |
Davide Pesavento | 45ab9a9 | 2017-11-05 19:34:31 -0500 | [diff] [blame^] | 100 | default: |
| 101 | BOOST_THROW_EXCEPTION(Error("Regexp compile error (cannot parse " + m_expr + ")")); |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 105 | inline void |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 106 | RegexComponentSetMatcher::compileSingleComponent() |
| 107 | { |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 108 | size_t end = extractComponent(1); |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 109 | if (m_expr.size() != end) |
Davide Pesavento | 45ab9a9 | 2017-11-05 19:34:31 -0500 | [diff] [blame^] | 110 | BOOST_THROW_EXCEPTION(Error("Component expr error " + m_expr)); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 111 | |
Davide Pesavento | 45ab9a9 | 2017-11-05 19:34:31 -0500 | [diff] [blame^] | 112 | m_components.insert(make_shared<RegexComponentMatcher>(m_expr.substr(1, end - 2), |
| 113 | m_backrefManager)); |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 114 | } |
| 115 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 116 | inline void |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 117 | RegexComponentSetMatcher::compileMultipleComponents(size_t start, size_t lastIndex) |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 118 | { |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 119 | size_t index = start; |
| 120 | size_t tempIndex = start; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 121 | |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 122 | while (index < lastIndex) { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 123 | if ('<' != m_expr[index]) |
Davide Pesavento | 45ab9a9 | 2017-11-05 19:34:31 -0500 | [diff] [blame^] | 124 | BOOST_THROW_EXCEPTION(Error("Component expr error " + m_expr)); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 125 | |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 126 | tempIndex = index + 1; |
| 127 | index = extractComponent(tempIndex); |
Davide Pesavento | 45ab9a9 | 2017-11-05 19:34:31 -0500 | [diff] [blame^] | 128 | m_components.insert(make_shared<RegexComponentMatcher>(m_expr.substr(tempIndex, index - tempIndex - 1), |
| 129 | m_backrefManager)); |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 130 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 131 | |
| 132 | if (index != lastIndex) |
Davide Pesavento | 45ab9a9 | 2017-11-05 19:34:31 -0500 | [diff] [blame^] | 133 | BOOST_THROW_EXCEPTION(Error("Not sufficient expr to parse " + m_expr)); |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 134 | } |
| 135 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 136 | inline bool |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 137 | RegexComponentSetMatcher::match(const Name& name, size_t offset, size_t len) |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 138 | { |
Davide Pesavento | 45ab9a9 | 2017-11-05 19:34:31 -0500 | [diff] [blame^] | 139 | // componentset only matches one component |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 140 | if (len != 1) |
Davide Pesavento | 45ab9a9 | 2017-11-05 19:34:31 -0500 | [diff] [blame^] | 141 | return false; |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 142 | |
Davide Pesavento | 45ab9a9 | 2017-11-05 19:34:31 -0500 | [diff] [blame^] | 143 | bool isMatched = false; |
| 144 | for (const auto& comp : m_components) { |
| 145 | if (comp->match(name, offset, len)) { |
| 146 | isMatched = true; |
| 147 | break; |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 148 | } |
Davide Pesavento | 45ab9a9 | 2017-11-05 19:34:31 -0500 | [diff] [blame^] | 149 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 150 | |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 151 | m_matchResult.clear(); |
| 152 | |
Davide Pesavento | 45ab9a9 | 2017-11-05 19:34:31 -0500 | [diff] [blame^] | 153 | if (m_isInclusion ? isMatched : !isMatched) { |
| 154 | m_matchResult.push_back(name.get(offset)); |
| 155 | return true; |
| 156 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 157 | else |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 158 | return false; |
| 159 | } |
| 160 | |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 161 | inline size_t |
Davide Pesavento | 45ab9a9 | 2017-11-05 19:34:31 -0500 | [diff] [blame^] | 162 | RegexComponentSetMatcher::extractComponent(size_t index) const |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 163 | { |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 164 | size_t lcount = 1; |
| 165 | size_t rcount = 0; |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 166 | |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 167 | while (lcount > rcount) { |
| 168 | switch (m_expr[index]) { |
Davide Pesavento | 45ab9a9 | 2017-11-05 19:34:31 -0500 | [diff] [blame^] | 169 | case '<': |
| 170 | lcount++; |
| 171 | break; |
| 172 | case '>': |
| 173 | rcount++; |
| 174 | break; |
| 175 | case 0: |
| 176 | BOOST_THROW_EXCEPTION(Error("Square brackets mismatch")); |
| 177 | break; |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 178 | } |
| 179 | index++; |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | return index; |
| 183 | } |
| 184 | |
| 185 | } // namespace ndn |
| 186 | |
Davide Pesavento | 45ab9a9 | 2017-11-05 19:34:31 -0500 | [diff] [blame^] | 187 | #endif // NDN_UTIL_REGEX_REGEX_COMPONENT_SET_MATCHER_HPP |