kanjivg-tools/Libraries/Megane/Source/Megane.cpp

49 lines
1.4 KiB
C++

#include "Megane/Megane.hpp"
#include <simdjson.h>
#include <print>
#include <string>
#include <string_view>
namespace Megane
{
Megane::Megane(const std::string& indexPath)
{
auto res = simdjson::padded_string::load(indexPath);
if (res.error()) {
throw Error("Could not load the file");
}
simdjson::padded_string rawJson = std::move(res.value());
simdjson::ondemand::parser parser;
simdjson::ondemand::document document = parser.iterate(rawJson);
for (auto field : document.get_object()) {
std::string_view key = field.unescaped_key();
std::string_view value;
for (auto candidate : field.value()) {
if (value.length() == 0) {
value = candidate.value_unsafe().get_string();
}
else {
std::string_view candidateValue(candidate);
if (candidateValue.length() < value.length()) {
value = candidateValue;
}
}
}
mStorage[std::string(key)] = value;
}
}
Megane::~Megane() = default;
std::string Megane::GetCode(const std::string& character) const
{
auto it = mStorage.find(character);
if (it == mStorage.end()) {
throw Error("Failed to find the provided character");
}
return it->second;
}
}