util: rewrite printHex using security::transform instead of iostreams

Change-Id: I84bebeb3b86d643c04ff3ee5a4df66b05ed6c9c6
Refs: #3006
diff --git a/src/util/string-helper.cpp b/src/util/string-helper.cpp
index 4a4808f..3d4ea3f 100644
--- a/src/util/string-helper.cpp
+++ b/src/util/string-helper.cpp
@@ -28,27 +28,15 @@
 #include "../security/transform/stream-sink.hpp"
 
 #include <sstream>
-#include <iomanip>
 
 namespace ndn {
 
 void
 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 (wantUpperCase) {
-    newFlags |= std::ios::uppercase;
-  }
-  auto oldFlags = os.flags(newFlags);
-  auto oldFill = os.fill('0');
-  for (size_t i = 0; i < length; ++i) {
-    os << std::setw(2) << static_cast<unsigned int>(buffer[i]);
-  }
-  os.fill(oldFill);
-  os.flags(oldFlags);
+  namespace tr = security::transform;
+  BOOST_ASSERT(buffer != nullptr || length == 0);
+  tr::bufferSource(buffer, length) >> tr::hexEncode(wantUpperCase) >> tr::streamSink(os);
 }
 
 void
@@ -60,23 +48,15 @@
 std::string
 toHex(const uint8_t* buffer, size_t length, bool wantUpperCase)
 {
-  BOOST_ASSERT(buffer != nullptr || length == 0);
-
-  namespace tr = security::transform;
-
   std::ostringstream result;
-  tr::bufferSource(buffer, length) >> tr::hexEncode(wantUpperCase) >> tr::streamSink(result);
+  printHex(result, buffer, length, wantUpperCase);
   return result.str();
 }
 
 std::string
 toHex(const Buffer& buffer, bool wantUpperCase)
 {
-  namespace tr = security::transform;
-
-  std::ostringstream result;
-  tr::bufferSource(buffer) >> tr::hexEncode(wantUpperCase) >> tr::streamSink(result);
-  return result.str();
+  return toHex(buffer.buf(), buffer.size(), wantUpperCase);
 }
 
 int
@@ -127,9 +107,10 @@
       // Skip ahead past the escaped value.
       i += 2;
     }
-    else
+    else {
       // Just copy through.
       result << str[i];
+    }
   }
 
   return result.str();