diff options
author | Dominick Allen <djallen@librehumanitas.org> | 2024-10-29 21:02:25 -0500 |
---|---|---|
committer | Dominick Allen <djallen@librehumanitas.org> | 2024-10-29 21:02:25 -0500 |
commit | 8ce397e8c0a83e49e390de9deb73d588e4931ecf (patch) | |
tree | 31f4f4facf0cb75535aaec130d606c54fe97b2d8 /source/fud_format.cpp | |
parent | f281050ddb3b9d658cff67a254eedc3b79de5c5d (diff) |
Reworking of Result.
Diffstat (limited to 'source/fud_format.cpp')
-rw-r--r-- | source/fud_format.cpp | 59 |
1 files changed, 31 insertions, 28 deletions
diff --git a/source/fud_format.cpp b/source/fud_format.cpp index 960a146..f1fc3cf 100644 --- a/source/fud_format.cpp +++ b/source/fud_format.cpp @@ -25,8 +25,9 @@ FudStatus getFormatSign(StringView& formatView, FormatSpec& spec); Result<FormatSpec, FudStatus> FormatSpec::parse(StringView formatView, size_t& specLength) { + using RetType = Result<FormatSpec, FudStatus>; if (formatView.length() < 2 || formatView[0] != FormatSpec::openBracket) { - return FudStatus::ArgumentInvalid; + return RetType::error(FudStatus::ArgumentInvalid); } auto hasEnded = [](StringView& view) { @@ -43,82 +44,82 @@ Result<FormatSpec, FudStatus> FormatSpec::parse(StringView formatView, size_t& s bool hasColon{false}; auto lengthResult = impl::preScanSpec(formatView, spec, hasPosition, hasColon); if (lengthResult.isError()) { - return lengthResult.takeError(); + return RetType::error(lengthResult.takeError()); } length += lengthResult.takeOkay(); if (length < 1) { - return FudStatus::FormatInvalid; + return RetType::error(FudStatus::FormatInvalid); } specLength = length; auto status = impl::getPosition(formatView, spec, hasPosition); if (status != FudStatus::Success) { - return status; + return RetType::error(status); } // check early ending if (!hasColon) { if (hasEnded(formatView)) { - return spec; + return RetType::okay(spec); } - return FudStatus::FormatInvalid; + return RetType::error(FudStatus::FormatInvalid); } // check validity for being non-default spec and advance if (formatView[0] != ':') { - return FudStatus::FormatInvalid; + return RetType::error(FudStatus::FormatInvalid); } formatView.advanceUnsafe(); if (hasEnded(formatView)) { - return spec; + return RetType::okay(spec); } // Spec status = impl::getFill(formatView, spec); if (status != FudStatus::Success) { - return status; + return RetType::error(status); } if (hasEnded(formatView)) { - return spec; + return RetType::okay(spec); } // sign, alternate, leading zero status = impl::getSpecialOpts(formatView, spec); if (status != FudStatus::Success) { - return status; + return RetType::error(status); } if (hasEnded(formatView)) { - return spec; + return RetType::okay(spec); } // Width status = impl::getWidth(formatView, spec); if (status != FudStatus::Success) { - return status; + return RetType::error(status); } if (hasEnded(formatView)) { - return spec; + return RetType::okay(spec); } // Precision status = impl::getPrecision(formatView, spec); if (status != FudStatus::Success) { - return status; + return RetType::error(status); } if (hasEnded(formatView)) { - return spec; + return RetType::okay(spec); } // Locale @@ -129,26 +130,27 @@ Result<FormatSpec, FudStatus> FormatSpec::parse(StringView formatView, size_t& s } if (hasEnded(formatView)) { - return spec; + return RetType::okay(spec); } // Format Sign status = impl::getFormatSign(formatView, spec); if (status != FudStatus::Success) { - return status; + return RetType::error(status); } if (!hasEnded(formatView)) { - return FudStatus::FormatInvalid; + return RetType::error(FudStatus::FormatInvalid); } - return spec; + return RetType::okay(spec); } namespace impl { Result<size_t, FudStatus> preScanSpec(StringView formatView, FormatSpec& spec, bool& hasPosition, bool& hasColon) { + using RetType = Result<size_t, FudStatus>; int nesting = 0; uint32_t captureGroups = 0; size_t index = 0; @@ -162,14 +164,14 @@ Result<size_t, FudStatus> preScanSpec(StringView formatView, FormatSpec& spec, b switch (letter) { case FormatSpec::openBracket: if (not hasColon) { - return FudStatus::FormatInvalid; + return RetType::error(FudStatus::FormatInvalid); } if (takesWidth) { spec.takesWidth = true; } else if (takesPrecision) { spec.takesPrecision = true; } else { - return FudStatus::FormatInvalid; + return RetType::error(FudStatus::FormatInvalid); } nesting++; captureGroups++; @@ -193,7 +195,7 @@ Result<size_t, FudStatus> preScanSpec(StringView formatView, FormatSpec& spec, b } if (nesting > 1) { - return FudStatus::FormatInvalid; + return RetType::error(FudStatus::FormatInvalid); } index++; @@ -201,10 +203,10 @@ Result<size_t, FudStatus> preScanSpec(StringView formatView, FormatSpec& spec, b if (nesting != 0 || captureGroups > 2 || index >= formatView.length() || formatView[index] != FormatSpec::closeBracket) { - return FudStatus::FormatInvalid; + return RetType::error(FudStatus::FormatInvalid); } - return index + 1U; + return RetType::okay(index + 1U); } FudStatus getPosition(StringView& formatView, FormatSpec& spec, bool& hasPosition) @@ -570,6 +572,7 @@ FudStatus fillUnsignedBuffer( [[nodiscard]] Result<bool, FudStatus> validateFloatFormatType(FormatType formatType) { + using RetType = Result<bool, FudStatus>; auto uppercase = false; switch (formatType) { @@ -604,13 +607,13 @@ FudStatus fillUnsignedBuffer( case FormatType::BinaryUpper: case FormatType::String: case FormatType::Escaped: - return FudStatus::FormatInvalid; + return RetType::error(FudStatus::FormatInvalid); break; default: - return FudStatus::Failure; + return RetType::error(FudStatus::Failure); } - return uppercase; + return RetType::okay(uppercase); } ExponentBuffer getScientificExponent(int exponent, uint8_t& exponentLength, bool uppercase) |