encoding: simplify Estimator
Change-Id: I29b2b4dd0f6c1dce35c6355905f0dc0bae3790c6
diff --git a/ndn-cxx/encoding/estimator.cpp b/ndn-cxx/encoding/estimator.cpp
index bd1fc9a..5e3a31a 100644
--- a/ndn-cxx/encoding/estimator.cpp
+++ b/ndn-cxx/encoding/estimator.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
- * Copyright (c) 2013-2018 Regents of the University of California.
+ * Copyright (c) 2013-2021 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
@@ -25,59 +25,33 @@
namespace encoding {
size_t
-Estimator::prependVarNumber(uint64_t varNumber) const noexcept
+Estimator::prependVarNumber(uint64_t n) const noexcept
{
- if (varNumber < 253) {
- return 1;
- }
- else if (varNumber <= std::numeric_limits<uint16_t>::max()) {
- return 3;
- }
- else if (varNumber <= std::numeric_limits<uint32_t>::max()) {
- return 5;
- }
- else {
- return 9;
- }
+ return tlv::sizeOfVarNumber(n);
}
size_t
-Estimator::appendVarNumber(uint64_t varNumber) const noexcept
+Estimator::appendVarNumber(uint64_t n) const noexcept
{
- return prependVarNumber(varNumber);
+ return tlv::sizeOfVarNumber(n);
}
size_t
-Estimator::prependNonNegativeInteger(uint64_t varNumber) const noexcept
+Estimator::prependNonNegativeInteger(uint64_t n) const noexcept
{
- if (varNumber <= std::numeric_limits<uint8_t>::max()) {
- return 1;
- }
- else if (varNumber <= std::numeric_limits<uint16_t>::max()) {
- return 2;
- }
- else if (varNumber <= std::numeric_limits<uint32_t>::max()) {
- return 4;
- }
- else {
- return 8;
- }
+ return tlv::sizeOfNonNegativeInteger(n);
}
size_t
-Estimator::appendNonNegativeInteger(uint64_t varNumber) const noexcept
+Estimator::appendNonNegativeInteger(uint64_t n) const noexcept
{
- return prependNonNegativeInteger(varNumber);
+ return tlv::sizeOfNonNegativeInteger(n);
}
size_t
Estimator::prependByteArrayBlock(uint32_t type, const uint8_t* array, size_t arraySize) const noexcept
{
- size_t totalLength = arraySize;
- totalLength += prependVarNumber(arraySize);
- totalLength += prependVarNumber(type);
-
- return totalLength;
+ return tlv::sizeOfVarNumber(type) + tlv::sizeOfVarNumber(arraySize) + arraySize;
}
size_t
diff --git a/ndn-cxx/encoding/estimator.hpp b/ndn-cxx/encoding/estimator.hpp
index b5b3654..5341ccd 100644
--- a/ndn-cxx/encoding/estimator.hpp
+++ b/ndn-cxx/encoding/estimator.hpp
@@ -28,15 +28,17 @@
namespace encoding {
/**
- * @brief Helper class to estimate size of TLV encoding
- * Interface of this class (mostly) matches interface of Encoder class
+ * @brief Helper class to estimate size of TLV encoding.
+ *
+ * The interface of this class (mostly) matches that of the Encoder class.
+ *
* @sa Encoder
*/
class Estimator : noncopyable
{
public: // common interface between Encoder and Estimator
/**
- * @brief Prepend a byte
+ * @brief Prepend a single byte
*/
constexpr size_t
prependByte(uint8_t) const noexcept
@@ -45,7 +47,7 @@
}
/**
- * @brief Append a byte
+ * @brief Append a single byte
*/
constexpr size_t
appendByte(uint8_t) const noexcept
@@ -72,7 +74,7 @@
}
/**
- * @brief Prepend range of bytes from the range [@p first, @p last)
+ * @brief Prepend bytes from the range [@p first, @p last)
*/
template<class Iterator>
size_t
@@ -82,7 +84,7 @@
}
/**
- * @brief Append range of bytes from the range [@p first, @p last)
+ * @brief Append bytes from the range [@p first, @p last)
*/
template<class Iterator>
size_t
@@ -92,32 +94,28 @@
}
/**
- * @brief Prepend VarNumber @p varNumber of NDN TLV encoding
- * @sa http://named-data.net/doc/ndn-tlv/
+ * @brief Prepend @p n in VarNumber encoding
*/
size_t
- prependVarNumber(uint64_t varNumber) const noexcept;
+ prependVarNumber(uint64_t n) const noexcept;
/**
- * @brief Prepend VarNumber @p varNumber of NDN TLV encoding
- * @sa http://named-data.net/doc/ndn-tlv/
+ * @brief Append @p n in VarNumber encoding
*/
size_t
- appendVarNumber(uint64_t varNumber) const noexcept;
+ appendVarNumber(uint64_t n) const noexcept;
/**
- * @brief Prepend non-negative integer @p integer of NDN TLV encoding
- * @sa http://named-data.net/doc/ndn-tlv/
+ * @brief Prepend @p n in NonNegativeInteger encoding
*/
size_t
- prependNonNegativeInteger(uint64_t integer) const noexcept;
+ prependNonNegativeInteger(uint64_t n) const noexcept;
/**
- * @brief Append non-negative integer @p integer of NDN TLV encoding
- * @sa http://named-data.net/doc/ndn-tlv/
+ * @brief Append @p n in NonNegativeInteger encoding
*/
size_t
- appendNonNegativeInteger(uint64_t integer) const noexcept;
+ appendNonNegativeInteger(uint64_t n) const noexcept;
/**
* @brief Prepend TLV block of type @p type and value from buffer @p array of size @p arraySize