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) |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame^] | 23 | : RegexMatcher(expr, EXPR_TOP) |
| 24 | , m_expand(expand) |
| 25 | , m_isSecondaryUsed(false) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 26 | { |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [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 | { |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 34 | } |
| 35 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 36 | void |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 37 | RegexTopMatcher::compile() |
| 38 | { |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 39 | std::string errMsg = "Error: RegexTopMatcher.Compile(): "; |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 40 | |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 41 | std::string expr = m_expr; |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 42 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 43 | if ('$' != expr[expr.size() - 1]) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 44 | expr = expr + "<.*>*"; |
| 45 | else |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame^] | 46 | expr = expr.substr(0, expr.size() - 1); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 47 | |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame^] | 48 | if ('^' != expr[0]) { |
| 49 | m_secondaryMatcher = make_shared<RegexPatternListMatcher>( |
| 50 | "<.*>*" + expr, |
| 51 | cref(m_secondaryBackrefManager)); |
| 52 | } |
| 53 | else { |
| 54 | expr = expr.substr(1, expr.size() - 1); |
| 55 | } |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 56 | |
Alexander Afanasyev | b67090a | 2014-04-29 22:31:01 -0700 | [diff] [blame] | 57 | m_primaryMatcher = make_shared<RegexPatternListMatcher>(func_lib::cref(expr), |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame^] | 58 | func_lib::cref(m_primaryBackrefManager)); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 59 | } |
| 60 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 61 | bool |
| 62 | RegexTopMatcher::match(const Name& name) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 63 | { |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame^] | 64 | m_isSecondaryUsed = false; |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 65 | |
| 66 | m_matchResult.clear(); |
| 67 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 68 | if (m_primaryMatcher->match(name, 0, name.size())) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 69 | { |
| 70 | m_matchResult = m_primaryMatcher->getMatchResult(); |
| 71 | return true; |
| 72 | } |
| 73 | else |
| 74 | { |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame^] | 75 | if (static_cast<bool>(m_secondaryMatcher) && m_secondaryMatcher->match(name, 0, name.size())) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 76 | { |
| 77 | m_matchResult = m_secondaryMatcher->getMatchResult(); |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame^] | 78 | m_isSecondaryUsed = true; |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 79 | return true; |
| 80 | } |
| 81 | return false; |
| 82 | } |
| 83 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 84 | |
| 85 | bool |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame^] | 86 | RegexTopMatcher::match(const Name& name, size_t, size_t) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 87 | { |
| 88 | return match(name); |
| 89 | } |
| 90 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 91 | Name |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame^] | 92 | RegexTopMatcher::expand(const std::string& expandStr) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 93 | { |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 94 | Name result; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 95 | |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame^] | 96 | shared_ptr<RegexBackrefManager> backrefManager = |
| 97 | (m_isSecondaryUsed ? m_secondaryBackrefManager : m_primaryBackrefManager); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 98 | |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame^] | 99 | size_t backrefNo = backrefManager->size(); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 100 | |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 101 | std::string expand; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 102 | |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame^] | 103 | if (!expandStr.empty()) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 104 | expand = expandStr; |
| 105 | else |
| 106 | expand = m_expand; |
| 107 | |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 108 | size_t offset = 0; |
| 109 | while (offset < expand.size()) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 110 | { |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 111 | std::string item = getItemFromExpand(expand, offset); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 112 | if (item[0] == '<') |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 113 | { |
| 114 | result.append(item.substr(1, item.size() - 2)); |
| 115 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 116 | if (item[0] == '\\') |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 117 | { |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame^] | 118 | size_t index = boost::lexical_cast<size_t>(item.substr(1, item.size() - 1)); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 119 | |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame^] | 120 | if (0 == index) { |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 121 | std::vector<name::Component>::iterator it = m_matchResult.begin(); |
| 122 | std::vector<name::Component>::iterator end = m_matchResult.end(); |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame^] | 123 | for (; it != end; it++) |
| 124 | result.append(*it); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 125 | } |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame^] | 126 | else if (index <= backrefNo) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 127 | { |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 128 | std::vector<name::Component>::const_iterator it = |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame^] | 129 | backrefManager->getBackref(index - 1)->getMatchResult().begin(); |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 130 | std::vector<name::Component>::const_iterator end = |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame^] | 131 | backrefManager->getBackref(index - 1)->getMatchResult().end(); |
| 132 | for (; it != end; it++) |
| 133 | result.append(*it); |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 134 | } |
| 135 | else |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame^] | 136 | throw RegexMatcher::Error("Exceed the range of back reference"); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 137 | } |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 138 | } |
| 139 | return result; |
| 140 | } |
| 141 | |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 142 | std::string |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 143 | RegexTopMatcher::getItemFromExpand(const std::string& expand, size_t& offset) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 144 | { |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 145 | size_t begin = offset; |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 146 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 147 | if (expand[offset] == '\\') |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 148 | { |
| 149 | offset++; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 150 | if (offset >= expand.size()) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 151 | throw RegexMatcher::Error("wrong format of expand string!"); |
| 152 | |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame^] | 153 | while (expand[offset] <= '9' and expand[offset] >= '0') { |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 154 | offset++; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 155 | if (offset > expand.size()) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 156 | throw RegexMatcher::Error("wrong format of expand string!"); |
| 157 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 158 | if (offset > begin + 1) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 159 | return expand.substr(begin, offset - begin); |
| 160 | else |
| 161 | throw RegexMatcher::Error("wrong format of expand string!"); |
| 162 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 163 | else if (expand[offset] == '<') |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 164 | { |
| 165 | offset++; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 166 | if (offset >= expand.size()) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 167 | throw RegexMatcher::Error("wrong format of expand string!"); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 168 | |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 169 | size_t left = 1; |
| 170 | size_t right = 0; |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame^] | 171 | while (right < left) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 172 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 173 | if (expand[offset] == '<') |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 174 | left++; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 175 | if (expand[offset] == '>') |
| 176 | right++; |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 177 | offset++; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 178 | if (offset >= expand.size()) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 179 | throw RegexMatcher::Error("wrong format of expand string!"); |
| 180 | } |
| 181 | return expand.substr(begin, offset - begin); |
| 182 | } |
| 183 | else |
| 184 | throw RegexMatcher::Error("wrong format of expand string!"); |
| 185 | } |
| 186 | |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 187 | shared_ptr<RegexTopMatcher> |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 188 | RegexTopMatcher::fromName(const Name& name, bool hasAnchor) |
| 189 | { |
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 | |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame^] | 192 | for (Name::const_iterator it = name.begin(); it != name.end(); it++) |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 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 | b6b21b3 | 2014-04-28 22:38:03 -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('\\'); |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame^] | 229 | // Fallthrough |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 230 | default: |
| 231 | newStr.push_back(c); |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame^] | 232 | break; |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 233 | } |
| 234 | } |
| 235 | |
| 236 | return newStr; |
| 237 | } |
| 238 | |
Alexander Afanasyev | b6b21b3 | 2014-04-28 22:38:03 -0700 | [diff] [blame^] | 239 | } // namespace ndn |