blob: b83aa2e21b33cf4c0759d6709a35172a293739ee [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-matcher.hpp"
9
10#include "../logging.hpp"
11
12INIT_LOGGER ("RegexMatcher");
13
14using namespace std;
15
16namespace ndn
17{
18RegexMatcher::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
29RegexMatcher::~RegexMatcher()
30{}
31
32bool
33RegexMatcher::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
55bool
56RegexMatcher::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