name: New method to get "successor" of a name
Change-Id: I41a53e64994970121766bc92cd9dd4796ba5fcfb
refs #1677
diff --git a/src/name-component.hpp b/src/name-component.hpp
index b7b529f..0e17580 100644
--- a/src/name-component.hpp
+++ b/src/name-component.hpp
@@ -290,6 +290,9 @@
return !hasValue();
}
+ Component
+ getSuccessor() const;
+
/**
* @brief Check if this is the same component as other
*
@@ -566,6 +569,34 @@
return std::memcmp(value(), other.value(), value_size());
}
+inline Component
+Component::getSuccessor() const
+{
+ size_t totalLength = 0;
+ EncodingBuffer encoder(size() + 1, 1); // + 1 in case there is an overflow
+ // in unlikely case TLV length changes more,
+ // EncodingBuffer will take care of that
+
+ bool isOverflow = true;
+ size_t i = value_size();
+ for (; isOverflow && i > 0; i--) {
+ uint8_t newValue = static_cast<uint8_t>((value()[i - 1] + 1) & 0xFF);
+ totalLength += encoder.prependByte(newValue);
+ isOverflow = (newValue == 0);
+ }
+ totalLength += encoder.prependByteArray(value(), i);
+
+ if (isOverflow) {
+ // new name components has to be extended
+ totalLength += encoder.appendByte(0);
+ }
+
+ totalLength += encoder.prependVarNumber(totalLength);
+ totalLength += encoder.prependVarNumber(Tlv::NameComponent);
+
+ return encoder.block();
+}
+
template<bool T>
inline size_t