From aa49abdf239bae6065fddd0839ec67a404c9748c Mon Sep 17 00:00:00 2001 From: Joel Klinghed Date: Mon, 15 Sep 2025 21:15:53 +0200 Subject: Add .clang-format Make it easier to keep a consistent style --- src/uio.cc | 130 +++++++++++++++++++++++++++++-------------------------------- 1 file changed, 62 insertions(+), 68 deletions(-) (limited to 'src/uio.cc') 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 #include @@ -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 +template class UnicodeReader : public io::Reader { public: UnicodeReader(std::unique_ptr in, u::ReaderConfig config) @@ -41,8 +39,7 @@ class UnicodeReader : public io::Reader { return std::unexpected(err.value()); auto* in = reinterpret_cast(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(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 operator()(uint8_t const* &in, + std::optional 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 ret; while (out < out_end) { @@ -154,9 +151,9 @@ struct U8ReaderStrict { }; struct U8Reader { - std::optional operator()(uint8_t const* &in, - uint8_t const* in_end, - bool in_eof, uint32_t* &out, + std::optional operator()(uint8_t const*& in, + uint8_t const* in_end, bool in_eof, + uint32_t*& out, uint32_t const* out_end) { std::optional ret; while (out < out_end) { @@ -180,10 +177,9 @@ struct U8Reader { }; struct U16NativeReaderStrict { - std::optional operator()(uint8_t const* &in, + std::optional 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(in); auto* const end = it + ((in_end - in) / 2); @@ -207,10 +203,9 @@ struct U16NativeReaderStrict { }; struct U16SwapReaderStrict { - std::optional operator()(uint8_t const* &in, + std::optional 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(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 ret; while (out < out_end) { @@ -241,10 +237,9 @@ struct U16SwapReaderStrict { }; struct U16NativeReader { - std::optional operator()(uint8_t const* &in, - uint8_t const* in_end, - bool in_eof, - uint32_t* &out, + std::optional 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(in); auto* const end = it + ((in_end - in) / 2); @@ -281,10 +276,9 @@ struct U16NativeReader { }; struct U16SwapReader { - std::optional operator()(uint8_t const* &in, - uint8_t const* in_end, - bool in_eof, - uint32_t* &out, + std::optional 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(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 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 operator()(uint8_t const* &in, - uint8_t const* in_end, - bool in_eof, - uint32_t* &out, + std::optional 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 operator()(uint8_t const* &in, - uint8_t const* in_end, - bool in_eof, - uint32_t* &out, + std::optional 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(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(out); auto* const end = it + (out_avail / 2); bool ret = true; @@ -505,12 +498,11 @@ namespace u8 { namespace { -template +template class UnicodeReaderU8Writer : public UnicodeReader, public virtual Reader { public: - UnicodeReaderU8Writer(std::unique_ptr in, - u::ReaderConfig config) + UnicodeReaderU8Writer(std::unique_ptr in, u::ReaderConfig config) : UnicodeReader(std::move(in), config) {} std::expected read(void* dst, size_t max) override { @@ -524,7 +516,6 @@ class UnicodeReaderU8Writer : public UnicodeReader, } // namespace - std::expected Reader::read(std::string& data, size_t max) { if (max > data.size()) @@ -536,8 +527,8 @@ std::expected Reader::read(std::string& data, return ret; } -std::expected Reader::repeat_read( - std::string& data, size_t max) { +std::expected 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 Reader::repeat_read( return ret; } -std::unique_ptr open( - std::unique_ptr reader, u::ReaderConfig config) { +std::unique_ptr open(std::unique_ptr reader, + u::ReaderConfig config) { switch (config.input) { case u::ReaderInputFormat::UTF8: if (config.strict) @@ -603,10 +594,10 @@ namespace u16 { namespace { -template -class UnicodeReaderU16NativeWriter : public UnicodeReader, - public virtual Reader { +template +class UnicodeReaderU16NativeWriter + : public UnicodeReader, + public virtual Reader { public: UnicodeReaderU16NativeWriter(std::unique_ptr in, u::ReaderConfig config) @@ -635,8 +626,8 @@ std::expected Reader::read(std::u16string& data, return ret; } -std::expected Reader::repeat_read( - std::u16string& data, size_t max) { +std::expected 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 Reader::repeat_read( return ret; } -std::unique_ptr open( - std::unique_ptr reader, u::ReaderConfig config) { +std::unique_ptr open(std::unique_ptr reader, + u::ReaderConfig config) { switch (config.input) { case u::ReaderInputFormat::UTF8: if (config.strict) @@ -658,22 +649,25 @@ std::unique_ptr open( std::move(reader), config); case kU16NativeInputFormat: if (config.strict) - return std::make_unique>(std::move(reader), config); + return std::make_unique< + UnicodeReaderU16NativeWriter>( + std::move(reader), config); return std::make_unique>( std::move(reader), config); break; case kU16SwapInputFormat: if (config.strict) - return std::make_unique>(std::move(reader), config); + return std::make_unique< + UnicodeReaderU16NativeWriter>( + std::move(reader), config); return std::make_unique>( std::move(reader), config); break; case u::ReaderInputFormat::DETECT: if (config.strict) - return std::make_unique>(std::move(reader), config); + return std::make_unique< + UnicodeReaderU16NativeWriter>(std::move(reader), + config); return std::make_unique>( std::move(reader), config); break; -- cgit v1.3