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 | |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 15 | #include "regex-top-matcher.hpp" |
| 16 | |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 17 | #include "regex-backref-manager.hpp" |
| 18 | #include "regex-pattern-list-matcher.hpp" |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 19 | |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 20 | namespace ndn { |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 21 | |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 22 | RegexTopMatcher::RegexTopMatcher(const std::string& expr, const std::string& expand) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 23 | : RegexMatcher(expr, EXPR_TOP), |
| 24 | m_expand(expand), |
| 25 | m_secondaryUsed(false) |
| 26 | { |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 27 | m_primaryBackRefManager = make_shared<RegexBackrefManager>(); |
| 28 | m_secondaryBackRefManager = make_shared<RegexBackrefManager>(); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 29 | compile(); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | RegexTopMatcher::~RegexTopMatcher() |
| 33 | { |
| 34 | // delete m_backRefManager; |
| 35 | } |
| 36 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 37 | void |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 38 | RegexTopMatcher::compile() |
| 39 | { |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 40 | std::string errMsg = "Error: RegexTopMatcher.Compile(): "; |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 41 | |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 42 | std::string expr = m_expr; |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 43 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 44 | if ('$' != expr[expr.size() - 1]) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 45 | expr = expr + "<.*>*"; |
| 46 | else |
| 47 | expr = expr.substr(0, expr.size()-1); |
| 48 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 49 | if ('^' != expr[0]) |
Alexander Afanasyev | b67090a | 2014-04-29 22:31:01 -0700 | [diff] [blame] | 50 | m_secondaryMatcher = make_shared<RegexPatternListMatcher>("<.*>*" + expr, |
| 51 | cref(m_secondaryBackRefManager)); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 52 | else |
| 53 | expr = expr.substr(1, expr.size()-1); |
| 54 | |
Alexander Afanasyev | b67090a | 2014-04-29 22:31:01 -0700 | [diff] [blame] | 55 | m_primaryMatcher = make_shared<RegexPatternListMatcher>(func_lib::cref(expr), |
| 56 | func_lib::cref(m_primaryBackRefManager)); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 57 | } |
| 58 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 59 | bool |
| 60 | RegexTopMatcher::match(const Name& name) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 61 | { |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 62 | m_secondaryUsed = false; |
| 63 | |
| 64 | m_matchResult.clear(); |
| 65 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 66 | if (m_primaryMatcher->match(name, 0, name.size())) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 67 | { |
| 68 | m_matchResult = m_primaryMatcher->getMatchResult(); |
| 69 | return true; |
| 70 | } |
| 71 | else |
| 72 | { |
| 73 | if (NULL != m_secondaryMatcher && m_secondaryMatcher->match(name, 0, name.size())) |
| 74 | { |
| 75 | m_matchResult = m_secondaryMatcher->getMatchResult(); |
| 76 | m_secondaryUsed = true; |
| 77 | return true; |
| 78 | } |
| 79 | return false; |
| 80 | } |
| 81 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 82 | |
| 83 | bool |
| 84 | RegexTopMatcher::match (const Name& name, const int& offset, const int& len) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 85 | { |
| 86 | return match(name); |
| 87 | } |
| 88 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 89 | Name |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 90 | RegexTopMatcher::expand (const std::string& expandStr) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 91 | { |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 92 | Name result; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 93 | |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 94 | shared_ptr<RegexBackrefManager> backRefManager = |
| 95 | (m_secondaryUsed ? m_secondaryBackRefManager : m_primaryBackRefManager); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 96 | |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 97 | int backRefNum = backRefManager->size(); |
| 98 | |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 99 | std::string expand; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 100 | |
| 101 | if (expandStr != "") |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 102 | expand = expandStr; |
| 103 | else |
| 104 | expand = m_expand; |
| 105 | |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 106 | size_t offset = 0; |
| 107 | while (offset < expand.size()) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 108 | { |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 109 | std::string item = getItemFromExpand(expand, offset); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 110 | if (item[0] == '<') |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 111 | { |
| 112 | result.append(item.substr(1, item.size() - 2)); |
| 113 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 114 | if (item[0] == '\\') |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 115 | { |
| 116 | |
| 117 | int index = atoi(item.substr(1, item.size() - 1).c_str()); |
| 118 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 119 | if (0 == index){ |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 120 | std::vector<name::Component>::iterator it = m_matchResult.begin(); |
| 121 | std::vector<name::Component>::iterator end = m_matchResult.end(); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 122 | for(; it != end; it++) |
| 123 | result.append (*it); |
| 124 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 125 | else if (index <= backRefNum) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 126 | { |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 127 | std::vector<name::Component>::const_iterator it = |
| 128 | backRefManager->getBackRef (index - 1)->getMatchResult ().begin(); |
| 129 | std::vector<name::Component>::const_iterator end = |
| 130 | backRefManager->getBackRef (index - 1)->getMatchResult ().end(); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 131 | for(; it != end; it++) |
| 132 | result.append (*it); |
| 133 | } |
| 134 | else |
| 135 | throw RegexMatcher::Error("Exceed the range of back reference!"); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 136 | } |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 137 | } |
| 138 | return result; |
| 139 | } |
| 140 | |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 141 | std::string |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 142 | RegexTopMatcher::getItemFromExpand(const std::string& expand, size_t& offset) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 143 | { |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 144 | size_t begin = offset; |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 145 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 146 | if (expand[offset] == '\\') |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 147 | { |
| 148 | offset++; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 149 | if (offset >= expand.size()) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 150 | throw RegexMatcher::Error("wrong format of expand string!"); |
| 151 | |
| 152 | while(expand[offset] <= '9' and expand[offset] >= '0'){ |
| 153 | offset++; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 154 | if (offset > expand.size()) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 155 | throw RegexMatcher::Error("wrong format of expand string!"); |
| 156 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 157 | if (offset > begin + 1) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 158 | return expand.substr(begin, offset - begin); |
| 159 | else |
| 160 | throw RegexMatcher::Error("wrong format of expand string!"); |
| 161 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 162 | else if (expand[offset] == '<') |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 163 | { |
| 164 | offset++; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 165 | if (offset >= expand.size()) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 166 | throw RegexMatcher::Error("wrong format of expand string!"); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 167 | |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 168 | size_t left = 1; |
| 169 | size_t right = 0; |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 170 | while(right < left) |
| 171 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 172 | if (expand[offset] == '<') |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 173 | left++; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 174 | if (expand[offset] == '>') |
| 175 | right++; |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 176 | offset++; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 177 | if (offset >= expand.size()) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 178 | throw RegexMatcher::Error("wrong format of expand string!"); |
| 179 | } |
| 180 | return expand.substr(begin, offset - begin); |
| 181 | } |
| 182 | else |
| 183 | throw RegexMatcher::Error("wrong format of expand string!"); |
| 184 | } |
| 185 | |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 186 | shared_ptr<RegexTopMatcher> |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 187 | RegexTopMatcher::fromName(const Name& name, bool hasAnchor) |
| 188 | { |
| 189 | Name::const_iterator it = name.begin(); |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 190 | std::string regexStr("^"); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 191 | |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 192 | for(; it != name.end(); it++) |
| 193 | { |
| 194 | regexStr.append("<"); |
| 195 | regexStr.append(convertSpecialChar(it->toEscapedString())); |
| 196 | regexStr.append(">"); |
| 197 | } |
| 198 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 199 | if (hasAnchor) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 200 | regexStr.append("$"); |
| 201 | |
Alexander Afanasyev | b67090a | 2014-04-29 22:31:01 -0700 | [diff] [blame] | 202 | // OSX 10.9 has problems with just cref |
| 203 | return make_shared<RegexTopMatcher>(func_lib::cref(regexStr)); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 204 | } |
| 205 | |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 206 | std::string |
| 207 | RegexTopMatcher::convertSpecialChar(const std::string& str) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 208 | { |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 209 | std::string newStr; |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 210 | for(size_t i = 0; i < str.size(); i++) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 211 | { |
| 212 | char c = str[i]; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 213 | switch (c) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 214 | { |
| 215 | case '.': |
| 216 | case '[': |
| 217 | case '{': |
| 218 | case '}': |
| 219 | case '(': |
| 220 | case ')': |
| 221 | case '\\': |
| 222 | case '*': |
| 223 | case '+': |
| 224 | case '?': |
| 225 | case '|': |
| 226 | case '^': |
| 227 | case '$': |
| 228 | newStr.push_back('\\'); |
| 229 | default: |
| 230 | newStr.push_back(c); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | return newStr; |
| 235 | } |
| 236 | |
| 237 | }//ndn |