diff --git a/conversion/stringconversion.h b/conversion/stringconversion.h index 5cb0122..6d8eb4a 100644 --- a/conversion/stringconversion.h +++ b/conversion/stringconversion.h @@ -352,23 +352,27 @@ StringType numberToString(FloatingType number, typename StringType::value_type b /*! * \brief Returns number/digit of the specified \a character representation using the specified \a base. * \throws A ConversionException will be thrown if the provided \a character does not represent a valid digit for the specified \a base. + * \todo Provide an alternative using std::expected (when switching to C++17). */ template CharType charToDigit(CharType character, CharType base) { - CharType res; + CharType res = base; if (character >= '0' && character <= '9') { res = character - '0'; } else if (character >= 'a' && character <= 'z') { res = character - 'a' + 10; } else if (character >= 'A' && character <= 'Z') { res = character - 'A' + 10; - } else { - throw ConversionException("The string is no valid number"); } - if (res >= base) { - throw ConversionException("The string is no valid number"); + if (res < base) { + return res; } - return res; + std::string errorMsg; + errorMsg.reserve(36); + errorMsg += "The character \""; + errorMsg += character; + errorMsg += "\" is no valid digit."; + throw ConversionException(std::move(errorMsg)); } /*! @@ -377,6 +381,7 @@ template CharType charToDigit(CharType character, CharType b * \tparam StringType The string type (should be an instantiation of the basic_string class template). * \throws A ConversionException will be thrown if the provided \a string is not a valid number. * \sa numberToString(), bufferToNumber() + * \todo Provide an alternative using std::expected (when switching to C++17). */ template , std::is_unsigned> * = nullptr> IntegralType stringToNumber(const StringType &string, typename StringType::value_type base = 10) @@ -398,6 +403,7 @@ IntegralType stringToNumber(const StringType &string, typename StringType::value * \tparam StringType The string type (should be an instantiation of the basic_string class template). * \throws A ConversionException will be thrown if the provided \a string is not a valid number. * \sa numberToString(), bufferToNumber() + * \todo Provide an alternative using std::expected (when switching to C++17). */ template , std::is_signed> * = nullptr> IntegralType stringToNumber(const StringType &string, typename StringType::value_type base = 10) @@ -432,6 +438,7 @@ IntegralType stringToNumber(const StringType &string, typename StringType::value * \remarks This function is using std::basic_stringstream interanlly and hence also has its limitations (eg. regarding * \a base and types). * \sa numberToString(), bufferToNumber() + * \todo Provide an alternative using std::expected (when switching to C++17). */ template > * = nullptr> FloatingType stringToNumber(const StringType &string, typename StringType::value_type base = 10) @@ -441,9 +448,13 @@ FloatingType stringToNumber(const StringType &string, typename StringType::value FloatingType result; if ((ss >> result) && ss.eof()) { return result; - } else { - throw ConversionException("The string is no valid number."); } + std::string errorMsg; + errorMsg.reserve(42 + string.size()); + errorMsg += "The string \""; + errorMsg += string; + errorMsg += "\" is no valid floating number."; + throw ConversionException(errorMsg); } /*! @@ -452,6 +463,7 @@ FloatingType stringToNumber(const StringType &string, typename StringType::value * \tparam StringType The string type (should be an instantiation of the basic_string class template). * \throws A ConversionException will be thrown if the provided \a string is not a valid number. * \sa numberToString(), bufferToNumber() + * \todo Provide an alternative using std::expected (when switching to C++17). */ template , std::is_unsigned> * = nullptr> IntegralType stringToNumber(const CharType *string, unsigned char base = 10)