From 41d70a2fc8d243d8c83ecc1c9ba648b625957437 Mon Sep 17 00:00:00 2001 From: pennae Date: Fri, 21 Jan 2022 14:44:00 +0100 Subject: return string_views from forceString* once a string has been forced we already have dynamic storage allocated for it, so we can easily reuse that storage instead of copying. --- src/libutil/hash.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/libutil/hash.cc') diff --git a/src/libutil/hash.cc b/src/libutil/hash.cc index 4df8b4ecb..6ed00d43c 100644 --- a/src/libutil/hash.cc +++ b/src/libutil/hash.cc @@ -259,7 +259,7 @@ Hash::Hash(std::string_view rest, HashType type, bool isSRI) throw BadHash("hash '%s' has wrong length for hash type '%s'", rest, printHashType(this->type)); } -Hash newHashAllowEmpty(std::string hashStr, std::optional ht) +Hash newHashAllowEmpty(std::string_view hashStr, std::optional ht) { if (hashStr.empty()) { if (!ht) -- cgit v1.2.3 From df552ff53e68dff8ca360adbdbea214ece1d08ee Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Fri, 25 Feb 2022 16:00:00 +0100 Subject: Remove std::string alias (for real this time) Also use std::string_view in a few more places. --- src/libutil/hash.cc | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) (limited to 'src/libutil/hash.cc') diff --git a/src/libutil/hash.cc b/src/libutil/hash.cc index 6ed00d43c..a4d632161 100644 --- a/src/libutil/hash.cc +++ b/src/libutil/hash.cc @@ -66,31 +66,31 @@ bool Hash::operator < (const Hash & h) const } -const string base16Chars = "0123456789abcdef"; +const std::string base16Chars = "0123456789abcdef"; -static string printHash16(const Hash & hash) +static std::string printHash16(const Hash & hash) { char buf[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]; } - return string(buf, hash.hashSize * 2); + return std::string(buf, hash.hashSize * 2); } // omitted: E O U T -const string base32Chars = "0123456789abcdfghijklmnpqrsvwxyz"; +const std::string base32Chars = "0123456789abcdfghijklmnpqrsvwxyz"; -static string printHash32(const Hash & hash) +static std::string printHash32(const Hash & hash) { assert(hash.hashSize); size_t len = hash.base32Len(); assert(len); - string s; + std::string s; s.reserve(len); for (int n = (int) len - 1; n >= 0; n--) { @@ -107,7 +107,7 @@ static string printHash32(const Hash & hash) } -string printHash16or32(const Hash & hash) +std::string printHash16or32(const Hash & hash) { assert(hash.type); return hash.to_string(hash.type == htMD5 ? Base16 : Base32, false); @@ -151,7 +151,8 @@ Hash Hash::parseSRI(std::string_view original) { } // Mutates the string to eliminate the prefixes when found -static std::pair, bool> getParsedTypeAndSRI(std::string_view & rest) { +static std::pair, bool> getParsedTypeAndSRI(std::string_view & rest) +{ bool isSRI = false; // Parse the has type before the separater, if there was one. @@ -402,7 +403,7 @@ HashType parseHashType(std::string_view s) throw UsageError("unknown hash algorithm '%1%'", s); } -string printHashType(HashType ht) +std::string printHashType(HashType ht) { switch (ht) { case htMD5: return "md5"; -- cgit v1.2.3