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