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 | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2014 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_REPEAT_MATCHER_HPP |
| 25 | #define NDN_UTIL_REGEX_REGEX_REPEAT_MATCHER_HPP |
| 26 | |
| 27 | #include "../../common.hpp" |
| 28 | |
| 29 | #include <boost/regex.hpp> |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 30 | |
| 31 | #include "regex-matcher.hpp" |
| 32 | |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 33 | namespace ndn { |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 34 | |
| 35 | class RegexRepeatMatcher : public RegexMatcher |
| 36 | { |
| 37 | public: |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 38 | RegexRepeatMatcher(const std::string& expr, |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 39 | shared_ptr<RegexBackrefManager> backrefManager, |
| 40 | size_t indicator); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 41 | |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 42 | virtual |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 43 | ~RegexRepeatMatcher() |
| 44 | { |
| 45 | } |
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 | virtual bool |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 48 | match(const Name& name, size_t offset, size_t len); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 49 | |
| 50 | protected: |
| 51 | /** |
| 52 | * @brief Compile the regular expression to generate the more matchers when necessary |
| 53 | * @returns true if compiling succeeds |
| 54 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 55 | virtual void |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 56 | compile(); |
| 57 | |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 58 | private: |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 59 | bool |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 60 | parseRepetition(); |
| 61 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 62 | bool |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 63 | recursiveMatch(size_t repeat, |
| 64 | const Name& name, |
| 65 | size_t offset, size_t len); |
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 | private: |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 68 | size_t m_indicator; |
| 69 | size_t m_repeatMin; |
| 70 | size_t m_repeatMax; |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 71 | }; |
| 72 | |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 73 | } // namespace ndn |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 74 | |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 75 | #include "regex-backref-matcher.hpp" |
| 76 | #include "regex-component-set-matcher.hpp" |
| 77 | |
| 78 | namespace ndn { |
| 79 | |
| 80 | inline |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 81 | RegexRepeatMatcher::RegexRepeatMatcher(const std::string& expr, |
| 82 | shared_ptr<RegexBackrefManager> backrefManager, |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 83 | size_t indicator) |
| 84 | : RegexMatcher(expr, EXPR_REPEAT_PATTERN, backrefManager) |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 85 | , m_indicator(indicator) |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 86 | { |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 87 | compile(); |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 88 | } |
| 89 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 90 | inline void |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 91 | RegexRepeatMatcher::compile() |
| 92 | { |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 93 | shared_ptr<RegexMatcher> matcher; |
| 94 | |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 95 | if ('(' == m_expr[0]) { |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 96 | matcher = make_shared<RegexBackrefMatcher>(m_expr.substr(0, m_indicator), m_backrefManager); |
| 97 | m_backrefManager->pushRef(matcher); |
Alexander Afanasyev | b67090a | 2014-04-29 22:31:01 -0700 | [diff] [blame] | 98 | dynamic_pointer_cast<RegexBackrefMatcher>(matcher)->lateCompile(); |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 99 | } |
| 100 | else{ |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 101 | matcher = make_shared<RegexComponentSetMatcher>(m_expr.substr(0, m_indicator), |
| 102 | m_backrefManager); |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 103 | } |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 104 | m_matchers.push_back(matcher); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 105 | |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 106 | parseRepetition(); |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 107 | } |
| 108 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 109 | inline bool |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 110 | RegexRepeatMatcher::parseRepetition() |
| 111 | { |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 112 | size_t exprSize = m_expr.size(); |
| 113 | const size_t MAX_REPETITIONS = std::numeric_limits<size_t>::max(); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 114 | |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 115 | if (exprSize == m_indicator) { |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 116 | m_repeatMin = 1; |
| 117 | m_repeatMax = 1; |
| 118 | |
| 119 | return true; |
| 120 | } |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 121 | else { |
| 122 | if (exprSize == (m_indicator + 1)) { |
| 123 | if ('?' == m_expr[m_indicator]) { |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 124 | m_repeatMin = 0; |
| 125 | m_repeatMax = 1; |
| 126 | return true; |
| 127 | } |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 128 | if ('+' == m_expr[m_indicator]) { |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 129 | m_repeatMin = 1; |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 130 | m_repeatMax = MAX_REPETITIONS; |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 131 | return true; |
| 132 | } |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 133 | if ('*' == m_expr[m_indicator]) { |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 134 | m_repeatMin = 0; |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 135 | m_repeatMax = MAX_REPETITIONS; |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 136 | return true; |
| 137 | } |
| 138 | } |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 139 | else { |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 140 | std::string repeatStruct = m_expr.substr(m_indicator, exprSize - m_indicator); |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 141 | size_t rsSize = repeatStruct.size(); |
| 142 | size_t min = 0; |
| 143 | size_t max = 0; |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 144 | |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 145 | if (boost::regex_match(repeatStruct, boost::regex("\\{[0-9]+,[0-9]+\\}"))) { |
| 146 | size_t separator = repeatStruct.find_first_of(',', 0); |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 147 | min = atoi(repeatStruct.substr(1, separator - 1).c_str()); |
| 148 | max = atoi(repeatStruct.substr(separator + 1, rsSize - separator - 2).c_str()); |
| 149 | } |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 150 | else if (boost::regex_match(repeatStruct, boost::regex("\\{,[0-9]+\\}"))) { |
| 151 | size_t separator = repeatStruct.find_first_of(',', 0); |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 152 | min = 0; |
| 153 | max = atoi(repeatStruct.substr(separator + 1, rsSize - separator - 2).c_str()); |
| 154 | } |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 155 | else if (boost::regex_match(repeatStruct, boost::regex("\\{[0-9]+,\\}"))) { |
| 156 | size_t separator = repeatStruct.find_first_of(',', 0); |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 157 | min = atoi(repeatStruct.substr(1, separator).c_str()); |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 158 | max = MAX_REPETITIONS; |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 159 | } |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 160 | else if (boost::regex_match(repeatStruct, boost::regex("\\{[0-9]+\\}"))) { |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 161 | min = atoi(repeatStruct.substr(1, rsSize - 1).c_str()); |
| 162 | max = min; |
| 163 | } |
| 164 | else |
| 165 | throw RegexMatcher::Error(std::string("Error: RegexRepeatMatcher.ParseRepetition(): ") |
| 166 | + "Unrecognized format "+ m_expr); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 167 | |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 168 | if (min > MAX_REPETITIONS || max > MAX_REPETITIONS || min > max) |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 169 | throw RegexMatcher::Error(std::string("Error: RegexRepeatMatcher.ParseRepetition(): ") |
| 170 | + "Wrong number " + m_expr); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 171 | |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 172 | m_repeatMin = min; |
| 173 | m_repeatMax = max; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 174 | |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 175 | return true; |
| 176 | } |
| 177 | } |
| 178 | return false; |
| 179 | } |
| 180 | |
| 181 | inline bool |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 182 | RegexRepeatMatcher::match(const Name& name, size_t offset, size_t len) |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 183 | { |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 184 | m_matchResult.clear(); |
| 185 | |
| 186 | if (0 == m_repeatMin) |
| 187 | if (0 == len) |
| 188 | return true; |
| 189 | |
| 190 | if (recursiveMatch(0, name, offset, len)) |
| 191 | { |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 192 | for (size_t i = offset; i < offset + len; i++) |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 193 | m_matchResult.push_back(name.get(i)); |
| 194 | return true; |
| 195 | } |
| 196 | else |
| 197 | return false; |
| 198 | } |
| 199 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 200 | inline bool |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 201 | RegexRepeatMatcher::recursiveMatch(size_t repeat, const Name& name, size_t offset, size_t len) |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 202 | { |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 203 | ssize_t tried = len; |
| 204 | shared_ptr<RegexMatcher> matcher = m_matchers[0]; |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 205 | |
| 206 | if (0 < len && repeat >= m_repeatMax) |
| 207 | { |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 208 | return false; |
| 209 | } |
| 210 | |
| 211 | if (0 == len && repeat < m_repeatMin) |
| 212 | { |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 213 | return false; |
| 214 | } |
| 215 | |
| 216 | if (0 == len && repeat >= m_repeatMin) |
| 217 | { |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 218 | return true; |
| 219 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 220 | |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 221 | while (tried >= 0) |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 222 | { |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 223 | if (matcher->match(name, offset, tried) && |
| 224 | recursiveMatch(repeat + 1, name, offset + tried, len - tried)) |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 225 | return true; |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame] | 226 | tried--; |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | return false; |
| 230 | } |
| 231 | |
| 232 | |
| 233 | } // namespace ndn |
| 234 | |
| 235 | #endif // NDN_UTIL_REGEX_REGEX_REPEAT_MATCHER_HPP |