diff options
Diffstat (limited to 'source')
-rw-r--r-- | source/fud_format.cpp | 84 | ||||
-rw-r--r-- | source/fud_string.cpp | 3 |
2 files changed, 83 insertions, 4 deletions
diff --git a/source/fud_format.cpp b/source/fud_format.cpp index f3e6894..960a146 100644 --- a/source/fud_format.cpp +++ b/source/fud_format.cpp @@ -390,9 +390,6 @@ FudStatus getPrecision(StringView& formatView, FormatSpec& spec) return precisionResult.takeError(); } auto precisionValue{precisionResult.takeOkay()}; - if (precisionValue.value < 1) { - return FudStatus::FormatInvalid; - } spec.precision = precisionValue.value; if (formatView.length() < precisionValue.nextIndex) { return FudStatus::Failure; @@ -571,6 +568,87 @@ FudStatus fillUnsignedBuffer( return FudStatus::Success; } +[[nodiscard]] Result<bool, FudStatus> validateFloatFormatType(FormatType formatType) +{ + auto uppercase = false; + + switch (formatType) { + case FormatType::Unspecified: + break; + case FormatType::FloatHexLower: + break; + case FormatType::FloatHexUpper: + uppercase = true; + break; + case FormatType::ScientificLower: + break; + case FormatType::ScientificUpper: + uppercase = true; + break; + case FormatType::FixedLower: + break; + case FormatType::FixedUpper: + uppercase = true; + break; + case FormatType::GeneralLower: + break; + case FormatType::GeneralUpper: + uppercase = true; + break; + case FormatType::Octal: + case FormatType::HexLower: + case FormatType::HexUpper: + case FormatType::Decimal: + case FormatType::Character: + case FormatType::BinaryLower: + case FormatType::BinaryUpper: + case FormatType::String: + case FormatType::Escaped: + return FudStatus::FormatInvalid; + break; + default: + return FudStatus::Failure; + } + + return uppercase; +} + +ExponentBuffer getScientificExponent(int exponent, uint8_t& exponentLength, bool uppercase) +{ + IntCharArray buffer{}; + uint8_t bufferLength = 0; + auto exponentStatus = fillSignedBuffer(buffer, exponent, bufferLength, Radix::Decimal, true); + fudAssert(exponentStatus == FudStatus::Success); + fudAssert(bufferLength <= maxDecimalExpLength); + ExponentBuffer exponentBuffer{}; + exponentBuffer[0] = uppercase ? 'E' : 'e'; + exponentBuffer[1] = exponent < 0 ? '-' : '+'; + + switch (bufferLength) { + case 0: + fudAssert(bufferLength > 0); + break; + case 1: + exponentBuffer[2] = '0'; + exponentBuffer[3] = buffer[0]; + break; + case 2: + exponentBuffer[2] = buffer[0]; + exponentBuffer[3] = buffer[1]; + break; + case 3: + exponentBuffer[2] = buffer[0]; + exponentBuffer[3] = buffer[1]; + exponentBuffer[4] = buffer[2]; + break; + default: + fudAssert(bufferLength <= maxDecimalExpLength); + } + + exponentLength = bufferLength < 3 ? 4 : 5; + return exponentBuffer; +} + } // namespace impl } // namespace fud diff --git a/source/fud_string.cpp b/source/fud_string.cpp index c444a74..3ba6603 100644 --- a/source/fud_string.cpp +++ b/source/fud_string.cpp @@ -500,8 +500,9 @@ DrainResult String::drain(StringView source) auto* destPtr = data() + length(); auto status = copyMem(destPtr, remainingLength(), source.data(), source.length()); fudAssert(status == FudStatus::Success); + result.bytesWritten += source.length(); - m_length += source.length(); + m_length = newLength; status = nullTerminate(); return result; |