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 | |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 8 | #ifndef NDN_UTIL_REGEX_REGEX_COMPONENT_MATCHER_HPP |
| 9 | #define NDN_UTIL_REGEX_REGEX_COMPONENT_MATCHER_HPP |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 10 | |
| 11 | #include <boost/regex.hpp> |
| 12 | |
| 13 | #include "regex-matcher.hpp" |
| 14 | #include "regex-pseudo-matcher.hpp" |
| 15 | |
| 16 | |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 17 | namespace ndn { |
| 18 | |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 19 | class RegexComponentMatcher : public RegexMatcher |
| 20 | { |
| 21 | public: |
| 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 | */ |
| 28 | RegexComponentMatcher(const std::string& expr, |
| 29 | ptr_lib::shared_ptr<RegexBackrefManager> backRefManager, |
| 30 | bool exact = true); |
| 31 | |
| 32 | virtual ~RegexComponentMatcher() {}; |
| 33 | |
| 34 | virtual bool |
| 35 | match(const Name & name, const int & offset, const int &len = 1); |
| 36 | |
| 37 | protected: |
| 38 | /** |
| 39 | * @brief Compile the regular expression to generate the more matchers when necessary |
| 40 | * @returns true if compiling succeeds |
| 41 | */ |
| 42 | virtual void |
| 43 | compile(); |
| 44 | |
| 45 | private: |
| 46 | bool m_exact; |
| 47 | boost::regex m_componentRegex; |
| 48 | std::vector<ptr_lib::shared_ptr<RegexPseudoMatcher> > m_pseudoMatcher; |
| 49 | |
| 50 | }; |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 51 | |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 52 | |
| 53 | inline |
| 54 | RegexComponentMatcher::RegexComponentMatcher (const std::string& expr, |
| 55 | ptr_lib::shared_ptr<RegexBackrefManager> backRefManager, |
| 56 | 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 | |
| 65 | inline void |
| 66 | RegexComponentMatcher::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 | |
| 75 | for (int i = 1; i < m_componentRegex.mark_count(); i++) |
| 76 | { |
| 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 | } |
| 81 | |
| 82 | |
| 83 | // _LOG_TRACE ("Exit RegexComponentMatcher::compile"); |
| 84 | } |
| 85 | |
| 86 | inline bool |
| 87 | RegexComponentMatcher::match (const Name & name, const int & offset, const int & len) |
| 88 | { |
| 89 | // _LOG_TRACE ("Enter RegexComponentMatcher::match "); |
| 90 | |
| 91 | m_matchResult.clear(); |
| 92 | |
| 93 | if("" == m_expr) |
| 94 | { |
| 95 | m_matchResult.push_back(name.get(offset)); |
| 96 | return true; |
| 97 | } |
| 98 | |
| 99 | if(true == m_exact) |
| 100 | { |
| 101 | boost::smatch subResult; |
| 102 | std::string targetStr = name.get(offset).toEscapedString(); |
| 103 | if(boost::regex_match(targetStr, subResult, m_componentRegex)) |
| 104 | { |
| 105 | for (int i = 1; i < m_componentRegex.mark_count(); i++) |
| 106 | { |
| 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 |