From 957f83207433fec3d9a9b464c6dad0488f3934c8 Mon Sep 17 00:00:00 2001 From: Vaci Koblizek Date: Tue, 28 Mar 2023 17:20:33 +0100 Subject: Avoid a string copy during Hash::to_string --- src/libutil/hash.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libutil/hash.cc b/src/libutil/hash.cc index d2fd0c15a..5a7be47b2 100644 --- a/src/libutil/hash.cc +++ b/src/libutil/hash.cc @@ -130,7 +130,7 @@ std::string Hash::to_string(Base base, bool includeType) const break; case Base64: case SRI: - s += base64Encode(std::string((const char *) hash, hashSize)); + s += base64Encode(std::string_view((const char *) hash, hashSize)); break; } return s; -- cgit v1.2.3 From ccf7ce26fe6207d7a648715a45985379f772931f Mon Sep 17 00:00:00 2001 From: Vaci Koblizek Date: Wed, 29 Mar 2023 10:44:22 +0100 Subject: return string_view from printHashType rather than string --- src/libutil/hash.cc | 2 +- src/libutil/hash.hh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libutil/hash.cc b/src/libutil/hash.cc index 5a7be47b2..84832c4ad 100644 --- a/src/libutil/hash.cc +++ b/src/libutil/hash.cc @@ -403,7 +403,7 @@ HashType parseHashType(std::string_view s) throw UsageError("unknown hash algorithm '%1%'", s); } -std::string printHashType(HashType ht) +std::string_view printHashType(HashType ht) { switch (ht) { case htMD5: return "md5"; diff --git a/src/libutil/hash.hh b/src/libutil/hash.hh index 00f70a572..38d09646e 100644 --- a/src/libutil/hash.hh +++ b/src/libutil/hash.hh @@ -133,7 +133,7 @@ HashType parseHashType(std::string_view s); std::optional parseHashTypeOpt(std::string_view s); /* And the reverse. */ -std::string printHashType(HashType ht); +std::string_view printHashType(HashType ht); union Ctx; -- cgit v1.2.3 From a4dd87a2b303af107e0671ce6e8873b5a2d90b3e Mon Sep 17 00:00:00 2001 From: Vaci Koblizek Date: Wed, 29 Mar 2023 10:54:27 +0100 Subject: avoid a string copy in printHash16 --- src/libutil/hash.cc | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/libutil/hash.cc b/src/libutil/hash.cc index 84832c4ad..5735e4715 100644 --- a/src/libutil/hash.cc +++ b/src/libutil/hash.cc @@ -71,12 +71,13 @@ const std::string base16Chars = "0123456789abcdef"; static std::string printHash16(const Hash & hash) { - char buf[hash.hashSize * 2]; + std::string buf; + buf.reserve(hash.hashSize * 2); for (unsigned int i = 0; i < hash.hashSize; i++) { - buf[i * 2] = base16Chars[hash.hash[i] >> 4]; - buf[i * 2 + 1] = base16Chars[hash.hash[i] & 0x0f]; + buf.push_back(base16Chars[hash.hash[i] >> 4]); + buf.push_back(base16Chars[hash.hash[i] & 0x0f]); } - return std::string(buf, hash.hashSize * 2); + return buf; } -- cgit v1.2.3