Fix reserving size for error message in `charToDigit()`

This commit is contained in:
Martchus 2024-02-27 02:16:52 +01:00
parent c9cd44ceee
commit 909346c199
1 changed files with 7 additions and 5 deletions

View File

@ -490,7 +490,7 @@ StringType numberToString(FloatingType number, int base = 10)
*/ */
template <typename CharType> CharType charToDigit(CharType character, CharType base) template <typename CharType> CharType charToDigit(CharType character, CharType base)
{ {
CharType res = base; auto res = base;
if (character >= '0' && character <= '9') { if (character >= '0' && character <= '9') {
res = character - '0'; res = character - '0';
} else if (character >= 'a' && character <= 'z') { } else if (character >= 'a' && character <= 'z') {
@ -501,11 +501,13 @@ template <typename CharType> CharType charToDigit(CharType character, CharType b
if (res < base) { if (res < base) {
return res; return res;
} }
std::string errorMsg; constexpr auto msgBegin = std::string_view("The character \"");
errorMsg.reserve(36); constexpr auto msgEnd = std::string_view("\" is no valid digit.");
errorMsg += "The character \""; auto errorMsg = std::string();
errorMsg.reserve(msgBegin.size() + msgEnd.size() + 2);
errorMsg += msgBegin;
errorMsg += character >= ' ' && character <= '~' ? static_cast<std::string::value_type>(character) : '?'; errorMsg += character >= ' ' && character <= '~' ? static_cast<std::string::value_type>(character) : '?';
errorMsg += "\" is no valid digit."; errorMsg += msgEnd;
throw ConversionException(std::move(errorMsg)); throw ConversionException(std::move(errorMsg));
} }