src: Improving consistency and correcting code style
As of this commit, all data structures can be directly constructed from
wire format.
This commit excludes full correction of code style in security/ and
tools/ndnsec*, which will be part of a different commit.
Change-Id: I121ac1f81948bc7468990df52cdefeb2988d91a1
Refs: #1403
diff --git a/src/util/command-interest-generator.hpp b/src/util/command-interest-generator.hpp
index 18bc960..8b51cd9 100644
--- a/src/util/command-interest-generator.hpp
+++ b/src/util/command-interest-generator.hpp
@@ -62,7 +62,7 @@
.append(name::Component::fromNumber(random::generateWord64()));
interest.setName(commandInterestName);
- if(certificateName.empty())
+ if (certificateName.empty())
m_keyChain.sign(interest);
else
m_keyChain.sign(interest, certificateName);
diff --git a/src/util/crypto.cpp b/src/util/crypto.cpp
index 1621005..d8e803a 100644
--- a/src/util/crypto.cpp
+++ b/src/util/crypto.cpp
@@ -10,17 +10,17 @@
namespace ndn {
-void ndn_digestSha256(const uint8_t *data, size_t dataLength, uint8_t *digest)
+void ndn_digestSha256(const uint8_t* data, size_t dataLength, uint8_t* digest)
{
try
{
using namespace CryptoPP;
-
+
CryptoPP::SHA256 hash;
OBufferStream os;
StringSource(data, dataLength, true, new HashFilter(hash, new ArraySink(digest, crypto::SHA256_DIGEST_SIZE)));
}
- catch(CryptoPP::Exception& e)
+ catch (CryptoPP::Exception& e)
{
return;
}
@@ -30,18 +30,18 @@
namespace crypto {
ConstBufferPtr
-sha256(const uint8_t *data, size_t dataLength)
+sha256(const uint8_t* data, size_t dataLength)
{
try
{
using namespace CryptoPP;
-
+
SHA256 hash;
OBufferStream os;
StringSource(data, dataLength, true, new HashFilter(hash, new FileSink(os)));
return os.buf();
}
- catch(CryptoPP::Exception& e)
+ catch (CryptoPP::Exception& e)
{
return ConstBufferPtr();
}
diff --git a/src/util/crypto.hpp b/src/util/crypto.hpp
index 58555a2..82e769f 100644
--- a/src/util/crypto.hpp
+++ b/src/util/crypto.hpp
@@ -18,7 +18,7 @@
* @param dataLength The length of data.
* @param digest A pointer to a buffer of size crypto::SHA256_DIGEST_SIZE to receive the data.
*/
-void ndn_digestSha256(const uint8_t *data, size_t dataLength, uint8_t *digest);
+void ndn_digestSha256(const uint8_t* data, size_t dataLength, uint8_t* digest);
namespace crypto {
@@ -33,7 +33,7 @@
* @return A pointer to a buffer of SHA256_DIGEST.
*/
ConstBufferPtr
-sha256(const uint8_t *data, size_t dataLength);
+sha256(const uint8_t* data, size_t dataLength);
} // namespace crypto
diff --git a/src/util/io.hpp b/src/util/io.hpp
index 6026f06..ff6852b 100644
--- a/src/util/io.hpp
+++ b/src/util/io.hpp
@@ -19,7 +19,15 @@
namespace ndn {
namespace io {
-struct Error : public std::runtime_error { Error(const std::string &what) : std::runtime_error(what) {} };
+class Error : public std::runtime_error
+{
+public:
+ explicit
+ Error(const std::string& what)
+ : std::runtime_error(what)
+ {
+ }
+};
enum IoEncoding {
NO_ENCODING,
@@ -27,8 +35,8 @@
HEX
};
-template<typename T>
-shared_ptr<T>
+template<typename T>
+shared_ptr<T>
load(std::istream& is, IoEncoding encoding = BASE_64)
{
typedef typename T::Error TypeError;
@@ -39,54 +47,54 @@
shared_ptr<T> object = make_shared<T>();
OBufferStream os;
-
- switch(encoding)
- {
- case NO_ENCODING:
- {
- FileSource ss(is, true, new FileSink(os));
- break;
- }
- case BASE_64:
- {
- FileSource ss(is, true, new Base64Decoder(new FileSink(os)));
- break;
- }
- case HEX:
- {
- FileSource ss(is, true, new HexDecoder(new FileSink(os)));
- break;
- }
- default:
- return shared_ptr<T>();
- }
+
+ switch (encoding)
+ {
+ case NO_ENCODING:
+ {
+ FileSource ss(is, true, new FileSink(os));
+ break;
+ }
+ case BASE_64:
+ {
+ FileSource ss(is, true, new Base64Decoder(new FileSink(os)));
+ break;
+ }
+ case HEX:
+ {
+ FileSource ss(is, true, new HexDecoder(new FileSink(os)));
+ break;
+ }
+ default:
+ return shared_ptr<T>();
+ }
object->wireDecode(Block(os.buf()));
return object;
}
- catch(CryptoPP::Exception& e)
+ catch (CryptoPP::Exception& e)
{
return shared_ptr<T>();
}
- catch(Block::Error& e)
+ catch (Block::Error& e)
{
return shared_ptr<T>();
}
- catch(TypeError& e)
+ catch (TypeError& e)
{
return shared_ptr<T>();
}
}
-template<typename T>
-shared_ptr<T>
+template<typename T>
+shared_ptr<T>
load(const std::string& file, IoEncoding encoding = BASE_64)
{
std::ifstream is(file.c_str());
return load<T>(is, encoding);
}
-template<typename T>
+template<typename T>
void
save(const T& object, std::ostream& os, IoEncoding encoding = BASE_64)
{
@@ -96,38 +104,41 @@
using namespace CryptoPP;
Block block = object.wireEncode();
-
- switch(encoding)
- {
- case NO_ENCODING:
- {
- StringSource ss(block.wire(), block.size(), true, new FileSink(os));
- break;
- }
- case BASE_64:
- {
- StringSource ss(block.wire(), block.size(), true, new Base64Encoder(new FileSink(os), true, 64));
- break;
- }
- case HEX:
- {
- StringSource ss(block.wire(), block.size(), true, new HexEncoder(new FileSink(os)));
- break;
- }
- default:
- return;
- }
+
+ switch (encoding)
+ {
+ case NO_ENCODING:
+ {
+ StringSource ss(block.wire(), block.size(), true,
+ new FileSink(os));
+ break;
+ }
+ case BASE_64:
+ {
+ StringSource ss(block.wire(), block.size(), true,
+ new Base64Encoder(new FileSink(os), true, 64));
+ break;
+ }
+ case HEX:
+ {
+ StringSource ss(block.wire(), block.size(), true,
+ new HexEncoder(new FileSink(os)));
+ break;
+ }
+ default:
+ return;
+ }
return;
}
- catch(CryptoPP::Exception& e)
+ catch (CryptoPP::Exception& e)
{
throw Error(e.what());
}
- catch(Block::Error& e)
+ catch (Block::Error& e)
{
throw Error(e.what());
}
- catch(TypeError& e)
+ catch (TypeError& e)
{
throw Error(e.what());
}
@@ -145,4 +156,3 @@
} // namespace ndn
#endif // NDN_UTIL_IO_HPP
-
diff --git a/src/util/regex/regex-backref-matcher.hpp b/src/util/regex/regex-backref-matcher.hpp
index e2eabe8..aa00605 100644
--- a/src/util/regex/regex-backref-matcher.hpp
+++ b/src/util/regex/regex-backref-matcher.hpp
@@ -18,17 +18,17 @@
{
public:
RegexBackrefMatcher(const std::string& expr, shared_ptr<RegexBackrefManager> backRefManager);
-
+
virtual ~RegexBackrefMatcher(){}
- void
+ void
lateCompile()
{
compile();
}
protected:
- virtual void
+ virtual void
compile();
};
@@ -44,11 +44,11 @@
// compile();
}
-inline void
+inline void
RegexBackrefMatcher::compile()
{
int lastIndex = m_expr.size() - 1;
- if('(' == m_expr[0] && ')' == m_expr[lastIndex]){
+ 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));
@@ -63,5 +63,3 @@
} // 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 50cb8de..34bcba7 100644
--- a/src/util/regex/regex-component-matcher.hpp
+++ b/src/util/regex/regex-component-matcher.hpp
@@ -25,13 +25,13 @@
* @param backRefManager The back reference manager
* @param exact The flag to provide exact match
*/
- RegexComponentMatcher(const std::string& expr,
- ptr_lib::shared_ptr<RegexBackrefManager> backRefManager,
+ RegexComponentMatcher(const std::string& expr,
+ ptr_lib::shared_ptr<RegexBackrefManager> backRefManager,
bool exact = true);
-
+
virtual ~RegexComponentMatcher() {};
- virtual bool
+ virtual bool
match(const Name & name, const int & offset, const int &len = 1);
protected:
@@ -39,20 +39,20 @@
* @brief Compile the regular expression to generate the more matchers when necessary
* @returns true if compiling succeeds
*/
- virtual void
+ virtual void
compile();
-
+
private:
bool m_exact;
boost::regex m_componentRegex;
std::vector<ptr_lib::shared_ptr<RegexPseudoMatcher> > m_pseudoMatcher;
-
+
};
inline
-RegexComponentMatcher::RegexComponentMatcher (const std::string& expr,
- ptr_lib::shared_ptr<RegexBackrefManager> backRefManager,
+RegexComponentMatcher::RegexComponentMatcher (const std::string& expr,
+ ptr_lib::shared_ptr<RegexBackrefManager> backRefManager,
bool exact)
: RegexMatcher (expr, EXPR_COMPONENT, backRefManager),
m_exact(exact)
@@ -62,7 +62,7 @@
// _LOG_TRACE ("Exit RegexComponentMatcher Constructor: ");
}
-inline void
+inline void
RegexComponentMatcher::compile ()
{
// _LOG_TRACE ("Enter RegexComponentMatcher::compile");
@@ -78,7 +78,7 @@
m_pseudoMatcher.push_back(pMatcher);
m_backrefManager->pushRef(ptr_lib::static_pointer_cast<RegexMatcher>(pMatcher));
}
-
+
// _LOG_TRACE ("Exit RegexComponentMatcher::compile");
}
@@ -90,17 +90,17 @@
m_matchResult.clear();
- if("" == m_expr)
+ if ("" == m_expr)
{
m_matchResult.push_back(name.get(offset));
return true;
}
- if(true == m_exact)
+ if (true == m_exact)
{
boost::smatch subResult;
std::string targetStr = name.get(offset).toEscapedString();
- if(boost::regex_match(targetStr, subResult, m_componentRegex))
+ if (boost::regex_match(targetStr, subResult, m_componentRegex))
{
for (size_t i = 1; i < m_componentRegex.mark_count(); i++)
{
diff --git a/src/util/regex/regex-component-set-matcher.hpp b/src/util/regex/regex-component-set-matcher.hpp
index 4039388..5aed58f 100644
--- a/src/util/regex/regex-component-set-matcher.hpp
+++ b/src/util/regex/regex-component-set-matcher.hpp
@@ -24,19 +24,19 @@
* @param exact The flag to provide exact match
* @param backRefNum The starting back reference number
*/
- RegexComponentSetMatcher(const std::string& expr, shared_ptr<RegexBackrefManager> backRefManager);
+ RegexComponentSetMatcher(const std::string& expr, shared_ptr<RegexBackrefManager> backRefManager);
virtual ~RegexComponentSetMatcher();
- virtual bool
+ virtual bool
match(const Name& name, const int& offset, const int& len = 1);
-protected:
+protected:
/**
* @brief Compile the regular expression to generate the more matchers when necessary
* @returns true if compiling succeeds
*/
- virtual void
+ virtual void
compile();
private:
@@ -45,7 +45,7 @@
void
compileSingleComponent();
-
+
void
compileMultipleComponents(const int start, const int lastIndex);
@@ -57,7 +57,8 @@
inline
-RegexComponentSetMatcher::RegexComponentSetMatcher(const std::string& expr, shared_ptr<RegexBackrefManager> backRefManager)
+RegexComponentSetMatcher::RegexComponentSetMatcher(const std::string& expr,
+ shared_ptr<RegexBackrefManager> backRefManager)
: RegexMatcher(expr, EXPR_COMPONENT_SET, backRefManager),
m_include(true)
{
@@ -75,20 +76,20 @@
// delete *it;
}
-inline void
+inline void
RegexComponentSetMatcher::compile()
{
- switch(m_expr[0]){
+ switch (m_expr[0]){
case '<':
return compileSingleComponent();
case '[':
{
int lastIndex = m_expr.size() - 1;
- if(']' != m_expr[lastIndex])
+ if (']' != m_expr[lastIndex])
throw RegexMatcher::Error(std::string("Error: RegexComponentSetMatcher.compile(): ")
+ " No matched ']' " + m_expr);
-
- if('^' == m_expr[1]){
+
+ if ('^' == m_expr[1]){
m_include = false;
compileMultipleComponents(2, lastIndex);
}
@@ -102,52 +103,55 @@
}
}
-inline void
+inline void
RegexComponentSetMatcher::compileSingleComponent()
{
size_t end = extractComponent(1);
if (m_expr.size() != end)
{
- throw RegexMatcher::Error(std::string("Error: RegexComponentSetMatcher.compileSingleComponent: ")
- + m_expr);
+ 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);
+
+ m_components.insert(component);
}
}
-inline void
+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);
-
+ 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);
-
+ 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);
+
+ 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)
+inline bool
+RegexComponentSetMatcher::match(const Name& name, const int& offset, const int& len)
{
bool matched = false;
@@ -161,13 +165,13 @@
it != m_components.end();
++it)
{
- if((*it)->match(name, offset, len))
+ if ((*it)->match(name, offset, len))
{
matched = true;
break;
}
}
-
+
m_matchResult.clear();
if (m_include ? matched : !matched)
@@ -175,18 +179,18 @@
m_matchResult.push_back(name.get(offset));
return true;
}
- else
+ else
return false;
}
-inline int
+inline int
RegexComponentSetMatcher::extractComponent(int index)
{
int lcount = 1;
int rcount = 0;
while(lcount > rcount){
- switch(m_expr[index]){
+ switch (m_expr[index]){
case '<':
lcount++;
break;
diff --git a/src/util/regex/regex-matcher.hpp b/src/util/regex/regex-matcher.hpp
index 0d34311..d62ab41 100644
--- a/src/util/regex/regex-matcher.hpp
+++ b/src/util/regex/regex-matcher.hpp
@@ -18,7 +18,15 @@
class RegexMatcher
{
public:
- struct Error : public std::runtime_error { Error(const std::string& what) : std::runtime_error(what) {} };
+ class Error : public std::runtime_error
+ {
+ public:
+ explicit
+ Error(const std::string& what)
+ : std::runtime_error(what)
+ {
+ }
+ };
enum RegexExprType{
EXPR_TOP,
@@ -26,52 +34,52 @@
EXPR_PATTERNLIST,
EXPR_REPEAT_PATTERN,
-
+
EXPR_BACKREF,
EXPR_COMPONENT_SET,
EXPR_COMPONENT,
EXPR_PSEUDO
- };
+ };
- RegexMatcher(const std::string& expr,
- const RegexExprType& type,
+ RegexMatcher(const std::string& expr,
+ const RegexExprType& type,
shared_ptr<RegexBackrefManager> backrefManager = shared_ptr<RegexBackrefManager>());
- virtual
+ virtual
~RegexMatcher();
- virtual bool
+ virtual bool
match(const Name& name, const int& offset, const int& len);
/**
* @brief get the matched name components
* @returns the matched name components
*/
- const std::vector<name::Component>&
+ const std::vector<name::Component>&
getMatchResult() const
{ return m_matchResult; }
const std::string&
getExpr() const
- { return m_expr; }
+ { return m_expr; }
protected:
/**
* @brief Compile the regular expression to generate the more matchers when necessary
* @returns true if compiling succeeds
*/
- virtual void
+ virtual void
compile() = 0;
private:
- bool
+ bool
recursiveMatch(size_t mId, const Name& name, size_t offset, size_t len);
protected:
const std::string m_expr;
- const RegexExprType m_type;
+ const RegexExprType m_type;
shared_ptr<RegexBackrefManager> m_backrefManager;
std::vector<shared_ptr<RegexMatcher> > m_matcherList;
std::vector<name::Component> m_matchResult;
@@ -84,14 +92,14 @@
namespace ndn {
inline
-RegexMatcher::RegexMatcher(const std::string& expr,
- const RegexExprType& type,
- shared_ptr<RegexBackrefManager> backrefManager)
- : m_expr(expr),
+RegexMatcher::RegexMatcher(const std::string& expr,
+ const RegexExprType& type,
+ shared_ptr<RegexBackrefManager> backrefManager)
+ : m_expr(expr),
m_type(type),
m_backrefManager(backrefManager)
{
- if(NULL == m_backrefManager)
+ if (NULL == m_backrefManager)
m_backrefManager = make_shared<RegexBackrefManager>();
}
@@ -100,7 +108,7 @@
{
}
-inline bool
+inline bool
RegexMatcher::match (const Name& name, const int& offset, const int& len)
{
// _LOG_TRACE ("Enter RegexMatcher::match");
@@ -108,7 +116,7 @@
m_matchResult.clear();
- if(recursiveMatch(0, name, offset, len))
+ if (recursiveMatch(0, name, offset, len))
{
for(int i = offset; i < offset + len ; i++)
m_matchResult.push_back(name.get(i));
@@ -122,23 +130,23 @@
// _LOG_TRACE ("Exit RegexMatcher::match");
return result;
}
-
-inline bool
+
+inline bool
RegexMatcher::recursiveMatch(size_t mId, const Name& name, size_t offset, size_t len)
{
// _LOG_TRACE ("Enter RegexMatcher::recursiveMatch");
int tried = len;
- if(mId >= m_matcherList.size())
+ if (mId >= m_matcherList.size())
return (len != 0 ? false : true);
-
+
shared_ptr<RegexMatcher> matcher = m_matcherList[mId];
while(tried >= 0)
{
- if(matcher->match(name, offset, tried) && recursiveMatch(mId + 1, name, offset + tried, len - tried))
- return true;
+ if (matcher->match(name, offset, tried) && recursiveMatch(mId + 1, name, offset + tried, len - tried))
+ return true;
tried--;
}
diff --git a/src/util/regex/regex-pattern-list-matcher.hpp b/src/util/regex/regex-pattern-list-matcher.hpp
index 3ae74f1..12e3c68 100644
--- a/src/util/regex/regex-pattern-list-matcher.hpp
+++ b/src/util/regex/regex-pattern-list-matcher.hpp
@@ -20,21 +20,21 @@
{
public:
RegexPatternListMatcher(const std::string& expr, shared_ptr<RegexBackrefManager> backRefManager);
-
+
virtual ~RegexPatternListMatcher(){};
-protected:
- virtual void
+protected:
+ virtual void
compile();
private:
- bool
+ bool
extractPattern(int index, int* next);
-
- int
+
+ int
extractSubPattern(const char left, const char right, size_t index);
-
- int
+
+ int
extractRepetition(size_t index);
private:
@@ -53,38 +53,38 @@
{
compile();
}
-
-inline void
+
+inline void
RegexPatternListMatcher::compile()
{
const int len = m_expr.size();
int index = 0;
int subHead = index;
-
+
while(index < len){
subHead = index;
- if(!extractPattern(subHead, &index))
+ if (!extractPattern(subHead, &index))
throw RegexMatcher::Error("RegexPatternListMatcher compile: cannot compile");
}
}
-inline bool
+inline bool
RegexPatternListMatcher::extractPattern(int index, int* next)
{
// std::string errMsg = "Error: RegexPatternListMatcher.ExtractSubPattern(): ";
-
+
const int start = index;
int end = index;
int indicator = index;
- switch(m_expr[index]){
+ switch (m_expr[index]){
case '(':
index++;
index = extractSubPattern('(', ')', index);
indicator = index;
end = extractRepetition(index);
- if(indicator == end){
+ if (indicator == end){
shared_ptr<RegexMatcher> matcher = make_shared<RegexBackrefMatcher>(m_expr.substr(start, end - start), m_backrefManager);
m_backrefManager->pushRef(matcher);
boost::dynamic_pointer_cast<RegexBackrefMatcher>(matcher)->lateCompile();
@@ -94,7 +94,7 @@
else
m_matcherList.push_back(make_shared<RegexRepeatMatcher>(m_expr.substr(start, end - start), m_backrefManager, indicator - start));
break;
-
+
case '<':
index++;
index = extractSubPattern ('<', '>', index);
@@ -119,8 +119,8 @@
return true;
}
-
-inline int
+
+inline int
RegexPatternListMatcher::extractSubPattern(const char left, const char right, size_t index)
{
size_t lcount = 1;
@@ -128,13 +128,13 @@
while(lcount > rcount){
- if(index >= m_expr.size())
+ if (index >= m_expr.size())
throw RegexMatcher::Error("Error: parenthesis mismatch");
- if(left == m_expr[index])
+ if (left == m_expr[index])
lcount++;
- if(right == m_expr[index])
+ if (right == m_expr[index])
rcount++;
index++;
@@ -142,25 +142,25 @@
return index;
}
-inline int
+inline int
RegexPatternListMatcher::extractRepetition(size_t index)
{
size_t exprSize = m_expr.size();
- if(index == exprSize)
+ if (index == exprSize)
return index;
-
- if(('+' == m_expr[index] || '?' == m_expr[index] || '*' == m_expr[index])){
+
+ if (('+' == m_expr[index] || '?' == m_expr[index] || '*' == m_expr[index])){
return ++index;
}
-
- if('{' == m_expr[index]){
+
+ if ('{' == m_expr[index]){
while('}' != m_expr[index]){
index++;
- if(index == exprSize)
+ if (index == exprSize)
break;
}
- if(index == exprSize)
+ if (index == exprSize)
throw RegexMatcher::Error(std::string("Error: RegexPatternListMatcher.ExtractRepetition(): ")
+ "Missing right brace bracket");
else
diff --git a/src/util/regex/regex-repeat-matcher.hpp b/src/util/regex/regex-repeat-matcher.hpp
index fb42042..5eefbd1 100644
--- a/src/util/regex/regex-repeat-matcher.hpp
+++ b/src/util/regex/regex-repeat-matcher.hpp
@@ -20,10 +20,10 @@
{
public:
RegexRepeatMatcher(const std::string& expr, shared_ptr<RegexBackrefManager> backRefManager, int indicator);
-
+
virtual ~RegexRepeatMatcher(){}
- virtual bool
+ virtual bool
match(const Name& name, const int& offset, const int& len);
protected:
@@ -31,19 +31,19 @@
* @brief Compile the regular expression to generate the more matchers when necessary
* @returns true if compiling succeeds
*/
- virtual void
+ virtual void
compile();
private:
- bool
+ bool
parseRepetition();
- bool
+ bool
recursiveMatch (int repeat,
- const Name & name,
- const int & offset,
- const int &len);
-
+ const Name& name,
+ const int& offset,
+ const int&len);
+
private:
int m_indicator;
int m_repeatMin;
@@ -67,14 +67,14 @@
// _LOG_TRACE ("Exit RegexRepeatMatcher Constructor");
}
-inline void
+inline void
RegexRepeatMatcher::compile()
{
// _LOG_TRACE ("Enter RegexRepeatMatcher::compile");
-
+
shared_ptr<RegexMatcher> matcher;
- if('(' == m_expr[0]){
+ if ('(' == m_expr[0]){
matcher = make_shared<RegexBackrefMatcher>(m_expr.substr(0, m_indicator), m_backrefManager);
m_backrefManager->pushRef(matcher);
boost::dynamic_pointer_cast<RegexBackrefMatcher>(matcher)->lateCompile();
@@ -83,40 +83,40 @@
matcher = make_shared<RegexComponentSetMatcher>(m_expr.substr(0, m_indicator), m_backrefManager);
}
m_matcherList.push_back(matcher);
-
+
parseRepetition();
// _LOG_TRACE ("Exit RegexRepeatMatcher::compile");
}
-inline bool
+inline bool
RegexRepeatMatcher::parseRepetition()
{
// _LOG_DEBUG ("Enter RegexRepeatMatcher::ParseRepetition()" << m_expr << " indicator: " << m_indicator);
-
+
int exprSize = m_expr.size();
int intMax = std::numeric_limits<int>::max();
-
- if(exprSize == m_indicator){
+
+ if (exprSize == m_indicator){
m_repeatMin = 1;
m_repeatMax = 1;
return true;
}
else{
- if(exprSize == (m_indicator + 1)){
- if('?' == m_expr[m_indicator]){
+ if (exprSize == (m_indicator + 1)){
+ if ('?' == m_expr[m_indicator]){
m_repeatMin = 0;
m_repeatMax = 1;
return true;
}
- if('+' == m_expr[m_indicator]){
+ if ('+' == m_expr[m_indicator]){
m_repeatMin = 1;
m_repeatMax = intMax;
return true;
}
- if('*' == m_expr[m_indicator]){
+ if ('*' == m_expr[m_indicator]){
m_repeatMin = 0;
m_repeatMax = intMax;
return true;
@@ -128,36 +128,36 @@
int min = 0;
int max = 0;
- if(boost::regex_match(repeatStruct, boost::regex("\\{[0-9]+,[0-9]+\\}"))){
+ if (boost::regex_match(repeatStruct, boost::regex("\\{[0-9]+,[0-9]+\\}"))){
int 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]+\\}"))){
+ else if (boost::regex_match(repeatStruct, boost::regex("\\{,[0-9]+\\}"))){
int 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]+,\\}"))){
+ else if (boost::regex_match(repeatStruct, boost::regex("\\{[0-9]+,\\}"))){
int separator = repeatStruct.find_first_of(',', 0);
min = atoi(repeatStruct.substr(1, separator).c_str());
max = intMax;
}
- else if(boost::regex_match(repeatStruct, boost::regex("\\{[0-9]+\\}"))){
+ else if (boost::regex_match(repeatStruct, boost::regex("\\{[0-9]+\\}"))){
min = atoi(repeatStruct.substr(1, rsSize - 1).c_str());
max = min;
}
else
throw RegexMatcher::Error(std::string("Error: RegexRepeatMatcher.ParseRepetition(): ")
+ "Unrecognized format "+ m_expr);
-
- if(min > intMax || max > intMax || min > max)
+
+ if (min > intMax || max > intMax || min > max)
throw RegexMatcher::Error(std::string("Error: RegexRepeatMatcher.ParseRepetition(): ")
+ "Wrong number " + m_expr);
-
+
m_repeatMin = min;
m_repeatMax = max;
-
+
return true;
}
}
@@ -165,7 +165,7 @@
}
inline bool
-RegexRepeatMatcher::match(const Name & name, const int & offset, const int & len)
+RegexRepeatMatcher::match(const Name& name, const int& offset, const int& len)
{
// _LOG_TRACE ("Enter RegexRepeatMatcher::match");
@@ -185,8 +185,8 @@
return false;
}
-inline bool
-RegexRepeatMatcher::recursiveMatch(int repeat, const Name & name, const int & offset, const int & len)
+inline bool
+RegexRepeatMatcher::recursiveMatch(int repeat, const Name& name, const int& offset, const int& len)
{
// _LOG_TRACE ("Enter RegexRepeatMatcher::recursiveMatch");
@@ -213,7 +213,7 @@
// _LOG_DEBUG("Match Succeed: No more components && reach m_repeatMin");
return true;
}
-
+
while(tried >= 0)
{
// _LOG_DEBUG("Attempt tried: " << tried);
diff --git a/src/util/regex/regex-top-matcher.cpp b/src/util/regex/regex-top-matcher.cpp
index d3b8271..78cb389 100644
--- a/src/util/regex/regex-top-matcher.cpp
+++ b/src/util/regex/regex-top-matcher.cpp
@@ -27,19 +27,19 @@
// delete m_backRefManager;
}
-void
+void
RegexTopMatcher::compile()
{
std::string errMsg = "Error: RegexTopMatcher.Compile(): ";
std::string expr = m_expr;
- if('$' != expr[expr.size() - 1])
+ if ('$' != expr[expr.size() - 1])
expr = expr + "<.*>*";
else
expr = expr.substr(0, expr.size()-1);
- if('^' != expr[0])
+ if ('^' != expr[0])
m_secondaryMatcher = make_shared<RegexPatternListMatcher>(boost::cref("<.*>*" + expr),
boost::cref(m_secondaryBackRefManager));
else
@@ -49,14 +49,14 @@
boost::cref(m_primaryBackRefManager));
}
-bool
-RegexTopMatcher::match(const Name & name)
+bool
+RegexTopMatcher::match(const Name& name)
{
m_secondaryUsed = false;
m_matchResult.clear();
- if(m_primaryMatcher->match(name, 0, name.size()))
+ if (m_primaryMatcher->match(name, 0, name.size()))
{
m_matchResult = m_primaryMatcher->getMatchResult();
return true;
@@ -72,25 +72,25 @@
return false;
}
}
-
-bool
-RegexTopMatcher::match (const Name & name, const int & offset, const int & len)
+
+bool
+RegexTopMatcher::match (const Name& name, const int& offset, const int& len)
{
return match(name);
}
-Name
+Name
RegexTopMatcher::expand (const std::string& expandStr)
{
Name result;
-
+
shared_ptr<RegexBackrefManager> backRefManager = (m_secondaryUsed ? m_secondaryBackRefManager : m_primaryBackRefManager);
-
+
int backRefNum = backRefManager->size();
std::string expand;
-
- if(expandStr != "")
+
+ if (expandStr != "")
expand = expandStr;
else
expand = m_expand;
@@ -99,22 +99,22 @@
while (offset < expand.size())
{
std::string item = getItemFromExpand(expand, offset);
- if(item[0] == '<')
+ if (item[0] == '<')
{
result.append(item.substr(1, item.size() - 2));
}
- if(item[0] == '\\')
+ if (item[0] == '\\')
{
int index = atoi(item.substr(1, item.size() - 1).c_str());
- if(0 == index){
+ 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 <= backRefNum)
+ else if (index <= backRefNum)
{
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();
@@ -123,7 +123,7 @@
}
else
throw RegexMatcher::Error("Exceed the range of back reference!");
- }
+ }
}
return result;
}
@@ -133,38 +133,38 @@
{
size_t begin = offset;
- if(expand[offset] == '\\')
+ if (expand[offset] == '\\')
{
offset++;
- if(offset >= expand.size())
+ if (offset >= expand.size())
throw RegexMatcher::Error("wrong format of expand string!");
while(expand[offset] <= '9' and expand[offset] >= '0'){
offset++;
- if(offset > expand.size())
+ if (offset > expand.size())
throw RegexMatcher::Error("wrong format of expand string!");
}
- if(offset > begin + 1)
+ if (offset > begin + 1)
return expand.substr(begin, offset - begin);
else
throw RegexMatcher::Error("wrong format of expand string!");
}
- else if(expand[offset] == '<')
+ else if (expand[offset] == '<')
{
offset++;
- if(offset >= expand.size())
+ if (offset >= expand.size())
throw RegexMatcher::Error("wrong format of expand string!");
-
+
size_t left = 1;
size_t right = 0;
while(right < left)
{
- if(expand[offset] == '<')
+ if (expand[offset] == '<')
left++;
- if(expand[offset] == '>')
- right++;
+ if (expand[offset] == '>')
+ right++;
offset++;
- if(offset >= expand.size())
+ if (offset >= expand.size())
throw RegexMatcher::Error("wrong format of expand string!");
}
return expand.substr(begin, offset - begin);
@@ -178,7 +178,7 @@
{
Name::const_iterator it = name.begin();
std::string regexStr("^");
-
+
for(; it != name.end(); it++)
{
regexStr.append("<");
@@ -186,7 +186,7 @@
regexStr.append(">");
}
- if(hasAnchor)
+ if (hasAnchor)
regexStr.append("$");
return make_shared<RegexTopMatcher>(boost::cref(regexStr));
@@ -199,7 +199,7 @@
for(size_t i = 0; i < str.size(); i++)
{
char c = str[i];
- switch(c)
+ switch (c)
{
case '.':
case '[':
diff --git a/src/util/string-helper.hpp b/src/util/string-helper.hpp
index 9e2329c..7059f0a 100644
--- a/src/util/string-helper.hpp
+++ b/src/util/string-helper.hpp
@@ -13,19 +13,19 @@
namespace ndn {
-static const char *WHITESPACE_CHARS = " \n\r\t";
+static const char* WHITESPACE_CHARS = " \n\r\t";
/**
* Return the hex representation of the bytes in array.
* @param array The array of bytes.
* @return Hex string.
*/
-inline std::string
+inline std::string
toHex(const uint8_t* array, size_t arraySize)
{
if (array == 0 || arraySize == 0)
return "";
-
+
std::ostringstream result;
result.flags(std::ios::hex | std::ios::uppercase);
for (size_t i = 0; i < arraySize; ++i) {
@@ -42,7 +42,7 @@
* Modify str in place to erase whitespace on the left.
* @param str
*/
-inline void
+inline void
trimLeft(std::string& str)
{
size_t found = str.find_first_not_of(WHITESPACE_CHARS);
@@ -52,14 +52,14 @@
}
else
// All whitespace
- str.clear();
+ str.clear();
}
/**
* Modify str in place to erase whitespace on the right.
* @param str
*/
-inline void
+inline void
trimRight(std::string& str)
{
size_t found = str.find_last_not_of(WHITESPACE_CHARS);
@@ -76,7 +76,7 @@
* Modify str in place to erase whitespace on the left and right.
* @param str
*/
-inline void
+inline void
trim(std::string& str)
{
trimLeft(str);
@@ -86,9 +86,9 @@
/**
* Convert the hex character to an integer from 0 to 15, or -1 if not a hex character.
* @param c
- * @return
+ * @return
*/
-inline int
+inline int
fromHexChar(uint8_t c)
{
if (c >= '0' && c <= '9')
@@ -105,22 +105,22 @@
* Return a copy of str, converting each escaped "%XX" to the char value.
* @param str
*/
-inline std::string
+inline std::string
unescape(const std::string& str)
{
std::ostringstream result;
-
+
for (size_t i = 0; i < str.size(); ++i) {
if (str[i] == '%' && i + 2 < str.size()) {
int hi = fromHexChar(str[i + 1]);
int lo = fromHexChar(str[i + 2]);
-
+
if (hi < 0 || lo < 0)
// Invalid hex characters, so just keep the escaped string.
result << str[i] << str[i + 1] << str[i + 2];
else
result << (uint8_t)(16 * hi + lo);
-
+
// Skip ahead past the escaped value.
i += 2;
}
@@ -128,7 +128,7 @@
// Just copy through.
result << str[i];
}
-
+
return result.str();
}