blob: c61fe625c66d54fc2d821b3c56d093868868a0b6 [file] [log] [blame]
Qiuhan Ding699725d2015-05-24 01:41:09 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2015 Regents of the University of California.
4 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
22#include "regex-component-matcher.hpp"
23
24namespace ndn {
25
26// Re: http://www.boost.org/users/history/version_1_56_0.html
27//
28// Breaking change: corrected behavior of basic_regex<>::mark_count() to match existing
29// documentation, basic_regex<>::subexpression(n) changed to match, see
30// https://svn.boost.org/trac/boost/ticket/9227
31static const size_t BOOST_REGEXP_MARK_COUNT_CORRECTION =
32#if BOOST_VERSION < 105600
33 1;
34#else
35 0;
36#endif
37
38RegexComponentMatcher::RegexComponentMatcher(const std::string& expr,
39 shared_ptr<RegexBackrefManager> backrefManager,
40 bool isExactMatch)
41 : RegexMatcher(expr, EXPR_COMPONENT, backrefManager)
42 , m_isExactMatch(isExactMatch)
43{
44 compile();
45}
46
47RegexComponentMatcher::~RegexComponentMatcher()
48{
49}
50
51bool
52RegexComponentMatcher::match(const Name& name, size_t offset, size_t len)
53{
54 m_matchResult.clear();
55
56 if (m_expr.empty()) {
57 m_matchResult.push_back(name.get(offset));
58 return true;
59 }
60
61 if (m_isExactMatch) {
62 boost::smatch subResult;
63 std::string targetStr = name.get(offset).toUri();
64 if (boost::regex_match(targetStr, subResult, m_componentRegex)) {
65 for (size_t i = 1;
66 i <= m_componentRegex.mark_count() - BOOST_REGEXP_MARK_COUNT_CORRECTION; i++) {
67 m_pseudoMatchers[i]->resetMatchResult();
68 m_pseudoMatchers[i]->setMatchResult(subResult[i]);
69 }
70 m_matchResult.push_back(name.get(offset));
71 return true;
72 }
73 }
74 else {
75 throw Error("Non-exact component search is not supported yet!");
76 }
77
78 return false;
79}
80
81void
82RegexComponentMatcher::compile()
83{
84 m_componentRegex = boost::regex(m_expr);
85
86 m_pseudoMatchers.clear();
87 m_pseudoMatchers.push_back(make_shared<RegexPseudoMatcher>());
88
89 for (size_t i = 1;
90 i <= m_componentRegex.mark_count() - BOOST_REGEXP_MARK_COUNT_CORRECTION; i++) {
91 auto pMatcher = make_shared<RegexPseudoMatcher>();
92 m_pseudoMatchers.push_back(pMatcher);
93 m_backrefManager->pushRef(static_pointer_cast<RegexMatcher>(pMatcher));
94 }
95}
96
97} // namespace ndn