util+build: Build optimization with regex util

Now only one class (RegexTopMatcher) is getting compiled and other
classes are implemented in header-only way.

Change-Id: I5310c2b1c9ad40366a7b159fe0cec597caca638b
diff --git a/src/util/regex/regex-component-set-matcher.hpp b/src/util/regex/regex-component-set-matcher.hpp
index d4c576e..ffb4c6d 100644
--- a/src/util/regex/regex-component-set-matcher.hpp
+++ b/src/util/regex/regex-component-set-matcher.hpp
@@ -5,19 +5,17 @@
  * See COPYING for copyright and distribution information.
  */
 
-#ifndef REGEX_COMPONENT_SET_MATCHER_HPP
-#define REGEX_COMPONENT_SET_MATCHER_HPP
+#ifndef NDN_UTIL_REGEX_COMPONENT_SET_MATCHER_HPP
+#define NDN_UTIL_REGEX_COMPONENT_SET_MATCHER_HPP
 
-#include <set>
+#include "../../common.hpp"
 
 #include "regex-matcher.hpp"
 #include "regex-component-matcher.hpp"
 
-namespace ndn
-{
+namespace ndn {
 
-class RegexComponentSetMatcher : public RegexMatcher
-{
+class RegexComponentSetMatcher : public RegexMatcher {
 
 public:
   /**
@@ -26,12 +24,12 @@
    * @param exact The flag to provide exact match
    * @param backRefNum The starting back reference number
    */
-  RegexComponentSetMatcher(const std::string expr, ptr_lib::shared_ptr<RegexBackrefManager> backRefManager);    
+  RegexComponentSetMatcher(const std::string& expr, shared_ptr<RegexBackrefManager> backRefManager);    
 
   virtual ~RegexComponentSetMatcher();
 
   virtual bool 
-  match(const Name & name, const int & offset, const int & len = 1);
+  match(const Name& name, const int& offset, const int& len = 1);
 
 protected:    
   /**
@@ -52,10 +50,164 @@
   compileMultipleComponents(const int start, const int lastIndex);
 
 private:
-  std::set<ptr_lib::shared_ptr<RegexComponentMatcher> > m_components;
+  typedef std::set<shared_ptr<RegexComponentMatcher> > ComponentsSet;
+  ComponentsSet m_components;
   bool m_include;
 };
 
-}//ndn
 
-#endif
+inline
+RegexComponentSetMatcher::RegexComponentSetMatcher(const std::string& expr, shared_ptr<RegexBackrefManager> backRefManager)
+  : RegexMatcher(expr, EXPR_COMPONENT_SET, backRefManager),
+    m_include(true)
+{
+  // _LOG_TRACE ("Enter RegexComponentSetMatcher Constructor");
+  compile();
+  // _LOG_TRACE ("Exit RegexComponentSetMatcher Constructor");
+}
+
+inline
+RegexComponentSetMatcher::~RegexComponentSetMatcher()
+{
+  // ComponentsSet::iterator it = m_components.begin();
+
+  // for(; it != m_components.end(); it++)
+  //   delete *it;
+}
+
+inline void 
+RegexComponentSetMatcher::compile()
+{
+  int index = 0;
+
+  switch(m_expr[0]){
+  case '<':
+    return compileSingleComponent();
+  case '[':
+    {
+      int lastIndex = m_expr.size() - 1;
+      if(']' != m_expr[lastIndex])
+        throw RegexMatcher::Error(std::string("Error: RegexComponentSetMatcher.compile(): ")
+                                  + " No matched ']' " + m_expr);
+      
+      if('^' == m_expr[1]){
+        m_include = false;
+        compileMultipleComponents(2, lastIndex);
+      }
+      else
+        compileMultipleComponents(1, lastIndex);
+      break;
+    }
+  default:
+    throw RegexMatcher::Error(std::string("Error: RegexComponentSetMatcher.compile(): ")
+                              + "Parsing error in expr " + m_expr);
+  }
+}
+
+inline void 
+RegexComponentSetMatcher::compileSingleComponent()
+{
+  int end = extractComponent(1);
+
+  if(m_expr.size() != end)
+    {
+      throw RegexMatcher::Error(std::string("Error: RegexComponentSetMatcher.compileSingleComponent: ")
+                                + m_expr);
+    }
+  else
+    {
+      shared_ptr<RegexComponentMatcher> component =
+        make_shared<RegexComponentMatcher>(m_expr.substr(1, end - 2), m_backrefManager);
+      
+      m_components.insert(component);      
+    }
+}
+
+inline void 
+RegexComponentSetMatcher::compileMultipleComponents(const int start, const int lastIndex)
+{
+  int index = start;
+  int tmp_index = start;
+    
+  while(index < lastIndex){
+    if('<' != m_expr[index])
+      throw RegexMatcher::Error(std::string("Error: RegexComponentSetMatcher.compileMultipleComponents: ")
+                                + "Component expr error " + m_expr);
+      
+    tmp_index = index + 1;
+    index = extractComponent(tmp_index);
+
+    shared_ptr<RegexComponentMatcher> component =
+      make_shared<RegexComponentMatcher>(m_expr.substr(tmp_index, index - tmp_index - 1), m_backrefManager);
+    
+    m_components.insert(component);
+  }
+    
+  if(index != lastIndex)
+    throw RegexMatcher::Error(std::string("Error: RegexComponentSetMatcher.compileMultipleComponents: ")
+                              + "Not sufficient expr to parse " + m_expr);        
+}
+
+inline bool 
+RegexComponentSetMatcher::match(const Name & name, const int & offset, const int & len)
+{
+  bool matched = false;
+
+  /* componentset only matches one component */
+  if (len != 1)
+    {
+      return false;
+    }
+
+  for (ComponentsSet::iterator it = m_components.begin();
+       it != m_components.end();
+       ++it)
+    {
+      if((*it)->match(name, offset, len))
+        {
+          matched = true;
+          break;
+        }
+    }
+    
+  m_matchResult.clear();
+
+  if (m_include ? matched : !matched)
+    {
+      m_matchResult.push_back(name.get(offset));
+      return true;
+    }
+  else 
+    return false;
+}
+
+inline int 
+RegexComponentSetMatcher::extractComponent(int index)
+{
+  int lcount = 1;
+  int rcount = 0;
+
+  while(lcount > rcount){
+    switch(m_expr[index]){
+    case '<':
+      lcount++;
+      break;
+
+    case '>':
+      rcount++;
+      break;
+
+    case 0:
+      throw RegexMatcher::Error("Error: square brackets mismatch");
+      break;
+    }
+    index++;
+
+  }
+
+  return index;
+}
+
+} // namespace ndn
+
+#endif // NDN_UTIL_REGEX_COMPONENT_SET_MATCHER_HPP