From 93ebef9c6db2fb6bc21d4485c7889669eb03bf52 Mon Sep 17 00:00:00 2001 From: Marcus Holland-Moritz Date: Sat, 15 Mar 2025 11:40:16 +0100 Subject: [PATCH] refactor(fstypes): replace maps with sorted_array_map --- src/fstypes.cpp | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/src/fstypes.cpp b/src/fstypes.cpp index e9bcadb6..053f0069 100644 --- a/src/fstypes.cpp +++ b/src/fstypes.cpp @@ -19,25 +19,25 @@ * along with dwarfs. If not, see . */ -#include #include #include #include -#include - #include #include #include +#include namespace dwarfs { namespace { +using namespace std::string_view_literals; + // clang-format off -const std::map sections{ -#define SECTION_TYPE_(x) {section_type::x, #x} +constexpr sorted_array_map sections{ +#define SECTION_TYPE_(x) std::pair{section_type::x, #x ## sv} SECTION_TYPE_(BLOCK), SECTION_TYPE_(METADATA_V2_SCHEMA), SECTION_TYPE_(METADATA_V2), @@ -46,8 +46,8 @@ const std::map sections{ #undef SECTION_TYPE_ }; -const std::map compressions { -#define DWARFS_COMPRESSION_TYPE_(name, _) {compression_type::name, #name} +constexpr sorted_array_map compressions { +#define DWARFS_COMPRESSION_TYPE_(name, _) std::pair{compression_type::name, #name ## sv} #define DWARFS_COMMA_ , DWARFS_COMPRESSION_TYPE_LIST(DWARFS_COMPRESSION_TYPE_, DWARFS_COMMA_) #undef DWARFS_COMPRESSION_TYPE_ @@ -55,22 +55,21 @@ const std::map compressions { }; // clang-format on -template -std::string get_default(const HT& ht, const typename HT::key_type& key) { - if (auto it = ht.find(key); it != ht.end()) { - return folly::to(it->second); +std::string get_default(auto const& map, auto key) { + if (auto value = map.get(key)) { + return std::string{*value}; } - - return folly::to("unknown (", key, ")"); + return fmt::format("unknown ({})", static_cast(key)); } + } // namespace bool is_known_compression_type(compression_type type) { - return compressions.count(type) > 0; + return compressions.contains(type); } bool is_known_section_type(section_type type) { - return sections.count(type) > 0; + return sections.contains(type); } std::string get_compression_name(compression_type type) { @@ -83,7 +82,7 @@ std::string get_section_name(section_type type) { void section_header::dump(std::ostream& os) const { os << "[V1] type=" << get_default(sections, type) << ", compression=" - << get_default(compressions, static_cast(compression)) + << get_compression_name(static_cast(compression)) << ", length=" << length; } @@ -98,7 +97,7 @@ void section_header_v2::dump(std::ostream& os) const { << "] num=" << number << ", type=" << get_default(sections, static_cast(type)) << ", compression=" - << get_default(compressions, static_cast(compression)) + << get_compression_name(static_cast(compression)) << ", length=" << length << ", checksum=" << fmt::format("{:#018x}", xxh3_64); }