blob: e3bbe0e54ccf883455cc33a1debf4ca33a133c68 [file] [log] [blame]
Yingdi Yu5e974202014-01-29 16:59:06 -08001/* -*- 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
12INIT_LOGGER ("RegexComponentMatcher");
13
14using namespace std;
15
16namespace ndn
17{
18
19RegexComponentMatcher::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
30void
31RegexComponentMatcher::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
51bool
52RegexComponentMatcher::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