Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2013 Regents of the University of California. |
| 4 | * @author: Yingdi Yu <yingdi@cs.ucla.edu> |
| 5 | * See COPYING for copyright and distribution information. |
| 6 | */ |
| 7 | |
| 8 | #include <limits> |
| 9 | #include <stdlib.h> |
| 10 | |
| 11 | #include <boost/regex.hpp> |
| 12 | |
| 13 | #include "regex-repeat-matcher.hpp" |
| 14 | #include "regex-backref-matcher.hpp" |
| 15 | #include "regex-component-set-matcher.hpp" |
| 16 | |
| 17 | #include "../logging.hpp" |
| 18 | |
| 19 | INIT_LOGGER ("RegexRepeatMatcher"); |
| 20 | |
| 21 | using namespace std; |
| 22 | |
| 23 | namespace ndn |
| 24 | { |
| 25 | RegexRepeatMatcher::RegexRepeatMatcher(const string expr, ptr_lib::shared_ptr<RegexBackrefManager> backrefManager, int indicator) |
| 26 | : RegexMatcher (expr, EXPR_REPEAT_PATTERN, backrefManager), |
| 27 | m_indicator(indicator) |
| 28 | { |
| 29 | // _LOG_TRACE ("Enter RegexRepeatMatcher Constructor"); |
| 30 | compile(); |
| 31 | // _LOG_TRACE ("Exit RegexRepeatMatcher Constructor"); |
| 32 | } |
| 33 | |
| 34 | void |
| 35 | RegexRepeatMatcher::compile() |
| 36 | { |
| 37 | // _LOG_TRACE ("Enter RegexRepeatMatcher::compile"); |
| 38 | |
| 39 | ptr_lib::shared_ptr<RegexMatcher> matcher; |
| 40 | |
| 41 | if('(' == m_expr[0]){ |
| 42 | matcher = ptr_lib::make_shared<RegexBackrefMatcher>(m_expr.substr(0, m_indicator), m_backrefManager); |
| 43 | m_backrefManager->pushRef(matcher); |
| 44 | boost::dynamic_pointer_cast<RegexBackrefMatcher>(matcher)->lateCompile(); |
| 45 | } |
| 46 | else{ |
| 47 | matcher = ptr_lib::make_shared<RegexComponentSetMatcher>(m_expr.substr(0, m_indicator), m_backrefManager); |
| 48 | } |
| 49 | m_matcherList.push_back(matcher); |
| 50 | |
| 51 | parseRepetition(); |
| 52 | |
| 53 | // _LOG_TRACE ("Exit RegexRepeatMatcher::compile"); |
| 54 | |
| 55 | } |
| 56 | |
| 57 | bool |
| 58 | RegexRepeatMatcher::parseRepetition() |
| 59 | { |
| 60 | // _LOG_DEBUG ("Enter RegexRepeatMatcher::ParseRepetition()" << m_expr << " indicator: " << m_indicator); |
| 61 | |
| 62 | string errMsg = "Error: RegexRepeatMatcher.ParseRepetition(): "; |
| 63 | |
| 64 | int exprSize = m_expr.size(); |
| 65 | int intMax = numeric_limits<int>::max(); |
| 66 | |
| 67 | if(exprSize == m_indicator){ |
| 68 | m_repeatMin = 1; |
| 69 | m_repeatMax = 1; |
| 70 | |
| 71 | return true; |
| 72 | } |
| 73 | else{ |
| 74 | if(exprSize == (m_indicator + 1)){ |
| 75 | if('?' == m_expr[m_indicator]){ |
| 76 | m_repeatMin = 0; |
| 77 | m_repeatMax = 1; |
| 78 | return true; |
| 79 | } |
| 80 | if('+' == m_expr[m_indicator]){ |
| 81 | m_repeatMin = 1; |
| 82 | m_repeatMax = intMax; |
| 83 | return true; |
| 84 | } |
| 85 | if('*' == m_expr[m_indicator]){ |
| 86 | m_repeatMin = 0; |
| 87 | m_repeatMax = intMax; |
| 88 | return true; |
| 89 | } |
| 90 | } |
| 91 | else{ |
| 92 | string repeatStruct = m_expr.substr(m_indicator, exprSize - m_indicator); |
| 93 | int rsSize = repeatStruct.size(); |
| 94 | int min = 0; |
| 95 | int max = 0; |
| 96 | |
| 97 | if(boost::regex_match(repeatStruct, boost::regex("\\{[0-9]+,[0-9]+\\}"))){ |
| 98 | int separator = repeatStruct.find_first_of(',', 0); |
| 99 | min = atoi(repeatStruct.substr(1, separator - 1).c_str()); |
| 100 | max = atoi(repeatStruct.substr(separator + 1, rsSize - separator - 2).c_str()); |
| 101 | } |
| 102 | else if(boost::regex_match(repeatStruct, boost::regex("\\{,[0-9]+\\}"))){ |
| 103 | int separator = repeatStruct.find_first_of(',', 0); |
| 104 | min = 0; |
| 105 | max = atoi(repeatStruct.substr(separator + 1, rsSize - separator - 2).c_str()); |
| 106 | } |
| 107 | else if(boost::regex_match(repeatStruct, boost::regex("\\{[0-9]+,\\}"))){ |
| 108 | int separator = repeatStruct.find_first_of(',', 0); |
| 109 | min = atoi(repeatStruct.substr(1, separator).c_str()); |
| 110 | max = intMax; |
| 111 | } |
| 112 | else if(boost::regex_match(repeatStruct, boost::regex("\\{[0-9]+\\}"))){ |
| 113 | min = atoi(repeatStruct.substr(1, rsSize - 1).c_str()); |
| 114 | max = min; |
| 115 | } |
| 116 | else |
| 117 | throw RegexMatcher::Error(errMsg + "Unrecognized format "+ m_expr); |
| 118 | |
| 119 | if(min > intMax || max > intMax || min > max) |
| 120 | throw RegexMatcher::Error(errMsg + "Wrong number " + m_expr); |
| 121 | |
| 122 | m_repeatMin = min; |
| 123 | m_repeatMax = max; |
| 124 | |
| 125 | return true; |
| 126 | } |
| 127 | } |
| 128 | return false; |
| 129 | } |
| 130 | |
| 131 | bool |
| 132 | RegexRepeatMatcher::match(const Name & name, const int & offset, const int & len) |
| 133 | { |
| 134 | // _LOG_TRACE ("Enter RegexRepeatMatcher::match"); |
| 135 | |
| 136 | m_matchResult.clear(); |
| 137 | |
| 138 | if (0 == m_repeatMin) |
| 139 | if (0 == len) |
| 140 | return true; |
| 141 | |
| 142 | if (recursiveMatch(0, name, offset, len)) |
| 143 | { |
| 144 | for (int i = offset; i < offset + len; i++) |
| 145 | m_matchResult.push_back(name.get(i)); |
| 146 | return true; |
| 147 | } |
| 148 | else |
| 149 | return false; |
| 150 | } |
| 151 | |
| 152 | bool |
| 153 | RegexRepeatMatcher::recursiveMatch(int repeat, const Name & name, const int & offset, const int & len) |
| 154 | { |
| 155 | // _LOG_TRACE ("Enter RegexRepeatMatcher::recursiveMatch"); |
| 156 | |
| 157 | // _LOG_DEBUG ("repeat: " << repeat << " offset: " << offset << " len: " << len); |
| 158 | // _LOG_DEBUG ("m_repeatMin: " << m_repeatMin << " m_repeatMax: " << m_repeatMax); |
| 159 | |
| 160 | int tried = len; |
| 161 | ptr_lib::shared_ptr<RegexMatcher> matcher = m_matcherList[0]; |
| 162 | |
| 163 | if (0 < len && repeat >= m_repeatMax) |
| 164 | { |
| 165 | // _LOG_DEBUG("Match Fail: Reach m_repeatMax && More components"); |
| 166 | return false; |
| 167 | } |
| 168 | |
| 169 | if (0 == len && repeat < m_repeatMin) |
| 170 | { |
| 171 | // _LOG_DEBUG("Match Fail: No more components && have NOT reached m_repeatMin " << len << ", " << m_repeatMin); |
| 172 | return false; |
| 173 | } |
| 174 | |
| 175 | if (0 == len && repeat >= m_repeatMin) |
| 176 | { |
| 177 | // _LOG_DEBUG("Match Succeed: No more components && reach m_repeatMin"); |
| 178 | return true; |
| 179 | } |
| 180 | |
| 181 | while(tried >= 0) |
| 182 | { |
| 183 | // _LOG_DEBUG("Attempt tried: " << tried); |
| 184 | |
| 185 | if (matcher->match(name, offset, tried) and recursiveMatch(repeat + 1, name, offset + tried, len - tried)) |
| 186 | return true; |
| 187 | // _LOG_DEBUG("Failed at tried: " << tried); |
| 188 | tried --; |
| 189 | } |
| 190 | |
| 191 | return false; |
| 192 | } |
| 193 | }//ndn |