From b23985875aaefcc252159f99566d78feb9dd7e20 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sun, 24 Mar 2024 16:32:59 +0800 Subject: [PATCH] perf: string constructor --- src/impl/wrapper.cc | 3 +-- test/hash.cc | 7 +++---- test/stream.cc | 3 +-- 3 files changed, 5 insertions(+), 8 deletions(-) diff --git a/src/impl/wrapper.cc b/src/impl/wrapper.cc index e982f93..6995e4c 100644 --- a/src/impl/wrapper.cc +++ b/src/impl/wrapper.cc @@ -5,8 +5,7 @@ using ::md5::impl::MD5; std::string MD5::Digest() const { - std::string result {}; - result.resize(32); + std::string result(32, 0x00); auto *ptr = reinterpret_cast(&ctx_); for (int i = 0; i < 32; ++ptr) { result[i++] = HexTable[*ptr >> 4]; diff --git a/test/hash.cc b/test/hash.cc index 43026e1..82fc6ae 100644 --- a/test/hash.cc +++ b/test/hash.cc @@ -262,11 +262,10 @@ const std::array, 256> test_items {{ {0xff, "11b7aaa64c413d2f0fccf893881c46a2"}, }}; -constexpr std::string build_test_data(uint8_t index) { - std::string data {}; - data.reserve(index); +std::string build_test_data(uint8_t index) { + std::string data(index, 0x00); for (uint8_t i = 0; i < index; ++i) { - data.push_back(static_cast(i)); + data[i] = static_cast(i); } return data; }; diff --git a/test/stream.cc b/test/stream.cc index fb45e94..21e4774 100644 --- a/test/stream.cc +++ b/test/stream.cc @@ -4,8 +4,7 @@ using md5::MD5; TEST(md5sum, stream) { - std::string test_data {}; - test_data.resize(256 * 256); // max -> 65536 + std::string test_data(256 * 256, 0x00); // max -> 65536 for (uint64_t i = 0; i < test_data.size(); ++i) { test_data[i] = static_cast(i & 0xff); }