diff options
author | Luboš Luňák <l.lunak@collabora.com> | 2021-11-29 19:02:53 +0100 |
---|---|---|
committer | Luboš Luňák <l.lunak@collabora.com> | 2021-11-29 19:42:41 +0100 |
commit | 00e104f88179ebd4b4e7d6f15497b2e968749ff0 (patch) | |
tree | 0bfd3b17504880dbb14d8b54580bc36700730a53 /common/Util.hpp | |
parent | quarantine: add the original file name to the quarantined file name (diff) | |
download | online-stringifyhexline.tar.gz online-stringifyhexline.zip |
make stringifyHexLine() simply work on std::string
stringifyhexline
Trying to construct a string using sprintf() and std::stringstream
is unnecessarily complicated for something as simple as this,
and it shows up in my profiling. This commit makes the hex values
change to uppercase because that's what hexFromByte() returns,
but I don't think it matters.
Signed-off-by: Luboš Luňák <l.lunak@collabora.com>
Change-Id: I31c7b8e7cf4c2a495eca0bf03ae4cab53b26a04b
Diffstat (limited to 'common/Util.hpp')
-rw-r--r-- | common/Util.hpp | 25 |
1 files changed, 14 insertions, 11 deletions
diff --git a/common/Util.hpp b/common/Util.hpp index 5974a8bf20..ee68e66651 100644 --- a/common/Util.hpp +++ b/common/Util.hpp @@ -307,31 +307,34 @@ namespace Util inline std::string stringifyHexLine(const T& buffer, std::size_t offset, const std::size_t width = 32) { - char scratch[64]; - std::stringstream os; + std::string str; + str.reserve(width * 4 + width / 8 + 3 + 1); for (unsigned int i = 0; i < width; i++) { if (i && (i % 8) == 0) - os << ' '; + str.push_back(' '); if ((offset + i) < buffer.size()) - sprintf (scratch, "%.2x ", (unsigned char)buffer[offset+i]); + { + const unsigned short hex = hexFromByte(buffer[offset+i]); + str.push_back(hex >> 8); + str.push_back(hex & 0xff); + str.push_back(' '); + } else - sprintf (scratch, " "); - os << scratch; + str.append(3, ' '); } - os << " | "; + str.append(" | "); for (unsigned int i = 0; i < width; i++) { if ((offset + i) < buffer.size()) - sprintf(scratch, "%c", ::isprint(buffer[offset + i]) ? buffer[offset + i] : '.'); + str.push_back(::isprint(buffer[offset + i]) ? buffer[offset + i] : '.'); else - sprintf(scratch, " "); // Leave blank if we are out of data. - os << scratch; + str.push_back(' '); // Leave blank if we are out of data. } - return os.str(); + return str; } /// Dump data as hex and chars to stream. |