name: Updating toEscapedString interface
Actual implementation is now based on const buffer, not const
std::vector.
Change-Id: Ie29c4be9df7cba0f1e71adb6b3684c9da23a0836
diff --git a/include/ndn-cpp/name.hpp b/include/ndn-cpp/name.hpp
index b6b2e01..192fa31 100644
--- a/include/ndn-cpp/name.hpp
+++ b/include/ndn-cpp/name.hpp
@@ -588,17 +588,34 @@
* @param result the string stream to write to.
*/
static void
- toEscapedString(const std::vector<uint8_t>& value, std::ostream& result);
+ toEscapedString(const uint8_t *value, size_t valueSize, std::ostream& result);
+ inline static void
+ toEscapedString(const std::vector<uint8_t>& value, std::ostream& result)
+ {
+ toEscapedString(&*value.begin(), value.size(), result);
+ }
+
/**
* Convert the value by escaping characters according to the NDN URI Scheme.
* This also adds "..." to a value with zero or more ".".
* @param value the buffer with the value to escape
* @return The escaped string.
*/
- static std::string
- toEscapedString(const std::vector<uint8_t>& value);
+ inline static std::string
+ toEscapedString(const uint8_t *value, size_t valueSize)
+ {
+ std::ostringstream result;
+ toEscapedString(value, valueSize, result);
+ return result.str();
+ }
+ static inline std::string
+ toEscapedString(const std::vector<uint8_t>& value)
+ {
+ return toEscapedString(&*value.begin(), value.size());
+ }
+
//
// vector equivalent interface.
//
diff --git a/src/name.cpp b/src/name.cpp
index 0a0b0e3..b71c23c 100644
--- a/src/name.cpp
+++ b/src/name.cpp
@@ -259,10 +259,10 @@
}
void
-Name::toEscapedString(const vector<uint8_t>& value, std::ostream& result)
+Name::toEscapedString(const uint8_t *value, size_t valueSize, std::ostream& result)
{
bool gotNonDot = false;
- for (unsigned i = 0; i < value.size(); ++i) {
+ for (unsigned i = 0; i < valueSize; ++i) {
if (value[i] != 0x2e) {
gotNonDot = true;
break;
@@ -271,14 +271,14 @@
if (!gotNonDot) {
// Special case for component of zero or more periods. Add 3 periods.
result << "...";
- for (size_t i = 0; i < value.size(); ++i)
+ for (size_t i = 0; i < valueSize; ++i)
result << '.';
}
else {
// In case we need to escape, set to upper case hex and save the previous flags.
ios::fmtflags saveFlags = result.flags(ios::hex | ios::uppercase);
- for (size_t i = 0; i < value.size(); ++i) {
+ for (size_t i = 0; i < valueSize; ++i) {
uint8_t x = value[i];
// Check for 0-9, A-Z, a-z, (+), (-), (.), (_)
if ((x >= 0x30 && x <= 0x39) || (x >= 0x41 && x <= 0x5a) ||
@@ -298,14 +298,6 @@
}
}
-string
-Name::toEscapedString(const vector<uint8_t>& value)
-{
- ostringstream result;
- toEscapedString(value, result);
- return result.str();
-}
-
bool
Name::breadthFirstLess(const Name& name1, const Name& name2)
{
@@ -363,6 +355,8 @@
void
Name::wireDecode(const Block &wire)
{
+ clear();
+
wire_ = wire;
wire_.parse();