blob: 8e397946c43f457d4c9179ac29089cd6d098a266 [file] [log] [blame]
Davide Pesavento19745dc2017-11-05 19:34:31 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2013-2017 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 * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/>
22 */
23
24#include "regex-component-set-matcher.hpp"
25#include "regex-component-matcher.hpp"
26
27namespace ndn {
28
29RegexComponentSetMatcher::RegexComponentSetMatcher(const std::string& expr,
30 shared_ptr<RegexBackrefManager> backrefManager)
31 : RegexMatcher(expr, EXPR_COMPONENT_SET, std::move(backrefManager))
32 , m_isInclusion(true)
33{
34 compile();
35}
36
37void
38RegexComponentSetMatcher::compile()
39{
40 if (m_expr.size() < 2)
41 BOOST_THROW_EXCEPTION(Error("Regexp compile error (cannot parse " + m_expr + ")"));
42
43 switch (m_expr[0]) {
44 case '<':
45 return compileSingleComponent();
46 case '[': {
47 size_t lastIndex = m_expr.size() - 1;
48 if (']' != m_expr[lastIndex])
49 BOOST_THROW_EXCEPTION(Error("Regexp compile error (no matching ']' in " + m_expr + ")"));
50
51 if ('^' == m_expr[1]) {
52 m_isInclusion = false;
53 compileMultipleComponents(2, lastIndex);
54 }
55 else
56 compileMultipleComponents(1, lastIndex);
57 break;
58 }
59 default:
60 BOOST_THROW_EXCEPTION(Error("Regexp compile error (cannot parse " + m_expr + ")"));
61 }
62}
63
64void
65RegexComponentSetMatcher::compileSingleComponent()
66{
67 size_t end = extractComponent(1);
68 if (m_expr.size() != end)
69 BOOST_THROW_EXCEPTION(Error("Component expr error " + m_expr));
70
71 m_components.insert(make_shared<RegexComponentMatcher>(m_expr.substr(1, end - 2),
72 m_backrefManager));
73}
74
75void
76RegexComponentSetMatcher::compileMultipleComponents(size_t start, size_t lastIndex)
77{
78 size_t index = start;
79 size_t tempIndex = start;
80
81 while (index < lastIndex) {
82 if ('<' != m_expr[index])
83 BOOST_THROW_EXCEPTION(Error("Component expr error " + m_expr));
84
85 tempIndex = index + 1;
86 index = extractComponent(tempIndex);
87 m_components.insert(make_shared<RegexComponentMatcher>(m_expr.substr(tempIndex, index - tempIndex - 1),
88 m_backrefManager));
89 }
90
91 if (index != lastIndex)
92 BOOST_THROW_EXCEPTION(Error("Not sufficient expr to parse " + m_expr));
93}
94
95bool
96RegexComponentSetMatcher::match(const Name& name, size_t offset, size_t len)
97{
98 // componentset only matches one component
99 if (len != 1)
100 return false;
101
102 bool isMatched = false;
103 for (const auto& comp : m_components) {
104 if (comp->match(name, offset, len)) {
105 isMatched = true;
106 break;
107 }
108 }
109
110 m_matchResult.clear();
111
112 if (m_isInclusion ? isMatched : !isMatched) {
113 m_matchResult.push_back(name.get(offset));
114 return true;
115 }
116 else
117 return false;
118}
119
120size_t
121RegexComponentSetMatcher::extractComponent(size_t index) const
122{
123 size_t lcount = 1;
124 size_t rcount = 0;
125
126 while (lcount > rcount) {
127 switch (m_expr[index]) {
128 case '<':
129 lcount++;
130 break;
131 case '>':
132 rcount++;
133 break;
134 case 0:
135 BOOST_THROW_EXCEPTION(Error("Square brackets mismatch"));
136 break;
137 }
138 index++;
139 }
140
141 return index;
142}
143
144} // namespace ndn