Use custom class instead of std::map

This commit is contained in:
topjohnwu
2021-03-02 23:16:10 -08:00
parent 027ec70262
commit 117ae71025
4 changed files with 33 additions and 28 deletions

View File

@@ -1,26 +1,9 @@
#include <string.h>
#include "format.hpp"
std::map<std::string_view, format_t> name2fmt;
Name2Fmt name2fmt;
Fmt2Name fmt2name;
Fmt2Ext fmt2ext;
class FormatInit {
public:
FormatInit() {
name2fmt["gzip"] = GZIP;
name2fmt["xz"] = XZ;
name2fmt["lzma"] = LZMA;
name2fmt["bzip2"] = BZIP2;
name2fmt["lz4"] = LZ4;
name2fmt["lz4_legacy"] = LZ4_LEGACY;
name2fmt["lz4_lg"] = LZ4_LG;
}
};
static FormatInit init;
#define CHECKED_MATCH(s) (len >= (sizeof(s) - 1) && BUFFER_MATCH(buf, s))
format_t check_fmt(const void *buf, size_t len) {
@@ -103,3 +86,17 @@ const char *Fmt2Ext::operator[](format_t fmt) {
return "";
}
}
#define CHECK(s, f) else if (name == s) return f;
format_t Name2Fmt::operator[](std::string_view name) {
if (0) {}
CHECK("gzip", GZIP)
CHECK("xz", XZ)
CHECK("lzma", LZMA)
CHECK("bzip2", BZIP2)
CHECK("lz4", LZ4)
CHECK("lz4_legacy", LZ4_LEGACY)
CHECK("lz4_lg", LZ4_LG)
else return UNKNOWN;
}