Universal additions (all generic, header-only, tested; full suite 1127 pass): - Void::Encoding::Url — RFC 3986 percent encode/decode + query_encode. - Void::Text::html_unescape — numeric + common named HTML entities -> UTF-8. - Void::Time::parse_iso8601_interval — parse PT#H#M#S / P#DT#H#M#S durations (distinct from the unit-suffix parse_duration). - Void::FileSystem::TempFile — RAII unique temp file (alongside TempDir), with keep() to persist. - Void::Random::random_string(n, charset) overload. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
114 lines
3.8 KiB
C++
114 lines
3.8 KiB
C++
#pragma once
|
|
|
|
#include <array>
|
|
#include <cstdint>
|
|
#include <string>
|
|
#include <string_view>
|
|
|
|
namespace Void::Text
|
|
{
|
|
|
|
namespace Detail
|
|
{
|
|
// Append `cp` as UTF-8 to `out`.
|
|
inline void append_utf8(std::string& out, std::uint32_t cp)
|
|
{
|
|
if (cp <= 0x7F) {
|
|
out += static_cast<char>(cp);
|
|
} else if (cp <= 0x7FF) {
|
|
out += static_cast<char>(0xC0 | (cp >> 6));
|
|
out += static_cast<char>(0x80 | (cp & 0x3F));
|
|
} else if (cp <= 0xFFFF) {
|
|
out += static_cast<char>(0xE0 | (cp >> 12));
|
|
out += static_cast<char>(0x80 | ((cp >> 6) & 0x3F));
|
|
out += static_cast<char>(0x80 | (cp & 0x3F));
|
|
} else if (cp <= 0x10FFFF) {
|
|
out += static_cast<char>(0xF0 | (cp >> 18));
|
|
out += static_cast<char>(0x80 | ((cp >> 12) & 0x3F));
|
|
out += static_cast<char>(0x80 | ((cp >> 6) & 0x3F));
|
|
out += static_cast<char>(0x80 | (cp & 0x3F));
|
|
}
|
|
}
|
|
|
|
// The common named HTML entities. Numeric entities cover everything else.
|
|
struct NamedEntity {
|
|
std::string_view name;
|
|
std::uint32_t cp;
|
|
};
|
|
inline constexpr std::array<NamedEntity, 12> named_entities{{
|
|
{"amp", '&'}, {"lt", '<'}, {"gt", '>'}, {"quot", '"'},
|
|
{"apos", '\''}, {"nbsp", 0xA0}, {"copy", 0xA9}, {"reg", 0xAE},
|
|
{"hellip", 0x2026}, {"mdash", 0x2014}, {"ndash", 0x2013}, {"trade", 0x2122},
|
|
}};
|
|
} // namespace Detail
|
|
|
|
/// Decode HTML character references in `s` to UTF-8: numeric (`Ӓ`,
|
|
/// `😀`) and the common named entities (& < > " '
|
|
/// © …). Unknown or malformed references are left verbatim.
|
|
/// Generic — no markup parsing, just entity substitution.
|
|
[[nodiscard]] inline std::string html_unescape(std::string_view s)
|
|
{
|
|
std::string out;
|
|
out.reserve(s.size());
|
|
for (std::size_t i = 0; i < s.size();) {
|
|
if (s[i] != '&') {
|
|
out += s[i++];
|
|
continue;
|
|
}
|
|
// Find the terminating ';' within a small window.
|
|
const std::size_t semi = s.find(';', i + 1);
|
|
if (semi == std::string_view::npos || semi - i > 12) {
|
|
out += s[i++];
|
|
continue;
|
|
}
|
|
const std::string_view body = s.substr(i + 1, semi - i - 1);
|
|
bool handled = false;
|
|
|
|
if (!body.empty() && body[0] == '#') {
|
|
// Numeric reference (decimal or hex).
|
|
std::uint32_t cp = 0;
|
|
bool ok = false;
|
|
if (body.size() > 1 && (body[1] == 'x' || body[1] == 'X')) {
|
|
for (std::size_t k = 2; k < body.size(); ++k) {
|
|
const char c = body[k];
|
|
int v;
|
|
if (c >= '0' && c <= '9') v = c - '0';
|
|
else if (c >= 'a' && c <= 'f') v = c - 'a' + 10;
|
|
else if (c >= 'A' && c <= 'F') v = c - 'A' + 10;
|
|
else { ok = false; break; }
|
|
cp = cp * 16 + static_cast<std::uint32_t>(v);
|
|
ok = true;
|
|
}
|
|
} else {
|
|
for (std::size_t k = 1; k < body.size(); ++k) {
|
|
const char c = body[k];
|
|
if (c < '0' || c > '9') { ok = false; break; }
|
|
cp = cp * 10 + static_cast<std::uint32_t>(c - '0');
|
|
ok = true;
|
|
}
|
|
}
|
|
if (ok && cp <= 0x10FFFF) {
|
|
Detail::append_utf8(out, cp);
|
|
handled = true;
|
|
}
|
|
} else {
|
|
for (const auto& e : Detail::named_entities) {
|
|
if (e.name == body) {
|
|
Detail::append_utf8(out, e.cp);
|
|
handled = true;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (handled) {
|
|
i = semi + 1;
|
|
} else {
|
|
out += s[i++]; // leave the '&' as-is
|
|
}
|
|
}
|
|
return out;
|
|
}
|
|
|
|
} // namespace Void::Text
|