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 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 28 | RegexComponentMatcher(const std::string& expr, |
| 29 | ptr_lib::shared_ptr<RegexBackrefManager> backRefManager, |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 30 | bool exact = true); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 31 | |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 32 | virtual ~RegexComponentMatcher() {}; |
| 33 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 34 | virtual bool |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 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 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 42 | virtual void |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 43 | compile(); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 44 | |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 45 | private: |
| 46 | bool m_exact; |
| 47 | boost::regex m_componentRegex; |
| 48 | std::vector<ptr_lib::shared_ptr<RegexPseudoMatcher> > m_pseudoMatcher; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 49 | |
Yingdi Yu | 5e97420 | 2014-01-29 16:59:06 -0800 | [diff] [blame] | 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 |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 54 | RegexComponentMatcher::RegexComponentMatcher (const std::string& expr, |
| 55 | ptr_lib::shared_ptr<RegexBackrefManager> backRefManager, |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 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 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 65 | inline void |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 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 | |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 75 | for (size_t i = 1; i < m_componentRegex.mark_count(); i++) |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 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 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 81 | |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 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 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 93 | if ("" == m_expr) |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 94 | { |
| 95 | m_matchResult.push_back(name.get(offset)); |
| 96 | return true; |
| 97 | } |
| 98 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 99 | if (true == m_exact) |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 100 | { |
| 101 | boost::smatch subResult; |
| 102 | std::string targetStr = name.get(offset).toEscapedString(); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 103 | if (boost::regex_match(targetStr, subResult, m_componentRegex)) |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 104 | { |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 105 | for (size_t i = 1; i < m_componentRegex.mark_count(); i++) |
Alexander Afanasyev | 36b84cf | 2014-02-17 19:34:18 -0800 | [diff] [blame] | 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 |