153 lines
6.7 KiB
C++
153 lines
6.7 KiB
C++
#include "core/string_table.h"
|
|
#include <charconv>
|
|
#include <cctype>
|
|
|
|
namespace playground {
|
|
namespace {
|
|
std::string_view local(std::string_view name) {
|
|
const auto colon = name.rfind(':');
|
|
return colon == std::string_view::npos ? name : name.substr(colon + 1);
|
|
}
|
|
|
|
void utf8(std::string& out, unsigned int code) {
|
|
if (code <= 0x7f) out.push_back(static_cast<char>(code));
|
|
else if (code <= 0x7ff) {
|
|
out.push_back(static_cast<char>(0xc0 | (code >> 6)));
|
|
out.push_back(static_cast<char>(0x80 | (code & 0x3f)));
|
|
} else if (code <= 0xffff) {
|
|
out.push_back(static_cast<char>(0xe0 | (code >> 12)));
|
|
out.push_back(static_cast<char>(0x80 | ((code >> 6) & 0x3f)));
|
|
out.push_back(static_cast<char>(0x80 | (code & 0x3f)));
|
|
} else {
|
|
out.push_back(static_cast<char>(0xf0 | (code >> 18)));
|
|
out.push_back(static_cast<char>(0x80 | ((code >> 12) & 0x3f)));
|
|
out.push_back(static_cast<char>(0x80 | ((code >> 6) & 0x3f)));
|
|
out.push_back(static_cast<char>(0x80 | (code & 0x3f)));
|
|
}
|
|
}
|
|
|
|
bool decode(std::string_view in, std::string& out) {
|
|
for (size_t i = 0; i < in.size();) {
|
|
if (in[i] != '&') { out.push_back(in[i++]); continue; }
|
|
const auto end = in.find(';', i + 1);
|
|
if (end == std::string_view::npos) return false;
|
|
const auto entity = in.substr(i + 1, end - i - 1);
|
|
if (entity == "amp") out.push_back('&');
|
|
else if (entity == "lt") out.push_back('<');
|
|
else if (entity == "gt") out.push_back('>');
|
|
else if (entity == "quot") out.push_back('"');
|
|
else if (entity == "apos") out.push_back('\'');
|
|
else if (entity.size() > 1 && entity[0] == '#') {
|
|
unsigned int code = 0;
|
|
const bool hex = entity.size() > 2 && (entity[1] == 'x' || entity[1] == 'X');
|
|
const auto digits = entity.substr(hex ? 2 : 1);
|
|
const auto result = std::from_chars(digits.data(), digits.data() + digits.size(), code, hex ? 16 : 10);
|
|
if (digits.empty() || result.ec != std::errc{} || result.ptr != digits.data() + digits.size() ||
|
|
code == 0 || code > 0x10ffff || (code >= 0xd800 && code <= 0xdfff)) return false;
|
|
utf8(out, code);
|
|
} else return false;
|
|
i = end + 1;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
std::string_view trim(std::string_view s) {
|
|
while (!s.empty() && std::isspace(static_cast<unsigned char>(s.front()))) s.remove_prefix(1);
|
|
while (!s.empty() && std::isspace(static_cast<unsigned char>(s.back()))) s.remove_suffix(1);
|
|
return s;
|
|
}
|
|
}
|
|
|
|
bool StringTable::loadXml(std::string_view xml, std::string* error) {
|
|
std::unordered_map<std::string, std::string> parsed;
|
|
std::string key, value;
|
|
std::vector<std::string_view> stack;
|
|
bool sawWorkbook = false, sawTable = false, inData = false;
|
|
int cell = 0;
|
|
size_t pos = 0;
|
|
auto fail = [&](const char* why) { if (error) *error = why; return false; };
|
|
while (pos < xml.size()) {
|
|
if (xml[pos] != '<') {
|
|
const auto end = xml.find('<', pos);
|
|
if (inData && !decode(xml.substr(pos, end == std::string_view::npos ? end : end - pos),
|
|
cell == 1 ? key : value)) return fail("invalid XML entity");
|
|
pos = end == std::string_view::npos ? xml.size() : end;
|
|
continue;
|
|
}
|
|
if (xml.substr(pos, 4) == "<!--") {
|
|
const auto end = xml.find("-->", pos + 4);
|
|
if (end == std::string_view::npos) return fail("unterminated XML comment");
|
|
pos = end + 3; continue;
|
|
}
|
|
if (xml.substr(pos, 9) == "<![CDATA[") {
|
|
const auto end = xml.find("]]>", pos + 9);
|
|
if (end == std::string_view::npos) return fail("unterminated CDATA");
|
|
if (inData) (cell == 1 ? key : value).append(xml.substr(pos + 9, end - pos - 9));
|
|
pos = end + 3; continue;
|
|
}
|
|
const auto end = xml.find('>', pos + 1);
|
|
if (end == std::string_view::npos) return fail("unterminated XML tag");
|
|
auto tag = trim(xml.substr(pos + 1, end - pos - 1));
|
|
pos = end + 1;
|
|
if (tag.empty()) return fail("empty XML tag");
|
|
if (tag[0] == '?' || tag[0] == '!') continue;
|
|
const bool closing = tag[0] == '/';
|
|
const bool empty = !closing && tag.back() == '/';
|
|
if (closing) tag.remove_prefix(1);
|
|
if (empty) tag.remove_suffix(1);
|
|
tag = trim(tag);
|
|
const auto space = tag.find_first_of(" \t\r\n");
|
|
const auto name = local(tag.substr(0, space));
|
|
if (closing) {
|
|
if (stack.empty() || stack.back() != name) return fail("mismatched XML tag");
|
|
} else {
|
|
stack.push_back(name);
|
|
if (name == "Workbook") sawWorkbook = true;
|
|
if (name == "Table") sawTable = true;
|
|
if (name == "Row") { cell = 0; key.clear(); value.clear(); }
|
|
if (name == "Cell" && stack.size() >= 2 && stack[stack.size()-2] == "Row") ++cell;
|
|
if (name == "Data" && stack.size() >= 2 && stack[stack.size()-2] == "Cell") inData = true;
|
|
}
|
|
if (closing || empty) {
|
|
if (name == "Data") inData = false;
|
|
if (name == "Row" && !key.empty()) parsed[key] = value;
|
|
stack.pop_back();
|
|
}
|
|
}
|
|
if (!stack.empty() || !sawWorkbook || !sawTable) return fail("incomplete XML Spreadsheet");
|
|
for (auto& [k, v] : parsed) values_[std::move(k)] = std::move(v);
|
|
return true;
|
|
}
|
|
|
|
const std::string* StringTable::find(std::string_view key) const {
|
|
auto it = values_.find(std::string(key));
|
|
return it == values_.end() ? nullptr : &it->second;
|
|
}
|
|
|
|
std::string StringTable::get(std::string_view key) const {
|
|
if (!key.empty() && key.front() == '#') return std::string(key.substr(1));
|
|
const auto* value = find(key);
|
|
return value ? *value : "#####";
|
|
}
|
|
|
|
std::string StringTable::format(std::string_view key, const std::vector<std::string>& args) const {
|
|
const auto source = get(key);
|
|
std::string out;
|
|
for (size_t i = 0; i < source.size();) {
|
|
if (source[i] != '%') { out.push_back(source[i++]); continue; }
|
|
if (i + 1 < source.size() && source[i+1] == '%') { out.push_back('%'); i += 2; continue; }
|
|
const auto end = source.find('%', i + 1);
|
|
if (end != std::string::npos) {
|
|
size_t number = 0;
|
|
const auto digits = std::string_view(source).substr(i + 1, end - i - 1);
|
|
const auto result = std::from_chars(digits.data(), digits.data() + digits.size(), number);
|
|
if (!digits.empty() && result.ec == std::errc{} && result.ptr == digits.data()+digits.size() &&
|
|
number >= 1 && number <= args.size()) {
|
|
out += args[number - 1]; i = end + 1; continue;
|
|
}
|
|
}
|
|
out.push_back(source[i++]);
|
|
}
|
|
return out;
|
|
}
|
|
} // namespace playground
|