name: Introducing new name::Component::is* methods
These methods allow checking if the name component follows the specific
naming convention without relying on exception handling.
Change-Id: I9799c860ad203f777d7ad6671c9a13506388c1c7
Refs: #2088
diff --git a/src/name-component.cpp b/src/name-component.cpp
index a045766..75d9cfa 100644
--- a/src/name-component.cpp
+++ b/src/name-component.cpp
@@ -142,18 +142,68 @@
return os.str();
}
+
+bool
+Component::isNumber() const
+{
+ return (value_size() == 1 || value_size() == 2 ||
+ value_size() == 4 || value_size() == 8);
+}
+
+bool
+Component::isNumberWithMarker(uint8_t marker) const
+{
+ return (!empty() && value()[0] == marker &&
+ (value_size() == 2 || value_size() == 3 ||
+ value_size() == 5 || value_size() == 9));
+}
+
+bool
+Component::isVersion() const
+{
+ return isNumberWithMarker(VERSION_MARKER);
+}
+
+bool
+Component::isSegment() const
+{
+ return isNumberWithMarker(SEGMENT_MARKER);
+}
+
+bool
+Component::isSegmentOffset() const
+{
+ return isNumberWithMarker(SEGMENT_OFFSET_MARKER);
+}
+
+bool
+Component::isTimestamp() const
+{
+ return isNumberWithMarker(TIMESTAMP_MARKER);
+}
+
+bool
+Component::isSequenceNumber() const
+{
+ return isNumberWithMarker(SEQUENCE_NUMBER_MARKER);
+}
+
+
uint64_t
Component::toNumber() const
{
- /// \todo Check if Component is of tlv::NumberComponent type
+ if (!isNumber())
+ throw Error("Name component does not have nonNegativeInteger value");
+
return readNonNegativeInteger(*this);
}
uint64_t
Component::toNumberWithMarker(uint8_t marker) const
{
- if (empty() || value()[0] != marker)
- throw Error("Name component does not have the requested marker");
+ if (!isNumberWithMarker(marker))
+ throw Error("Name component does not have the requested marker "
+ "or the value is not a nonNegativeInteger");
Buffer::const_iterator valueBegin = value_begin() + 1;
return tlv::readNonNegativeInteger(value_size() - 1, valueBegin, value_end());
diff --git a/src/name-component.hpp b/src/name-component.hpp
index 7213f9a..ea68084 100644
--- a/src/name-component.hpp
+++ b/src/name-component.hpp
@@ -255,6 +255,59 @@
std::string
toUri() const;
+ ////////////////////////////////////////////////////////////////////////////////
+
+ /**
+ * @brief Check if the component is nonNegativeInteger
+ * @see http://named-data.net/doc/ndn-tlv/tlv.html#non-negative-integer-encoding
+ */
+ bool
+ isNumber() const;
+
+ /**
+ * @brief Check if the component is NameComponentWithMarker per NDN naming conventions
+ * @see http://named-data.net/doc/tech-memos/naming-conventions.pdf
+ */
+ bool
+ isNumberWithMarker(uint8_t marker) const;
+
+ /**
+ * @brief Check if the component is version per NDN naming conventions
+ * @see http://named-data.net/doc/tech-memos/naming-conventions.pdf
+ */
+ bool
+ isVersion() const;
+
+ /**
+ * @brief Check if the component is segment number per NDN naming conventions
+ * @see http://named-data.net/doc/tech-memos/naming-conventions.pdf
+ */
+ bool
+ isSegment() const;
+
+ /**
+ * @brief Check if the component is segment offset per NDN naming conventions
+ * @see http://named-data.net/doc/tech-memos/naming-conventions.pdf
+ */
+ bool
+ isSegmentOffset() const;
+
+ /**
+ * @brief Check if the component is timestamp per NDN naming conventions
+ * @see http://named-data.net/doc/tech-memos/naming-conventions.pdf
+ */
+ bool
+ isTimestamp() const;
+
+ /**
+ * @brief Check if the component is sequence number per NDN naming conventions
+ * @see http://named-data.net/doc/tech-memos/naming-conventions.pdf
+ */
+ bool
+ isSequenceNumber() const;
+
+ ////////////////////////////////////////////////////////////////////////////////
+
/**
* @brief Interpret this name component as nonNegativeInteger
*
@@ -333,6 +386,8 @@
uint64_t
toSequenceNumber() const;
+ ////////////////////////////////////////////////////////////////////////////////
+
/**
* @brief Create a component encoded as nonNegativeInteger
*