Files
Library/include/Void/Text/Url.hpp
T
faumarayandClaude Opus 4.8 fb0506581f 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>
2026-06-22 16:15:01 +03:00

93 lines
3.0 KiB
C++

#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