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 "regex-matcher.hpp" |
| 9 | |
| 10 | #include "../logging.hpp" |
| 11 | |
| 12 | INIT_LOGGER ("RegexMatcher"); |
| 13 | |
| 14 | using namespace std; |
| 15 | |
| 16 | namespace ndn |
| 17 | { |
| 18 | RegexMatcher::RegexMatcher(const std::string& expr, |
| 19 | const RegexExprType& type, |
| 20 | ptr_lib::shared_ptr<RegexBackrefManager> backrefManager) |
| 21 | : m_expr(expr), |
| 22 | m_type(type), |
| 23 | m_backrefManager(backrefManager) |
| 24 | { |
| 25 | if(NULL == m_backrefManager) |
| 26 | m_backrefManager = ptr_lib::shared_ptr<RegexBackrefManager>(new RegexBackrefManager); |
| 27 | } |
| 28 | |
| 29 | RegexMatcher::~RegexMatcher() |
| 30 | {} |
| 31 | |
| 32 | bool |
| 33 | RegexMatcher::match (const Name& name, const int& offset, const int& len) |
| 34 | { |
| 35 | // _LOG_TRACE ("Enter RegexMatcher::match"); |
| 36 | bool result = false; |
| 37 | |
| 38 | m_matchResult.clear(); |
| 39 | |
| 40 | if(recursiveMatch(0, name, offset, len)) |
| 41 | { |
| 42 | for(int i = offset; i < offset + len ; i++) |
| 43 | m_matchResult.push_back(name.get(i)); |
| 44 | result = true; |
| 45 | } |
| 46 | else |
| 47 | { |
| 48 | result = false; |
| 49 | } |
| 50 | |
| 51 | // _LOG_TRACE ("Exit RegexMatcher::match"); |
| 52 | return result; |
| 53 | } |
| 54 | |
| 55 | bool |
| 56 | RegexMatcher::recursiveMatch(const int& mId, const Name& name, const int& offset, const int& len) |
| 57 | { |
| 58 | // _LOG_TRACE ("Enter RegexMatcher::recursiveMatch"); |
| 59 | |
| 60 | int tried = len; |
| 61 | |
| 62 | if(mId >= m_matcherList.size()) |
| 63 | return (len != 0 ? false : true); |
| 64 | |
| 65 | ptr_lib::shared_ptr<RegexMatcher> matcher = m_matcherList[mId]; |
| 66 | |
| 67 | while(tried >= 0) |
| 68 | { |
| 69 | if(matcher->match(name, offset, tried) && recursiveMatch(mId + 1, name, offset + tried, len - tried)) |
| 70 | return true; |
| 71 | tried--; |
| 72 | } |
| 73 | |
| 74 | return false; |
| 75 | } |
| 76 | |
| 77 | }//ndn |
| 78 | |