summaryrefslogtreecommitdiff
path: root/include/fud_string_convert.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'include/fud_string_convert.hpp')
-rw-r--r--include/fud_string_convert.hpp51
1 files changed, 26 insertions, 25 deletions
diff --git a/include/fud_string_convert.hpp b/include/fud_string_convert.hpp
index 597c6a9..d7a62f0 100644
--- a/include/fud_string_convert.hpp
+++ b/include/fud_string_convert.hpp
@@ -111,12 +111,12 @@ StringConvertResult<T> unsignedFromString(StringView nextView, size_t skipIndex,
static_assert(std::is_unsigned_v<T> && std::is_integral_v<T>);
auto status = checkPlusSigned(nextView, skipIndex);
if (status != FudStatus::Success) {
- return FudStatus::ArgumentInvalid;
+ return FudError{FudStatus::ArgumentInvalid};
}
auto radixResult = impl::getRadix(nextView, skipIndex, specifiedRadixOption);
if (radixResult.isError()) {
- return radixResult.takeError();
+ return FudError{radixResult.takeError()};
}
auto radix = radixResult.takeOkay();
@@ -131,20 +131,20 @@ StringConvertResult<T> unsignedFromString(StringView nextView, size_t skipIndex,
auto digit = static_cast<uint8_t>(digitResult);
if (std::numeric_limits<T>::max() / radix < num) {
- return FudStatus::RangeError;
+ return FudError{FudStatus::RangeError};
}
num *= radix;
if (std::numeric_limits<T>::max() - digit < num) {
- return FudStatus::RangeError;
+ return FudError{FudStatus::RangeError};
}
num += digit;
digitIndex++;
}
if (digitIndex < 1) {
- return FudStatus::ArgumentInvalid;
+ return FudError{FudStatus::ArgumentInvalid};
}
- return ConvertValue{skipIndex + digitIndex, num};
+ return Okay{ConvertValue{skipIndex + digitIndex, num}};
}
template <typename T>
@@ -203,12 +203,12 @@ StringConvertResult<T> signedFromString(StringView nextView, size_t skipIndex, O
static_assert(std::is_signed_v<T> && std::is_integral_v<T>);
auto status = impl::checkPlusSigned(nextView, skipIndex);
if (status != FudStatus::Success) {
- return FudStatus::ArgumentInvalid;
+ return FudError{FudStatus::ArgumentInvalid};
}
auto radixResult = impl::getRadix(nextView, skipIndex, specifiedRadixOption);
if (radixResult.isError()) {
- return radixResult.takeError();
+ return FudError{radixResult.takeError()};
}
auto radix = radixResult.takeOkay();
@@ -217,7 +217,7 @@ StringConvertResult<T> signedFromString(StringView nextView, size_t skipIndex, O
auto isNegativeResult = checkNegative(nextView, skipIndex);
if (isNegativeResult.isError()) {
- return isNegativeResult.takeError();
+ return FudError{isNegativeResult.takeError()};
}
const auto isNegative = isNegativeResult.takeOkay();
@@ -228,14 +228,14 @@ StringConvertResult<T> signedFromString(StringView nextView, size_t skipIndex, O
}
if (status != FudStatus::Success) {
- return status;
+ return FudError{status};
}
if (digitIndex < 1) {
- return FudStatus::ArgumentInvalid;
+ return FudError{FudStatus::ArgumentInvalid};
}
- return ConvertValue{skipIndex + digitIndex, num};
+ return Okay{ConvertValue{skipIndex + digitIndex, num}};
}
template <typename T>
@@ -343,21 +343,22 @@ FudStatus getFraction(const StringView view, size_t& digitIndex, T& num, T sign,
template <typename T>
StringConvertResult<T> floatFromString(StringView nextView, size_t skipIndex, Option<uint8_t> specifiedRadixOption)
{
+ using RetType = StringConvertResult<T>;
static_assert(std::is_floating_point_v<T>);
if (nextView.length() < 1) {
- return FudStatus::ArgumentInvalid;
+ return FudError{FudStatus::ArgumentInvalid};
}
auto isNegativeResult = checkNegative(nextView, skipIndex);
if (isNegativeResult.isError()) {
- return isNegativeResult.takeError();
+ return FudError{isNegativeResult.takeError()};
}
const auto isNegative = isNegativeResult.takeOkay();
if (!isNegative) {
auto status = checkPlusSigned(nextView, skipIndex);
if (status != FudStatus::Success) {
- return FudStatus::ArgumentInvalid;
+ return FudError{FudStatus::ArgumentInvalid};
}
}
T sign = isNegative ? -1.0 : 1.0;
@@ -365,7 +366,7 @@ StringConvertResult<T> floatFromString(StringView nextView, size_t skipIndex, Op
T num = 0;
size_t digitIndex = 0;
- auto retSuccess = [&]() { return ConvertValue{skipIndex + digitIndex, num}; };
+ auto retSuccess = [&]() { return RetType::okay(ConvertValue{skipIndex + digitIndex, num}); };
if (impl::isNanOrInf(num, nextView, sign, digitIndex)) {
return retSuccess();
@@ -373,7 +374,7 @@ StringConvertResult<T> floatFromString(StringView nextView, size_t skipIndex, Op
auto radixResult = impl::getRadix(nextView, skipIndex, specifiedRadixOption);
if (radixResult.isError()) {
- return radixResult.takeError();
+ return FudError{radixResult.takeError()};
}
auto radix = radixResult.takeOkay();
@@ -386,12 +387,12 @@ StringConvertResult<T> floatFromString(StringView nextView, size_t skipIndex, Op
}
if (status != FudStatus::Success) {
- return status;
+ return FudError{status};
}
if (!foundDecimal) {
if (digitIndex < 1) {
- return FudStatus::ArgumentInvalid;
+ return FudError{FudStatus::ArgumentInvalid};
}
return retSuccess();
@@ -402,17 +403,17 @@ StringConvertResult<T> floatFromString(StringView nextView, size_t skipIndex, Op
if (foundExponent) {
status = getExponent(nextView, digitIndex, num, radix);
if (status != FudStatus::Success) {
- return status;
+ return FudError{status};
}
}
if (digitIndex < 1) {
- return FudStatus::ArgumentInvalid;
+ return FudError{FudStatus::ArgumentInvalid};
}
if (std::isinf(num) || std::isnan(num)) // isnan is dubious here - likely unreachable
{
- return FudStatus::RangeError;
+ return FudError{FudStatus::RangeError};
}
return retSuccess();
@@ -430,13 +431,13 @@ template <typename T>
StringConvertResult<T> fromString(StringView inputView, Option<uint8_t> specifiedRadixOption)
{
if (inputView.data() == nullptr) {
- return FudStatus::NullPointer;
+ return FudError{FudStatus::NullPointer};
}
StringView nextView{inputView};
auto skipResult = nextView.skipWhitespace();
if (skipResult.isError()) {
- return skipResult.takeError();
+ return FudError{skipResult.takeError()};
}
size_t skipIndex = skipResult.takeOkay();
@@ -447,7 +448,7 @@ StringConvertResult<T> fromString(StringView inputView, Option<uint8_t> specifie
} else if constexpr (std::is_floating_point_v<T>) {
return impl::floatFromString<T>(nextView, skipIndex, specifiedRadixOption);
} else {
- return FudStatus::NotImplemented;
+ return FudError{FudStatus::NotImplemented};
}
}