util: rewrite toHex using security::transform instead of printHex
Change-Id: If07c434419f44decc866c97adf083051597e7ba6
Refs: #3006
diff --git a/src/util/string-helper.cpp b/src/util/string-helper.cpp
index ed3c961..44083b3 100644
--- a/src/util/string-helper.cpp
+++ b/src/util/string-helper.cpp
@@ -23,6 +23,7 @@
#include "../encoding/buffer-stream.hpp"
#include "../security/transform/buffer-source.hpp"
#include "../security/transform/hex-decode.hpp"
+#include "../security/transform/hex-encode.hpp"
#include "../security/transform/stream-sink.hpp"
#include <boost/algorithm/string/trim.hpp>
@@ -33,13 +34,13 @@
namespace ndn {
void
-printHex(std::ostream& os, const uint8_t* buffer, size_t length, bool isUpperCase/* = true*/)
+printHex(std::ostream& os, const uint8_t* buffer, size_t length, bool wantUpperCase)
{
if (buffer == nullptr || length == 0)
return;
auto newFlags = std::ios::hex;
- if (isUpperCase) {
+ if (wantUpperCase) {
newFlags |= std::ios::uppercase;
}
auto oldFlags = os.flags(newFlags);
@@ -52,26 +53,31 @@
}
void
-printHex(std::ostream& os, const Buffer& buffer, bool isUpperCase/* = true*/)
+printHex(std::ostream& os, const Buffer& buffer, bool wantUpperCase)
{
- return printHex(os, buffer.buf(), buffer.size(), isUpperCase);
+ return printHex(os, buffer.buf(), buffer.size(), wantUpperCase);
}
std::string
-toHex(const uint8_t* buffer, size_t length, bool isUpperCase/* = true*/)
+toHex(const uint8_t* buffer, size_t length, bool wantUpperCase)
{
- if (buffer == nullptr || length == 0)
- return "";
+ BOOST_ASSERT(buffer != nullptr || length == 0);
+
+ namespace tr = security::transform;
std::ostringstream result;
- printHex(result, buffer, length, isUpperCase);
+ tr::bufferSource(buffer, length) >> tr::hexEncode(wantUpperCase) >> tr::streamSink(result);
return result.str();
}
std::string
-toHex(const Buffer& buffer, bool isUpperCase/* = true*/)
+toHex(const Buffer& buffer, bool wantUpperCase)
{
- return toHex(buffer.buf(), buffer.size(), isUpperCase);
+ namespace tr = security::transform;
+
+ std::ostringstream result;
+ tr::bufferSource(buffer) >> tr::hexEncode(wantUpperCase) >> tr::streamSink(result);
+ return result.str();
}
int
diff --git a/src/util/string-helper.hpp b/src/util/string-helper.hpp
index 3b2011f..7785f69 100644
--- a/src/util/string-helper.hpp
+++ b/src/util/string-helper.hpp
@@ -43,7 +43,7 @@
* @param os Output stream
* @param buffer The array of bytes
* @param length Size of the array
- * @param isUpperCase if true (default) output use uppercase for hex values
+ * @param wantUpperCase if true (default) output use uppercase for hex values
*
* Examples:
*
@@ -55,24 +55,24 @@
* The output string is a continuous sequence of hex characters without any whitespace separators.
*/
void
-printHex(std::ostream& os, const uint8_t* buffer, size_t length, bool isUpperCase = true);
+printHex(std::ostream& os, const uint8_t* buffer, size_t length, bool wantUpperCase = true);
/**
* @brief Output the hex representation of the bytes in the @p buffer to the output stream @p os
*
* @param os Output stream
* @param buffer The array of bytes
- * @param isUpperCase if true (default) output use uppercase for hex values
+ * @param wantUpperCase if true (default) output use uppercase for hex values
*/
void
-printHex(std::ostream& os, const Buffer& buffer, bool isUpperCase = true);
+printHex(std::ostream& os, const Buffer& buffer, bool wantUpperCase = true);
/**
* @brief Return the hex representation of the bytes in array
*
* @param buffer The array of bytes
* @param length Size of the array
- * @param isUpperCase if true (default) output use uppercase for hex values
+ * @param wantUpperCase if true (default) output use uppercase for hex values
*
* Examples:
*
@@ -84,16 +84,16 @@
* The output string is a continuous sequence of hex characters without any whitespace separators.
*/
std::string
-toHex(const uint8_t* buffer, size_t length, bool isUpperCase = true);
+toHex(const uint8_t* buffer, size_t length, bool wantUpperCase = true);
/**
* @brief Return the hex representation of the bytes in the @p buffer to the output stream @p os
*
* @param buffer The array of bytes
- * @param isUpperCase if true (default) output use uppercase for hex values
+ * @param wantUpperCase if true (default) output use uppercase for hex values
*/
std::string
-toHex(const Buffer& buffer, bool isUpperCase = true);
+toHex(const Buffer& buffer, bool wantUpperCase = true);
/**
* @brief Convert the hex character to an integer from 0 to 15, or -1 if not a hex character