From 8ce397e8c0a83e49e390de9deb73d588e4931ecf Mon Sep 17 00:00:00 2001 From: Dominick Allen Date: Tue, 29 Oct 2024 21:02:25 -0500 Subject: Reworking of Result. --- include/fud_string_convert.hpp | 51 +++++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 25 deletions(-) (limited to 'include/fud_string_convert.hpp') 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 unsignedFromString(StringView nextView, size_t skipIndex, static_assert(std::is_unsigned_v && std::is_integral_v); 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 unsignedFromString(StringView nextView, size_t skipIndex, auto digit = static_cast(digitResult); if (std::numeric_limits::max() / radix < num) { - return FudStatus::RangeError; + return FudError{FudStatus::RangeError}; } num *= radix; if (std::numeric_limits::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 @@ -203,12 +203,12 @@ StringConvertResult signedFromString(StringView nextView, size_t skipIndex, O static_assert(std::is_signed_v && std::is_integral_v); 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 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 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 @@ -343,21 +343,22 @@ FudStatus getFraction(const StringView view, size_t& digitIndex, T& num, T sign, template StringConvertResult floatFromString(StringView nextView, size_t skipIndex, Option specifiedRadixOption) { + using RetType = StringConvertResult; static_assert(std::is_floating_point_v); 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 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 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 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 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 StringConvertResult fromString(StringView inputView, Option 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 fromString(StringView inputView, Option specifie } else if constexpr (std::is_floating_point_v) { return impl::floatFromString(nextView, skipIndex, specifiedRadixOption); } else { - return FudStatus::NotImplemented; + return FudError{FudStatus::NotImplemented}; } } -- cgit v1.2.3