diff options
author | Dominick Allen <djallen@librehumanitas.org> | 2024-10-25 17:13:49 -0500 |
---|---|---|
committer | Dominick Allen <djallen@librehumanitas.org> | 2024-10-25 17:13:49 -0500 |
commit | 7b30a5425eaf7aae1d72d5ba564092e342901fe8 (patch) | |
tree | 8a74d0810ea4db23d4d0c6c4ba8d91518d569db2 /source/fud_format.cpp | |
parent | 11968f674a7de34fb7de744598a8086330cd88a3 (diff) |
A lot of work on formatting.
Diffstat (limited to 'source/fud_format.cpp')
-rw-r--r-- | source/fud_format.cpp | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/source/fud_format.cpp b/source/fud_format.cpp index 6bd5aae..f3e6894 100644 --- a/source/fud_format.cpp +++ b/source/fud_format.cpp @@ -507,6 +507,70 @@ FudStatus getFormatSign(StringView& formatView, FormatSpec& spec) return FudStatus::Success; } +size_t findSpec(StringView scanView) +{ + size_t index = 0; + bool foundBracket = false; + while (index < scanView.length()) { + auto letter = scanView[index]; + if (letter == FormatSpec::openBracket && not foundBracket) { + foundBracket = true; + } else if (letter == FormatSpec::openBracket && foundBracket) { + foundBracket = false; + } else if (foundBracket) { + index--; + break; + } + index++; + } + + return index; +} + +FudStatus fillUnsignedBuffer( + Array<utf8, maxIntCharCount>& buffer, + uint64_t value, + uint8_t& bufferLength, + Radix radix, + bool uppercase) +{ + static_assert(maxIntCharCount < std::numeric_limits<uint8_t>::max()); + bufferLength = 0; + + constexpr Array<uint8_t, static_cast<size_t>(Radix::Hexadecimal)> lower{ + {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}}; + constexpr Array<uint8_t, static_cast<size_t>(Radix::Hexadecimal)> upper{ + {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}}; + const auto& lookup = uppercase ? upper : lower; + + if (value == 0) { + buffer[0] = '0'; + bufferLength = 1; + return FudStatus::Success; + } + + static_assert(std::is_same_v<std::underlying_type_t<Radix>, uint8_t>); + auto radixValue = static_cast<uint8_t>(radix); + while (value > 0 && bufferLength < maxIntCharCount) { + auto digit = static_cast<uint8_t>(value % radixValue); + buffer[bufferLength] = lookup[digit]; + value /= radixValue; + bufferLength++; + } + + if (value > 0 || bufferLength > maxIntCharCount) { + return FudStatus::Failure; + } + + // TODO: implement fud_algorithm reverse + for (size_t idx = 0; idx < bufferLength / 2; ++idx) { + auto rhsIndex = bufferLength - idx - 1; + std::swap(buffer[idx], buffer[rhsIndex]); + } + + return FudStatus::Success; +} + } // namespace impl } // namespace fud |