summaryrefslogtreecommitdiff
path: root/include/fud_format.hpp
diff options
context:
space:
mode:
authorDominick Allen <djallen@librehumanitas.org>2024-10-30 09:51:54 -0500
committerDominick Allen <djallen@librehumanitas.org>2024-10-30 09:51:54 -0500
commit6c7fd1db481ff10a16ecab958c6542784fa60b9c (patch)
treee0162f930ec44a7c53b07061311d52910c36e481 /include/fud_format.hpp
parent8dcb1de91e15ff7fc66279cd9cd9ad8a70f624e0 (diff)
Use std::byte* instead of void* for allocators.
Diffstat (limited to 'include/fud_format.hpp')
-rw-r--r--include/fud_format.hpp61
1 files changed, 27 insertions, 34 deletions
diff --git a/include/fud_format.hpp b/include/fud_format.hpp
index 4d38c26..2102dc9 100644
--- a/include/fud_format.hpp
+++ b/include/fud_format.hpp
@@ -20,6 +20,7 @@
// #include "fud_assert.hpp"
#include "dragonbox/dragonbox.h"
+#include "fud_drain.hpp"
#include "fud_array.hpp"
#include "fud_option.hpp"
#include "fud_result.hpp"
@@ -270,21 +271,14 @@ enum class FormatCharMode
ValidUtf8
};
-struct FormatResult {
- size_t bytesWritten{0};
- FudStatus status{FudStatus::Failure};
- [[nodiscard]] constexpr bool isOkay()
- {
- return status == FudStatus::Success;
- }
-};
+using FormatResult = DrainResult;
/* TODO : require concept of a sink that takes pushBack() -> FudStatus */
/* TODO : sink concept also requires drain() -> FudStatus */
-template <typename Sink>
+template <typename Sink> requires Drainable<Sink, StringView>
FormatResult format(Sink& sink, FormatCharMode formatMode, FormatString fmt);
-template <typename Sink, typename... Args>
+template <typename Sink, typename... Args> requires Drainable<Sink, StringView>
FormatResult format(Sink& sink, FormatCharMode formatMode, FormatString fmt, Args&&... args);
template <typename Sink>
@@ -341,7 +335,7 @@ size_t findSpec(StringView scanView);
} // namespace impl
-template <typename Sink, typename... Args>
+template <typename Sink, typename... Args> requires Drainable<Sink, StringView>
FormatResult format(Sink& sink, FormatCharMode formatMode, FormatString fmt, Args&&... args)
{
static_cast<void>(sink);
@@ -355,7 +349,7 @@ FormatResult format(Sink& sink, FormatCharMode formatMode, FormatString fmt, Arg
return impl::vFormat(sink, formatMode, fmt, formatArguments);
}
-template <typename Sink>
+template <typename Sink> requires Drainable<Sink, StringView>
FormatResult format(Sink& sink, FormatCharMode formatMode, FormatString fmt)
{
if (formatMode != FormatCharMode::Unchecked) {
@@ -368,8 +362,7 @@ FormatResult format(Sink& sink, FormatCharMode formatMode, FormatString fmt)
return {0, FudStatus::FormatInvalid};
}
- auto drainResult = sink.drain(fmt.view());
- return FormatResult{drainResult.bytesWritten, drainResult.status};
+ return sink.drain(fmt.view());
}
namespace impl {
@@ -419,7 +412,7 @@ FormatResult vFormat(Sink& sink, FormatCharMode formatMode, FormatString fmt, co
fudAssert(specIndex <= scanView.length());
StringView run{specIndex, scanView.data()};
auto drainResult = sink.drain(run);
- result.bytesWritten += drainResult.bytesWritten;
+ result.bytesDrained += drainResult.bytesDrained;
if (drainResult.status != FudStatus::Success) {
result.status = drainResult.status;
return result;
@@ -451,7 +444,7 @@ FormatResult vFormat(Sink& sink, FormatCharMode formatMode, FormatString fmt, co
result.status = argResult.status;
return result;
}
- result.bytesWritten += argResult.bytesWritten;
+ result.bytesDrained += argResult.bytesDrained;
if (!formatSpec.takesPosition()) {
argIndex++;
argIndex += static_cast<uint8_t>(formatSpec.takesWidth);
@@ -904,7 +897,7 @@ FormatResult drainIntegral(
if (sign.length() > 0) {
auto drainResult = sink.drain(sign);
- result.bytesWritten += drainResult.bytesWritten;
+ result.bytesDrained += drainResult.bytesDrained;
result.status = drainResult.status;
}
@@ -914,7 +907,7 @@ FormatResult drainIntegral(
if (prefix.length() > 0) {
auto drainResult = sink.drain(prefix);
- result.bytesWritten += drainResult.bytesWritten;
+ result.bytesDrained += drainResult.bytesDrained;
result.status = drainResult.status;
}
@@ -925,7 +918,7 @@ FormatResult drainIntegral(
if (padZero) {
auto zeroPadSize = width - calculatedWidth;
auto padResult = fillPad(sink, '0', zeroPadSize);
- result.bytesWritten += padResult.bytesWritten;
+ result.bytesDrained += padResult.bytesDrained;
result.status = padResult.status;
}
@@ -934,7 +927,7 @@ FormatResult drainIntegral(
}
auto drainNumberResult = sink.drain(buffer);
- result.bytesWritten += drainNumberResult.bytesWritten;
+ result.bytesDrained += drainNumberResult.bytesDrained;
result.status = drainNumberResult.status;
if (result.status != FudStatus::Success) {
@@ -942,7 +935,7 @@ FormatResult drainIntegral(
}
auto rightPadResult = rightPad(sink, align, width, fill, calculatedWidth);
- result.bytesWritten += rightPadResult.bytesWritten;
+ result.bytesDrained += rightPadResult.bytesDrained;
result.status = rightPadResult.status;
return result;
@@ -964,19 +957,19 @@ template <typename Sink>
for (auto bufferSize : bufferSizes) {
while (count > bufferSize) {
auto drainResult = sink.drain(StringView{bufferSize, backingBuffer.data()});
- result.bytesWritten += drainResult.bytesWritten;
+ result.bytesDrained += drainResult.bytesDrained;
result.status = drainResult.status;
if (result.status != FudStatus::Success) {
return result;
}
- count -= drainResult.bytesWritten;
+ count -= drainResult.bytesDrained;
}
}
if (count > 0) {
fudAssert(count < backingBuffer.size());
auto drainResult = sink.drain(StringView{count, backingBuffer.data()});
- result.bytesWritten += drainResult.bytesWritten;
+ result.bytesDrained += drainResult.bytesDrained;
result.status = drainResult.status;
}
@@ -1201,45 +1194,45 @@ template <typename Sink, typename DecimalRepr>
if (width > calculatedWidth && not padZero) {
auto padResult = leftPad(sink, align, width, fill, calculatedWidth);
- result.bytesWritten += padResult.bytesWritten;
+ result.bytesDrained += padResult.bytesDrained;
result.status = padResult.status;
}
if (result.status == FudStatus::Success && sign.length() > 0) {
auto drainResult = sink.drain(sign);
- result.bytesWritten += drainResult.bytesWritten;
+ result.bytesDrained += drainResult.bytesDrained;
result.status = drainResult.status;
}
if (result.status == FudStatus::Success && padZero && width > calculatedWidth) {
auto zeroPadSize = width - calculatedWidth;
auto padResult = fillPad(sink, '0', zeroPadSize);
- result.bytesWritten += padResult.bytesWritten;
+ result.bytesDrained += padResult.bytesDrained;
result.status = padResult.status;
}
if (result.status == FudStatus::Success) {
auto drainNumberResult = sink.drain(numberView);
- result.bytesWritten += drainNumberResult.bytesWritten;
+ result.bytesDrained += drainNumberResult.bytesDrained;
result.status = drainNumberResult.status;
}
if (result.status == FudStatus::Success && precision > precisionPlaces) {
auto remainingPlaces = precision - precisionPlaces;
auto precisionResult = fillPad(sink, '0', remainingPlaces);
- result.bytesWritten += precisionResult.bytesWritten;
+ result.bytesDrained += precisionResult.bytesDrained;
result.status = precisionResult.status;
}
if (result.status == FudStatus::Success) {
auto exponentDrainResult = sink.drain(exponentView);
- result.bytesWritten += exponentDrainResult.bytesWritten;
+ result.bytesDrained += exponentDrainResult.bytesDrained;
result.status = exponentDrainResult.status;
}
if (result.status == FudStatus::Success) {
auto padResult = rightPad(sink, align, width, fill, calculatedWidth);
- result.bytesWritten += padResult.bytesWritten;
+ result.bytesDrained += padResult.bytesDrained;
result.status = padResult.status;
}
@@ -1441,7 +1434,7 @@ FormatResult format(Sink& sink, FormatCharMode formatMode, const FormatSpec& for
}
auto padResult = impl::leftPad(sink, align, width, fill, calculatedWidth);
- result.bytesWritten += padResult.bytesWritten;
+ result.bytesDrained += padResult.bytesDrained;
result.status = padResult.status;
if (result.status != FudStatus::Success) {
@@ -1449,7 +1442,7 @@ FormatResult format(Sink& sink, FormatCharMode formatMode, const FormatSpec& for
}
auto drainViewResult = sink.drain(arg);
- result.bytesWritten += drainViewResult.bytesWritten;
+ result.bytesDrained += drainViewResult.bytesDrained;
result.status = drainViewResult.status;
if (result.status != FudStatus::Success) {
@@ -1457,7 +1450,7 @@ FormatResult format(Sink& sink, FormatCharMode formatMode, const FormatSpec& for
}
padResult = impl::rightPad(sink, align, width, fill, calculatedWidth);
- result.bytesWritten += padResult.bytesWritten;
+ result.bytesDrained += padResult.bytesDrained;
result.status = padResult.status;
return result;