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-component-matcher.hpp" |
| 9 | |
| 10 | #include "../logging.hpp" |
| 11 | |
| 12 | INIT_LOGGER ("RegexComponentMatcher"); |
| 13 | |
| 14 | using namespace std; |
| 15 | |
| 16 | namespace ndn |
| 17 | { |
| 18 | |
| 19 | RegexComponentMatcher::RegexComponentMatcher (const string & expr, |
| 20 | ptr_lib::shared_ptr<RegexBackrefManager> backRefManager, |
| 21 | bool exact) |
| 22 | : RegexMatcher (expr, EXPR_COMPONENT, backRefManager), |
| 23 | m_exact(exact) |
| 24 | { |
| 25 | // _LOG_TRACE ("Enter RegexComponentMatcher Constructor: "); |
| 26 | compile(); |
| 27 | // _LOG_TRACE ("Exit RegexComponentMatcher Constructor: "); |
| 28 | } |
| 29 | |
| 30 | void |
| 31 | RegexComponentMatcher::compile () |
| 32 | { |
| 33 | // _LOG_TRACE ("Enter RegexComponentMatcher::compile"); |
| 34 | |
| 35 | m_componentRegex = boost::regex (m_expr); |
| 36 | |
| 37 | m_pseudoMatcher.clear(); |
| 38 | m_pseudoMatcher.push_back(ptr_lib::make_shared<RegexPseudoMatcher>()); |
| 39 | |
| 40 | for (int i = 1; i < m_componentRegex.mark_count(); i++) |
| 41 | { |
| 42 | ptr_lib::shared_ptr<RegexPseudoMatcher> pMatcher = ptr_lib::make_shared<RegexPseudoMatcher>(); |
| 43 | m_pseudoMatcher.push_back(pMatcher); |
| 44 | m_backrefManager->pushRef(ptr_lib::static_pointer_cast<RegexMatcher>(pMatcher)); |
| 45 | } |
| 46 | |
| 47 | |
| 48 | // _LOG_TRACE ("Exit RegexComponentMatcher::compile"); |
| 49 | } |
| 50 | |
| 51 | bool |
| 52 | RegexComponentMatcher::match (const Name & name, const int & offset, const int & len) |
| 53 | { |
| 54 | // _LOG_TRACE ("Enter RegexComponentMatcher::match "); |
| 55 | |
| 56 | m_matchResult.clear(); |
| 57 | |
| 58 | if("" == m_expr) |
| 59 | { |
| 60 | m_matchResult.push_back(name.get(offset)); |
| 61 | return true; |
| 62 | } |
| 63 | |
| 64 | if(true == m_exact) |
| 65 | { |
| 66 | boost::smatch subResult; |
| 67 | string targetStr = name.get(offset).toEscapedString(); |
| 68 | if(boost::regex_match(targetStr, subResult, m_componentRegex)) |
| 69 | { |
| 70 | for (int i = 1; i < m_componentRegex.mark_count(); i++) |
| 71 | { |
| 72 | m_pseudoMatcher[i]->resetMatchResult(); |
| 73 | m_pseudoMatcher[i]->setMatchResult(subResult[i]); |
| 74 | } |
| 75 | m_matchResult.push_back(name.get(offset)); |
| 76 | return true; |
| 77 | } |
| 78 | } |
| 79 | else |
| 80 | { |
| 81 | throw RegexMatcher::Error("Non-exact component search is not supported yet!"); |
| 82 | } |
| 83 | |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | } //ndn |