blob: 3baaa46982ee5dc96eb2899434f02d8c3e001ca6 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Yingdi Yu5e974202014-01-29 16:59:06 -08002/**
Alexander Afanasyev73e30042015-09-17 17:09:51 -07003 * Copyright (c) 2013-2015 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * 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 Afanasyevdfa52c42014-04-24 21:10:11 -070020 *
21 * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/>
Yingdi Yu5e974202014-01-29 16:59:06 -080022 */
23
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -080024#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 Yu5e974202014-01-29 16:59:06 -080030
31#include "regex-matcher.hpp"
32
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -080033namespace ndn {
Yingdi Yu5e974202014-01-29 16:59:06 -080034
35class RegexRepeatMatcher : public RegexMatcher
36{
37public:
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070038 RegexRepeatMatcher(const std::string& expr,
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -070039 shared_ptr<RegexBackrefManager> backrefManager,
40 size_t indicator);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070041
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070042 virtual
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -070043 ~RegexRepeatMatcher()
44 {
45 }
Yingdi Yu5e974202014-01-29 16:59:06 -080046
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070047 virtual bool
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -070048 match(const Name& name, size_t offset, size_t len);
Yingdi Yu5e974202014-01-29 16:59:06 -080049
50protected:
51 /**
52 * @brief Compile the regular expression to generate the more matchers when necessary
Yingdi Yu5e974202014-01-29 16:59:06 -080053 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070054 virtual void
Yingdi Yu5e974202014-01-29 16:59:06 -080055 compile();
56
Yingdi Yu5e974202014-01-29 16:59:06 -080057private:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070058 bool
Yingdi Yu5e974202014-01-29 16:59:06 -080059 parseRepetition();
60
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070061 bool
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -070062 recursiveMatch(size_t repeat,
63 const Name& name,
64 size_t offset, size_t len);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070065
Yingdi Yu5e974202014-01-29 16:59:06 -080066private:
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -070067 size_t m_indicator;
68 size_t m_repeatMin;
69 size_t m_repeatMax;
Yingdi Yu5e974202014-01-29 16:59:06 -080070};
71
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -080072} // namespace ndn
Yingdi Yu5e974202014-01-29 16:59:06 -080073
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -080074#include "regex-backref-matcher.hpp"
75#include "regex-component-set-matcher.hpp"
76
77namespace ndn {
78
79inline
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070080RegexRepeatMatcher::RegexRepeatMatcher(const std::string& expr,
81 shared_ptr<RegexBackrefManager> backrefManager,
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -070082 size_t indicator)
83 : RegexMatcher(expr, EXPR_REPEAT_PATTERN, backrefManager)
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070084 , m_indicator(indicator)
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -080085{
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -080086 compile();
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -080087}
88
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070089inline void
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -080090RegexRepeatMatcher::compile()
91{
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -080092 shared_ptr<RegexMatcher> matcher;
93
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -070094 if ('(' == m_expr[0]) {
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -080095 matcher = make_shared<RegexBackrefMatcher>(m_expr.substr(0, m_indicator), m_backrefManager);
96 m_backrefManager->pushRef(matcher);
Alexander Afanasyevb67090a2014-04-29 22:31:01 -070097 dynamic_pointer_cast<RegexBackrefMatcher>(matcher)->lateCompile();
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -080098 }
99 else{
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -0700100 matcher = make_shared<RegexComponentSetMatcher>(m_expr.substr(0, m_indicator),
101 m_backrefManager);
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800102 }
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700103 m_matchers.push_back(matcher);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700104
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800105 parseRepetition();
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800106}
107
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700108inline bool
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800109RegexRepeatMatcher::parseRepetition()
110{
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700111 size_t exprSize = m_expr.size();
112 const size_t MAX_REPETITIONS = std::numeric_limits<size_t>::max();
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700113
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700114 if (exprSize == m_indicator) {
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800115 m_repeatMin = 1;
116 m_repeatMax = 1;
117
118 return true;
119 }
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700120 else {
121 if (exprSize == (m_indicator + 1)) {
122 if ('?' == m_expr[m_indicator]) {
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800123 m_repeatMin = 0;
124 m_repeatMax = 1;
125 return true;
126 }
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700127 if ('+' == m_expr[m_indicator]) {
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800128 m_repeatMin = 1;
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700129 m_repeatMax = MAX_REPETITIONS;
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800130 return true;
131 }
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700132 if ('*' == m_expr[m_indicator]) {
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800133 m_repeatMin = 0;
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700134 m_repeatMax = MAX_REPETITIONS;
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800135 return true;
136 }
137 }
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700138 else {
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800139 std::string repeatStruct = m_expr.substr(m_indicator, exprSize - m_indicator);
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700140 size_t rsSize = repeatStruct.size();
141 size_t min = 0;
142 size_t max = 0;
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800143
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700144 if (boost::regex_match(repeatStruct, boost::regex("\\{[0-9]+,[0-9]+\\}"))) {
145 size_t separator = repeatStruct.find_first_of(',', 0);
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800146 min = atoi(repeatStruct.substr(1, separator - 1).c_str());
147 max = atoi(repeatStruct.substr(separator + 1, rsSize - separator - 2).c_str());
148 }
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700149 else if (boost::regex_match(repeatStruct, boost::regex("\\{,[0-9]+\\}"))) {
150 size_t separator = repeatStruct.find_first_of(',', 0);
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800151 min = 0;
152 max = atoi(repeatStruct.substr(separator + 1, rsSize - separator - 2).c_str());
153 }
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700154 else if (boost::regex_match(repeatStruct, boost::regex("\\{[0-9]+,\\}"))) {
155 size_t separator = repeatStruct.find_first_of(',', 0);
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800156 min = atoi(repeatStruct.substr(1, separator).c_str());
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700157 max = MAX_REPETITIONS;
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800158 }
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700159 else if (boost::regex_match(repeatStruct, boost::regex("\\{[0-9]+\\}"))) {
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800160 min = atoi(repeatStruct.substr(1, rsSize - 1).c_str());
161 max = min;
162 }
163 else
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700164 BOOST_THROW_EXCEPTION(RegexMatcher::Error(std::string("Error: RegexRepeatMatcher.ParseRepetition():")
165 + " Unrecognized format "+ m_expr));
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700166
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700167 if (min > MAX_REPETITIONS || max > MAX_REPETITIONS || min > max)
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700168 BOOST_THROW_EXCEPTION(RegexMatcher::Error(std::string("Error: RegexRepeatMatcher.ParseRepetition():")
169 + " Wrong number " + m_expr));
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700170
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800171 m_repeatMin = min;
172 m_repeatMax = max;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700173
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800174 return true;
175 }
176 }
177 return false;
178}
179
180inline bool
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700181RegexRepeatMatcher::match(const Name& name, size_t offset, size_t len)
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800182{
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800183 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 {
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700191 for (size_t i = offset; i < offset + len; i++)
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800192 m_matchResult.push_back(name.get(i));
193 return true;
194 }
195 else
196 return false;
197}
198
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700199inline bool
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700200RegexRepeatMatcher::recursiveMatch(size_t repeat, const Name& name, size_t offset, size_t len)
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800201{
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700202 ssize_t tried = len;
203 shared_ptr<RegexMatcher> matcher = m_matchers[0];
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800204
205 if (0 < len && repeat >= m_repeatMax)
206 {
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800207 return false;
208 }
209
210 if (0 == len && repeat < m_repeatMin)
211 {
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800212 return false;
213 }
214
215 if (0 == len && repeat >= m_repeatMin)
216 {
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800217 return true;
218 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700219
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700220 while (tried >= 0)
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800221 {
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700222 if (matcher->match(name, offset, tried) &&
223 recursiveMatch(repeat + 1, name, offset + tried, len - tried))
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800224 return true;
Alexander Afanasyevb6b21b32014-04-28 22:38:03 -0700225 tried--;
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800226 }
227
228 return false;
229}
230
231
232} // namespace ndn
233
234#endif // NDN_UTIL_REGEX_REGEX_REPEAT_MATCHER_HPP