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)
{
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 <typename CharType> 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<std::string::value_type>(character) : '?';
errorMsg += "\" is no valid digit.";
errorMsg += msgEnd;
throw ConversionException(std::move(errorMsg));
}