regex: modernize and simplify code

Change-Id: Ia4a3046d409ab1e5d8507da4d369dfee203f6256
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;
 }