Add Url/Html encoding, ISO-8601 interval parse, TempFile, random_string charset
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>
This commit is contained in:
1 parent
80293ee56f
commit
fb0506581f
9 files changed
+645
-6
No files matched your search
@@ -0,0 +1,113 @@
|
||||
#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
|
||||
@@ -0,0 +1,92 @@
|
||||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
namespace Void::Encoding::Url
|
||||
{
|
||||
|
||||
namespace Detail
|
||||
{
|
||||
// RFC 3986 unreserved characters: ALPHA / DIGIT / "-" / "." / "_" / "~".
|
||||
[[nodiscard]] constexpr std::array<bool, 256> make_unreserved() noexcept
|
||||
{
|
||||
std::array<bool, 256> t{};
|
||||
for (char c = 'A'; c <= 'Z'; ++c) t[static_cast<unsigned char>(c)] = true;
|
||||
for (char c = 'a'; c <= 'z'; ++c) t[static_cast<unsigned char>(c)] = true;
|
||||
for (char c = '0'; c <= '9'; ++c) t[static_cast<unsigned char>(c)] = true;
|
||||
for (char c : {'-', '.', '_', '~'}) t[static_cast<unsigned char>(c)] = true;
|
||||
return t;
|
||||
}
|
||||
|
||||
inline constexpr std::array<bool, 256> unreserved = make_unreserved();
|
||||
inline constexpr std::string_view hex_upper = "0123456789ABCDEF";
|
||||
|
||||
[[nodiscard]] constexpr int hex_value(unsigned char c) noexcept
|
||||
{
|
||||
if (c >= '0' && c <= '9') return c - '0';
|
||||
if (c >= 'a' && c <= 'f') return c - 'a' + 10;
|
||||
if (c >= 'A' && c <= 'F') return c - 'A' + 10;
|
||||
return -1;
|
||||
}
|
||||
} // namespace Detail
|
||||
|
||||
/// Percent-encode `s` per RFC 3986. Unreserved characters pass through; every
|
||||
/// other byte becomes %XX (uppercase hex). When `space_as_plus` is true a space
|
||||
/// is emitted as '+' (application/x-www-form-urlencoded form encoding) instead
|
||||
/// of %20.
|
||||
[[nodiscard]] inline std::string encode(std::string_view s,
|
||||
bool space_as_plus = false)
|
||||
{
|
||||
std::string out;
|
||||
out.reserve(s.size() * 3);
|
||||
for (unsigned char c : s) {
|
||||
if (Detail::unreserved[c]) {
|
||||
out += static_cast<char>(c);
|
||||
} else if (space_as_plus && c == ' ') {
|
||||
out += '+';
|
||||
} else {
|
||||
out += '%';
|
||||
out += Detail::hex_upper[c >> 4];
|
||||
out += Detail::hex_upper[c & 0x0F];
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/// Encode for a query-string value: form encoding with spaces as '+'.
|
||||
[[nodiscard]] inline std::string query_encode(std::string_view s)
|
||||
{
|
||||
return encode(s, /*space_as_plus=*/true);
|
||||
}
|
||||
|
||||
/// Decode a percent-encoded string. '+' is treated as a space when
|
||||
/// `plus_as_space` is true (form decoding). Returns nullopt on a malformed
|
||||
/// escape (a '%' not followed by two hex digits).
|
||||
[[nodiscard]] inline std::optional<std::string>
|
||||
decode(std::string_view s, bool plus_as_space = false)
|
||||
{
|
||||
std::string out;
|
||||
out.reserve(s.size());
|
||||
for (std::size_t i = 0; i < s.size(); ++i) {
|
||||
const unsigned char c = static_cast<unsigned char>(s[i]);
|
||||
if (c == '%') {
|
||||
if (i + 2 >= s.size()) return std::nullopt;
|
||||
const int hi = Detail::hex_value(static_cast<unsigned char>(s[i + 1]));
|
||||
const int lo = Detail::hex_value(static_cast<unsigned char>(s[i + 2]));
|
||||
if (hi < 0 || lo < 0) return std::nullopt;
|
||||
out += static_cast<char>((hi << 4) | lo);
|
||||
i += 2;
|
||||
} else if (plus_as_space && c == '+') {
|
||||
out += ' ';
|
||||
} else {
|
||||
out += static_cast<char>(c);
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
} // namespace Void::Encoding::Url
|
||||
Reference in new issue
Block a user