Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2014, Regents of the University of California. |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 7 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 8 | * |
| 9 | * This file licensed under New BSD License. See COPYING for detailed information about |
| 10 | * ndn-cxx library copyright, permissions, and redistribution restrictions. |
| 11 | */ |
| 12 | |
| 13 | #include "interest-filter.hpp" |
| 14 | |
| 15 | #include "util/regex/regex-pattern-list-matcher.hpp" |
| 16 | |
| 17 | namespace ndn { |
| 18 | |
| 19 | InterestFilter::InterestFilter(const Name& prefix, const std::string& regexFilter) |
| 20 | : m_prefix(prefix) |
| 21 | , m_regexFilter(ndn::make_shared<RegexPatternListMatcher>(regexFilter, |
| 22 | shared_ptr<RegexBackrefManager>())) |
| 23 | { |
| 24 | } |
| 25 | |
| 26 | bool |
| 27 | InterestFilter::doesMatch(const Name& name) const |
| 28 | { |
| 29 | if (name.size() < m_prefix.size()) |
| 30 | return false; |
| 31 | |
| 32 | if (hasRegexFilter()) { |
| 33 | // perform prefix match and regular expression match for the remaining components |
| 34 | bool isMatch = m_prefix.isPrefixOf(name); |
| 35 | |
| 36 | if (!isMatch) |
| 37 | return false; |
| 38 | |
| 39 | return m_regexFilter->match(name, m_prefix.size(), name.size() - m_prefix.size()); |
| 40 | } |
| 41 | else { |
| 42 | // perform just prefix match |
| 43 | |
| 44 | return m_prefix.isPrefixOf(name); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | std::ostream& |
| 49 | operator<<(std::ostream& os, const InterestFilter& filter) |
| 50 | { |
| 51 | os << filter.getPrefix(); |
| 52 | if (filter.hasRegexFilter()) { |
| 53 | os << "?regex=" << filter.getRegexFilter(); |
| 54 | } |
| 55 | return os; |
| 56 | } |
| 57 | |
| 58 | } // namespace ndn |