summaryrefslogtreecommitdiff
path: root/source/fud_format.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'source/fud_format.cpp')
-rw-r--r--source/fud_format.cpp64
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