model: Bug fixes and small API change in ndn.cxx
ndn.cxx API need to be back-ported back, since it has important and not
very obvious problem.
Refs #1011 (http://redmine.named-data.net/issues/1011)
diff --git a/ndn.cxx/name-component.cc b/ndn.cxx/name-component.cc
index fed4bab..8aa522c 100644
--- a/ndn.cxx/name-component.cc
+++ b/ndn.cxx/name-component.cc
@@ -23,6 +23,24 @@
Component::Component (const std::string &uri)
{
+ copy (uri.begin (), uri.end (), back_inserter (*this));
+}
+
+Component::Component (std::string::const_iterator begin, std::string::const_iterator end)
+{
+ copy (begin, end, back_inserter (*this));
+}
+
+Component::Component (const void *buf, size_t length)
+{
+ copy (static_cast<const char*> (buf),
+ static_cast<const char*> (buf)+length,
+ back_inserter (*this));
+}
+
+Component &
+Component::fromUri (const std::string &uri)
+{
try
{
Uri::fromEscaped (uri.begin (), uri.end (), back_inserter (*this));
@@ -34,9 +52,12 @@
<< error::msg (uri)
<< error::pos (error::get_pos (err)));
}
+
+ return *this;
}
-Component::Component (std::string::const_iterator begin, std::string::const_iterator end)
+Component &
+Component::fromUri (std::string::const_iterator begin, std::string::const_iterator end)
{
try
{
@@ -49,13 +70,7 @@
<< error::msg (std::string (begin, end))
<< error::pos (error::get_pos (err)));
}
-}
-
-Component::Component (const void *buf, size_t length)
-{
- copy (static_cast<const char*> (buf),
- static_cast<const char*> (buf)+length,
- back_inserter (*this));
+ return *this;
}
int
@@ -76,33 +91,31 @@
return (std::lexicographical_compare (diff.first, end (), diff.second, other.end ())) ? -1 : +1;
}
-Component
+Component &
Component::fromNumber (uint64_t number)
{
- Component comp;
while (number > 0)
{
- comp.push_back (static_cast<unsigned char> (number & 0xFF));
+ this->push_back (static_cast<unsigned char> (number & 0xFF));
number >>= 8;
}
- std::reverse (comp.begin (), comp.end ());
- return comp;
+ std::reverse (this->begin (), this->end ());
+ return *this;
}
-Component
+Component &
Component::fromNumberWithMarker (uint64_t number, unsigned char marker)
{
- Component comp;
- comp.push_back (marker);
+ this->push_back (marker);
while (number > 0)
{
- comp.push_back (static_cast<unsigned char> (number & 0xFF));
+ this->push_back (static_cast<unsigned char> (number & 0xFF));
number >>= 8;
}
- std::reverse (comp.begin () + 1, comp.end ());
- return comp;
+ std::reverse (this->begin () + 1, this->end ());
+ return *this;
}
std::string
diff --git a/ndn.cxx/name-component.h b/ndn.cxx/name-component.h
index 56291ea..e1b7296 100644
--- a/ndn.cxx/name-component.h
+++ b/ndn.cxx/name-component.h
@@ -104,9 +104,27 @@
////////////////////////////////////
/**
+ * @brief Create component from URI encoded string
+ * @param uri URI encoded name component (convert escaped with % characters)
+ * @return *this
+ */
+ Component &
+ fromUri (const std::string &uri);
+
+ /**
+ * @brief Create component from URI encoded string, with string specified by a pair of iterators
+ * @param begin begin iterator pointing to the URI encoded name
+ * @param end end iterator
+ * @return *this
+ */
+ Component &
+ fromUri (std::string::const_iterator begin, std::string::const_iterator end);
+
+ /**
* @brief Create network-ordered numeric component
*
* @param number number to be encoded and added as a component
+ * @return *this
*
* Number is encoded and added in network order. Tail zero-bytes are not included.
* For example, if the number is 1, then 1-byte binary blob will be added 0x01.
@@ -114,7 +132,7 @@
*
* If the number is zero, an empty component will be created
*/
- static Component
+ Component &
fromNumber (uint64_t number);
/**
@@ -134,7 +152,7 @@
*
* @see fromNumber
*/
- static Component
+ Component &
fromNumberWithMarker (uint64_t number, unsigned char marker);
//////////////////////////////////////////////////////////////////////////////////
diff --git a/ndn.cxx/name.cc b/ndn.cxx/name.cc
index 3bb2416..c0a1a57 100644
--- a/ndn.cxx/name.cc
+++ b/ndn.cxx/name.cc
@@ -92,7 +92,8 @@
break;
string::const_iterator endOfComponent = std::find (i, end, '/');
- append (name::Component (i, endOfComponent));
+ name::Component comp;
+ appendBySwap (comp.fromUri (i, endOfComponent));
i = endOfComponent;
}
diff --git a/ndn.cxx/name.h b/ndn.cxx/name.h
index b456bc7..8731323 100644
--- a/ndn.cxx/name.h
+++ b/ndn.cxx/name.h
@@ -483,16 +483,14 @@
Name::appendNumber (uint64_t number)
{
name::Component comp;
- name::Component::fromNumber (number).swap (comp);
- return appendBySwap (comp);
+ return appendBySwap (comp.fromNumber (number));
}
Name &
Name::appendNumberWithMarker (uint64_t number, unsigned char marker)
{
name::Component comp;
- name::Component::fromNumberWithMarker (number, marker).swap (comp);
- return appendBySwap (comp);
+ return appendBySwap (comp.fromNumberWithMarker (number, marker));
}
inline Name &