diff options
| author | Joel Klinghed <the_jk@spawned.biz> | 2025-09-15 21:15:53 +0200 |
|---|---|---|
| committer | Joel Klinghed <the_jk@spawned.biz> | 2025-09-15 21:15:53 +0200 |
| commit | aa49abdf239bae6065fddd0839ec67a404c9748c (patch) | |
| tree | 631fbb2df58aa35df3d60c65c037ef74b1807114 /src | |
| parent | bf41a601fd0447bcf3a2937a595a1cd8ca5c1633 (diff) | |
Add .clang-format
Make it easier to keep a consistent style
Diffstat (limited to 'src')
| -rw-r--r-- | src/args.cc | 93 | ||||
| -rw-r--r-- | src/args.hh | 24 | ||||
| -rw-r--r-- | src/buffer.cc | 28 | ||||
| -rw-r--r-- | src/check.hh | 12 | ||||
| -rw-r--r-- | src/csv.cc | 7 | ||||
| -rw-r--r-- | src/csv.hh | 2 | ||||
| -rw-r--r-- | src/decompress.hh | 2 | ||||
| -rw-r--r-- | src/decompress_lzma.cc | 3 | ||||
| -rw-r--r-- | src/decompress_z.cc | 10 | ||||
| -rw-r--r-- | src/gen_ugc.cc | 60 | ||||
| -rw-r--r-- | src/io.cc | 26 | ||||
| -rw-r--r-- | src/line.cc | 23 | ||||
| -rw-r--r-- | src/line.hh | 2 | ||||
| -rw-r--r-- | src/main.cc | 4 | ||||
| -rw-r--r-- | src/str.cc | 4 | ||||
| -rw-r--r-- | src/u.hh | 12 | ||||
| -rw-r--r-- | src/u16.hh | 27 | ||||
| -rw-r--r-- | src/u8.hh | 39 | ||||
| -rw-r--r-- | src/uio.cc | 130 | ||||
| -rw-r--r-- | src/uio.hh | 20 | ||||
| -rw-r--r-- | src/umod8.hh | 36 | ||||
| -rw-r--r-- | src/unique_fd.hh | 21 |
22 files changed, 291 insertions, 294 deletions
diff --git a/src/args.cc b/src/args.cc index 078db1a..1794941 100644 --- a/src/args.cc +++ b/src/args.cc @@ -26,10 +26,13 @@ class OptionImpl : public Args::OptionArgument { OptionalArgument, }; - OptionImpl(Type type, char shortname, std::string longname, - std::string arg, std::string help) - : type(type), shortname(shortname), longname(std::move(longname)), - arg(std::move(arg)), help(std::move(help)) {} + OptionImpl(Type type, char shortname, std::string longname, std::string arg, + std::string help) + : type(type), + shortname(shortname), + longname(std::move(longname)), + arg(std::move(arg)), + help(std::move(help)) {} const Type type; const char shortname; @@ -57,13 +60,13 @@ class OptionImpl : public Args::OptionArgument { void set_argument(std::string value) { assert(type == Type::RequiredArgument || type == Type::OptionalArgument); - is_set_= true; + is_set_ = true; value_ = std::move(value); } void set_no_argument() { assert(type == Type::NoArgument || type == Type::OptionalArgument); - is_set_= true; + is_set_ = true; value_.reset(); } @@ -74,11 +77,10 @@ class OptionImpl : public Args::OptionArgument { class ArgsImpl : public Args { public: - explicit ArgsImpl(std::string prgname) - : prgname_(std::move(prgname)) {} + explicit ArgsImpl(std::string prgname) : prgname_(std::move(prgname)) {} - std::shared_ptr<Option> option( - char shortname, std::string longname, std::string help) override { + std::shared_ptr<Option> option(char shortname, std::string longname, + std::string help) override { auto opt = std::make_shared<OptionImpl>( OptionImpl::Type::NoArgument, shortname, std::move(longname), /* arg */ std::string(), std::move(help)); @@ -86,13 +88,15 @@ class ArgsImpl : public Args { return opt; } - std::shared_ptr<OptionArgument> option_argument( - char shortname, std::string longname, std::string arg, - std::string help, bool required) override { + std::shared_ptr<OptionArgument> option_argument(char shortname, + std::string longname, + std::string arg, + std::string help, + bool required) override { auto opt = std::make_shared<OptionImpl>( - required ? OptionImpl::Type::RequiredArgument : - OptionImpl::Type::OptionalArgument, shortname, std::move(longname), - std::move(arg), std::move(help)); + required ? OptionImpl::Type::RequiredArgument + : OptionImpl::Type::OptionalArgument, + shortname, std::move(longname), std::move(arg), std::move(help)); add(opt); return opt; } @@ -118,9 +122,11 @@ class ArgsImpl : public Args { if (argv[a][1] == '-') { // long option size_t eq = 2; - while (argv[a][eq] != '=' && argv[a][eq] != '\0') ++eq; + while (argv[a][eq] != '=' && argv[a][eq] != '\0') + ++eq; size_t end = eq; - while (argv[a][end] != '\0') ++end; + while (argv[a][end] != '\0') + ++end; if (end == 2) { // "--", no more options signal @@ -134,8 +140,8 @@ class ArgsImpl : public Args { auto name = std::string_view(argv[a] + 2, eq - 2); auto it = long_.find(name); if (it == long_.end()) { - last_error_ = std::format("{}: unrecognized option '--{}'", - prgname, name); + last_error_ = + std::format("{}: unrecognized option '--{}'", prgname, name); return false; } auto& opt = options_[it->second]; @@ -144,9 +150,9 @@ class ArgsImpl : public Args { // long option with argument after equal sign switch (opt->type) { case OptionImpl::Type::NoArgument: - last_error_ = std::format( - "{}: option '--{}' doesn't allow an argument", - prgname, name); + last_error_ = + std::format("{}: option '--{}' doesn't allow an argument", + prgname, name); return false; case OptionImpl::Type::RequiredArgument: case OptionImpl::Type::OptionalArgument: @@ -163,8 +169,7 @@ class ArgsImpl : public Args { case OptionImpl::Type::RequiredArgument: if (++a >= argc) { last_error_ = std::format( - "{}: option '--{}' requires an argument", - prgname, name); + "{}: option '--{}' requires an argument", prgname, name); return false; } opt->set_argument(argv[a]); @@ -177,8 +182,8 @@ class ArgsImpl : public Args { for (; *current; ++current) { auto it = short_.find(*current); if (it == short_.end()) { - last_error_ = std::format("{}: invalid option -- '{}'", - prgname, *current); + last_error_ = + std::format("{}: invalid option -- '{}'", prgname, *current); return false; } @@ -190,9 +195,9 @@ class ArgsImpl : public Args { break; case OptionImpl::Type::RequiredArgument: if (++a >= argc) { - last_error_ = std::format( - "{}: option requires an argument -- '{}'", - prgname, *current); + last_error_ = + std::format("{}: option requires an argument -- '{}'", + prgname, *current); return false; } opt->set_argument(argv[a]); @@ -209,13 +214,15 @@ class ArgsImpl : public Args { } void print_error(std::ostream& out) const override { - if (last_error_.empty()) return; + if (last_error_.empty()) + return; out << last_error_ << '\n'; } void print_help(std::ostream& out, uint32_t width = 79) const override { - if (options_.empty()) return; + if (options_.empty()) + return; uint32_t indent = 0; const uint32_t max_need = width / 2; @@ -223,7 +230,7 @@ class ArgsImpl : public Args { for (auto const& opt : options_) { uint32_t need; if (opt->longname.empty()) { - need = 4; // -O + need = 4; // -O switch (opt->type) { case OptionImpl::Type::NoArgument: case OptionImpl::Type::OptionalArgument: @@ -233,7 +240,7 @@ class ArgsImpl : public Args { break; } } else { - need = 8 + opt->longname.size(); // -O, --option + need = 8 + opt->longname.size(); // -O, --option switch (opt->type) { case OptionImpl::Type::NoArgument: break; @@ -247,7 +254,7 @@ class ArgsImpl : public Args { break; } } - need += 2; // margin + need += 2; // margin option_need.emplace_back(need); if (need <= max_need) { @@ -255,7 +262,8 @@ class ArgsImpl : public Args { } } - print_wrap(out, width, /* indent */ 0, "Mandatory arguments to long options" + print_wrap(out, width, /* indent */ 0, + "Mandatory arguments to long options" " are mandatory for short options too."); auto need_it = option_need.begin(); for (auto const& opt : options_) { @@ -321,12 +329,10 @@ class ArgsImpl : public Args { options_.emplace_back(std::move(opt)); } - static inline bool is_whitespace(char c) { - return c == ' ' || c == '\t'; - } + static inline bool is_whitespace(char c) { return c == ' ' || c == '\t'; } static void print_wrap(std::ostream& out, uint32_t width, uint32_t indent, - const std::string& str) { + const std::string& str) { if (indent + str.size() <= width) { out << str << '\n'; return; @@ -341,7 +347,8 @@ class ArgsImpl : public Args { size_t offset = 0; while (offset + avail < str.size()) { uint32_t i = avail; - while (i > 0 && !is_whitespace(str[offset + i])) --i; + while (i > 0 && !is_whitespace(str[offset + i])) + --i; if (i == 0) { out << str.substr(offset, avail - 1); out << "-\n"; @@ -366,8 +373,8 @@ class ArgsImpl : public Args { } // namespace -std::shared_ptr<Args::Option> Args::option( - std::string longname, std::string help) { +std::shared_ptr<Args::Option> Args::option(std::string longname, + std::string help) { return option(/* shortname */ '\0', std::move(longname), std::move(help)); } diff --git a/src/args.hh b/src/args.hh index 2436696..14f3716 100644 --- a/src/args.hh +++ b/src/args.hh @@ -32,27 +32,21 @@ class Args { static std::unique_ptr<Args> create(std::string prgname = std::string()); - virtual std::shared_ptr<Option> option( - char shortname, - std::string longname = std::string(), - std::string help = std::string()) = 0; + virtual std::shared_ptr<Option> option(char shortname, + std::string longname = std::string(), + std::string help = std::string()) = 0; - std::shared_ptr<Option> option( - std::string longname, - std::string help = std::string()); + std::shared_ptr<Option> option(std::string longname, + std::string help = std::string()); virtual std::shared_ptr<OptionArgument> option_argument( - char shortname, - std::string longname = std::string(), - std::string arg = std::string(), - std::string help = std::string(), + char shortname, std::string longname = std::string(), + std::string arg = std::string(), std::string help = std::string(), bool required = true) = 0; std::shared_ptr<OptionArgument> option_argument( - std::string longname, - std::string arg = std::string(), - std::string help = std::string(), - bool required = true); + std::string longname, std::string arg = std::string(), + std::string help = std::string(), bool required = true); virtual bool run(int argc, char** argv, std::vector<std::string_view>* arguments = nullptr) = 0; diff --git a/src/buffer.cc b/src/buffer.cc index 3073a45..18913a5 100644 --- a/src/buffer.cc +++ b/src/buffer.cc @@ -10,8 +10,7 @@ namespace { class FixedBuffer : public Buffer { public: - explicit FixedBuffer(size_t size) - : size_(size) {} + explicit FixedBuffer(size_t size) : size_(size) {} void const* rptr(size_t& avail, size_t need) override { if (rptr_ < wptr_) { @@ -29,7 +28,8 @@ class FixedBuffer : public Buffer { } void consume(size_t size) override { - if (size == 0) return; + if (size == 0) + return; if (rptr_ < wptr_) { assert(std::cmp_greater_equal(wptr_ - rptr_, size)); rptr_ += size; @@ -68,7 +68,8 @@ class FixedBuffer : public Buffer { } void commit(size_t size) override { - if (size == 0) return; + if (size == 0) + return; if (wptr_ < rptr_) { assert(std::cmp_greater_equal(rptr_ - wptr_, size)); wptr_ += size; @@ -87,13 +88,9 @@ class FixedBuffer : public Buffer { } } - [[nodiscard]] bool full() const override { - return rptr_ == wptr_ && full_; - } + [[nodiscard]] bool full() const override { return rptr_ == wptr_ && full_; } - [[nodiscard]] bool empty() const override { - return rptr_ == wptr_ && !full_; - } + [[nodiscard]] bool empty() const override { return rptr_ == wptr_ && !full_; } private: void reset() { @@ -163,9 +160,8 @@ class DynamicBuffer : public Buffer { avail = end_ - wptr_; } else if (std::cmp_less(end_ - data_.get(), max_size_)) { size_t current_size = end_ - data_.get(); - size_t new_size = std::min(max_size_, - current_size + std::max(need - avail, - current_size)); + size_t new_size = std::min( + max_size_, current_size + std::max(need - avail, current_size)); auto tmp = std::make_unique_for_overwrite<char[]>(new_size); memcpy(tmp.get(), rptr_, wptr_ - rptr_); end_ = tmp.get() + new_size; @@ -185,12 +181,10 @@ class DynamicBuffer : public Buffer { [[nodiscard]] bool full() const override { return rptr_ == data_.get() && wptr_ == end_ && - std::cmp_equal(end_ - data_.get(), max_size_); + std::cmp_equal(end_ - data_.get(), max_size_); } - [[nodiscard]] bool empty() const override { - return rptr_ == wptr_; - } + [[nodiscard]] bool empty() const override { return rptr_ == wptr_; } private: void reset() { diff --git a/src/check.hh b/src/check.hh index be65437..91c1717 100644 --- a/src/check.hh +++ b/src/check.hh @@ -7,8 +7,8 @@ namespace check { -template<typename T> -requires std::is_arithmetic_v<T> +template <typename T> + requires std::is_arithmetic_v<T> T add(T a, T b) { T ret; if (ckd_add(&ret, a, b)) @@ -16,8 +16,8 @@ T add(T a, T b) { return ret; } -template<typename T> -requires std::is_arithmetic_v<T> +template <typename T> + requires std::is_arithmetic_v<T> T sub(T a, T b) { T ret; if (ckd_sub(&ret, a, b)) @@ -25,8 +25,8 @@ T sub(T a, T b) { return ret; } -template<typename T> -requires std::is_arithmetic_v<T> +template <typename T> + requires std::is_arithmetic_v<T> T mul(T a, T b) { T ret; if (ckd_mul(&ret, a, b)) @@ -18,8 +18,7 @@ namespace { class ReaderImpl : public Reader { public: ReaderImpl(std::unique_ptr<line::Reader> reader, char separator) - : reader_(std::move(reader)), separator_(separator) { - } + : reader_(std::move(reader)), separator_(separator) {} [[nodiscard]] std::expected<std::span<std::string_view>, io::ReadError> read() override { @@ -38,9 +37,7 @@ class ReaderImpl : public Reader { } } - [[nodiscard]] uint64_t number() const override { - return reader_->number(); - } + [[nodiscard]] uint64_t number() const override { return reader_->number(); } private: std::unique_ptr<line::Reader> reader_; @@ -1,7 +1,7 @@ #ifndef CSV_HH #define CSV_HH -#include "io.hh" // IWYU pragma: export +#include "io.hh" // IWYU pragma: export #include "line.hh" #include <expected> diff --git a/src/decompress.hh b/src/decompress.hh index a15efdc..fa9ffa1 100644 --- a/src/decompress.hh +++ b/src/decompress.hh @@ -1,7 +1,7 @@ #ifndef DECOMPRESS_HH #define DECOMPRESS_HH -#include "io.hh" // IWYU pragma: export +#include "io.hh" // IWYU pragma: export namespace decompress { diff --git a/src/decompress_lzma.cc b/src/decompress_lzma.cc index 299803d..6ba2976 100644 --- a/src/decompress_lzma.cc +++ b/src/decompress_lzma.cc @@ -2,13 +2,12 @@ #include "buffer.hh" -#include <lzma.h> - #include <algorithm> #include <cstddef> #include <cstdint> #include <cstring> #include <expected> +#include <lzma.h> #include <memory> #include <optional> #include <utility> diff --git a/src/decompress_z.cc b/src/decompress_z.cc index e888e77..491eff7 100644 --- a/src/decompress_z.cc +++ b/src/decompress_z.cc @@ -3,8 +3,6 @@ #include "buffer.hh" #define ZLIB_CONST -#include <zlib.h> - #include <algorithm> #include <cstddef> #include <expected> @@ -12,6 +10,7 @@ #include <memory> #include <optional> #include <utility> +#include <zlib.h> namespace decompress { @@ -116,12 +115,13 @@ class DecompressReader : public io::Reader { } // namespace std::unique_ptr<io::Reader> zlib(std::unique_ptr<io::Reader> reader) { - return std::make_unique<DecompressReader>(std::move(reader), /* gzip = */ false); + return std::make_unique<DecompressReader>(std::move(reader), + /* gzip = */ false); } std::unique_ptr<io::Reader> gzip(std::unique_ptr<io::Reader> reader) { - return std::make_unique<DecompressReader>(std::move(reader), /* gzip = */ true); + return std::make_unique<DecompressReader>(std::move(reader), + /* gzip = */ true); } - } // namespace decompress diff --git a/src/gen_ugc.cc b/src/gen_ugc.cc index 7583272..bc7c91b 100644 --- a/src/gen_ugc.cc +++ b/src/gen_ugc.cc @@ -165,17 +165,17 @@ std::expected<std::pair<uint32_t, u::GeneralCategory>, std::string> parse_row( auto category_col = row[2]; uint32_t code; - auto [ptr, ec] = std::from_chars(code_col.data(), - code_col.data() + code_col.size(), code, - /* base */ 16); + auto [ptr, ec] = + std::from_chars(code_col.data(), code_col.data() + code_col.size(), code, + /* base */ 16); if (ec != std::errc() || ptr != code_col.data() + code_col.size()) { return std::unexpected(std::format("Invalid code value {}", code_col)); } u::GeneralCategory category; auto it = str2gc.find(category_col); if (it == str2gc.end()) { - return std::unexpected(std::format("Invalid general category {}", - category_col)); + return std::unexpected( + std::format("Invalid general category {}", category_col)); } category = it->second; @@ -186,9 +186,9 @@ std::expected<std::map<uint32_t, u::GeneralCategory>, std::string> read( std::string_view filename) { auto maybe_reader = io::open(std::string(filename)); if (!maybe_reader.has_value()) { - return std::unexpected(std::format( - "Unable to open {} for reading: {}", - filename, ioerr2str(maybe_reader.error()))); + return std::unexpected(std::format("Unable to open {} for reading: {}", + filename, + ioerr2str(maybe_reader.error()))); } auto reader = std::move(maybe_reader.value()); if (filename.ends_with(".gz")) { @@ -202,17 +202,17 @@ std::expected<std::map<uint32_t, u::GeneralCategory>, std::string> read( while (true) { auto row = csv_reader->read(); if (!row.has_value()) { - return std::unexpected(std::format( - "{}:{}: Error reading file: {}", - filename, csv_reader->number(), ioerr2str(row.error()))); + return std::unexpected(std::format("{}:{}: Error reading file: {}", + filename, csv_reader->number(), + ioerr2str(row.error()))); } if (row->empty()) break; auto pair = parse_row(row.value()); if (!pair.has_value()) { - return std::unexpected(std::format( - "{}:{}: {}", filename, csv_reader->number(), pair.error())); + return std::unexpected(std::format("{}:{}: {}", filename, + csv_reader->number(), pair.error())); } auto name_col = (*row)[1]; @@ -220,15 +220,15 @@ std::expected<std::map<uint32_t, u::GeneralCategory>, std::string> read( std::string prefix(name_col.substr(0, name_col.size() - 8)); row = csv_reader->read(); if (!row.has_value()) { - return std::unexpected(std::format( - "{}:{}: Error reading file: {}", - filename, csv_reader->number(), ioerr2str(row.error()))); + return std::unexpected(std::format("{}:{}: Error reading file: {}", + filename, csv_reader->number(), + ioerr2str(row.error()))); } auto second_pair = parse_row(row.value()); if (!pair.has_value()) { - return std::unexpected(std::format( - "{}:{}: {}", filename, csv_reader->number(), pair.error())); + return std::unexpected(std::format("{}:{}: {}", filename, + csv_reader->number(), pair.error())); } name_col = (*row)[1]; @@ -236,29 +236,29 @@ std::expected<std::map<uint32_t, u::GeneralCategory>, std::string> read( name_col.substr(0, name_col.size() - 7) == prefix) { if (pair->second != second_pair->second) { return std::unexpected(std::format( - "{}:{}: Invalid range, general category doesn't match", - filename, csv_reader->number())); + "{}:{}: Invalid range, general category doesn't match", filename, + csv_reader->number())); } for (uint32_t c = pair->first; c <= second_pair->first; ++c) { auto emplace_ret = ret.emplace(c, pair->second); if (!emplace_ret.second) { - return std::unexpected(std::format( - "{}:{}: Duplicate value for {:#08x}", - filename, csv_reader->number(), c)); + return std::unexpected( + std::format("{}:{}: Duplicate value for {:#08x}", filename, + csv_reader->number(), c)); } } } else { - return std::unexpected(std::format( - "{}:{}: Invalid range, {} doesn't match {}", - filename, csv_reader->number(), prefix, name_col)); + return std::unexpected( + std::format("{}:{}: Invalid range, {} doesn't match {}", filename, + csv_reader->number(), prefix, name_col)); } } else { auto emplace_ret = ret.emplace(std::move(pair.value())); if (!emplace_ret.second) { - return std::unexpected(std::format( - "{}:{}: Duplicate value for {:#08x}", - filename, csv_reader->number(), emplace_ret.first->first)); + return std::unexpected(std::format("{}:{}: Duplicate value for {:#08x}", + filename, csv_reader->number(), + emplace_ret.first->first)); } } } @@ -310,7 +310,7 @@ int main(int argc, char** argv) { print_footer(std::cout, prefix); } else { std::fstream out{std::string(arguments[1]), - std::fstream::trunc | std::fstream::out}; + std::fstream::trunc | std::fstream::out}; print_header(out, prefix); print_body(out, general_categories.value()); print_footer(out, prefix); @@ -24,15 +24,14 @@ namespace { class BasicReader : public Reader { public: - explicit BasicReader(unique_fd fd) - : fd_(std::move(fd)) { - } + explicit BasicReader(unique_fd fd) : fd_(std::move(fd)) {} [[nodiscard]] std::expected<size_t, ReadError> read(void* dst, size_t max) override { ssize_t ret = ::read( - fd_.get(), dst, std::min(static_cast<size_t>( - std::numeric_limits<ssize_t>::max()), max)); + fd_.get(), dst, + std::min(static_cast<size_t>(std::numeric_limits<ssize_t>::max()), + max)); if (ret < 0) { switch (errno) { case EINTR: @@ -50,8 +49,10 @@ class BasicReader : public Reader { off_t ret; if (sizeof(size_t) > sizeof(off_t)) { // NOLINTNEXTLINE(bugprone-narrowing-conversions) - ret = lseek(fd_.get(), std::min(static_cast<size_t>( - std::numeric_limits<off_t>::max()), max), SEEK_CUR); + ret = lseek( + fd_.get(), + std::min(static_cast<size_t>(std::numeric_limits<off_t>::max()), max), + SEEK_CUR); } else { ret = lseek(fd_.get(), static_cast<off_t>(max), SEEK_CUR); } @@ -93,9 +94,7 @@ class BasicReader : public Reader { class MemoryReader : public Reader { public: - MemoryReader(void* ptr, size_t size) - : ptr_(ptr), size_(size) { - } + MemoryReader(void* ptr, size_t size) : ptr_(ptr), size_(size) {} [[nodiscard]] std::expected<size_t, ReadError> read(void* dst, size_t max) override { @@ -125,12 +124,9 @@ class MemoryReader : public Reader { class MmapReader : public MemoryReader { public: MmapReader(unique_fd fd, void* ptr, size_t size) - : MemoryReader(ptr, size), fd_(std::move(fd)) { - } + : MemoryReader(ptr, size), fd_(std::move(fd)) {} - ~MmapReader() override { - munmap(ptr_, size_); - } + ~MmapReader() override { munmap(ptr_, size_); } private: unique_fd fd_; diff --git a/src/line.cc b/src/line.cc index 2eeb116..8ee7134 100644 --- a/src/line.cc +++ b/src/line.cc @@ -20,16 +20,19 @@ const char kLineTerminators[] = "\r\n"; class ReaderImpl : public Reader { public: ReaderImpl(std::unique_ptr<io::Reader> reader, size_t max_len) - : reader_(std::move(reader)), max_len_(max_len), + : reader_(std::move(reader)), + max_len_(max_len), buffer_(std::make_unique_for_overwrite<char[]>( check::add(max_len, static_cast<size_t>(2)))), - rptr_(buffer_.get()), wptr_(buffer_.get()), search_(rptr_), + rptr_(buffer_.get()), + wptr_(buffer_.get()), + search_(rptr_), end_(buffer_.get() + check::add(max_len, static_cast<size_t>(2))) {} [[nodiscard]] std::expected<std::string_view, ReadError> read() override { while (true) { - search_ = std::find_first_of(search_, wptr_, - kLineTerminators, kLineTerminators + 2); + search_ = std::find_first_of(search_, wptr_, kLineTerminators, + kLineTerminators + 2); if (search_ < wptr_) { if (std::cmp_greater(search_ - rptr_, max_len_)) { return line(max_len_, 0); @@ -91,9 +94,11 @@ class ReaderImpl : public Reader { void make_space_if_needed() { size_t free = rptr_ - buffer_.get(); - if (free == 0) return; + if (free == 0) + return; size_t avail = end_ - wptr_; - if (avail > 1024) return; + if (avail > 1024) + return; memmove(buffer_.get(), rptr_, wptr_ - rptr_); search_ -= free; wptr_ -= free; @@ -119,11 +124,9 @@ class ReaderImpl : public Reader { } // namespace -ReadError::ReadError() - : eof(true) {} +ReadError::ReadError() : eof(true) {} -ReadError::ReadError(io::ReadError error) - : eof(false), io_error(error) {} +ReadError::ReadError(io::ReadError error) : eof(false), io_error(error) {} std::unique_ptr<Reader> open(std::unique_ptr<io::Reader> reader, size_t max_len) { diff --git a/src/line.hh b/src/line.hh index 94e3646..5ce42dd 100644 --- a/src/line.hh +++ b/src/line.hh @@ -1,7 +1,7 @@ #ifndef LINE_HH #define LINE_HH -#include "io.hh" // IWYU pragma: export +#include "io.hh" // IWYU pragma: export #include <cstddef> #include <expected> diff --git a/src/main.cc b/src/main.cc index 27b4854..a1c8cd5 100644 --- a/src/main.cc +++ b/src/main.cc @@ -1,8 +1,8 @@ -#include <iostream> - #include "args.hh" #include "config.h" +#include <iostream> + #ifndef VERSION # define VERSION "unknown" #endif @@ -24,8 +24,8 @@ void split(std::string_view str, std::vector<std::string_view>& out, } } -std::vector<std::string_view> split(std::string_view str, - char separator, bool keep_empty) { +std::vector<std::string_view> split(std::string_view str, char separator, + bool keep_empty) { std::vector<std::string_view> vec; split(str, vec, separator, keep_empty); return vec; @@ -1,19 +1,19 @@ #ifndef U_HH #define U_HH -#include "ugc.hh" // IWYU pragma: export +#include "ugc.hh" // IWYU pragma: export namespace u { enum class ReadError : uint8_t { - Invalid, // Invalid sequence - End, // At end (it == end) - Incomplete, // Too few bytes + Invalid, // Invalid sequence + End, // At end (it == end) + Incomplete, // Too few bytes }; enum class ReadErrorReplace : uint8_t { - End, // At end (it == end) - Incomplete, // Too few bytes + End, // At end (it == end) + Incomplete, // Too few bytes }; enum class Version : uint8_t { @@ -1,9 +1,9 @@ #ifndef U16_HH #define U16_HH -#include "u.hh" // IWYU pragma: export +#include "u.hh" // IWYU pragma: export -#include <cstdint> // IWYU pragma: export +#include <cstdint> // IWYU pragma: export #include <expected> #include <iterator> #include <type_traits> @@ -11,10 +11,11 @@ namespace u16 { -template<std::forward_iterator T> +template <std::forward_iterator T> requires std::is_same_v<std::iter_value_t<T>, uint16_t> std::expected<uint32_t, u::ReadError> read(T& start, const T& end) { - if (start == end) return std::unexpected(u::ReadError::End); + if (start == end) + return std::unexpected(u::ReadError::End); uint16_t u = *start; if (u >= 0xd800 && u <= 0xdbff) { if (std::distance(start, end) < 2) { @@ -35,7 +36,7 @@ std::expected<uint32_t, u::ReadError> read(T& start, const T& end) { return u; } -template<std::forward_iterator T> +template <std::forward_iterator T> requires std::is_same_v<std::iter_value_t<T>, uint16_t> std::expected<uint32_t, u::ReadErrorReplace> read_replace(T& start, const T& end, @@ -58,14 +59,16 @@ std::expected<uint32_t, u::ReadErrorReplace> read_replace(T& start, return 0xfffd; } -template<std::forward_iterator T> +template <std::forward_iterator T> requires std::is_same_v<std::iter_value_t<T>, uint16_t> bool write(T& start, const T& end, uint32_t code) { if (code < 0x10000) { - if (start == end) return false; + if (start == end) + return false; *start = static_cast<uint16_t>(code); } else { - if (std::distance(start, end) < 2) return false; + if (std::distance(start, end) < 2) + return false; code -= 0x10000; *start = static_cast<uint16_t>(0xd800 + (code >> 10)); std::advance(start, 1); @@ -75,12 +78,14 @@ bool write(T& start, const T& end, uint32_t code) { return true; } -template<std::forward_iterator T> +template <std::forward_iterator T> requires std::is_same_v<std::iter_value_t<T>, uint16_t> bool skip(T& start, const T& end) { - if (start == end) return false; + if (start == end) + return false; if (*start >= 0xd800 && *start <= 0xdbff) { - if (std::distance(start, end) < 2) return false; + if (std::distance(start, end) < 2) + return false; std::advance(start, 2); return true; } @@ -1,9 +1,9 @@ #ifndef U8_HH #define U8_HH -#include "u.hh" // IWYU pragma: export +#include "u.hh" // IWYU pragma: export -#include <cstdint> // IWYU pragma: export +#include <cstdint> // IWYU pragma: export #include <expected> #include <iterator> #include <type_traits> @@ -11,10 +11,11 @@ namespace u8 { -template<std::forward_iterator T> +template <std::forward_iterator T> requires std::is_same_v<std::iter_value_t<T>, uint8_t> std::expected<uint32_t, u::ReadError> read(T& start, const T& end) { - if (start == end) return std::unexpected(u::ReadError::End); + if (start == end) + return std::unexpected(u::ReadError::End); uint32_t u; switch (*start >> 4) { case 0xf: @@ -102,7 +103,7 @@ std::expected<uint32_t, u::ReadError> read(T& start, const T& end) { return u; } -template<std::forward_iterator T> +template <std::forward_iterator T> requires std::is_same_v<std::iter_value_t<T>, uint8_t> std::expected<uint32_t, u::ReadErrorReplace> read_replace(T& start, const T& end, @@ -125,26 +126,30 @@ std::expected<uint32_t, u::ReadErrorReplace> read_replace(T& start, return 0xfffd; } -template<std::forward_iterator T> +template <std::forward_iterator T> requires std::is_same_v<std::iter_value_t<T>, uint8_t> bool write(T& start, const T& end, uint32_t code) { if (code < 0x80) { - if (start == end) return false; + if (start == end) + return false; *start = static_cast<uint8_t>(code); } else if (code < 0x800) { - if (std::distance(start, end) < 2) return false; + if (std::distance(start, end) < 2) + return false; *start = 0xc0 | static_cast<uint8_t>(code >> 6); std::advance(start, 1); *start = 0x80 | static_cast<uint8_t>(code & 0x3f); } else if (code < 0x10000) { - if (std::distance(start, end) < 3) return false; + if (std::distance(start, end) < 3) + return false; *start = 0xe0 | static_cast<uint8_t>(code >> 12); std::advance(start, 1); *start = 0x80 | static_cast<uint8_t>((code >> 6) & 0x3f); std::advance(start, 1); *start = 0x80 | static_cast<uint8_t>(code & 0x3f); } else { - if (std::distance(start, end) < 4) return false; + if (std::distance(start, end) < 4) + return false; *start = 0xf0 | static_cast<uint8_t>(code >> 18); std::advance(start, 1); *start = 0x80 | static_cast<uint8_t>((code >> 12) & 0x3f); @@ -157,22 +162,26 @@ bool write(T& start, const T& end, uint32_t code) { return true; } -template<std::forward_iterator T> +template <std::forward_iterator T> requires std::is_same_v<std::iter_value_t<T>, uint8_t> bool skip(T& start, const T& end) { - if (start == end) return false; + if (start == end) + return false; switch (*start >> 4) { case 0xf: - if (std::distance(start, end) < 4) return false; + if (std::distance(start, end) < 4) + return false; std::advance(start, 4); break; case 0xe: - if (std::distance(start, end) < 3) return false; + if (std::distance(start, end) < 3) + return false; std::advance(start, 3); break; case 0xc: case 0xd: - if (std::distance(start, end) < 2) return false; + if (std::distance(start, end) < 2) + return false; std::advance(start, 2); break; default: @@ -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; @@ -1,7 +1,7 @@ #ifndef UIO_HH #define UIO_HH -#include "io.hh" // IWYU pragma: export +#include "io.hh" // IWYU pragma: export #include <cstddef> #include <expected> @@ -25,7 +25,7 @@ struct ReaderConfig { bool skip_bom{true}; }; -} // namespace u8 +} // namespace u namespace u8 { @@ -34,15 +34,15 @@ class Reader : public io::Reader { using io::Reader::read; using io::Reader::repeat_read; - [[nodiscard]] std::expected<size_t, io::ReadError> read( - std::string& data, size_t max); + [[nodiscard]] std::expected<size_t, io::ReadError> read(std::string& data, + size_t max); [[nodiscard]] std::expected<size_t, io::ReadError> repeat_read( std::string& data, size_t max); }; -[[nodiscard]] std::unique_ptr<Reader> open( - std::unique_ptr<io::Reader> reader, u::ReaderConfig config = {}); +[[nodiscard]] std::unique_ptr<Reader> open(std::unique_ptr<io::Reader> reader, + u::ReaderConfig config = {}); [[nodiscard]] std::expected<std::unique_ptr<Reader>, io::OpenError> open( const std::string& file_path, u::ReaderConfig config = {}); @@ -58,15 +58,15 @@ class Reader : public io::Reader { using io::Reader::read; using io::Reader::repeat_read; - [[nodiscard]] std::expected<size_t, io::ReadError> read( - std::u16string& data, size_t max); + [[nodiscard]] std::expected<size_t, io::ReadError> read(std::u16string& data, + size_t max); [[nodiscard]] std::expected<size_t, io::ReadError> repeat_read( std::u16string& data, size_t max); }; -[[nodiscard]] std::unique_ptr<Reader> open( - std::unique_ptr<io::Reader> reader, u::ReaderConfig config = {}); +[[nodiscard]] std::unique_ptr<Reader> open(std::unique_ptr<io::Reader> reader, + u::ReaderConfig config = {}); [[nodiscard]] std::expected<std::unique_ptr<Reader>, io::OpenError> open( const std::string& file_path, u::ReaderConfig config = {}); diff --git a/src/umod8.hh b/src/umod8.hh index b91b199..4731942 100644 --- a/src/umod8.hh +++ b/src/umod8.hh @@ -1,9 +1,9 @@ #ifndef UMOD8_HH #define UMOD8_HH -#include "u.hh" // IWYU pragma: export +#include "u.hh" // IWYU pragma: export -#include <cstdint> // IWYU pragma: export +#include <cstdint> // IWYU pragma: export #include <expected> #include <iterator> #include <type_traits> @@ -11,10 +11,11 @@ namespace umod8 { -template<std::forward_iterator T> +template <std::forward_iterator T> requires std::is_same_v<std::iter_value_t<T>, uint8_t> std::expected<uint32_t, u::ReadError> read(T& start, const T& end) { - if (start == end) return std::unexpected(u::ReadError::End); + if (start == end) + return std::unexpected(u::ReadError::End); uint32_t u; switch (*start >> 4) { case 0xe: { @@ -44,7 +45,8 @@ std::expected<uint32_t, u::ReadError> read(T& start, const T& end) { std::advance(start, 1); // Not going recursive here as we don't want it unbounded // Lone surrogate pair at end == invalid. - if (start == end) return std::unexpected(u::ReadError::Invalid); + if (start == end) + return std::unexpected(u::ReadError::Invalid); if ((*start >> 4) == 0xe) { if (std::distance(start, end) < 3) { start = tmp; @@ -110,7 +112,7 @@ std::expected<uint32_t, u::ReadError> read(T& start, const T& end) { return u; } -template<std::forward_iterator T> +template <std::forward_iterator T> requires std::is_same_v<std::iter_value_t<T>, uint8_t> std::expected<uint32_t, u::ReadErrorReplace> read_replace(T& start, const T& end, @@ -133,19 +135,22 @@ std::expected<uint32_t, u::ReadErrorReplace> read_replace(T& start, return 0xfffd; } -template<std::forward_iterator T> +template <std::forward_iterator T> requires std::is_same_v<std::iter_value_t<T>, uint8_t> bool write(T& start, const T& end, uint32_t code) { if (code > 0 && code < 0x80) { - if (start == end) return false; + if (start == end) + return false; *start = static_cast<uint8_t>(code); } else if (code < 0x800) { - if (std::distance(start, end) < 2) return false; + if (std::distance(start, end) < 2) + return false; *start = 0xc0 | static_cast<uint8_t>(code >> 6); std::advance(start, 1); *start = 0x80 | static_cast<uint8_t>(code & 0x3f); } else if (code < 0x10000) { - if (std::distance(start, end) < 3) return false; + if (std::distance(start, end) < 3) + return false; *start = 0xe0 | static_cast<uint8_t>(code >> 12); std::advance(start, 1); *start = 0x80 | static_cast<uint8_t>((code >> 6) & 0x3f); @@ -165,20 +170,23 @@ bool write(T& start, const T& end, uint32_t code) { return true; } -template<std::forward_iterator T> +template <std::forward_iterator T> requires std::is_same_v<std::iter_value_t<T>, uint8_t> bool skip(T& start, const T& end) { - if (start == end) return false; + if (start == end) + return false; switch (*start >> 4) { case 0xe: { auto tmp = start; - if (read(start, end).has_value()) return true; + if (read(start, end).has_value()) + return true; start = tmp; return false; } case 0xc: case 0xd: - if (std::distance(start, end) < 2) return false; + if (std::distance(start, end) < 2) + return false; std::advance(start, 2); break; default: diff --git a/src/unique_fd.hh b/src/unique_fd.hh index 189d513..2950905 100644 --- a/src/unique_fd.hh +++ b/src/unique_fd.hh @@ -3,28 +3,19 @@ class unique_fd { public: - constexpr unique_fd() - : fd_(-1) {} - explicit constexpr unique_fd(int fd) - : fd_(fd) {} + constexpr unique_fd() : fd_(-1) {} + explicit constexpr unique_fd(int fd) : fd_(fd) {} unique_fd(unique_fd& fd) = delete; unique_fd& operator=(unique_fd& fd) = delete; - unique_fd(unique_fd&& fd) - : fd_(fd.release()) {} + unique_fd(unique_fd&& fd) : fd_(fd.release()) {} unique_fd& operator=(unique_fd&& fd) { reset(fd.release()); return *this; } - ~unique_fd() { - reset(); - } + ~unique_fd() { reset(); } - bool operator==(unique_fd const& fd) const { - return get() == fd.get(); - } - bool operator!=(unique_fd const& fd) const { - return get() != fd.get(); - } + bool operator==(unique_fd const& fd) const { return get() == fd.get(); } + bool operator!=(unique_fd const& fd) const { return get() != fd.get(); } int get() const { return fd_; } explicit operator bool() const { return fd_ != -1; } |
