From a5de33496ccd27ca6d11a245c69f70fd28457092 Mon Sep 17 00:00:00 2001 From: Luboš Luňák Date: Mon, 29 Nov 2021 19:02:53 +0100 Subject: make stringifyHexLine() simply work on std::string MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Change-Id: I31c7b8e7cf4c2a495eca0bf03ae4cab53b26a04b --- common/Util.hpp | 25 ++++++++++++++----------- test/Makefile.am | 1 + test/UtilTests.cpp | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 11 deletions(-) create mode 100644 test/UtilTests.cpp 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. diff --git a/test/Makefile.am b/test/Makefile.am index 22b949c11a..f648150ea6 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -81,6 +81,7 @@ test_base_source = \ WhiteBoxTests.cpp \ HttpWhiteBoxTests.cpp \ DeltaTests.cpp \ + UtilTests.cpp \ WopiProofTests.cpp \ $(wsd_sources) diff --git a/test/UtilTests.cpp b/test/UtilTests.cpp new file mode 100644 index 0000000000..67f26fc3f5 --- /dev/null +++ b/test/UtilTests.cpp @@ -0,0 +1,39 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include + +#include + +#include + +/// Util unit-tests. +class UtilTests : public CPPUNIT_NS::TestFixture +{ + CPPUNIT_TEST_SUITE(UtilTests); + + CPPUNIT_TEST(testStringifyHexLine); + + CPPUNIT_TEST_SUITE_END(); + + void testStringifyHexLine(); +}; + +void UtilTests::testStringifyHexLine() +{ + std::string test("hello here\ntest"); + std::string result1("68 65 6C 6C 6F 20 68 65 72 65 0A 74 65 73 74" + " " + "| hello here.test "); + std::string result2("68 65 72 65 0A 74 | here.t"); + LOK_ASSERT_EQUAL(result1, Util::stringifyHexLine(test, 0)); + LOK_ASSERT_EQUAL(result2, Util::stringifyHexLine(test, 6, 6)); +} + +CPPUNIT_TEST_SUITE_REGISTRATION(UtilTests); + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ -- cgit