From 909346c1993d1409ea36e7957f0bb87086f07db6 Mon Sep 17 00:00:00 2001 From: Martchus Date: Tue, 27 Feb 2024 02:16:52 +0100 Subject: [PATCH] Fix reserving size for error message in `charToDigit()` --- conversion/stringconversion.h | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/conversion/stringconversion.h b/conversion/stringconversion.h index 53a1d76..222b44f 100644 --- a/conversion/stringconversion.h +++ b/conversion/stringconversion.h @@ -490,7 +490,7 @@ StringType numberToString(FloatingType number, int base = 10) */ template CharType charToDigit(CharType character, CharType base) { - CharType res = base; + auto res = base; if (character >= '0' && character <= '9') { res = character - '0'; } else if (character >= 'a' && character <= 'z') { @@ -501,11 +501,13 @@ template CharType charToDigit(CharType character, CharType b if (res < base) { return res; } - std::string errorMsg; - errorMsg.reserve(36); - errorMsg += "The character \""; + constexpr auto msgBegin = std::string_view("The character \""); + constexpr auto msgEnd = std::string_view("\" is no valid digit."); + auto errorMsg = std::string(); + errorMsg.reserve(msgBegin.size() + msgEnd.size() + 2); + errorMsg += msgBegin; errorMsg += character >= ' ' && character <= '~' ? static_cast(character) : '?'; - errorMsg += "\" is no valid digit."; + errorMsg += msgEnd; throw ConversionException(std::move(errorMsg)); }