summaryrefslogtreecommitdiff
path: root/src/uio.cc
diff options
context:
space:
mode:
authorJoel Klinghed <the_jk@spawned.biz>2025-09-15 21:15:53 +0200
committerJoel Klinghed <the_jk@spawned.biz>2025-09-15 21:15:53 +0200
commitaa49abdf239bae6065fddd0839ec67a404c9748c (patch)
tree631fbb2df58aa35df3d60c65c037ef74b1807114 /src/uio.cc
parentbf41a601fd0447bcf3a2937a595a1cd8ca5c1633 (diff)
Add .clang-format
Make it easier to keep a consistent style
Diffstat (limited to 'src/uio.cc')
-rw-r--r--src/uio.cc130
1 files changed, 62 insertions, 68 deletions
diff --git a/src/uio.cc b/src/uio.cc
index 1bf5e40..475d793 100644
--- a/src/uio.cc
+++ b/src/uio.cc
@@ -1,8 +1,8 @@
#include "uio.hh"
#include "buffer.hh"
-#include "u8.hh"
#include "u16.hh"
+#include "u8.hh"
#include <bit>
#include <cassert>
@@ -16,20 +16,18 @@
namespace {
constexpr u::ReaderInputFormat kU16NativeInputFormat =
- (std::endian::native == std::endian::big)
- ? u::ReaderInputFormat::UTF16_BE
- : u::ReaderInputFormat::UTF16_LE;
+ (std::endian::native == std::endian::big) ? u::ReaderInputFormat::UTF16_BE
+ : u::ReaderInputFormat::UTF16_LE;
constexpr u::ReaderInputFormat kU16SwapInputFormat =
- (std::endian::native == std::endian::big)
- ? u::ReaderInputFormat::UTF16_LE
- : u::ReaderInputFormat::UTF16_BE;
+ (std::endian::native == std::endian::big) ? u::ReaderInputFormat::UTF16_LE
+ : u::ReaderInputFormat::UTF16_BE;
constexpr size_t kByteBufferSize = 65535;
constexpr size_t kUnicodeBufferSize = 8192;
constexpr size_t kUSwapBufferSize = kByteBufferSize / 4;
-template<typename UReader, typename UWriter>
+template <typename UReader, typename UWriter>
class UnicodeReader : public io::Reader {
public:
UnicodeReader(std::unique_ptr<io::Reader> in, u::ReaderConfig config)
@@ -41,8 +39,7 @@ class UnicodeReader : public io::Reader {
return std::unexpected(err.value());
auto* in = reinterpret_cast<uint8_t const*>(in_ptr_);
- auto read_err = reader_(in, in + in_avail_, in_eof_,
- u_buffer_wptr_,
+ auto read_err = reader_(in, in + in_avail_, in_eof_, u_buffer_wptr_,
u_buffer_ + kUnicodeBufferSize);
byte_buffer_->consume(in - reinterpret_cast<uint8_t const*>(in_ptr_));
if (read_err.has_value()) {
@@ -84,7 +81,8 @@ class UnicodeReader : public io::Reader {
u_buffer_wptr_ = u_buffer_;
} else if (u_out == u_buffer_) {
// Unable to write anything.
- if (max == 0) return 0;
+ if (max == 0)
+ return 0;
return std::unexpected(io::ReadError::MaxTooSmall);
} else {
size_t left = u_buffer_wptr_ - u_out;
@@ -132,10 +130,9 @@ class UnicodeReader : public io::Reader {
};
struct U8ReaderStrict {
- std::optional<u::ReadError> operator()(uint8_t const* &in,
+ std::optional<u::ReadError> operator()(uint8_t const*& in,
uint8_t const* in_end,
- bool /* in_eof */,
- uint32_t* &out,
+ bool /* in_eof */, uint32_t*& out,
uint32_t const* out_end) {
std::optional<u::ReadError> ret;
while (out < out_end) {
@@ -154,9 +151,9 @@ struct U8ReaderStrict {
};
struct U8Reader {
- std::optional<u::ReadError> operator()(uint8_t const* &in,
- uint8_t const* in_end,
- bool in_eof, uint32_t* &out,
+ std::optional<u::ReadError> operator()(uint8_t const*& in,
+ uint8_t const* in_end, bool in_eof,
+ uint32_t*& out,
uint32_t const* out_end) {
std::optional<u::ReadError> ret;
while (out < out_end) {
@@ -180,10 +177,9 @@ struct U8Reader {
};
struct U16NativeReaderStrict {
- std::optional<u::ReadError> operator()(uint8_t const* &in,
+ std::optional<u::ReadError> operator()(uint8_t const*& in,
uint8_t const* in_end,
- bool /* in_eof */,
- uint32_t* &out,
+ bool /* in_eof */, uint32_t*& out,
uint32_t const* out_end) {
auto* it = reinterpret_cast<uint16_t const*>(in);
auto* const end = it + ((in_end - in) / 2);
@@ -207,10 +203,9 @@ struct U16NativeReaderStrict {
};
struct U16SwapReaderStrict {
- std::optional<u::ReadError> operator()(uint8_t const* &in,
+ std::optional<u::ReadError> operator()(uint8_t const*& in,
uint8_t const* in_end,
- bool /* in_eof */,
- uint32_t* &out,
+ bool /* in_eof */, uint32_t*& out,
uint32_t const* out_end) {
auto* it = buffer_;
auto* const end = it + ((in_end - in) / 2);
@@ -218,7 +213,8 @@ struct U16SwapReaderStrict {
return u::ReadError::Incomplete;
{
auto* in2 = reinterpret_cast<uint16_t const*>(in);
- for (auto* it2 = it; it2 != end; ++it2) *it2 = std::byteswap(*(in2++));
+ for (auto* it2 = it; it2 != end; ++it2)
+ *it2 = std::byteswap(*(in2++));
}
std::optional<u::ReadError> ret;
while (out < out_end) {
@@ -241,10 +237,9 @@ struct U16SwapReaderStrict {
};
struct U16NativeReader {
- std::optional<u::ReadError> operator()(uint8_t const* &in,
- uint8_t const* in_end,
- bool in_eof,
- uint32_t* &out,
+ std::optional<u::ReadError> operator()(uint8_t const*& in,
+ uint8_t const* in_end, bool in_eof,
+ uint32_t*& out,
uint32_t const* out_end) {
auto* it = reinterpret_cast<uint16_t const*>(in);
auto* const end = it + ((in_end - in) / 2);
@@ -281,10 +276,9 @@ struct U16NativeReader {
};
struct U16SwapReader {
- std::optional<u::ReadError> operator()(uint8_t const* &in,
- uint8_t const* in_end,
- bool in_eof,
- uint32_t* &out,
+ std::optional<u::ReadError> operator()(uint8_t const*& in,
+ uint8_t const* in_end, bool in_eof,
+ uint32_t*& out,
uint32_t const* out_end) {
auto* it = buffer_;
auto* const end = it + ((in_end - in) / 2);
@@ -300,7 +294,8 @@ struct U16SwapReader {
}
{
auto* in2 = reinterpret_cast<uint16_t const*>(in);
- for (auto* it2 = it; it2 != end; ++it2) *it2 = std::byteswap(*(in2++));
+ for (auto* it2 = it; it2 != end; ++it2)
+ *it2 = std::byteswap(*(in2++));
}
std::optional<u::ReadError> ret;
while (out < out_end) {
@@ -328,7 +323,7 @@ struct U16SwapReader {
};
bool detect(uint8_t const* in, uint8_t const* in_end, bool in_eof,
- u::ReaderInputFormat &format) {
+ u::ReaderInputFormat& format) {
if (in == in_end) {
if (in_eof) {
// Doesn't matter, go with UTF-8 just to get out of "detect"
@@ -382,10 +377,9 @@ bool detect(uint8_t const* in, uint8_t const* in_end, bool in_eof,
}
struct DetectReaderStrict {
- std::optional<u::ReadError> operator()(uint8_t const* &in,
- uint8_t const* in_end,
- bool in_eof,
- uint32_t* &out,
+ std::optional<u::ReadError> operator()(uint8_t const*& in,
+ uint8_t const* in_end, bool in_eof,
+ uint32_t*& out,
uint32_t const* out_end) {
switch (format_) {
case u::ReaderInputFormat::DETECT:
@@ -411,10 +405,9 @@ struct DetectReaderStrict {
};
struct DetectReader {
- std::optional<u::ReadError> operator()(uint8_t const* &in,
- uint8_t const* in_end,
- bool in_eof,
- uint32_t* &out,
+ std::optional<u::ReadError> operator()(uint8_t const*& in,
+ uint8_t const* in_end, bool in_eof,
+ uint32_t*& out,
uint32_t const* out_end) {
switch (format_) {
case u::ReaderInputFormat::DETECT:
@@ -440,8 +433,8 @@ struct DetectReader {
};
struct U8Writer {
- bool operator()(uint32_t const* &in, uint32_t const* in_end,
- void* &out, size_t out_avail) {
+ bool operator()(uint32_t const*& in, uint32_t const* in_end, void*& out,
+ size_t out_avail) {
auto* it = reinterpret_cast<uint8_t*>(out);
auto* const end = it + out_avail;
bool ret = true;
@@ -459,8 +452,8 @@ struct U8Writer {
struct U16NativeWriter {
public:
- bool operator()(uint32_t const* &in, uint32_t const* in_end,
- void* &out, size_t out_avail) {
+ bool operator()(uint32_t const*& in, uint32_t const* in_end, void*& out,
+ size_t out_avail) {
auto* it = reinterpret_cast<uint16_t*>(out);
auto* const end = it + (out_avail / 2);
bool ret = true;
@@ -505,12 +498,11 @@ namespace u8 {
namespace {
-template<typename UReader>
+template <typename UReader>
class UnicodeReaderU8Writer : public UnicodeReader<UReader, U8Writer>,
public virtual Reader {
public:
- UnicodeReaderU8Writer(std::unique_ptr<io::Reader> in,
- u::ReaderConfig config)
+ UnicodeReaderU8Writer(std::unique_ptr<io::Reader> in, u::ReaderConfig config)
: UnicodeReader<UReader, U8Writer>(std::move(in), config) {}
std::expected<size_t, io::ReadError> read(void* dst, size_t max) override {
@@ -524,7 +516,6 @@ class UnicodeReaderU8Writer : public UnicodeReader<UReader, U8Writer>,
} // namespace
-
std::expected<size_t, io::ReadError> Reader::read(std::string& data,
size_t max) {
if (max > data.size())
@@ -536,8 +527,8 @@ std::expected<size_t, io::ReadError> Reader::read(std::string& data,
return ret;
}
-std::expected<size_t, io::ReadError> Reader::repeat_read(
- std::string& data, size_t max) {
+std::expected<size_t, io::ReadError> Reader::repeat_read(std::string& data,
+ size_t max) {
if (max > data.size())
data.resize(max);
auto ret = repeat_read(data.data(), max);
@@ -547,8 +538,8 @@ std::expected<size_t, io::ReadError> Reader::repeat_read(
return ret;
}
-std::unique_ptr<Reader> open(
- std::unique_ptr<io::Reader> reader, u::ReaderConfig config) {
+std::unique_ptr<Reader> open(std::unique_ptr<io::Reader> reader,
+ u::ReaderConfig config) {
switch (config.input) {
case u::ReaderInputFormat::UTF8:
if (config.strict)
@@ -603,10 +594,10 @@ namespace u16 {
namespace {
-template<typename UReader>
-class UnicodeReaderU16NativeWriter : public UnicodeReader<UReader,
- U16NativeWriter>,
- public virtual Reader {
+template <typename UReader>
+class UnicodeReaderU16NativeWriter
+ : public UnicodeReader<UReader, U16NativeWriter>,
+ public virtual Reader {
public:
UnicodeReaderU16NativeWriter(std::unique_ptr<io::Reader> in,
u::ReaderConfig config)
@@ -635,8 +626,8 @@ std::expected<size_t, io::ReadError> Reader::read(std::u16string& data,
return ret;
}
-std::expected<size_t, io::ReadError> Reader::repeat_read(
- std::u16string& data, size_t max) {
+std::expected<size_t, io::ReadError> Reader::repeat_read(std::u16string& data,
+ size_t max) {
if (max > data.size())
data.resize(max);
auto ret = repeat_read(data.data(), max * 2);
@@ -647,8 +638,8 @@ std::expected<size_t, io::ReadError> Reader::repeat_read(
return ret;
}
-std::unique_ptr<Reader> open(
- std::unique_ptr<io::Reader> reader, u::ReaderConfig config) {
+std::unique_ptr<Reader> open(std::unique_ptr<io::Reader> reader,
+ u::ReaderConfig config) {
switch (config.input) {
case u::ReaderInputFormat::UTF8:
if (config.strict)
@@ -658,22 +649,25 @@ std::unique_ptr<Reader> open(
std::move(reader), config);
case kU16NativeInputFormat:
if (config.strict)
- return std::make_unique<UnicodeReaderU16NativeWriter<
- U16NativeReaderStrict>>(std::move(reader), config);
+ return std::make_unique<
+ UnicodeReaderU16NativeWriter<U16NativeReaderStrict>>(
+ std::move(reader), config);
return std::make_unique<UnicodeReaderU16NativeWriter<U16NativeReader>>(
std::move(reader), config);
break;
case kU16SwapInputFormat:
if (config.strict)
- return std::make_unique<UnicodeReaderU16NativeWriter<
- U16SwapReaderStrict>>(std::move(reader), config);
+ return std::make_unique<
+ UnicodeReaderU16NativeWriter<U16SwapReaderStrict>>(
+ std::move(reader), config);
return std::make_unique<UnicodeReaderU16NativeWriter<U16SwapReader>>(
std::move(reader), config);
break;
case u::ReaderInputFormat::DETECT:
if (config.strict)
- return std::make_unique<UnicodeReaderU16NativeWriter<
- DetectReaderStrict>>(std::move(reader), config);
+ return std::make_unique<
+ UnicodeReaderU16NativeWriter<DetectReaderStrict>>(std::move(reader),
+ config);
return std::make_unique<UnicodeReaderU16NativeWriter<DetectReader>>(
std::move(reader), config);
break;