regex: modernize and simplify code
Change-Id: Ia4a3046d409ab1e5d8507da4d369dfee203f6256
diff --git a/src/util/regex/regex-backref-manager.hpp b/src/util/regex/regex-backref-manager.hpp
index be0f3dd..4e112d7 100644
--- a/src/util/regex/regex-backref-manager.hpp
+++ b/src/util/regex/regex-backref-manager.hpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2013-2016 Regents of the University of California.
+/*
+ * Copyright (c) 2013-2017 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
@@ -39,10 +39,16 @@
pushRef(const shared_ptr<RegexMatcher>& matcher);
void
- popRef();
+ popRef()
+ {
+ m_backrefs.pop_back();
+ }
size_t
- size() const;
+ size() const
+ {
+ return m_backrefs.size();
+ }
shared_ptr<RegexMatcher>
getBackref(size_t i) const;
@@ -51,28 +57,14 @@
std::vector<weak_ptr<RegexMatcher>> m_backrefs;
};
-
inline size_t
RegexBackrefManager::pushRef(const shared_ptr<RegexMatcher>& matcher)
{
- size_t last = m_backrefs.size();
- m_backrefs.push_back(matcher);
-
+ auto last = m_backrefs.size();
+ m_backrefs.emplace_back(matcher);
return last;
}
-inline void
-RegexBackrefManager::popRef()
-{
- m_backrefs.pop_back();
-}
-
-inline size_t
-RegexBackrefManager::size() const
-{
- return m_backrefs.size();
-}
-
inline shared_ptr<RegexMatcher>
RegexBackrefManager::getBackref(size_t i) const
{
diff --git a/src/util/regex/regex-backref-matcher.hpp b/src/util/regex/regex-backref-matcher.hpp
index d514572..947abc7 100644
--- a/src/util/regex/regex-backref-matcher.hpp
+++ b/src/util/regex/regex-backref-matcher.hpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2013-2015 Regents of the University of California.
+/*
+ * Copyright (c) 2013-2017 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
@@ -24,8 +24,6 @@
#ifndef NDN_UTIL_REGEX_REGEX_BACKREF_MATCHER_HPP
#define NDN_UTIL_REGEX_REGEX_BACKREF_MATCHER_HPP
-#include "../../common.hpp"
-
#include "regex-matcher.hpp"
namespace ndn {
@@ -35,11 +33,6 @@
public:
RegexBackrefMatcher(const std::string& expr, shared_ptr<RegexBackrefManager> backrefManager);
- virtual
- ~RegexBackrefMatcher()
- {
- }
-
void
lateCompile()
{
@@ -47,8 +40,8 @@
}
protected:
- virtual void
- compile();
+ void
+ compile() override;
};
} // namespace ndn
@@ -60,30 +53,25 @@
inline
RegexBackrefMatcher::RegexBackrefMatcher(const std::string& expr,
shared_ptr<RegexBackrefManager> backrefManager)
- : RegexMatcher(expr, EXPR_BACKREF, backrefManager)
+ : RegexMatcher(expr, EXPR_BACKREF, std::move(backrefManager))
{
- // compile();
}
inline void
RegexBackrefMatcher::compile()
{
if (m_expr.size() < 2)
- BOOST_THROW_EXCEPTION(RegexMatcher::Error("Unrecognized format: " + m_expr));
+ BOOST_THROW_EXCEPTION(Error("Unrecognized format: " + m_expr));
size_t lastIndex = m_expr.size() - 1;
if ('(' == m_expr[0] && ')' == m_expr[lastIndex]) {
- // m_backRefManager->pushRef(this);
-
- shared_ptr<RegexMatcher> matcher(new RegexPatternListMatcher(m_expr.substr(1, lastIndex - 1),
- m_backrefManager));
- m_matchers.push_back(matcher);
+ m_matchers.push_back(make_shared<RegexPatternListMatcher>(m_expr.substr(1, lastIndex - 1),
+ m_backrefManager));
}
else
- BOOST_THROW_EXCEPTION(RegexMatcher::Error("Unrecognized format: " + m_expr));
+ BOOST_THROW_EXCEPTION(Error("Unrecognized format: " + m_expr));
}
-
} // namespace ndn
#endif // NDN_UTIL_REGEX_REGEX_BACKREF_MATCHER_HPP
diff --git a/src/util/regex/regex-component-matcher.hpp b/src/util/regex/regex-component-matcher.hpp
index 08cefdb..3140375 100644
--- a/src/util/regex/regex-component-matcher.hpp
+++ b/src/util/regex/regex-component-matcher.hpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2013-2015 Regents of the University of California.
+/*
+ * Copyright (c) 2013-2017 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
@@ -24,11 +24,11 @@
#ifndef NDN_UTIL_REGEX_REGEX_COMPONENT_MATCHER_HPP
#define NDN_UTIL_REGEX_REGEX_COMPONENT_MATCHER_HPP
-#include <boost/regex.hpp>
-
#include "regex-matcher.hpp"
#include "regex-pseudo-matcher.hpp"
+#include <boost/regex.hpp>
+
namespace ndn {
class RegexComponentMatcher : public RegexMatcher
@@ -44,34 +44,24 @@
shared_ptr<RegexBackrefManager> backrefManager,
bool isExactMatch = true);
- virtual
- ~RegexComponentMatcher()
- {
- }
-
- virtual bool
- match(const Name& name, size_t offset, size_t len = 1);
+ bool
+ match(const Name& name, size_t offset, size_t len = 1) override;
protected:
- /**
- * @brief Compile the regular expression to generate the more matchers when necessary
- */
- virtual void
- compile();
+ void
+ compile() override;
private:
bool m_isExactMatch;
boost::regex m_componentRegex;
- std::vector<shared_ptr<RegexPseudoMatcher> > m_pseudoMatchers;
-
+ std::vector<shared_ptr<RegexPseudoMatcher>> m_pseudoMatchers;
};
-
inline
RegexComponentMatcher::RegexComponentMatcher(const std::string& expr,
shared_ptr<RegexBackrefManager> backrefManager,
bool isExactMatch)
- : RegexMatcher(expr, EXPR_COMPONENT, backrefManager)
+ : RegexMatcher(expr, EXPR_COMPONENT, std::move(backrefManager))
, m_isExactMatch(isExactMatch)
{
compile();
@@ -82,11 +72,12 @@
// Breaking change: corrected behavior of basic_regex<>::mark_count() to match existing
// documentation, basic_regex<>::subexpression(n) changed to match, see
// https://svn.boost.org/trac/boost/ticket/9227
-static const size_t BOOST_REGEXP_MARK_COUNT_CORRECTION =
+//
+static constexpr size_t BOOST_REGEXP_MARK_COUNT_CORRECTION =
#if BOOST_VERSION < 105600
- 1;
+ 1;
#else
- 0;
+ 0;
#endif
inline void
@@ -97,13 +88,10 @@
m_pseudoMatchers.clear();
m_pseudoMatchers.push_back(make_shared<RegexPseudoMatcher>());
- for (size_t i = 1;
- i <= m_componentRegex.mark_count() - BOOST_REGEXP_MARK_COUNT_CORRECTION; i++)
- {
- shared_ptr<RegexPseudoMatcher> pMatcher = make_shared<RegexPseudoMatcher>();
- m_pseudoMatchers.push_back(pMatcher);
- m_backrefManager->pushRef(static_pointer_cast<RegexMatcher>(pMatcher));
- }
+ for (size_t i = 1; i <= m_componentRegex.mark_count() - BOOST_REGEXP_MARK_COUNT_CORRECTION; i++) {
+ m_pseudoMatchers.push_back(make_shared<RegexPseudoMatcher>());
+ m_backrefManager->pushRef(m_pseudoMatchers.back());
+ }
}
inline bool
@@ -111,38 +99,28 @@
{
m_matchResult.clear();
- if (m_expr.empty())
- {
- m_matchResult.push_back(name.get(offset));
- return true;
- }
+ if (m_expr.empty()) {
+ m_matchResult.push_back(name.get(offset));
+ return true;
+ }
- if (m_isExactMatch)
- {
- boost::smatch subResult;
- std::string targetStr = name.get(offset).toUri();
- if (boost::regex_match(targetStr, subResult, m_componentRegex))
- {
- for (size_t i = 1;
- i <= m_componentRegex.mark_count() - BOOST_REGEXP_MARK_COUNT_CORRECTION; i++)
- {
- m_pseudoMatchers[i]->resetMatchResult();
- m_pseudoMatchers[i]->setMatchResult(subResult[i]);
- }
- m_matchResult.push_back(name.get(offset));
- return true;
- }
+ if (!m_isExactMatch)
+ BOOST_THROW_EXCEPTION(Error("Non-exact component search is not supported yet"));
+
+ boost::smatch subResult;
+ std::string targetStr = name.get(offset).toUri();
+ if (boost::regex_match(targetStr, subResult, m_componentRegex)) {
+ for (size_t i = 1; i <= m_componentRegex.mark_count() - BOOST_REGEXP_MARK_COUNT_CORRECTION; i++) {
+ m_pseudoMatchers[i]->resetMatchResult();
+ m_pseudoMatchers[i]->setMatchResult(subResult[i]);
}
- else
- {
- BOOST_THROW_EXCEPTION(RegexMatcher::Error("Non-exact component search is not supported "
- "yet"));
- }
+ m_matchResult.push_back(name.get(offset));
+ return true;
+ }
return false;
}
-
} // namespace ndn
#endif // NDN_UTIL_REGEX_REGEX_COMPONENT_MATCHER_HPP
diff --git a/src/util/regex/regex-component-set-matcher.hpp b/src/util/regex/regex-component-set-matcher.hpp
index 7bf5a5e..46d8527 100644
--- a/src/util/regex/regex-component-set-matcher.hpp
+++ b/src/util/regex/regex-component-set-matcher.hpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2013-2015 Regents of the University of California.
+/*
+ * Copyright (c) 2013-2017 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
@@ -21,13 +21,11 @@
* @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/>
*/
-#ifndef NDN_UTIL_REGEX_COMPONENT_SET_MATCHER_HPP
-#define NDN_UTIL_REGEX_COMPONENT_SET_MATCHER_HPP
+#ifndef NDN_UTIL_REGEX_REGEX_COMPONENT_SET_MATCHER_HPP
+#define NDN_UTIL_REGEX_REGEX_COMPONENT_SET_MATCHER_HPP
-#include "../../common.hpp"
-
-#include "regex-matcher.hpp"
#include "regex-component-matcher.hpp"
+#include "regex-matcher.hpp"
#include <set>
@@ -43,66 +41,53 @@
*/
RegexComponentSetMatcher(const std::string& expr, shared_ptr<RegexBackrefManager> backrefManager);
- virtual
- ~RegexComponentSetMatcher();
-
- virtual bool
- match(const Name& name, size_t offset, size_t len = 1);
+ bool
+ match(const Name& name, size_t offset, size_t len = 1) override;
protected:
/**
* @brief Compile the regular expression to generate the more matchers when necessary
*/
- virtual void
- compile();
+ void
+ compile() override;
private:
- size_t
- extractComponent(size_t index);
-
void
compileSingleComponent();
void
compileMultipleComponents(size_t start, size_t lastIndex);
+ size_t
+ extractComponent(size_t index) const;
+
private:
- typedef std::set<shared_ptr<RegexComponentMatcher> > ComponentsSet;
- ComponentsSet m_components;
+ std::set<shared_ptr<RegexComponentMatcher>> m_components;
bool m_isInclusion;
};
-
inline
RegexComponentSetMatcher::RegexComponentSetMatcher(const std::string& expr,
shared_ptr<RegexBackrefManager> backrefManager)
- : RegexMatcher(expr, EXPR_COMPONENT_SET, backrefManager)
+ : RegexMatcher(expr, EXPR_COMPONENT_SET, std::move(backrefManager))
, m_isInclusion(true)
{
compile();
}
-inline
-RegexComponentSetMatcher::~RegexComponentSetMatcher()
-{
-}
-
inline void
RegexComponentSetMatcher::compile()
{
if (m_expr.size() < 2)
- BOOST_THROW_EXCEPTION(RegexMatcher::Error("Regexp compile error (cannot parse " +
- m_expr + ")"));
+ BOOST_THROW_EXCEPTION(Error("Regexp compile error (cannot parse " + m_expr + ")"));
switch (m_expr[0]) {
- case '<':
- return compileSingleComponent();
- case '[':
- {
+ case '<':
+ return compileSingleComponent();
+ case '[': {
size_t lastIndex = m_expr.size() - 1;
if (']' != m_expr[lastIndex])
- BOOST_THROW_EXCEPTION(RegexMatcher::Error("Regexp compile error (no matching ']' in " +
- m_expr + ")"));
+ BOOST_THROW_EXCEPTION(Error("Regexp compile error (no matching ']' in " + m_expr + ")"));
if ('^' == m_expr[1]) {
m_isInclusion = false;
@@ -112,9 +97,8 @@
compileMultipleComponents(1, lastIndex);
break;
}
- default:
- BOOST_THROW_EXCEPTION(RegexMatcher::Error("Regexp compile error (cannot parse " +
- m_expr + ")"));
+ default:
+ BOOST_THROW_EXCEPTION(Error("Regexp compile error (cannot parse " + m_expr + ")"));
}
}
@@ -122,18 +106,11 @@
RegexComponentSetMatcher::compileSingleComponent()
{
size_t end = extractComponent(1);
-
if (m_expr.size() != end)
- {
- BOOST_THROW_EXCEPTION(RegexMatcher::Error("Component expr error " + m_expr));
- }
- else
- {
- shared_ptr<RegexComponentMatcher> component =
- make_shared<RegexComponentMatcher>(m_expr.substr(1, end - 2), m_backrefManager);
+ BOOST_THROW_EXCEPTION(Error("Component expr error " + m_expr));
- m_components.insert(component);
- }
+ m_components.insert(make_shared<RegexComponentMatcher>(m_expr.substr(1, end - 2),
+ m_backrefManager));
}
inline void
@@ -144,77 +121,62 @@
while (index < lastIndex) {
if ('<' != m_expr[index])
- BOOST_THROW_EXCEPTION(RegexMatcher::Error("Component expr error " + m_expr));
+ BOOST_THROW_EXCEPTION(Error("Component expr error " + m_expr));
tempIndex = index + 1;
index = extractComponent(tempIndex);
-
- shared_ptr<RegexComponentMatcher> component =
- make_shared<RegexComponentMatcher>(m_expr.substr(tempIndex, index - tempIndex - 1),
- m_backrefManager);
-
- m_components.insert(component);
+ m_components.insert(make_shared<RegexComponentMatcher>(m_expr.substr(tempIndex, index - tempIndex - 1),
+ m_backrefManager));
}
if (index != lastIndex)
- BOOST_THROW_EXCEPTION(RegexMatcher::Error("Not sufficient expr to parse " + m_expr));
+ BOOST_THROW_EXCEPTION(Error("Not sufficient expr to parse " + m_expr));
}
inline bool
RegexComponentSetMatcher::match(const Name& name, size_t offset, size_t len)
{
- bool isMatched = false;
-
- /* componentset only matches one component */
+ // componentset only matches one component
if (len != 1)
- {
- return false;
- }
+ return false;
- for (ComponentsSet::iterator it = m_components.begin();
- it != m_components.end();
- ++it)
- {
- if ((*it)->match(name, offset, len))
- {
- isMatched = true;
- break;
- }
+ bool isMatched = false;
+ for (const auto& comp : m_components) {
+ if (comp->match(name, offset, len)) {
+ isMatched = true;
+ break;
}
+ }
m_matchResult.clear();
- if (m_isInclusion ? isMatched : !isMatched)
- {
- m_matchResult.push_back(name.get(offset));
- return true;
- }
+ if (m_isInclusion ? isMatched : !isMatched) {
+ m_matchResult.push_back(name.get(offset));
+ return true;
+ }
else
return false;
}
inline size_t
-RegexComponentSetMatcher::extractComponent(size_t index)
+RegexComponentSetMatcher::extractComponent(size_t index) const
{
size_t lcount = 1;
size_t rcount = 0;
while (lcount > rcount) {
switch (m_expr[index]) {
- case '<':
- lcount++;
- break;
-
- case '>':
- rcount++;
- break;
-
- case 0:
- BOOST_THROW_EXCEPTION(RegexMatcher::Error("Error: square brackets mismatch"));
- break;
+ case '<':
+ lcount++;
+ break;
+ case '>':
+ rcount++;
+ break;
+ case 0:
+ BOOST_THROW_EXCEPTION(Error("Square brackets mismatch"));
+ break;
}
index++;
-
}
return index;
@@ -222,4 +184,4 @@
} // namespace ndn
-#endif // NDN_UTIL_REGEX_COMPONENT_SET_MATCHER_HPP
+#endif // NDN_UTIL_REGEX_REGEX_COMPONENT_SET_MATCHER_HPP
diff --git a/src/util/regex/regex-matcher.hpp b/src/util/regex/regex-matcher.hpp
index 9b5d26f..5916565 100644
--- a/src/util/regex/regex-matcher.hpp
+++ b/src/util/regex/regex-matcher.hpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2013-2014 Regents of the University of California.
+/*
+ * Copyright (c) 2013-2017 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
@@ -21,16 +21,14 @@
* @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/>
*/
-#ifndef NDN_UTIL_REGEX_REGEX_MATCHER_H
-#define NDN_UTIL_REGEX_REGEX_MATCHER_H
+#ifndef NDN_UTIL_REGEX_REGEX_MATCHER_HPP
+#define NDN_UTIL_REGEX_REGEX_MATCHER_HPP
-#include "../../common.hpp"
+#include "regex-backref-manager.hpp"
#include "../../name.hpp"
namespace ndn {
-class RegexBackrefManager;
-
class RegexMatcher
{
public:
@@ -54,12 +52,11 @@
EXPR_PSEUDO
};
- RegexMatcher(const std::string& expr,
- const RegexExprType& type,
- shared_ptr<RegexBackrefManager> backrefManager = shared_ptr<RegexBackrefManager>());
+ RegexMatcher(const std::string& expr, const RegexExprType& type,
+ shared_ptr<RegexBackrefManager> backrefManager = nullptr);
virtual
- ~RegexMatcher();
+ ~RegexMatcher() = default;
virtual bool
match(const Name& name, size_t offset, size_t len);
@@ -91,89 +88,65 @@
bool
recursiveMatch(size_t matcherNo, const Name& name, size_t offset, size_t len);
-
protected:
const std::string m_expr;
const RegexExprType m_type;
shared_ptr<RegexBackrefManager> m_backrefManager;
- std::vector<shared_ptr<RegexMatcher> > m_matchers;
+ std::vector<shared_ptr<RegexMatcher>> m_matchers;
std::vector<name::Component> m_matchResult;
};
inline std::ostream&
-operator<<(std::ostream& os, const RegexMatcher& regex)
+operator<<(std::ostream& os, const RegexMatcher& rm)
{
- os << regex.getExpr();
- return os;
+ return os << rm.getExpr();
}
-} // namespace ndn
-
-#include "regex-backref-manager.hpp"
-
-namespace ndn {
-
inline
-RegexMatcher::RegexMatcher(const std::string& expr,
- const RegexExprType& type,
+RegexMatcher::RegexMatcher(const std::string& expr, const RegexExprType& type,
shared_ptr<RegexBackrefManager> backrefManager)
: m_expr(expr)
, m_type(type)
- , m_backrefManager(backrefManager)
{
- if (!static_cast<bool>(m_backrefManager))
+ if (backrefManager)
+ m_backrefManager = std::move(backrefManager);
+ else
m_backrefManager = make_shared<RegexBackrefManager>();
}
-inline
-RegexMatcher::~RegexMatcher()
-{
-}
-
inline bool
RegexMatcher::match(const Name& name, size_t offset, size_t len)
{
- bool result = false;
-
m_matchResult.clear();
- if (recursiveMatch(0, name, offset, len))
- {
- for (size_t i = offset; i < offset + len ; i++)
- m_matchResult.push_back(name.get(i));
- result = true;
- }
- else
- {
- result = false;
- }
+ if (recursiveMatch(0, name, offset, len)) {
+ for (size_t i = offset; i < offset + len; i++)
+ m_matchResult.push_back(name.get(i));
+ return true;
+ }
- return result;
+ return false;
}
inline bool
RegexMatcher::recursiveMatch(size_t matcherNo, const Name& name, size_t offset, size_t len)
{
- ssize_t tried = len;
-
if (matcherNo >= m_matchers.size())
- return (len == 0);
+ return len == 0;
- shared_ptr<RegexMatcher> matcher = m_matchers[matcherNo];
+ ssize_t tried = len;
+ auto matcher = m_matchers[matcherNo];
- while (tried >= 0)
- {
- if (matcher->match(name, offset, tried) &&
- recursiveMatch(matcherNo + 1, name, offset + tried, len - tried))
- return true;
- tried--;
- }
+ while (tried >= 0) {
+ if (matcher->match(name, offset, tried) &&
+ recursiveMatch(matcherNo + 1, name, offset + tried, len - tried))
+ return true;
+ tried--;
+ }
return false;
}
-
} // namespace ndn
-
-#endif // NDN_UTIL_REGEX_REGEX_MATCHER_H
+#endif // NDN_UTIL_REGEX_REGEX_MATCHER_HPP
diff --git a/src/util/regex/regex-pattern-list-matcher.hpp b/src/util/regex/regex-pattern-list-matcher.hpp
index ccac4b8..7bf0366 100644
--- a/src/util/regex/regex-pattern-list-matcher.hpp
+++ b/src/util/regex/regex-pattern-list-matcher.hpp
@@ -24,14 +24,10 @@
#ifndef NDN_UTIL_REGEX_REGEX_PATTERN_LIST_MATCHER_HPP
#define NDN_UTIL_REGEX_REGEX_PATTERN_LIST_MATCHER_HPP
-#include "../../common.hpp"
-
#include "regex-matcher.hpp"
namespace ndn {
-class RegexBackrefManager;
-
class RegexPatternListMatcher : public RegexMatcher
{
public:
@@ -45,24 +41,24 @@
bool
extractPattern(size_t index, size_t* next);
- int
+ size_t
extractSubPattern(const char left, const char right, size_t index);
- int
+ size_t
extractRepetition(size_t index);
};
} // namespace ndn
-#include "regex-repeat-matcher.hpp"
#include "regex-backref-matcher.hpp"
+#include "regex-repeat-matcher.hpp"
namespace ndn {
inline
RegexPatternListMatcher::RegexPatternListMatcher(const std::string& expr,
shared_ptr<RegexBackrefManager> backrefManager)
- : RegexMatcher(expr, EXPR_PATTERN_LIST, backrefManager)
+ : RegexMatcher(expr, EXPR_PATTERN_LIST, std::move(backrefManager))
{
compile();
}
@@ -78,7 +74,7 @@
subHead = index;
if (!extractPattern(subHead, &index))
- BOOST_THROW_EXCEPTION(RegexMatcher::Error("Compile error"));
+ BOOST_THROW_EXCEPTION(Error("Compile error"));
}
}
@@ -96,12 +92,11 @@
indicator = index;
end = extractRepetition(index);
if (indicator == end) {
- shared_ptr<RegexMatcher> matcher =
- make_shared<RegexBackrefMatcher>(m_expr.substr(start, end - start), m_backrefManager);
+ auto matcher = make_shared<RegexBackrefMatcher>(m_expr.substr(start, end - start),
+ m_backrefManager);
m_backrefManager->pushRef(matcher);
- dynamic_pointer_cast<RegexBackrefMatcher>(matcher)->lateCompile();
-
- m_matchers.push_back(matcher);
+ matcher->lateCompile();
+ m_matchers.push_back(std::move(matcher));
}
else
m_matchers.push_back(make_shared<RegexRepeatMatcher>(m_expr.substr(start, end - start),
@@ -127,24 +122,22 @@
break;
default:
- BOOST_THROW_EXCEPTION(RegexMatcher::Error("Unexpected syntax"));
+ BOOST_THROW_EXCEPTION(Error("Unexpected syntax"));
}
*next = end;
-
return true;
}
-inline int
+inline size_t
RegexPatternListMatcher::extractSubPattern(const char left, const char right, size_t index)
{
size_t lcount = 1;
size_t rcount = 0;
while (lcount > rcount) {
-
if (index >= m_expr.size())
- BOOST_THROW_EXCEPTION(RegexMatcher::Error("Parenthesis mismatch"));
+ BOOST_THROW_EXCEPTION(Error("Parenthesis mismatch"));
if (left == m_expr[index])
lcount++;
@@ -154,10 +147,11 @@
index++;
}
+
return index;
}
-inline int
+inline size_t
RegexPatternListMatcher::extractRepetition(size_t index)
{
size_t exprSize = m_expr.size();
@@ -176,7 +170,7 @@
break;
}
if (index == exprSize)
- BOOST_THROW_EXCEPTION(RegexMatcher::Error("Missing right brace bracket"));
+ BOOST_THROW_EXCEPTION(Error("Missing right brace bracket"));
else
return ++index;
}
diff --git a/src/util/regex/regex-pseudo-matcher.hpp b/src/util/regex/regex-pseudo-matcher.hpp
index 990840a..a2c19a0 100644
--- a/src/util/regex/regex-pseudo-matcher.hpp
+++ b/src/util/regex/regex-pseudo-matcher.hpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2013-2014 Regents of the University of California.
+/*
+ * Copyright (c) 2013-2017 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
@@ -24,7 +24,6 @@
#ifndef NDN_UTIL_REGEX_REGEX_PSEUDO_MATCHER_HPP
#define NDN_UTIL_REGEX_REGEX_PSEUDO_MATCHER_HPP
-#include "../../common.hpp"
#include "regex-matcher.hpp"
namespace ndn {
@@ -34,21 +33,15 @@
public:
RegexPseudoMatcher();
- virtual
- ~RegexPseudoMatcher()
- {
- }
-
- virtual void
- compile()
- {
- }
-
void
setMatchResult(const std::string& str);
void
resetMatchResult();
+
+protected:
+ void
+ compile() override;
};
inline
@@ -60,8 +53,7 @@
inline void
RegexPseudoMatcher::setMatchResult(const std::string& str)
{
- m_matchResult.push_back(name::Component(reinterpret_cast<const uint8_t*>(str.c_str()),
- str.size()));
+ m_matchResult.push_back(name::Component(reinterpret_cast<const uint8_t*>(str.data()), str.size()));
}
inline void
@@ -70,6 +62,10 @@
m_matchResult.clear();
}
+inline void
+RegexPseudoMatcher::compile()
+{
+}
} // namespace ndn
diff --git a/src/util/regex/regex-repeat-matcher.hpp b/src/util/regex/regex-repeat-matcher.hpp
index 3baaa46..86af906 100644
--- a/src/util/regex/regex-repeat-matcher.hpp
+++ b/src/util/regex/regex-repeat-matcher.hpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2013-2015 Regents of the University of California.
+/*
+ * Copyright (c) 2013-2017 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
@@ -24,10 +24,6 @@
#ifndef NDN_UTIL_REGEX_REGEX_REPEAT_MATCHER_HPP
#define NDN_UTIL_REGEX_REGEX_REPEAT_MATCHER_HPP
-#include "../../common.hpp"
-
-#include <boost/regex.hpp>
-
#include "regex-matcher.hpp"
namespace ndn {
@@ -39,29 +35,19 @@
shared_ptr<RegexBackrefManager> backrefManager,
size_t indicator);
- virtual
- ~RegexRepeatMatcher()
- {
- }
-
- virtual bool
- match(const Name& name, size_t offset, size_t len);
+ bool
+ match(const Name& name, size_t offset, size_t len) override;
protected:
- /**
- * @brief Compile the regular expression to generate the more matchers when necessary
- */
- virtual void
- compile();
+ void
+ compile() override;
private:
bool
parseRepetition();
bool
- recursiveMatch(size_t repeat,
- const Name& name,
- size_t offset, size_t len);
+ recursiveMatch(size_t repeat, const Name& name, size_t offset, size_t len);
private:
size_t m_indicator;
@@ -80,7 +66,7 @@
RegexRepeatMatcher::RegexRepeatMatcher(const std::string& expr,
shared_ptr<RegexBackrefManager> backrefManager,
size_t indicator)
- : RegexMatcher(expr, EXPR_REPEAT_PATTERN, backrefManager)
+ : RegexMatcher(expr, EXPR_REPEAT_PATTERN, std::move(backrefManager))
, m_indicator(indicator)
{
compile();
@@ -89,18 +75,16 @@
inline void
RegexRepeatMatcher::compile()
{
- shared_ptr<RegexMatcher> matcher;
-
if ('(' == m_expr[0]) {
- matcher = make_shared<RegexBackrefMatcher>(m_expr.substr(0, m_indicator), m_backrefManager);
+ auto matcher = make_shared<RegexBackrefMatcher>(m_expr.substr(0, m_indicator), m_backrefManager);
m_backrefManager->pushRef(matcher);
- dynamic_pointer_cast<RegexBackrefMatcher>(matcher)->lateCompile();
+ matcher->lateCompile();
+ m_matchers.push_back(std::move(matcher));
}
- else{
- matcher = make_shared<RegexComponentSetMatcher>(m_expr.substr(0, m_indicator),
- m_backrefManager);
+ else {
+ m_matchers.push_back(make_shared<RegexComponentSetMatcher>(m_expr.substr(0, m_indicator),
+ m_backrefManager));
}
- m_matchers.push_back(matcher);
parseRepetition();
}
@@ -114,66 +98,65 @@
if (exprSize == m_indicator) {
m_repeatMin = 1;
m_repeatMax = 1;
-
return true;
}
- else {
- if (exprSize == (m_indicator + 1)) {
- if ('?' == m_expr[m_indicator]) {
- m_repeatMin = 0;
- m_repeatMax = 1;
- return true;
- }
- if ('+' == m_expr[m_indicator]) {
- m_repeatMin = 1;
- m_repeatMax = MAX_REPETITIONS;
- return true;
- }
- if ('*' == m_expr[m_indicator]) {
- m_repeatMin = 0;
- m_repeatMax = MAX_REPETITIONS;
- return true;
- }
+
+ if (exprSize == (m_indicator + 1)) {
+ if ('?' == m_expr[m_indicator]) {
+ m_repeatMin = 0;
+ m_repeatMax = 1;
+ return true;
}
- else {
- std::string repeatStruct = m_expr.substr(m_indicator, exprSize - m_indicator);
- size_t rsSize = repeatStruct.size();
- size_t min = 0;
- size_t max = 0;
-
- if (boost::regex_match(repeatStruct, boost::regex("\\{[0-9]+,[0-9]+\\}"))) {
- size_t separator = repeatStruct.find_first_of(',', 0);
- min = atoi(repeatStruct.substr(1, separator - 1).c_str());
- max = atoi(repeatStruct.substr(separator + 1, rsSize - separator - 2).c_str());
- }
- else if (boost::regex_match(repeatStruct, boost::regex("\\{,[0-9]+\\}"))) {
- size_t separator = repeatStruct.find_first_of(',', 0);
- min = 0;
- max = atoi(repeatStruct.substr(separator + 1, rsSize - separator - 2).c_str());
- }
- else if (boost::regex_match(repeatStruct, boost::regex("\\{[0-9]+,\\}"))) {
- size_t separator = repeatStruct.find_first_of(',', 0);
- min = atoi(repeatStruct.substr(1, separator).c_str());
- max = MAX_REPETITIONS;
- }
- else if (boost::regex_match(repeatStruct, boost::regex("\\{[0-9]+\\}"))) {
- min = atoi(repeatStruct.substr(1, rsSize - 1).c_str());
- max = min;
- }
- else
- BOOST_THROW_EXCEPTION(RegexMatcher::Error(std::string("Error: RegexRepeatMatcher.ParseRepetition():")
- + " Unrecognized format "+ m_expr));
-
- if (min > MAX_REPETITIONS || max > MAX_REPETITIONS || min > max)
- BOOST_THROW_EXCEPTION(RegexMatcher::Error(std::string("Error: RegexRepeatMatcher.ParseRepetition():")
- + " Wrong number " + m_expr));
-
- m_repeatMin = min;
- m_repeatMax = max;
-
+ if ('+' == m_expr[m_indicator]) {
+ m_repeatMin = 1;
+ m_repeatMax = MAX_REPETITIONS;
+ return true;
+ }
+ if ('*' == m_expr[m_indicator]) {
+ m_repeatMin = 0;
+ m_repeatMax = MAX_REPETITIONS;
return true;
}
}
+ else {
+ std::string repeatStruct = m_expr.substr(m_indicator, exprSize - m_indicator);
+ size_t rsSize = repeatStruct.size();
+ size_t min = 0;
+ size_t max = 0;
+
+ if (boost::regex_match(repeatStruct, boost::regex("\\{[0-9]+,[0-9]+\\}"))) {
+ size_t separator = repeatStruct.find_first_of(',', 0);
+ min = std::atoi(repeatStruct.substr(1, separator - 1).data());
+ max = std::atoi(repeatStruct.substr(separator + 1, rsSize - separator - 2).data());
+ }
+ else if (boost::regex_match(repeatStruct, boost::regex("\\{,[0-9]+\\}"))) {
+ size_t separator = repeatStruct.find_first_of(',', 0);
+ min = 0;
+ max = std::atoi(repeatStruct.substr(separator + 1, rsSize - separator - 2).data());
+ }
+ else if (boost::regex_match(repeatStruct, boost::regex("\\{[0-9]+,\\}"))) {
+ size_t separator = repeatStruct.find_first_of(',', 0);
+ min = std::atoi(repeatStruct.substr(1, separator).data());
+ max = MAX_REPETITIONS;
+ }
+ else if (boost::regex_match(repeatStruct, boost::regex("\\{[0-9]+\\}"))) {
+ min = std::atoi(repeatStruct.substr(1, rsSize - 1).data());
+ max = min;
+ }
+ else
+ BOOST_THROW_EXCEPTION(Error(std::string("Error: RegexRepeatMatcher.ParseRepetition():")
+ + " Unrecognized format " + m_expr));
+
+ if (min > MAX_REPETITIONS || max > MAX_REPETITIONS || min > max)
+ BOOST_THROW_EXCEPTION(Error(std::string("Error: RegexRepeatMatcher.ParseRepetition():")
+ + " Wrong number " + m_expr));
+
+ m_repeatMin = min;
+ m_repeatMax = max;
+
+ return true;
+ }
+
return false;
}
@@ -182,53 +165,47 @@
{
m_matchResult.clear();
- if (0 == m_repeatMin)
- if (0 == len)
+ if (m_repeatMin == 0)
+ if (len == 0)
return true;
- if (recursiveMatch(0, name, offset, len))
- {
- for (size_t i = offset; i < offset + len; i++)
- m_matchResult.push_back(name.get(i));
- return true;
- }
- else
- return false;
+ if (recursiveMatch(0, name, offset, len)) {
+ for (size_t i = offset; i < offset + len; i++)
+ m_matchResult.push_back(name.get(i));
+ return true;
+ }
+
+ return false;
}
inline bool
RegexRepeatMatcher::recursiveMatch(size_t repeat, const Name& name, size_t offset, size_t len)
{
ssize_t tried = len;
- shared_ptr<RegexMatcher> matcher = m_matchers[0];
- if (0 < len && repeat >= m_repeatMax)
- {
- return false;
- }
+ if (0 < len && repeat >= m_repeatMax) {
+ return false;
+ }
- if (0 == len && repeat < m_repeatMin)
- {
- return false;
- }
+ if (0 == len && repeat < m_repeatMin) {
+ return false;
+ }
- if (0 == len && repeat >= m_repeatMin)
- {
+ if (0 == len && repeat >= m_repeatMin) {
+ return true;
+ }
+
+ auto matcher = m_matchers[0];
+ while (tried >= 0) {
+ if (matcher->match(name, offset, tried) &&
+ recursiveMatch(repeat + 1, name, offset + tried, len - tried))
return true;
- }
-
- while (tried >= 0)
- {
- if (matcher->match(name, offset, tried) &&
- recursiveMatch(repeat + 1, name, offset + tried, len - tried))
- return true;
- tried--;
- }
+ tried--;
+ }
return false;
}
-
} // namespace ndn
#endif // NDN_UTIL_REGEX_REGEX_REPEAT_MATCHER_HPP
diff --git a/src/util/regex/regex-top-matcher.cpp b/src/util/regex/regex-top-matcher.cpp
index d9ff669..0c2ac9a 100644
--- a/src/util/regex/regex-top-matcher.cpp
+++ b/src/util/regex/regex-top-matcher.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2013-2015 Regents of the University of California.
+/*
+ * Copyright (c) 2013-2017 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
@@ -40,15 +40,9 @@
compile();
}
-RegexTopMatcher::~RegexTopMatcher()
-{
-}
-
void
RegexTopMatcher::compile()
{
- std::string errMsg = "Error: RegexTopMatcher.Compile(): ";
-
std::string expr = m_expr;
if ('$' != expr[expr.size() - 1])
@@ -57,18 +51,14 @@
expr = expr.substr(0, expr.size() - 1);
if ('^' != expr[0]) {
- m_secondaryMatcher = make_shared<RegexPatternListMatcher>(
- "<.*>*" + expr,
- m_secondaryBackrefManager);
+ m_secondaryMatcher = make_shared<RegexPatternListMatcher>("<.*>*" + expr,
+ m_secondaryBackrefManager);
}
else {
expr = expr.substr(1, expr.size() - 1);
}
- // On OSX 10.9, boost, and C++03 the following doesn't work without ndn::
- // because the argument-dependent lookup prefers STL to boost
- m_primaryMatcher = ndn::make_shared<RegexPatternListMatcher>(expr,
- m_primaryBackrefManager);
+ m_primaryMatcher = make_shared<RegexPatternListMatcher>(expr, m_primaryBackrefManager);
}
bool
@@ -78,21 +68,18 @@
m_matchResult.clear();
- if (m_primaryMatcher->match(name, 0, name.size()))
- {
- m_matchResult = m_primaryMatcher->getMatchResult();
+ if (m_primaryMatcher->match(name, 0, name.size())) {
+ m_matchResult = m_primaryMatcher->getMatchResult();
+ return true;
+ }
+ else {
+ if (m_secondaryMatcher != nullptr && m_secondaryMatcher->match(name, 0, name.size())) {
+ m_matchResult = m_secondaryMatcher->getMatchResult();
+ m_isSecondaryUsed = true;
return true;
}
- else
- {
- if (static_cast<bool>(m_secondaryMatcher) && m_secondaryMatcher->match(name, 0, name.size()))
- {
- m_matchResult = m_secondaryMatcher->getMatchResult();
- m_isSecondaryUsed = true;
- return true;
- }
- return false;
- }
+ return false;
+ }
}
bool
@@ -104,51 +91,37 @@
Name
RegexTopMatcher::expand(const std::string& expandStr)
{
- Name result;
-
- shared_ptr<RegexBackrefManager> backrefManager =
- (m_isSecondaryUsed ? m_secondaryBackrefManager : m_primaryBackrefManager);
-
+ auto backrefManager = m_isSecondaryUsed ? m_secondaryBackrefManager : m_primaryBackrefManager;
size_t backrefNo = backrefManager->size();
std::string expand;
-
if (!expandStr.empty())
expand = expandStr;
else
expand = m_expand;
+ Name result;
size_t offset = 0;
- while (offset < expand.size())
- {
- std::string item = getItemFromExpand(expand, offset);
- if (item[0] == '<')
- {
- result.append(item.substr(1, item.size() - 2));
- }
- if (item[0] == '\\')
- {
- size_t index = boost::lexical_cast<size_t>(item.substr(1, item.size() - 1));
-
- if (0 == index) {
- std::vector<name::Component>::iterator it = m_matchResult.begin();
- std::vector<name::Component>::iterator end = m_matchResult.end();
- for (; it != end; it++)
- result.append(*it);
- }
- else if (index <= backrefNo)
- {
- std::vector<name::Component>::const_iterator it =
- backrefManager->getBackref(index - 1)->getMatchResult().begin();
- std::vector<name::Component>::const_iterator end =
- backrefManager->getBackref(index - 1)->getMatchResult().end();
- for (; it != end; it++)
- result.append(*it);
- }
- else
- BOOST_THROW_EXCEPTION(RegexMatcher::Error("Exceed the range of back reference"));
- }
+ while (offset < expand.size()) {
+ std::string item = getItemFromExpand(expand, offset);
+ if (item[0] == '<') {
+ result.append(item.substr(1, item.size() - 2));
}
+ if (item[0] == '\\') {
+ size_t index = boost::lexical_cast<size_t>(item.substr(1, item.size() - 1));
+ if (index == 0) {
+ for (const auto& i : m_matchResult)
+ result.append(i);
+ }
+ else if (index <= backrefNo) {
+ for (const auto& i : backrefManager->getBackref(index - 1)->getMatchResult())
+ result.append(i);
+ }
+ else
+ BOOST_THROW_EXCEPTION(Error("Exceed the range of back reference"));
+ }
+ }
+
return result;
}
@@ -157,44 +130,41 @@
{
size_t begin = offset;
- if (expand[offset] == '\\')
- {
- offset++;
- if (offset >= expand.size())
- BOOST_THROW_EXCEPTION(RegexMatcher::Error("wrong format of expand string!"));
+ if (expand[offset] == '\\') {
+ offset++;
+ if (offset >= expand.size())
+ BOOST_THROW_EXCEPTION(Error("Wrong format of expand string"));
- while (expand[offset] <= '9' and expand[offset] >= '0') {
- offset++;
- if (offset > expand.size())
- BOOST_THROW_EXCEPTION(RegexMatcher::Error("wrong format of expand string!"));
- }
- if (offset > begin + 1)
- return expand.substr(begin, offset - begin);
- else
- BOOST_THROW_EXCEPTION(RegexMatcher::Error("wrong format of expand string!"));
+ while (expand[offset] <= '9' and expand[offset] >= '0') {
+ offset++;
+ if (offset > expand.size())
+ BOOST_THROW_EXCEPTION(Error("Wrong format of expand string"));
}
- else if (expand[offset] == '<')
- {
- offset++;
- if (offset >= expand.size())
- BOOST_THROW_EXCEPTION(RegexMatcher::Error("wrong format of expand string!"));
-
- size_t left = 1;
- size_t right = 0;
- while (right < left)
- {
- if (expand[offset] == '<')
- left++;
- if (expand[offset] == '>')
- right++;
- offset++;
- if (offset >= expand.size())
- BOOST_THROW_EXCEPTION(RegexMatcher::Error("wrong format of expand string!"));
- }
+ if (offset > begin + 1)
return expand.substr(begin, offset - begin);
+ else
+ BOOST_THROW_EXCEPTION(Error("Wrong format of expand string"));
+ }
+ else if (expand[offset] == '<') {
+ offset++;
+ if (offset >= expand.size())
+ BOOST_THROW_EXCEPTION(Error("Wrong format of expand string"));
+
+ size_t left = 1;
+ size_t right = 0;
+ while (right < left) {
+ if (expand[offset] == '<')
+ left++;
+ if (expand[offset] == '>')
+ right++;
+ offset++;
+ if (offset >= expand.size())
+ BOOST_THROW_EXCEPTION(Error("Wrong format of expand string"));
}
+ return expand.substr(begin, offset - begin);
+ }
else
- BOOST_THROW_EXCEPTION(RegexMatcher::Error("wrong format of expand string!"));
+ BOOST_THROW_EXCEPTION(Error("Wrong format of expand string"));
}
shared_ptr<RegexTopMatcher>
@@ -202,50 +172,46 @@
{
std::string regexStr("^");
- for (Name::const_iterator it = name.begin(); it != name.end(); it++)
- {
- regexStr.append("<");
- regexStr.append(convertSpecialChar(it->toUri()));
- regexStr.append(">");
- }
+ for (auto it = name.begin(); it != name.end(); it++) {
+ regexStr.append("<");
+ regexStr.append(convertSpecialChar(it->toUri()));
+ regexStr.append(">");
+ }
if (hasAnchor)
regexStr.append("$");
- // On OSX 10.9, boost, and C++03 the following doesn't work without ndn::
- // because the argument-dependent lookup prefers STL to boost
- return ndn::make_shared<RegexTopMatcher>(regexStr);
+ return make_shared<RegexTopMatcher>(regexStr);
}
std::string
RegexTopMatcher::convertSpecialChar(const std::string& str)
{
std::string newStr;
- for (size_t i = 0; i < str.size(); i++)
- {
- char c = str[i];
- switch (c)
- {
- case '.':
- case '[':
- case '{':
- case '}':
- case '(':
- case ')':
- case '\\':
- case '*':
- case '+':
- case '?':
- case '|':
- case '^':
- case '$':
- newStr.push_back('\\');
- // Fallthrough
- default:
- newStr.push_back(c);
- break;
- }
+
+ for (size_t i = 0; i < str.size(); i++) {
+ char c = str[i];
+ switch (c) {
+ case '.':
+ case '[':
+ case '{':
+ case '}':
+ case '(':
+ case ')':
+ case '\\':
+ case '*':
+ case '+':
+ case '?':
+ case '|':
+ case '^':
+ case '$':
+ newStr.push_back('\\');
+ NDN_CXX_FALLTHROUGH;
+ default:
+ newStr.push_back(c);
+ break;
}
+ }
return newStr;
}
diff --git a/src/util/regex/regex-top-matcher.hpp b/src/util/regex/regex-top-matcher.hpp
index 777a4de..a60e758 100644
--- a/src/util/regex/regex-top-matcher.hpp
+++ b/src/util/regex/regex-top-matcher.hpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2013-2016 Regents of the University of California.
+/*
+ * Copyright (c) 2013-2017 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
@@ -24,8 +24,6 @@
#ifndef NDN_UTIL_REGEX_REGEX_TOP_MATCHER_HPP
#define NDN_UTIL_REGEX_REGEX_TOP_MATCHER_HPP
-#include "../../common.hpp"
-
#include "regex-matcher.hpp"
namespace ndn {
@@ -33,32 +31,30 @@
class RegexPatternListMatcher;
class RegexBackrefManager;
-class RegexTopMatcher: public RegexMatcher
+class RegexTopMatcher : public RegexMatcher
{
public:
+ explicit
RegexTopMatcher(const std::string& expr, const std::string& expand = "");
- virtual
- ~RegexTopMatcher();
-
bool
match(const Name& name);
- virtual bool
- match(const Name& name, size_t offset, size_t len);
+ bool
+ match(const Name& name, size_t offset, size_t len) override;
virtual Name
expand(const std::string& expand = "");
static shared_ptr<RegexTopMatcher>
- fromName(const Name& name, bool hasAnchor=false);
+ fromName(const Name& name, bool hasAnchor = false);
protected:
- virtual void
- compile();
+ void
+ compile() override;
private:
- std::string
+ static std::string
getItemFromExpand(const std::string& expand, size_t& offset);
static std::string