blob: 34bcba782a167907d70933f9330f9f73dd151229 [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
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -08008#ifndef NDN_UTIL_REGEX_REGEX_COMPONENT_MATCHER_HPP
9#define NDN_UTIL_REGEX_REGEX_COMPONENT_MATCHER_HPP
Yingdi Yu5e974202014-01-29 16:59:06 -080010
11#include <boost/regex.hpp>
12
13#include "regex-matcher.hpp"
14#include "regex-pseudo-matcher.hpp"
15
16
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -080017namespace ndn {
18
Yingdi Yu5e974202014-01-29 16:59:06 -080019class RegexComponentMatcher : public RegexMatcher
20{
21public:
22 /**
23 * @brief Create a RegexComponent matcher from expr
24 * @param expr The standard regular expression to match a component
25 * @param backRefManager The back reference manager
26 * @param exact The flag to provide exact match
27 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070028 RegexComponentMatcher(const std::string& expr,
29 ptr_lib::shared_ptr<RegexBackrefManager> backRefManager,
Yingdi Yu5e974202014-01-29 16:59:06 -080030 bool exact = true);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070031
Yingdi Yu5e974202014-01-29 16:59:06 -080032 virtual ~RegexComponentMatcher() {};
33
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070034 virtual bool
Yingdi Yu5e974202014-01-29 16:59:06 -080035 match(const Name & name, const int & offset, const int &len = 1);
36
37protected:
38 /**
39 * @brief Compile the regular expression to generate the more matchers when necessary
40 * @returns true if compiling succeeds
41 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070042 virtual void
Yingdi Yu5e974202014-01-29 16:59:06 -080043 compile();
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070044
Yingdi Yu5e974202014-01-29 16:59:06 -080045private:
46 bool m_exact;
47 boost::regex m_componentRegex;
48 std::vector<ptr_lib::shared_ptr<RegexPseudoMatcher> > m_pseudoMatcher;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070049
Yingdi Yu5e974202014-01-29 16:59:06 -080050};
Yingdi Yu5e974202014-01-29 16:59:06 -080051
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -080052
53inline
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070054RegexComponentMatcher::RegexComponentMatcher (const std::string& expr,
55 ptr_lib::shared_ptr<RegexBackrefManager> backRefManager,
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -080056 bool exact)
57 : RegexMatcher (expr, EXPR_COMPONENT, backRefManager),
58 m_exact(exact)
59{
60 // _LOG_TRACE ("Enter RegexComponentMatcher Constructor: ");
61 compile();
62 // _LOG_TRACE ("Exit RegexComponentMatcher Constructor: ");
63}
64
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070065inline void
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -080066RegexComponentMatcher::compile ()
67{
68 // _LOG_TRACE ("Enter RegexComponentMatcher::compile");
69
70 m_componentRegex = boost::regex (m_expr);
71
72 m_pseudoMatcher.clear();
73 m_pseudoMatcher.push_back(ptr_lib::make_shared<RegexPseudoMatcher>());
74
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -070075 for (size_t i = 1; i < m_componentRegex.mark_count(); i++)
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -080076 {
77 ptr_lib::shared_ptr<RegexPseudoMatcher> pMatcher = ptr_lib::make_shared<RegexPseudoMatcher>();
78 m_pseudoMatcher.push_back(pMatcher);
79 m_backrefManager->pushRef(ptr_lib::static_pointer_cast<RegexMatcher>(pMatcher));
80 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070081
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -080082
83 // _LOG_TRACE ("Exit RegexComponentMatcher::compile");
84}
85
86inline bool
87RegexComponentMatcher::match (const Name & name, const int & offset, const int & len)
88{
89 // _LOG_TRACE ("Enter RegexComponentMatcher::match ");
90
91 m_matchResult.clear();
92
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070093 if ("" == m_expr)
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -080094 {
95 m_matchResult.push_back(name.get(offset));
96 return true;
97 }
98
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070099 if (true == m_exact)
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800100 {
101 boost::smatch subResult;
102 std::string targetStr = name.get(offset).toEscapedString();
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700103 if (boost::regex_match(targetStr, subResult, m_componentRegex))
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800104 {
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700105 for (size_t i = 1; i < m_componentRegex.mark_count(); i++)
Alexander Afanasyev36b84cf2014-02-17 19:34:18 -0800106 {
107 m_pseudoMatcher[i]->resetMatchResult();
108 m_pseudoMatcher[i]->setMatchResult(subResult[i]);
109 }
110 m_matchResult.push_back(name.get(offset));
111 return true;
112 }
113 }
114 else
115 {
116 throw RegexMatcher::Error("Non-exact component search is not supported yet!");
117 }
118
119 return false;
120}
121
122
123} // namespace ndn
124
125#endif // NDN_UTIL_REGEX_REGEX_COMPONENT_MATCHER_HPP