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