summaryrefslogtreecommitdiff
path: root/source/fud_format.cpp
diff options
context:
space:
mode:
authorDominick Allen <djallen@librehumanitas.org>2024-10-27 20:13:52 -0500
committerDominick Allen <djallen@librehumanitas.org>2024-10-27 20:13:52 -0500
commitcbf3ad2b284a9e79ef5df564be6b16e8e746cb2b (patch)
treef3c3a1cf9c1286144d002611b209f63541b14426 /source/fud_format.cpp
parentb8345246dcc2121bcb6d1515a9341789de20199f (diff)
Setup float formatting.
Diffstat (limited to 'source/fud_format.cpp')
-rw-r--r--source/fud_format.cpp84
1 files changed, 81 insertions, 3 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