Make error messages in stringconversion.h more verbose

This commit is contained in:
Martchus 2018-05-31 22:28:38 +02:00
parent f4920abebc
commit 7125e52e87
1 changed files with 20 additions and 8 deletions

View File

@ -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 <typename CharType> 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 <typename CharType> 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 <typename IntegralType, typename StringType, Traits::EnableIf<std::is_integral<IntegralType>, std::is_unsigned<IntegralType>> * = 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 <typename IntegralType, class StringType, Traits::EnableIf<std::is_integral<IntegralType>, std::is_signed<IntegralType>> * = 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 <typename FloatingType, class StringType, Traits::EnableIf<std::is_floating_point<FloatingType>> * = 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 <typename IntegralType, class CharType, Traits::EnableIf<std::is_integral<IntegralType>, std::is_unsigned<IntegralType>> * = nullptr>
IntegralType stringToNumber(const CharType *string, unsigned char base = 10)