diff options
| -rw-r--r-- | .clang-format | 315 | ||||
| -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 | ||||
| -rw-r--r-- | test/args.cc | 138 | ||||
| -rw-r--r-- | test/buffer.cc | 10 | ||||
| -rw-r--r-- | test/csv.cc | 4 | ||||
| -rw-r--r-- | test/decompress.cc | 60 | ||||
| -rw-r--r-- | test/io.cc | 20 | ||||
| -rw-r--r-- | test/io_test_helper.cc | 6 | ||||
| -rw-r--r-- | test/io_test_helper.hh | 2 | ||||
| -rw-r--r-- | test/line.cc | 24 | ||||
| -rw-r--r-- | test/str.cc | 4 | ||||
| -rw-r--r-- | test/u.cc | 53 | ||||
| -rw-r--r-- | test/uio.cc | 269 |
34 files changed, 903 insertions, 587 deletions
diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..aa4a4dc --- /dev/null +++ b/.clang-format @@ -0,0 +1,315 @@ +--- +Language: Cpp +AccessModifierOffset: -1 +AlignAfterOpenBracket: Align +AlignArrayOfStructures: None +AlignConsecutiveAssignments: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + AlignFunctionDeclarations: false + AlignFunctionPointers: false + PadOperators: true +AlignConsecutiveBitFields: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + AlignFunctionDeclarations: false + AlignFunctionPointers: false + PadOperators: false +AlignConsecutiveDeclarations: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + AlignFunctionDeclarations: true + AlignFunctionPointers: false + PadOperators: false +AlignConsecutiveMacros: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + AlignFunctionDeclarations: false + AlignFunctionPointers: false + PadOperators: false +AlignConsecutiveShortCaseStatements: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCaseArrows: false + AlignCaseColons: false +AlignConsecutiveTableGenBreakingDAGArgColons: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + AlignFunctionDeclarations: false + AlignFunctionPointers: false + PadOperators: false +AlignConsecutiveTableGenCondOperatorColons: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + AlignFunctionDeclarations: false + AlignFunctionPointers: false + PadOperators: false +AlignConsecutiveTableGenDefinitionColons: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + AlignFunctionDeclarations: false + AlignFunctionPointers: false + PadOperators: false +AlignEscapedNewlines: Left +AlignOperands: Align +AlignTrailingComments: + Kind: Always + OverEmptyLines: 0 +AllowAllArgumentsOnNextLine: true +AllowAllParametersOfDeclarationOnNextLine: true +AllowBreakBeforeNoexceptSpecifier: Never +AllowShortBlocksOnASingleLine: Never +AllowShortCaseExpressionOnASingleLine: true +AllowShortCaseLabelsOnASingleLine: false +AllowShortCompoundRequirementOnASingleLine: true +AllowShortEnumsOnASingleLine: true +AllowShortFunctionsOnASingleLine: All +AllowShortIfStatementsOnASingleLine: Never +AllowShortLambdasOnASingleLine: All +AllowShortLoopsOnASingleLine: false +AllowShortNamespacesOnASingleLine: false +AlwaysBreakAfterDefinitionReturnType: None +AlwaysBreakBeforeMultilineStrings: true +AttributeMacros: + - __capability +BinPackArguments: true +BinPackParameters: BinPack +BitFieldColonSpacing: Both +BraceWrapping: + AfterCaseLabel: false + AfterClass: false + AfterControlStatement: Never + AfterEnum: false + AfterExternBlock: false + AfterFunction: false + AfterNamespace: false + AfterObjCDeclaration: false + AfterStruct: false + AfterUnion: false + BeforeCatch: false + BeforeElse: false + BeforeLambdaBody: false + BeforeWhile: false + IndentBraces: false + SplitEmptyFunction: true + SplitEmptyRecord: true + SplitEmptyNamespace: true +BracedInitializerIndentWidth: 2 +BreakAdjacentStringLiterals: true +BreakAfterAttributes: Leave +BreakAfterJavaFieldAnnotations: false +BreakAfterReturnType: None +BreakArrays: true +BreakBeforeBinaryOperators: None +BreakBeforeConceptDeclarations: Always +BreakBeforeBraces: Attach +BreakBeforeInlineASMColon: OnlyMultiline +BreakBeforeTernaryOperators: true +BreakBinaryOperations: Never +BreakConstructorInitializers: BeforeColon +BreakFunctionDefinitionParameters: false +BreakInheritanceList: BeforeColon +BreakStringLiterals: true +BreakTemplateDeclarations: Yes +ColumnLimit: 80 +CommentPragmas: '^ IWYU pragma:' +CompactNamespaces: false +ConstructorInitializerIndentWidth: 4 +ContinuationIndentWidth: 4 +Cpp11BracedListStyle: true +DerivePointerAlignment: true +DisableFormat: false +EmptyLineAfterAccessModifier: Never +EmptyLineBeforeAccessModifier: LogicalBlock +ExperimentalAutoDetectBinPacking: false +FixNamespaceComments: true +ForEachMacros: + - foreach + - Q_FOREACH + - BOOST_FOREACH +IfMacros: + - KJ_IF_MAYBE +IncludeBlocks: Regroup +IncludeCategories: + - Regex: '^<.*' + Priority: 2 + SortPriority: 0 + CaseSensitive: false + - Regex: '.*' + Priority: 1 + SortPriority: 0 + CaseSensitive: false +IncludeIsMainRegex: '([-_](lzma|z|test|unittest))?$' +IncludeIsMainSourceRegex: '' +IndentAccessModifiers: false +IndentCaseBlocks: false +IndentCaseLabels: true +IndentExportBlock: true +IndentExternBlock: AfterExternBlock +IndentGotoLabels: true +IndentPPDirectives: AfterHash +IndentRequiresClause: true +IndentWidth: 2 +IndentWrappedFunctionNames: false +InsertBraces: false +InsertNewlineAtEOF: false +InsertTrailingCommas: None +IntegerLiteralSeparator: + Binary: 0 + BinaryMinDigits: 0 + Decimal: 0 + DecimalMinDigits: 0 + Hex: 0 + HexMinDigits: 0 +JavaScriptQuotes: Leave +JavaScriptWrapImports: true +KeepEmptyLines: + AtEndOfFile: false + AtStartOfBlock: false + AtStartOfFile: true +KeepFormFeed: false +LambdaBodyIndentation: Signature +LineEnding: DeriveLF +MacroBlockBegin: '' +MacroBlockEnd: '' +MainIncludeChar: Quote +MaxEmptyLinesToKeep: 1 +NamespaceIndentation: None +ObjCBinPackProtocolList: Never +ObjCBlockIndentWidth: 2 +ObjCBreakBeforeNestedBlockParam: true +ObjCSpaceAfterProperty: false +ObjCSpaceBeforeProtocolList: true +PackConstructorInitializers: NextLine +PenaltyBreakAssignment: 2 +PenaltyBreakBeforeFirstCallParameter: 1 +PenaltyBreakBeforeMemberAccess: 150 +PenaltyBreakComment: 300 +PenaltyBreakFirstLessLess: 120 +PenaltyBreakOpenParenthesis: 0 +PenaltyBreakScopeResolution: 500 +PenaltyBreakString: 1000 +PenaltyBreakTemplateDeclaration: 10 +PenaltyExcessCharacter: 1000000 +PenaltyIndentedWhitespace: 0 +PenaltyReturnTypeOnItsOwnLine: 200 +PointerAlignment: Left +PPIndentWidth: 1 +QualifierAlignment: Leave +RawStringFormats: + - Language: Cpp + Delimiters: + - cc + - CC + - cpp + - Cpp + - CPP + - 'c++' + - 'C++' + CanonicalDelimiter: '' + BasedOnStyle: google + - Language: TextProto + Delimiters: + - pb + - PB + - proto + - PROTO + EnclosingFunctions: + - EqualsProto + - EquivToProto + - PARSE_PARTIAL_TEXT_PROTO + - PARSE_TEST_PROTO + - PARSE_TEXT_PROTO + - ParseTextOrDie + - ParseTextProtoOrDie + - ParseTestProto + - ParsePartialTestProto + CanonicalDelimiter: pb + BasedOnStyle: google +ReferenceAlignment: Pointer +ReflowComments: IndentOnly +RemoveBracesLLVM: false +RemoveEmptyLinesInUnwrappedLines: false +RemoveParentheses: Leave +RemoveSemicolon: false +RequiresClausePosition: OwnLine +RequiresExpressionIndentation: OuterScope +SeparateDefinitionBlocks: Leave +ShortNamespaceLines: 1 +SkipMacroDefinitionBody: false +SortIncludes: CaseSensitive +SortJavaStaticImport: Before +SortUsingDeclarations: LexicographicNumeric +SpaceAfterCStyleCast: false +SpaceAfterLogicalNot: false +SpaceAfterTemplateKeyword: true +SpaceAroundPointerQualifiers: Default +SpaceBeforeAssignmentOperators: true +SpaceBeforeCaseColon: false +SpaceBeforeCpp11BracedList: false +SpaceBeforeCtorInitializerColon: true +SpaceBeforeInheritanceColon: true +SpaceBeforeJsonColon: false +SpaceBeforeParens: ControlStatements +SpaceBeforeParensOptions: + AfterControlStatements: true + AfterForeachMacros: true + AfterFunctionDefinitionName: false + AfterFunctionDeclarationName: false + AfterIfMacros: true + AfterOverloadedOperator: false + AfterPlacementOperator: true + AfterRequiresInClause: false + AfterRequiresInExpression: false + BeforeNonEmptyParentheses: false +SpaceBeforeRangeBasedForLoopColon: true +SpaceBeforeSquareBrackets: false +SpaceInEmptyBlock: false +SpacesBeforeTrailingComments: 2 +SpacesInAngles: Never +SpacesInContainerLiterals: true +SpacesInLineCommentPrefix: + Minimum: 1 + Maximum: -1 +SpacesInParens: Never +SpacesInParensOptions: + ExceptDoubleParentheses: false + InCStyleCasts: false + InConditionalStatements: false + InEmptyParentheses: false + Other: false +SpacesInSquareBrackets: false +Standard: Auto +StatementAttributeLikeMacros: + - Q_EMIT +StatementMacros: + - Q_UNUSED + - QT_REQUIRE_VERSION +TableGenBreakInsideDAGArg: DontBreak +TabWidth: 8 +UseTab: Never +VerilogBreakBetweenInstancePorts: true +WhitespaceSensitiveMacros: + - BOOST_PP_STRINGIZE + - CF_SWIFT_NAME + - NS_SWIFT_NAME + - PP_STRINGIZE + - STRINGIZE +WrapNamespaceBodyWithEmptyLines: Leave +... + 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; } diff --git a/test/args.cc b/test/args.cc index a43c59c..22159d2 100644 --- a/test/args.cc +++ b/test/args.cc @@ -1,8 +1,7 @@ -#include <gtest/gtest.h> - #include "args.hh" #include <cstddef> +#include <gtest/gtest.h> #include <memory> #include <sstream> #include <string> @@ -10,17 +9,17 @@ #include <utility> #include <vector> -#define SETUP_OPTIONS(args) \ - auto short_only = (args)->option('a', "", "an option"); \ - auto short_long = (args)->option('b', "bold", "set font style to bold"); \ - auto long_only = (args)->option("cold", "use if it is cold outside"); \ - auto short_only_req = (args)->option_argument('d', "", "", "distance"); \ - auto short_long_req = (args)->option_argument( \ - 'e', "eat", "FOOD", "what to order, what to eat?"); \ - auto long_only_req = (args)->option_argument( \ - "form", "", "circle, shape or something else?"); \ - auto short_only_opt = (args)->option_argument('g', "", "", "", false); \ - auto short_long_opt = (args)->option_argument('h', "hold", "", "", false); \ +#define SETUP_OPTIONS(args) \ + auto short_only = (args)->option('a', "", "an option"); \ + auto short_long = (args)->option('b', "bold", "set font style to bold"); \ + auto long_only = (args)->option("cold", "use if it is cold outside"); \ + auto short_only_req = (args)->option_argument('d', "", "", "distance"); \ + auto short_long_req = (args)->option_argument( \ + 'e', "eat", "FOOD", "what to order, what to eat?"); \ + auto long_only_req = \ + (args)->option_argument("form", "", "circle, shape or something else?"); \ + auto short_only_opt = (args)->option_argument('g', "", "", "", false); \ + auto short_long_opt = (args)->option_argument('h', "hold", "", "", false); \ auto long_only_opt = (args)->option_argument("invert", "", "", false); namespace { @@ -30,8 +29,7 @@ class Arguments { [[nodiscard]] int c() const { return static_cast<int>(str_.size()); }; [[nodiscard]] char** v() const { return ptr_.get(); } - explicit Arguments(std::vector<std::string> str) - : str_(std::move(str)) { + explicit Arguments(std::vector<std::string> str) : str_(std::move(str)) { ptr_ = std::make_unique<char*[]>(str_.size()); for (size_t i = 0; i < str_.size(); ++i) { ptr_[i] = const_cast<char*>(str_[i].c_str()); @@ -48,9 +46,7 @@ class Arguments { class Builder { public: - Arguments build() { - return Arguments(std::move(str_)); - } + Arguments build() { return Arguments(std::move(str_)); } Builder& add(std::string str) { str_.emplace_back(std::move(str)); @@ -110,16 +106,18 @@ TEST(args, options_set) { SETUP_OPTIONS(args); Builder builder; auto arg = builder.add("foo") - .add("-a") - .add("-b") - .add("--cold") - .add("-d").add("10") - .add("-e").add("hamburger") - .add("--form=circle") - .add("-g") - .add("-h") - .add("--invert") - .build(); + .add("-a") + .add("-b") + .add("--cold") + .add("-d") + .add("10") + .add("-e") + .add("hamburger") + .add("--form=circle") + .add("-g") + .add("-h") + .add("--invert") + .build(); std::vector<std::string_view> out; EXPECT_TRUE(args->run(arg.c(), arg.v(), &out)); EXPECT_TRUE(out.empty()); @@ -148,16 +146,18 @@ TEST(args, options_set_variant) { SETUP_OPTIONS(args); Builder builder; auto arg = builder.add("foo") - .add("-a") - .add("--bold") - .add("--cold") - .add("-d").add("10") - .add("--eat=hamburger") - .add("--form").add("circle") - .add("-g") - .add("--hold=foo") - .add("--invert=bar") - .build(); + .add("-a") + .add("--bold") + .add("--cold") + .add("-d") + .add("10") + .add("--eat=hamburger") + .add("--form") + .add("circle") + .add("-g") + .add("--hold=foo") + .add("--invert=bar") + .build(); std::vector<std::string_view> out; EXPECT_TRUE(args->run(arg.c(), arg.v(), &out)); EXPECT_TRUE(out.empty()); @@ -187,9 +187,7 @@ TEST(args, options_short_missing_value) { auto args = Args::create(); SETUP_OPTIONS(args); Builder builder; - auto arg = builder.add("foo") - .add("-d") - .build(); + auto arg = builder.add("foo").add("-d").build(); EXPECT_FALSE(args->run(arg.c(), arg.v())); std::stringstream ss; args->print_error(ss); @@ -200,9 +198,7 @@ TEST(args, options_short_unknown_value) { auto args = Args::create(); SETUP_OPTIONS(args); Builder builder; - auto arg = builder.add("foo") - .add("-X") - .build(); + auto arg = builder.add("foo").add("-X").build(); EXPECT_FALSE(args->run(arg.c(), arg.v())); std::stringstream ss; args->print_error(ss); @@ -213,9 +209,7 @@ TEST(args, options_long_missing_value) { auto args = Args::create(); SETUP_OPTIONS(args); Builder builder; - auto arg = builder.add("foo") - .add("--form") - .build(); + auto arg = builder.add("foo").add("--form").build(); EXPECT_FALSE(args->run(arg.c(), arg.v())); std::stringstream ss; args->print_error(ss); @@ -226,9 +220,7 @@ TEST(args, options_long_unsupported_value) { auto args = Args::create(); SETUP_OPTIONS(args); Builder builder; - auto arg = builder.add("foo") - .add("--cold=feet") - .build(); + auto arg = builder.add("foo").add("--cold=feet").build(); EXPECT_FALSE(args->run(arg.c(), arg.v())); std::stringstream ss; args->print_error(ss); @@ -239,9 +231,7 @@ TEST(args, options_long_unknown_value) { auto args = Args::create(); SETUP_OPTIONS(args); Builder builder; - auto arg = builder.add("foo") - .add("--experience") - .build(); + auto arg = builder.add("foo").add("--experience").build(); EXPECT_FALSE(args->run(arg.c(), arg.v())); std::stringstream ss; args->print_error(ss); @@ -252,9 +242,7 @@ TEST(args, options_short_dash_value) { auto args = Args::create(); SETUP_OPTIONS(args); Builder builder; - auto arg = builder.add("foo") - .add("-d").add("-") - .build(); + auto arg = builder.add("foo").add("-d").add("-").build(); std::vector<std::string_view> out; EXPECT_TRUE(args->run(arg.c(), arg.v(), &out)); EXPECT_TRUE(out.empty()); @@ -267,9 +255,7 @@ TEST(args, options_long_dash_dash_value) { auto args = Args::create(); SETUP_OPTIONS(args); Builder builder; - auto arg = builder.add("foo") - .add("--eat").add("--") - .build(); + auto arg = builder.add("foo").add("--eat").add("--").build(); std::vector<std::string_view> out; EXPECT_TRUE(args->run(arg.c(), arg.v(), &out)); EXPECT_TRUE(out.empty()); @@ -283,11 +269,13 @@ TEST(args, options_dash_dash) { SETUP_OPTIONS(args); Builder builder; auto arg = builder.add("foo") - .add("--") - .add("-d").add("10") - .add("--eat=hamburger") - .add("--form").add("circle") - .build(); + .add("--") + .add("-d") + .add("10") + .add("--eat=hamburger") + .add("--form") + .add("circle") + .build(); std::vector<std::string_view> out; EXPECT_TRUE(args->run(arg.c(), arg.v(), &out)); ASSERT_EQ(5, out.size()); @@ -305,9 +293,7 @@ TEST(args, options_dash_dash_end) { auto args = Args::create(); SETUP_OPTIONS(args); Builder builder; - auto arg = builder.add("foo") - .add("--") - .build(); + auto arg = builder.add("foo").add("--").build(); std::vector<std::string_view> out; EXPECT_TRUE(args->run(arg.c(), arg.v(), &out)); EXPECT_TRUE(out.empty()); @@ -330,7 +316,8 @@ TEST(args, help) { -g -h, --hold=[ARG] --invert=[ARG] -)", ss.str()); +)", + ss.str()); } TEST(args, help_wide) { @@ -338,7 +325,8 @@ TEST(args, help_wide) { SETUP_OPTIONS(args); std::stringstream ss; args->print_help(ss, 100); - EXPECT_EQ(R"(Mandatory arguments to long options are mandatory for short options too. + EXPECT_EQ( + R"(Mandatory arguments to long options are mandatory for short options too. -a an option -b, --bold set font style to bold --cold use if it is cold outside @@ -348,7 +336,8 @@ TEST(args, help_wide) { -g -h, --hold=[ARG] --invert=[ARG] -)", ss.str()); +)", + ss.str()); } TEST(args, help_narrow) { @@ -372,7 +361,8 @@ circle, shape or something else? -g -h, --hold=[ARG] --invert=[ARG] -)", ss.str()); +)", + ss.str()); } TEST(args, help_very_narrow) { @@ -401,7 +391,8 @@ circle, shape or -g -h, --hold=[ARG] --invert=[ARG] -)", ss.str()); +)", + ss.str()); } TEST(args, help_long_word) { @@ -416,5 +407,6 @@ TEST(args, help_long_word) { -a, --arg aaaaaaaaaaaaaaaaaaa- aaaaaaaaaaaaaaaaaaa -)", ss.str()); +)", + ss.str()); } diff --git a/test/buffer.cc b/test/buffer.cc index e747fb7..897bd88 100644 --- a/test/buffer.cc +++ b/test/buffer.cc @@ -1,9 +1,8 @@ -#include <gtest/gtest.h> - #include "buffer.hh" #include <cstdint> #include <cstring> +#include <gtest/gtest.h> #include <memory> #include <utility> @@ -240,10 +239,9 @@ TEST_P(BufferTest, full) { buffer->consume(0); } -INSTANTIATE_TEST_SUITE_P( - AllTypes, - BufferTest, - testing::Values(BufferType::Fixed, BufferType::Dynamic)); +INSTANTIATE_TEST_SUITE_P(AllTypes, BufferTest, + testing::Values(BufferType::Fixed, + BufferType::Dynamic)); TEST(buffer, dynamic_increase) { auto buffer = Buffer::dynamic(10, 20); diff --git a/test/csv.cc b/test/csv.cc index 49fe540..b915006 100644 --- a/test/csv.cc +++ b/test/csv.cc @@ -1,7 +1,7 @@ -#include <gtest/gtest.h> - #include "csv.hh" +#include <gtest/gtest.h> + TEST(csv, empty) { auto csv = csv::open(io::memory("")); auto line = csv->read(); diff --git a/test/decompress.cc b/test/decompress.cc index 35c4477..df1d08c 100644 --- a/test/decompress.cc +++ b/test/decompress.cc @@ -1,16 +1,14 @@ -#include <gtest/gtest.h> - #include "decompress.hh" +#include <gtest/gtest.h> + TEST(z_decompress, empty) { - static const unsigned char data[] = { - 0x1f, 0x8b, 0x08, 0x08, 0x33, 0xd4, 0xbd, 0x68, - 0x02, 0x03, 0x65, 0x6d, 0x70, 0x74, 0x79, 0x00, - 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00 - }; - auto reader = decompress::gzip(io::memory(std::string( - reinterpret_cast<const char*>(data), sizeof(data)))); + static const unsigned char data[] = {0x1f, 0x8b, 0x08, 0x08, 0x33, 0xd4, 0xbd, + 0x68, 0x02, 0x03, 0x65, 0x6d, 0x70, 0x74, + 0x79, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00}; + auto reader = decompress::gzip(io::memory( + std::string(reinterpret_cast<const char*>(data), sizeof(data)))); char buf[10]; auto got = reader->read(buf, sizeof(buf)); ASSERT_TRUE(got.has_value()); @@ -19,13 +17,12 @@ TEST(z_decompress, empty) { TEST(z_decompress, hello) { static const unsigned char data[] = { - 0x1f, 0x8b, 0x08, 0x08, 0xf7, 0xd5, 0xbd, 0x68, - 0x02, 0x03, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00, - 0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0x07, 0x00, 0x82, - 0x89, 0xd1, 0xf7, 0x05, 0x00, 0x00, 0x00, + 0x1f, 0x8b, 0x08, 0x08, 0xf7, 0xd5, 0xbd, 0x68, 0x02, 0x03, 0x68, + 0x65, 0x6c, 0x6c, 0x6f, 0x00, 0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0x07, + 0x00, 0x82, 0x89, 0xd1, 0xf7, 0x05, 0x00, 0x00, 0x00, }; - auto reader = decompress::gzip(io::memory(std::string( - reinterpret_cast<const char*>(data), sizeof(data)))); + auto reader = decompress::gzip(io::memory( + std::string(reinterpret_cast<const char*>(data), sizeof(data)))); char buf[10]; auto got = reader->read(buf, sizeof(buf)); ASSERT_TRUE(got.has_value()); @@ -36,13 +33,11 @@ TEST(z_decompress, hello) { TEST(xz_decompress, empty) { static const unsigned char data[] = { - 0xfd, 0x37, 0x7a, 0x58, 0x5a, 0x00, 0x00, 0x04, - 0xe6, 0xd6, 0xb4, 0x46, 0x00, 0x00, 0x00, 0x00, - 0x1c, 0xdf, 0x44, 0x21, 0x1f, 0xb6, 0xf3, 0x7d, - 0x01, 0x00, 0x00, 0x00, 0x00, 0x04, 0x59, 0x5a - }; - auto reader = decompress::xz(io::memory(std::string( - reinterpret_cast<const char*>(data), sizeof(data)))); + 0xfd, 0x37, 0x7a, 0x58, 0x5a, 0x00, 0x00, 0x04, 0xe6, 0xd6, 0xb4, + 0x46, 0x00, 0x00, 0x00, 0x00, 0x1c, 0xdf, 0x44, 0x21, 0x1f, 0xb6, + 0xf3, 0x7d, 0x01, 0x00, 0x00, 0x00, 0x00, 0x04, 0x59, 0x5a}; + auto reader = decompress::xz(io::memory( + std::string(reinterpret_cast<const char*>(data), sizeof(data)))); char buf[10]; auto got = reader->read(buf, sizeof(buf)); ASSERT_TRUE(got.has_value()); @@ -51,18 +46,15 @@ TEST(xz_decompress, empty) { TEST(xz_decompress, hello) { static const unsigned char data[] = { - 0xfd, 0x37, 0x7a, 0x58, 0x5a, 0x00, 0x00, 0x04, - 0xe6, 0xd6, 0xb4, 0x46, 0x04, 0xc0, 0x09, 0x05, - 0x21, 0x01, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x76, 0xe9, 0x07, 0x70, - 0x01, 0x00, 0x04, 0x48, 0x65, 0x6c, 0x6c, 0x6f, - 0x00, 0x00, 0x00, 0x00, 0xc8, 0xac, 0x7b, 0xc8, - 0x3b, 0x5c, 0xcf, 0x51, 0x00, 0x01, 0x25, 0x05, - 0x43, 0x91, 0x1f, 0xb8, 0x1f, 0xb6, 0xf3, 0x7d, - 0x01, 0x00, 0x00, 0x00, 0x00, 0x04, 0x59, 0x5a, + 0xfd, 0x37, 0x7a, 0x58, 0x5a, 0x00, 0x00, 0x04, 0xe6, 0xd6, 0xb4, 0x46, + 0x04, 0xc0, 0x09, 0x05, 0x21, 0x01, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x76, 0xe9, 0x07, 0x70, 0x01, 0x00, 0x04, 0x48, + 0x65, 0x6c, 0x6c, 0x6f, 0x00, 0x00, 0x00, 0x00, 0xc8, 0xac, 0x7b, 0xc8, + 0x3b, 0x5c, 0xcf, 0x51, 0x00, 0x01, 0x25, 0x05, 0x43, 0x91, 0x1f, 0xb8, + 0x1f, 0xb6, 0xf3, 0x7d, 0x01, 0x00, 0x00, 0x00, 0x00, 0x04, 0x59, 0x5a, }; - auto reader = decompress::xz(io::memory(std::string( - reinterpret_cast<const char*>(data), sizeof(data)))); + auto reader = decompress::xz(io::memory( + std::string(reinterpret_cast<const char*>(data), sizeof(data)))); char buf[10]; auto got = reader->read(buf, sizeof(buf)); ASSERT_TRUE(got.has_value()); @@ -1,12 +1,12 @@ -#include <gtest/gtest.h> - #include "io.hh" + #include "io_test_helper.hh" #include <cerrno> #include <cstdlib> #include <dirent.h> #include <fcntl.h> +#include <gtest/gtest.h> #include <sys/stat.h> #include <unistd.h> #include <utility> @@ -15,11 +15,14 @@ namespace { bool remove_recursive(int fd) { auto* dir = fdopendir(fd); - if (!dir) return false; + if (!dir) + return false; while (auto* ent = readdir(dir)) { if (ent->d_name[0] == '.') { - if (ent->d_name[1] == '\0') continue; - if (ent->d_name[1] == '.' && ent->d_name[2] == '\0') continue; + if (ent->d_name[1] == '\0') + continue; + if (ent->d_name[1] == '.' && ent->d_name[2] == '\0') + continue; } bool is_dir; if (ent->d_type == DT_DIR) { @@ -86,14 +89,13 @@ class IoTest : public testing::Test { rmdir(tmpdir_.c_str()); } - [[nodiscard]] int dirfd() const { - return dirfd_; - } + [[nodiscard]] int dirfd() const { return dirfd_; } void touch(const std::string& name, const std::string& value = "") { auto fd = openat(dirfd(), name.c_str(), O_CREAT | O_WRONLY | O_TRUNC, 0700); EXPECT_NE(-1, fd); - if (fd == -1) return; + if (fd == -1) + return; size_t offset = 0; while (offset < value.size()) { auto ret = write(fd, value.data() + offset, value.size() - offset); diff --git a/test/io_test_helper.cc b/test/io_test_helper.cc index 514e888..9ac663a 100644 --- a/test/io_test_helper.cc +++ b/test/io_test_helper.cc @@ -70,9 +70,9 @@ class MaxBlockReader : public io::Reader { } // namespace -std::unique_ptr<io::Reader> io_make_breaking( - std::unique_ptr<io::Reader> reader, size_t offset, - io::ReadError error) { +std::unique_ptr<io::Reader> io_make_breaking(std::unique_ptr<io::Reader> reader, + size_t offset, + io::ReadError error) { return std::make_unique<BreakingReader>(std::move(reader), offset, error); } diff --git a/test/io_test_helper.hh b/test/io_test_helper.hh index ce191cf..b99b8fa 100644 --- a/test/io_test_helper.hh +++ b/test/io_test_helper.hh @@ -1,7 +1,7 @@ #ifndef IO_TEST_HELPER_HH #define IO_TEST_HELPER_HH -#include "io.hh" // IWYU pragma: export +#include "io.hh" // IWYU pragma: export #include <cstddef> #include <memory> diff --git a/test/line.cc b/test/line.cc index 0f90723..71d75c5 100644 --- a/test/line.cc +++ b/test/line.cc @@ -1,9 +1,9 @@ -#include <gtest/gtest.h> +#include "line.hh" #include "io_test_helper.hh" -#include "line.hh" #include <cstddef> +#include <gtest/gtest.h> #include <limits> #include <utility> @@ -124,9 +124,8 @@ TEST(line, read_error_newline) { } TEST(line, blocky) { - auto reader = line::open( - io_make_max_block(io::memory("foo bar\r\nfim zam"), - /* max_block_size */ 1)); + auto reader = line::open(io_make_max_block(io::memory("foo bar\r\nfim zam"), + /* max_block_size */ 1)); auto line = reader->read(); ASSERT_TRUE(line.has_value()); EXPECT_EQ("foo bar", line.value()); @@ -139,9 +138,8 @@ TEST(line, blocky) { } TEST(line, blocky_newline) { - auto reader = line::open( - io_make_max_block(io::memory("foo bar\r\nfim zam"), - /* max_block_size */ 8)); + auto reader = line::open(io_make_max_block(io::memory("foo bar\r\nfim zam"), + /* max_block_size */ 8)); auto line = reader->read(); ASSERT_TRUE(line.has_value()); EXPECT_EQ("foo bar", line.value()); @@ -177,8 +175,10 @@ TEST(line, max_newline) { } TEST(line, max_line_overflow) { - EXPECT_DEATH_IF_SUPPORTED({ - std::ignore = line::open(io::memory(""), - std::numeric_limits<size_t>::max()); - }, ""); + EXPECT_DEATH_IF_SUPPORTED( + { + std::ignore = + line::open(io::memory(""), std::numeric_limits<size_t>::max()); + }, + ""); } diff --git a/test/str.cc b/test/str.cc index 35d70d7..1339bb0 100644 --- a/test/str.cc +++ b/test/str.cc @@ -1,7 +1,7 @@ -#include <gtest/gtest.h> - #include "str.hh" +#include <gtest/gtest.h> + TEST(str, split) { auto ret = str::split(""); EXPECT_EQ(0, ret.size()); @@ -1,16 +1,14 @@ -#include <gtest/gtest.h> - +#include "u16.hh" #include "u8.hh" #include "umod8.hh" -#include "u16.hh" +#include <gtest/gtest.h> #include <iterator> #include <vector> namespace { -class UnicodeVersionTest : public testing::TestWithParam<u::Version> { -}; +class UnicodeVersionTest : public testing::TestWithParam<u::Version> {}; } // namespace @@ -389,8 +387,7 @@ TEST(umod8, examples) { } } -TEST(umod8, overlong) { -} +TEST(umod8, overlong) {} TEST(umod8, incomplete) { { @@ -709,8 +706,7 @@ TEST_P(UnicodeVersionTest, lookup_gc) { u::GeneralCategory::LETTER_TITLECASE); EXPECT_EQ(u::lookup_gc(0x374, GetParam()), u::GeneralCategory::LETTER_MODIFIER); - EXPECT_EQ(u::lookup_gc(0x34ff, GetParam()), - u::GeneralCategory::LETTER_OTHER); + EXPECT_EQ(u::lookup_gc(0x34ff, GetParam()), u::GeneralCategory::LETTER_OTHER); EXPECT_EQ(u::lookup_gc(0x483, GetParam()), u::GeneralCategory::MARK_NONSPACING); @@ -719,12 +715,10 @@ TEST_P(UnicodeVersionTest, lookup_gc) { EXPECT_EQ(u::lookup_gc(0x20de, GetParam()), u::GeneralCategory::MARK_SPACING_ENCLOSING); - EXPECT_EQ(u::lookup_gc(0xa620, GetParam()), - u::GeneralCategory::NUMBER_DIGIT); + EXPECT_EQ(u::lookup_gc(0xa620, GetParam()), u::GeneralCategory::NUMBER_DIGIT); EXPECT_EQ(u::lookup_gc(0xa6e6, GetParam()), u::GeneralCategory::NUMBER_LETTER); - EXPECT_EQ(u::lookup_gc(0xa830, GetParam()), - u::GeneralCategory::NUMBER_OTHER); + EXPECT_EQ(u::lookup_gc(0xa830, GetParam()), u::GeneralCategory::NUMBER_OTHER); EXPECT_EQ(u::lookup_gc(0xfe33, GetParam()), u::GeneralCategory::PUNCTUATION_CONNECTOR); @@ -741,14 +735,12 @@ TEST_P(UnicodeVersionTest, lookup_gc) { EXPECT_EQ(u::lookup_gc(0xff1a, GetParam()), u::GeneralCategory::PUNCTUATION_OTHER); - EXPECT_EQ(u::lookup_gc(0xd7, GetParam()), - u::GeneralCategory::SYMBOL_MATH); + EXPECT_EQ(u::lookup_gc(0xd7, GetParam()), u::GeneralCategory::SYMBOL_MATH); EXPECT_EQ(u::lookup_gc(0x58f, GetParam()), u::GeneralCategory::SYMBOL_CURRENCY); EXPECT_EQ(u::lookup_gc(0x5e, GetParam()), u::GeneralCategory::SYMBOL_MODIFIER); - EXPECT_EQ(u::lookup_gc(0xf03, GetParam()), - u::GeneralCategory::SYMBOL_OTHER); + EXPECT_EQ(u::lookup_gc(0xf03, GetParam()), u::GeneralCategory::SYMBOL_OTHER); EXPECT_EQ(u::lookup_gc(0x20, GetParam()), u::GeneralCategory::SEPARATOR_SPACE); @@ -757,10 +749,8 @@ TEST_P(UnicodeVersionTest, lookup_gc) { EXPECT_EQ(u::lookup_gc(0x2029, GetParam()), u::GeneralCategory::SEPARATOR_PARAGRAPH); - EXPECT_EQ(u::lookup_gc(0xa, GetParam()), - u::GeneralCategory::OTHER_CONTROL); - EXPECT_EQ(u::lookup_gc(0x202d, GetParam()), - u::GeneralCategory::OTHER_FORMAT); + EXPECT_EQ(u::lookup_gc(0xa, GetParam()), u::GeneralCategory::OTHER_CONTROL); + EXPECT_EQ(u::lookup_gc(0x202d, GetParam()), u::GeneralCategory::OTHER_FORMAT); EXPECT_EQ(u::lookup_gc(0xd800, GetParam()), u::GeneralCategory::OTHER_SURROGATE); EXPECT_EQ(u::lookup_gc(0xdbff, GetParam()), @@ -775,18 +765,9 @@ TEST_P(UnicodeVersionTest, lookup_gc) { } INSTANTIATE_TEST_SUITE_P( - AllVersions, - UnicodeVersionTest, - testing::Values( - u::Version::u6_2_0, - u::Version::u8_0_0, - u::Version::u10_0_0, - u::Version::u11_0_0, - u::Version::u12_1_0, - u::Version::u13_0_0, - u::Version::u14_0_0, - u::Version::u15_0_0, - u::Version::u15_1_0, - u::Version::u16_0_0 - ) -); + AllVersions, UnicodeVersionTest, + testing::Values(u::Version::u6_2_0, u::Version::u8_0_0, u::Version::u10_0_0, + u::Version::u11_0_0, u::Version::u12_1_0, + u::Version::u13_0_0, u::Version::u14_0_0, + u::Version::u15_0_0, u::Version::u15_1_0, + u::Version::u16_0_0)); diff --git a/test/uio.cc b/test/uio.cc index ce666c1..7c8d175 100644 --- a/test/uio.cc +++ b/test/uio.cc @@ -1,9 +1,9 @@ -#include <gtest/gtest.h> +#include "uio.hh" #include "io.hh" #include "io_test_helper.hh" -#include "uio.hh" +#include <gtest/gtest.h> #include <string> using namespace std::literals::string_literals; @@ -25,18 +25,23 @@ TEST(uio_u16, empty) { } TEST(uio_u8, sample) { - auto uio = u8::open(io::memory("\xf0\x90\x8D\x85" "es"), - u::ReaderConfig{.input=u::ReaderInputFormat::UTF8}); + auto uio = u8::open(io::memory("\xf0\x90\x8D\x85" + "es"), + u::ReaderConfig{.input = u::ReaderInputFormat::UTF8}); std::string tmp; auto ret = uio->repeat_read(tmp, 10); ASSERT_TRUE(ret.has_value()); EXPECT_EQ(6, ret.value()); - EXPECT_EQ("\xf0\x90\x8D\x85" "es", tmp); + EXPECT_EQ( + "\xf0\x90\x8D\x85" + "es", + tmp); } TEST(uio_u16, sample_be) { - auto uio = u16::open(io::memory("\x00\x24\xD8\x01\xDC\x37"s), - u::ReaderConfig{.input=u::ReaderInputFormat::UTF16_BE}); + auto uio = + u16::open(io::memory("\x00\x24\xD8\x01\xDC\x37"s), + u::ReaderConfig{.input = u::ReaderInputFormat::UTF16_BE}); std::u16string tmp; auto ret = uio->repeat_read(tmp, 5); ASSERT_TRUE(ret.has_value()); @@ -47,8 +52,9 @@ TEST(uio_u16, sample_be) { } TEST(uio_u16, sample_le) { - auto uio = u16::open(io::memory("\x24\x00\x01\xD8\x37\xDC"s), - u::ReaderConfig{.input=u::ReaderInputFormat::UTF16_LE}); + auto uio = + u16::open(io::memory("\x24\x00\x01\xD8\x37\xDC"s), + u::ReaderConfig{.input = u::ReaderInputFormat::UTF16_LE}); std::u16string tmp; auto ret = uio->repeat_read(tmp, 5); ASSERT_TRUE(ret.has_value()); @@ -59,12 +65,17 @@ TEST(uio_u16, sample_le) { } TEST(uio_u8, sample_detect) { - auto uio = u8::open(io::memory("\xf0\x90\x8D\x85" "es")); + auto uio = + u8::open(io::memory("\xf0\x90\x8D\x85" + "es")); std::string tmp; auto ret = uio->repeat_read(tmp, 10); ASSERT_TRUE(ret.has_value()); EXPECT_EQ(6, ret.value()); - EXPECT_EQ("\xf0\x90\x8D\x85" "es", tmp); + EXPECT_EQ( + "\xf0\x90\x8D\x85" + "es", + tmp); } TEST(uio_u16, sample_detect_be) { @@ -92,8 +103,8 @@ TEST(uio_u16, sample_detect_le) { TEST(uio_u8, invalid) { auto uio = u8::open(io::memory("r\xe4ksm\xf6rg\xe5s"), u::ReaderConfig{ - .strict=true, - .input=u::ReaderInputFormat::UTF8, + .strict = true, + .input = u::ReaderInputFormat::UTF8, }); std::string tmp; auto ret = uio->repeat_read(tmp, 20); @@ -107,7 +118,7 @@ TEST(uio_u8, invalid) { TEST(uio_u8, invalid_detect) { auto uio = u8::open(io::memory("r\xe4ksm\xf6rg\xe5s"), - u::ReaderConfig{.strict=true}); + u::ReaderConfig{.strict = true}); std::string tmp; auto ret = uio->repeat_read(tmp, 20); ASSERT_TRUE(ret.has_value()); @@ -121,8 +132,8 @@ TEST(uio_u8, invalid_detect) { TEST(uio_u8, invalid_replace) { auto uio = u8::open(io::memory("r\xe4ksm\xf6rg\xe5s"), u::ReaderConfig{ - .strict=false, - .input=u::ReaderInputFormat::UTF8, + .strict = false, + .input = u::ReaderInputFormat::UTF8, }); std::string tmp; auto ret = uio->repeat_read(tmp, 20); @@ -133,7 +144,7 @@ TEST(uio_u8, invalid_replace) { TEST(uio_u8, read_error) { auto uio = u8::open(io_make_breaking(io::memory("\xef\xbf\xbd"), 1), - u::ReaderConfig{.input=u::ReaderInputFormat::UTF8}); + u::ReaderConfig{.input = u::ReaderInputFormat::UTF8}); std::string tmp; auto ret = uio->repeat_read(tmp, 10); ASSERT_FALSE(ret.has_value()); @@ -141,8 +152,9 @@ TEST(uio_u8, read_error) { } TEST(uio_u16, read_error) { - auto uio = u16::open(io_make_breaking(io::memory("\x00\x24"s), 1), - u::ReaderConfig{.input=u::ReaderInputFormat::UTF16_BE}); + auto uio = + u16::open(io_make_breaking(io::memory("\x00\x24"s), 1), + u::ReaderConfig{.input = u::ReaderInputFormat::UTF16_BE}); std::u16string tmp; auto ret = uio->repeat_read(tmp, 10); ASSERT_FALSE(ret.has_value()); @@ -150,11 +162,10 @@ TEST(uio_u16, read_error) { } TEST(uio_u8, read_incomplete_strict) { - auto uio = u8::open(io::memory("\xef"), - u::ReaderConfig{ - .strict=true, - .input=u::ReaderInputFormat::UTF8, - }); + auto uio = u8::open(io::memory("\xef"), u::ReaderConfig{ + .strict = true, + .input = u::ReaderInputFormat::UTF8, + }); std::string tmp; auto ret = uio->repeat_read(tmp, 10); ASSERT_FALSE(ret.has_value()); @@ -162,11 +173,10 @@ TEST(uio_u8, read_incomplete_strict) { } TEST(uio_u8, read_incomplete) { - auto uio = u8::open(io::memory("\xef"), - u::ReaderConfig{ - .strict=false, - .input=u::ReaderInputFormat::UTF8, - }); + auto uio = u8::open(io::memory("\xef"), u::ReaderConfig{ + .strict = false, + .input = u::ReaderInputFormat::UTF8, + }); std::string tmp; auto ret = uio->repeat_read(tmp, 10); ASSERT_TRUE(ret.has_value()); @@ -175,11 +185,11 @@ TEST(uio_u8, read_incomplete) { } TEST(uio_u16, read_incomplete_strict_be) { - auto uio = u16::open(io::memory("\x00"s), - u::ReaderConfig{ - .strict=true, - .input=u::ReaderInputFormat::UTF16_BE, - }); + auto uio = + u16::open(io::memory("\x00"s), u::ReaderConfig{ + .strict = true, + .input = u::ReaderInputFormat::UTF16_BE, + }); std::u16string tmp; auto ret = uio->repeat_read(tmp, 10); ASSERT_FALSE(ret.has_value()); @@ -187,11 +197,11 @@ TEST(uio_u16, read_incomplete_strict_be) { } TEST(uio_u16, read_incomplete_be) { - auto uio = u16::open(io::memory("\x00"s), - u::ReaderConfig{ - .strict=false, - .input=u::ReaderInputFormat::UTF16_BE, - }); + auto uio = + u16::open(io::memory("\x00"s), u::ReaderConfig{ + .strict = false, + .input = u::ReaderInputFormat::UTF16_BE, + }); std::u16string tmp; auto ret = uio->repeat_read(tmp, 10); ASSERT_TRUE(ret.has_value()); @@ -200,11 +210,11 @@ TEST(uio_u16, read_incomplete_be) { } TEST(uio_u16, read_incomplete_strict_le) { - auto uio = u16::open(io::memory("$"), - u::ReaderConfig{ - .strict=true, - .input=u::ReaderInputFormat::UTF16_LE, - }); + auto uio = + u16::open(io::memory("$"), u::ReaderConfig{ + .strict = true, + .input = u::ReaderInputFormat::UTF16_LE, + }); std::u16string tmp; auto ret = uio->repeat_read(tmp, 10); ASSERT_FALSE(ret.has_value()); @@ -212,11 +222,11 @@ TEST(uio_u16, read_incomplete_strict_le) { } TEST(uio_u16, read_incomplete_le) { - auto uio = u16::open(io::memory("$"), - u::ReaderConfig{ - .strict=false, - .input=u::ReaderInputFormat::UTF16_LE, - }); + auto uio = + u16::open(io::memory("$"), u::ReaderConfig{ + .strict = false, + .input = u::ReaderInputFormat::UTF16_LE, + }); std::u16string tmp; auto ret = uio->repeat_read(tmp, 10); ASSERT_TRUE(ret.has_value()); @@ -225,8 +235,9 @@ TEST(uio_u16, read_incomplete_le) { } TEST(uio_u8, max_too_small) { - auto uio = u8::open(io::memory("\xf0\x90\x8D\x85" "es"), - u::ReaderConfig{.input=u::ReaderInputFormat::UTF8}); + auto uio = u8::open(io::memory("\xf0\x90\x8D\x85" + "es"), + u::ReaderConfig{.input = u::ReaderInputFormat::UTF8}); std::string tmp; auto ret = uio->read(tmp.data(), 0); ASSERT_TRUE(ret.has_value()); @@ -238,8 +249,9 @@ TEST(uio_u8, max_too_small) { } TEST(uio_u16, max_too_small_be) { - auto uio = u16::open(io::memory("\xD8\x01\xDC\x37"), - u::ReaderConfig{.input=u::ReaderInputFormat::UTF16_BE}); + auto uio = + u16::open(io::memory("\xD8\x01\xDC\x37"), + u::ReaderConfig{.input = u::ReaderInputFormat::UTF16_BE}); std::u16string tmp; auto ret = uio->read(tmp.data(), 0); ASSERT_TRUE(ret.has_value()); @@ -251,8 +263,9 @@ TEST(uio_u16, max_too_small_be) { } TEST(uio_u16, max_too_small_le) { - auto uio = u16::open(io::memory("\x01\xD8\x37\xDC"), - u::ReaderConfig{.input=u::ReaderInputFormat::UTF16_LE}); + auto uio = + u16::open(io::memory("\x01\xD8\x37\xDC"), + u::ReaderConfig{.input = u::ReaderInputFormat::UTF16_LE}); std::u16string tmp; auto ret = uio->read(tmp.data(), 0); ASSERT_TRUE(ret.has_value()); @@ -264,8 +277,9 @@ TEST(uio_u16, max_too_small_le) { } TEST(uio_u8, partial) { - auto uio = u8::open(io::memory("\xf0\x90\x8D\x85" "es"), - u::ReaderConfig{.input=u::ReaderInputFormat::UTF8}); + auto uio = u8::open(io::memory("\xf0\x90\x8D\x85" + "es"), + u::ReaderConfig{.input = u::ReaderInputFormat::UTF8}); std::string tmp; auto ret = uio->repeat_read(tmp, 4); ASSERT_TRUE(ret.has_value()); @@ -279,8 +293,9 @@ TEST(uio_u8, partial) { } TEST(uio_u16, partial_be) { - auto uio = u16::open(io::memory("\x00\x24\xD8\x01\xDC\x37"s), - u::ReaderConfig{.input=u::ReaderInputFormat::UTF16_BE}); + auto uio = + u16::open(io::memory("\x00\x24\xD8\x01\xDC\x37"s), + u::ReaderConfig{.input = u::ReaderInputFormat::UTF16_BE}); std::u16string tmp; auto ret = uio->repeat_read(tmp, 1); ASSERT_TRUE(ret.has_value()); @@ -295,8 +310,9 @@ TEST(uio_u16, partial_be) { } TEST(uio_u16, partial_le) { - auto uio = u16::open(io::memory("\x24\x00\x01\xD8\x37\xDC"s), - u::ReaderConfig{.input=u::ReaderInputFormat::UTF16_LE}); + auto uio = + u16::open(io::memory("\x24\x00\x01\xD8\x37\xDC"s), + u::ReaderConfig{.input = u::ReaderInputFormat::UTF16_LE}); std::u16string tmp; auto ret = uio->repeat_read(tmp, 1); ASSERT_TRUE(ret.has_value()); @@ -312,10 +328,10 @@ TEST(uio_u16, partial_le) { TEST(uio_u16, invalid_be) { auto uio = u16::open(io::memory("\x00\x24\xd8\x01"s), - u::ReaderConfig{ - .strict=true, - .input=u::ReaderInputFormat::UTF16_BE, - }); + u::ReaderConfig{ + .strict = true, + .input = u::ReaderInputFormat::UTF16_BE, + }); std::u16string tmp; auto ret = uio->repeat_read(tmp, 10); ASSERT_TRUE(ret.has_value()); @@ -328,7 +344,7 @@ TEST(uio_u16, invalid_be) { TEST(uio_u16, invalid_detect_be) { auto uio = u16::open(io::memory("\x00\x24\xd8\x01"s), - u::ReaderConfig{.strict=true}); + u::ReaderConfig{.strict = true}); std::u16string tmp; auto ret = uio->repeat_read(tmp, 10); ASSERT_TRUE(ret.has_value()); @@ -341,10 +357,10 @@ TEST(uio_u16, invalid_detect_be) { TEST(uio_u16, invalid_replace_be) { auto uio = u16::open(io::memory("\x00\x24\xd8\x01"s), - u::ReaderConfig{ - .strict=false, - .input=u::ReaderInputFormat::UTF16_BE, - }); + u::ReaderConfig{ + .strict = false, + .input = u::ReaderInputFormat::UTF16_BE, + }); std::u16string tmp; auto ret = uio->repeat_read(tmp, 10); ASSERT_TRUE(ret.has_value()); @@ -355,10 +371,10 @@ TEST(uio_u16, invalid_replace_be) { TEST(uio_u16, invalid_le) { auto uio = u16::open(io::memory("\x24\x00\x01\xd8"s), - u::ReaderConfig{ - .strict=true, - .input=u::ReaderInputFormat::UTF16_LE, - }); + u::ReaderConfig{ + .strict = true, + .input = u::ReaderInputFormat::UTF16_LE, + }); std::u16string tmp; auto ret = uio->repeat_read(tmp, 10); ASSERT_TRUE(ret.has_value()); @@ -371,7 +387,7 @@ TEST(uio_u16, invalid_le) { TEST(uio_u16, invalid_detect_le) { auto uio = u16::open(io::memory("\x24\x00\x01\xd8"s), - u::ReaderConfig{.strict=true}); + u::ReaderConfig{.strict = true}); std::u16string tmp; auto ret = uio->repeat_read(tmp, 10); ASSERT_TRUE(ret.has_value()); @@ -384,10 +400,10 @@ TEST(uio_u16, invalid_detect_le) { TEST(uio_u16, invalid_replace_le) { auto uio = u16::open(io::memory("\x24\x00\x01\xd8"s), - u::ReaderConfig{ - .strict=false, - .input=u::ReaderInputFormat::UTF16_LE, - }); + u::ReaderConfig{ + .strict = false, + .input = u::ReaderInputFormat::UTF16_LE, + }); std::u16string tmp; auto ret = uio->repeat_read(tmp, 10); ASSERT_TRUE(ret.has_value()); @@ -397,31 +413,40 @@ TEST(uio_u16, invalid_replace_le) { } TEST(uio_u8, bom) { - auto uio = u8::open(io::memory("\xef\xbb\xbf\xf0\x90\x8D\x85" "es"), - u::ReaderConfig{.input=u::ReaderInputFormat::UTF8}); + auto uio = u8::open(io::memory("\xef\xbb\xbf\xf0\x90\x8D\x85" + "es"), + u::ReaderConfig{.input = u::ReaderInputFormat::UTF8}); std::string tmp; auto ret = uio->repeat_read(tmp, 10); ASSERT_TRUE(ret.has_value()); EXPECT_EQ(6, ret.value()); - EXPECT_EQ("\xf0\x90\x8D\x85" "es", tmp); + EXPECT_EQ( + "\xf0\x90\x8D\x85" + "es", + tmp); } TEST(uio_u8, bom_keep) { - auto uio = u8::open(io::memory("\xef\xbb\xbf\xf0\x90\x8D\x85" "es"), + auto uio = u8::open(io::memory("\xef\xbb\xbf\xf0\x90\x8D\x85" + "es"), u::ReaderConfig{ - .input=u::ReaderInputFormat::UTF8, - .skip_bom=false, + .input = u::ReaderInputFormat::UTF8, + .skip_bom = false, }); std::string tmp; auto ret = uio->repeat_read(tmp, 10); ASSERT_TRUE(ret.has_value()); EXPECT_EQ(9, ret.value()); - EXPECT_EQ("\xef\xbb\xbf\xf0\x90\x8D\x85" "es", tmp); + EXPECT_EQ( + "\xef\xbb\xbf\xf0\x90\x8D\x85" + "es", + tmp); } TEST(uio_u16, bom_be) { - auto uio = u16::open(io::memory("\xfe\xff\x00\x24\xD8\x01\xDC\x37"s), - u::ReaderConfig{.input=u::ReaderInputFormat::UTF16_BE}); + auto uio = + u16::open(io::memory("\xfe\xff\x00\x24\xD8\x01\xDC\x37"s), + u::ReaderConfig{.input = u::ReaderInputFormat::UTF16_BE}); std::u16string tmp; auto ret = uio->repeat_read(tmp, 5); ASSERT_TRUE(ret.has_value()); @@ -432,8 +457,9 @@ TEST(uio_u16, bom_be) { } TEST(uio_u16, bom_le) { - auto uio = u16::open(io::memory("\xff\xfe\x24\x00\x01\xD8\x37\xDC"s), - u::ReaderConfig{.input=u::ReaderInputFormat::UTF16_LE}); + auto uio = + u16::open(io::memory("\xff\xfe\x24\x00\x01\xD8\x37\xDC"s), + u::ReaderConfig{.input = u::ReaderInputFormat::UTF16_LE}); std::u16string tmp; auto ret = uio->repeat_read(tmp, 5); ASSERT_TRUE(ret.has_value()); @@ -445,10 +471,10 @@ TEST(uio_u16, bom_le) { TEST(uio_u16, bom_keep_be) { auto uio = u16::open(io::memory("\xfe\xff\x00\x24\xD8\x01\xDC\x37"s), - u::ReaderConfig{ - .input=u::ReaderInputFormat::UTF16_BE, - .skip_bom=false, - }); + u::ReaderConfig{ + .input = u::ReaderInputFormat::UTF16_BE, + .skip_bom = false, + }); std::u16string tmp; auto ret = uio->repeat_read(tmp, 5); ASSERT_TRUE(ret.has_value()); @@ -461,10 +487,10 @@ TEST(uio_u16, bom_keep_be) { TEST(uio_u16, bom_keep_le) { auto uio = u16::open(io::memory("\xff\xfe\x24\x00\x01\xD8\x37\xDC"s), - u::ReaderConfig{ - .input=u::ReaderInputFormat::UTF16_LE, - .skip_bom=false, - }); + u::ReaderConfig{ + .input = u::ReaderInputFormat::UTF16_LE, + .skip_bom = false, + }); std::u16string tmp; auto ret = uio->repeat_read(tmp, 5); ASSERT_TRUE(ret.has_value()); @@ -476,22 +502,31 @@ TEST(uio_u16, bom_keep_le) { } TEST(uio_u8, bom_detect) { - auto uio = u8::open(io::memory("\xef\xbb\xbf\xf0\x90\x8D\x85" "es")); + auto uio = + u8::open(io::memory("\xef\xbb\xbf\xf0\x90\x8D\x85" + "es")); std::string tmp; auto ret = uio->repeat_read(tmp, 10); ASSERT_TRUE(ret.has_value()); EXPECT_EQ(6, ret.value()); - EXPECT_EQ("\xf0\x90\x8D\x85" "es", tmp); + EXPECT_EQ( + "\xf0\x90\x8D\x85" + "es", + tmp); } TEST(uio_u8, bom_keep_detect) { - auto uio = u8::open(io::memory("\xef\xbb\xbf\xf0\x90\x8D\x85" "es"), - u::ReaderConfig{.skip_bom=false}); + auto uio = u8::open(io::memory("\xef\xbb\xbf\xf0\x90\x8D\x85" + "es"), + u::ReaderConfig{.skip_bom = false}); std::string tmp; auto ret = uio->repeat_read(tmp, 10); ASSERT_TRUE(ret.has_value()); EXPECT_EQ(9, ret.value()); - EXPECT_EQ("\xef\xbb\xbf\xf0\x90\x8D\x85" "es", tmp); + EXPECT_EQ( + "\xef\xbb\xbf\xf0\x90\x8D\x85" + "es", + tmp); } TEST(uio_u16, bom_detect_be) { @@ -518,7 +553,7 @@ TEST(uio_u16, bom_detect_le) { TEST(uio_u16, bom_keep_detect_be) { auto uio = u16::open(io::memory("\xfe\xff\x00\x24\xD8\x01\xDC\x37"s), - u::ReaderConfig{.skip_bom=false}); + u::ReaderConfig{.skip_bom = false}); std::u16string tmp; auto ret = uio->repeat_read(tmp, 5); ASSERT_TRUE(ret.has_value()); @@ -531,7 +566,7 @@ TEST(uio_u16, bom_keep_detect_be) { TEST(uio_u16, bom_keep_detect_le) { auto uio = u16::open(io::memory("\xff\xfe\x24\x00\x01\xD8\x37\xDC"s), - u::ReaderConfig{.skip_bom=false}); + u::ReaderConfig{.skip_bom = false}); std::u16string tmp; auto ret = uio->repeat_read(tmp, 5); ASSERT_TRUE(ret.has_value()); @@ -544,7 +579,7 @@ TEST(uio_u16, bom_keep_detect_le) { TEST(uio_u8, input_utf16_be) { auto uio = u8::open(io::memory("\x00\x24\xD8\x01\xDC\x37"s), - u::ReaderConfig{.input=u::ReaderInputFormat::UTF16_BE}); + u::ReaderConfig{.input = u::ReaderInputFormat::UTF16_BE}); std::string tmp; auto ret = uio->repeat_read(tmp, 10); ASSERT_TRUE(ret.has_value()); @@ -554,7 +589,7 @@ TEST(uio_u8, input_utf16_be) { TEST(uio_u8, input_utf16_le) { auto uio = u8::open(io::memory("\x24\x00\x01\xD8\x37\xDC"s), - u::ReaderConfig{.input=u::ReaderInputFormat::UTF16_LE}); + u::ReaderConfig{.input = u::ReaderInputFormat::UTF16_LE}); std::string tmp; auto ret = uio->repeat_read(tmp, 10); ASSERT_TRUE(ret.has_value()); @@ -563,8 +598,9 @@ TEST(uio_u8, input_utf16_le) { } TEST(uio_u16, input_utf8) { - auto uio = u16::open(io::memory("\xf0\x90\x8D\x85" "es"), - u::ReaderConfig{.input=u::ReaderInputFormat::UTF8}); + auto uio = u16::open(io::memory("\xf0\x90\x8D\x85" + "es"), + u::ReaderConfig{.input = u::ReaderInputFormat::UTF8}); std::u16string tmp; auto ret = uio->repeat_read(tmp, 5); ASSERT_TRUE(ret.has_value()); @@ -576,8 +612,9 @@ TEST(uio_u16, input_utf8) { } TEST(uio_u8, skip) { - auto uio = u8::open(io::memory("\xf0\x90\x8D\x85" "es"), - u::ReaderConfig{.input=u::ReaderInputFormat::UTF8}); + auto uio = u8::open(io::memory("\xf0\x90\x8D\x85" + "es"), + u::ReaderConfig{.input = u::ReaderInputFormat::UTF8}); std::string tmp; auto ret = uio->repeat_skip(3); ASSERT_FALSE(ret.has_value()); @@ -592,8 +629,9 @@ TEST(uio_u8, skip) { } TEST(uio_u16, skip_be) { - auto uio = u16::open(io::memory("\x00\x24\xD8\x01\xDC\x37"s), - u::ReaderConfig{.input=u::ReaderInputFormat::UTF16_BE}); + auto uio = + u16::open(io::memory("\x00\x24\xD8\x01\xDC\x37"s), + u::ReaderConfig{.input = u::ReaderInputFormat::UTF16_BE}); std::u16string tmp; auto ret = uio->repeat_skip(4); // Note that this is in bytes ASSERT_TRUE(ret.has_value()); @@ -606,8 +644,9 @@ TEST(uio_u16, skip_be) { } TEST(uio_u16, skip_le) { - auto uio = u16::open(io::memory("\x24\x00\x01\xD8\x37\xDC"s), - u::ReaderConfig{.input=u::ReaderInputFormat::UTF16_LE}); + auto uio = + u16::open(io::memory("\x24\x00\x01\xD8\x37\xDC"s), + u::ReaderConfig{.input = u::ReaderInputFormat::UTF16_LE}); std::u16string tmp; auto ret = uio->repeat_skip(4); // Note that this is in bytes ASSERT_TRUE(ret.has_value()); |
