Use string_view in mapping

This commit is contained in:
Marcus Holland-Moritz 2021-03-06 14:47:47 +01:00
parent 54b4d8494a
commit a9b7bf35b1

View File

@ -22,6 +22,7 @@
#include <map> #include <map>
#include <sstream> #include <sstream>
#include <string> #include <string>
#include <string_view>
#include <folly/Conv.h> #include <folly/Conv.h>
@ -33,7 +34,7 @@ namespace dwarfs {
namespace { namespace {
const std::map<section_type, std::string> sections{ const std::map<section_type, std::string_view> sections{
#define SECTION_TYPE_(x) {section_type::x, #x} #define SECTION_TYPE_(x) {section_type::x, #x}
SECTION_TYPE_(BLOCK), SECTION_TYPE_(BLOCK),
SECTION_TYPE_(METADATA_V2_SCHEMA), SECTION_TYPE_(METADATA_V2_SCHEMA),
@ -41,7 +42,7 @@ const std::map<section_type, std::string> sections{
#undef SECTION_TYPE_ #undef SECTION_TYPE_
}; };
const std::map<compression_type, std::string> compressions{ const std::map<compression_type, std::string_view> compressions{
#define COMPRESSION_TYPE_(x) {compression_type::x, #x} #define COMPRESSION_TYPE_(x) {compression_type::x, #x}
COMPRESSION_TYPE_(NONE), COMPRESSION_TYPE_(LZMA), COMPRESSION_TYPE_(ZSTD), COMPRESSION_TYPE_(NONE), COMPRESSION_TYPE_(LZMA), COMPRESSION_TYPE_(ZSTD),
COMPRESSION_TYPE_(LZ4), COMPRESSION_TYPE_(LZ4HC) COMPRESSION_TYPE_(LZ4), COMPRESSION_TYPE_(LZ4HC)
@ -49,15 +50,12 @@ const std::map<compression_type, std::string> compressions{
}; };
template <typename HT> template <typename HT>
typename HT::mapped_type std::string get_default(const HT& ht, const typename HT::key_type& key) {
get_default(const HT& ht, const typename HT::key_type& key) { if (auto it = ht.find(key); it != ht.end()) {
auto i = ht.find(key); return folly::to<std::string>(it->second);
if (i != ht.end()) {
return i->second;
} }
return folly::to<typename HT::mapped_type>("unknown (", key, ")"); return folly::to<std::string>("unknown (", key, ")");
} }
} // namespace } // namespace