Make error messages in stringconversion.h more verbose
This commit is contained in:
parent
f4920abebc
commit
7125e52e87
|
@ -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.
|
* \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.
|
* \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)
|
template <typename CharType> CharType charToDigit(CharType character, CharType base)
|
||||||
{
|
{
|
||||||
CharType res;
|
CharType 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') {
|
||||||
res = character - 'a' + 10;
|
res = character - 'a' + 10;
|
||||||
} else if (character >= 'A' && character <= 'Z') {
|
} else if (character >= 'A' && character <= 'Z') {
|
||||||
res = character - 'A' + 10;
|
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).
|
* \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.
|
* \throws A ConversionException will be thrown if the provided \a string is not a valid number.
|
||||||
* \sa numberToString(), bufferToNumber()
|
* \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>
|
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)
|
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).
|
* \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.
|
* \throws A ConversionException will be thrown if the provided \a string is not a valid number.
|
||||||
* \sa numberToString(), bufferToNumber()
|
* \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>
|
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)
|
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
|
* \remarks This function is using std::basic_stringstream interanlly and hence also has its limitations (eg. regarding
|
||||||
* \a base and types).
|
* \a base and types).
|
||||||
* \sa numberToString(), bufferToNumber()
|
* \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>
|
template <typename FloatingType, class StringType, Traits::EnableIf<std::is_floating_point<FloatingType>> * = nullptr>
|
||||||
FloatingType stringToNumber(const StringType &string, typename StringType::value_type base = 10)
|
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;
|
FloatingType result;
|
||||||
if ((ss >> result) && ss.eof()) {
|
if ((ss >> result) && ss.eof()) {
|
||||||
return result;
|
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).
|
* \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.
|
* \throws A ConversionException will be thrown if the provided \a string is not a valid number.
|
||||||
* \sa numberToString(), bufferToNumber()
|
* \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>
|
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)
|
IntegralType stringToNumber(const CharType *string, unsigned char base = 10)
|
||||||
|
|
Loading…
Reference in New Issue