[Megane] Add tiny library to translate utf8 characters to kvg code
This commit is contained in:
parent
4943f35d91
commit
3b5eb6f1a4
8 changed files with 145 additions and 0 deletions
49
Libraries/Megane/Source/Megane.cpp
Normal file
49
Libraries/Megane/Source/Megane.cpp
Normal file
|
@ -0,0 +1,49 @@
|
|||
#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;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue