Use std::string_view throughout util strencodings/string

This commit is contained in:
Pieter Wuille
2022-04-04 15:05:47 -04:00
committed by MacroFake
parent 8ffbd1412d
commit e7d2fbda63
9 changed files with 69 additions and 56 deletions

View File

@@ -94,7 +94,7 @@ std::vector<unsigned char> ParseHex(std::string_view str)
return vch;
}
void SplitHostPort(std::string in, uint16_t& portOut, std::string& hostOut)
void SplitHostPort(std::string_view in, uint16_t& portOut, std::string& hostOut)
{
size_t colon = in.find_last_of(':');
// if a : is found, and it either follows a [...], or no other : is in the string, treat it as port separator
@@ -176,7 +176,7 @@ std::string EncodeBase32(Span<const unsigned char> input, bool pad)
return str;
}
std::string EncodeBase32(const std::string& str, bool pad)
std::string EncodeBase32(std::string_view str, bool pad)
{
return EncodeBase32(MakeUCharSpan(str), pad);
}
@@ -221,7 +221,7 @@ std::optional<std::vector<unsigned char>> DecodeBase32(std::string_view str)
namespace {
template <typename T>
bool ParseIntegral(const std::string& str, T* out)
bool ParseIntegral(std::string_view str, T* out)
{
static_assert(std::is_integral<T>::value);
// Replicate the exact behavior of strtol/strtoll/strtoul/strtoull when
@@ -240,37 +240,37 @@ bool ParseIntegral(const std::string& str, T* out)
}
}; // namespace
bool ParseInt32(const std::string& str, int32_t* out)
bool ParseInt32(std::string_view str, int32_t* out)
{
return ParseIntegral<int32_t>(str, out);
}
bool ParseInt64(const std::string& str, int64_t* out)
bool ParseInt64(std::string_view str, int64_t* out)
{
return ParseIntegral<int64_t>(str, out);
}
bool ParseUInt8(const std::string& str, uint8_t* out)
bool ParseUInt8(std::string_view str, uint8_t* out)
{
return ParseIntegral<uint8_t>(str, out);
}
bool ParseUInt16(const std::string& str, uint16_t* out)
bool ParseUInt16(std::string_view str, uint16_t* out)
{
return ParseIntegral<uint16_t>(str, out);
}
bool ParseUInt32(const std::string& str, uint32_t* out)
bool ParseUInt32(std::string_view str, uint32_t* out)
{
return ParseIntegral<uint32_t>(str, out);
}
bool ParseUInt64(const std::string& str, uint64_t* out)
bool ParseUInt64(std::string_view str, uint64_t* out)
{
return ParseIntegral<uint64_t>(str, out);
}
std::string FormatParagraph(const std::string& in, size_t width, size_t indent)
std::string FormatParagraph(std::string_view in, size_t width, size_t indent)
{
assert(width >= indent);
std::stringstream out;
@@ -339,7 +339,7 @@ static inline bool ProcessMantissaDigit(char ch, int64_t &mantissa, int &mantiss
return true;
}
bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out)
bool ParseFixedPoint(std::string_view val, int decimals, int64_t *amount_out)
{
int64_t mantissa = 0;
int64_t exponent = 0;
@@ -431,14 +431,14 @@ bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out)
return true;
}
std::string ToLower(const std::string& str)
std::string ToLower(std::string_view str)
{
std::string r;
for (auto ch : str) r += ToLower(ch);
return r;
}
std::string ToUpper(const std::string& str)
std::string ToUpper(std::string_view str)
{
std::string r;
for (auto ch : str) r += ToUpper(ch);
@@ -466,7 +466,7 @@ std::string HexStr(const Span<const uint8_t> s)
return rv;
}
std::optional<uint64_t> ParseByteUnits(const std::string& str, ByteUnit default_multiplier)
std::optional<uint64_t> ParseByteUnits(std::string_view str, ByteUnit default_multiplier)
{
if (str.empty()) {
return std::nullopt;