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:
faumarayandClaude Opus 4.8 committed 2026-06-22 16:15:01 +03:00
1 parent 80293ee56f
commit fb0506581f
9 files changed
+645 -6

No files matched your search

+28
View File
@@ -5,6 +5,34 @@
#include <algorithm>
#include <vector>
// ── random_string charset ────────────────────────────────────────────────────
Void::Testing::Test random_string_length_and_charset()
{
const std::string charset = "ABC";
const auto s = Void::Random::random_string(64, charset);
Void::Testing::expect_eq(s.size(), std::size_t{64});
for (char c : s) {
Void::Testing::expect(charset.find(c) != std::string::npos);
}
}
Void::Testing::Test random_string_default_alphanumeric()
{
const auto s = Void::Random::random_string(32);
Void::Testing::expect_eq(s.size(), std::size_t{32});
for (char c : s) {
const bool alnum = (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
(c >= '0' && c <= '9');
Void::Testing::expect(alnum);
}
}
Void::Testing::Test random_string_empty_charset_is_empty()
{
Void::Testing::expect(Void::Random::random_string(10, "").empty());
}
// ── Xoshiro256 ───────────────────────────────────────────────────────────────
Void::Testing::Test xoshiro_produces_output()
+60
View File
@@ -0,0 +1,60 @@
#include <Void/System/TempFile.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <filesystem>
#include <fstream>
#include <utility>
Void::Testing::Test temp_file_creates_under_temp_and_removes()
{
std::filesystem::path captured;
{
Void::FileSystem::TempFile tmp;
captured = tmp.path();
Void::Testing::expect(std::filesystem::is_regular_file(captured));
Void::Testing::expect(captured.parent_path() ==
std::filesystem::temp_directory_path());
}
Void::Testing::expect(!std::filesystem::exists(captured));
}
Void::Testing::Test temp_file_honors_extension()
{
Void::FileSystem::TempFile tmp{"void_", ".so"};
Void::Testing::expect_eq(tmp.path().extension().string(),
std::string{".so"});
}
Void::Testing::Test temp_file_keep_persists()
{
std::filesystem::path captured;
{
Void::FileSystem::TempFile tmp;
captured = tmp.keep();
}
Void::Testing::expect(std::filesystem::exists(captured));
std::error_code ec;
std::filesystem::remove(captured, ec); // manual cleanup
}
Void::Testing::Test temp_file_move_transfers_ownership()
{
std::filesystem::path captured;
{
Void::FileSystem::TempFile a;
captured = a.path();
Void::FileSystem::TempFile b = std::move(a);
Void::Testing::expect_eq(b.path(), captured);
Void::Testing::expect(a.path().empty());
}
// Removed once (by b); no double-remove crash.
Void::Testing::expect(!std::filesystem::exists(captured));
}
Void::Testing::Test temp_files_are_unique()
{
Void::FileSystem::TempFile a;
Void::FileSystem::TempFile b;
Void::Testing::expect(a.path() != b.path());
}
+81
View File
@@ -1,5 +1,7 @@
#include <Void/Text/Base64.hpp>
#include <Void/Text/Hex.hpp>
#include <Void/Text/Html.hpp>
#include <Void/Text/Url.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
@@ -157,3 +159,82 @@ Void::Testing::Test hex_round_trip()
Void::Testing::expect(decoded.has_value());
Void::Testing::expect_eq(*decoded, original);
}
// ── URL / percent encoding ─────────────────────────────────────────────────────
Void::Testing::Test url_encode_unreserved_passthrough()
{
Void::Testing::expect_eq(Void::Encoding::Url::encode("AZaz09-._~"),
std::string{"AZaz09-._~"});
}
Void::Testing::Test url_encode_reserved()
{
Void::Testing::expect_eq(Void::Encoding::Url::encode("a b/c?d=e&f"),
std::string{"a%20b%2Fc%3Fd%3De%26f"});
}
Void::Testing::Test url_query_encode_space_as_plus()
{
Void::Testing::expect_eq(Void::Encoding::Url::query_encode("a b c"),
std::string{"a+b+c"});
}
Void::Testing::Test url_decode_basic()
{
const auto r = Void::Encoding::Url::decode("a%20b%2Fc");
Void::Testing::expect(r.has_value());
Void::Testing::expect_eq(*r, std::string{"a b/c"});
}
Void::Testing::Test url_decode_plus_as_space()
{
const auto r = Void::Encoding::Url::decode("a+b", /*plus_as_space=*/true);
Void::Testing::expect(r.has_value());
Void::Testing::expect_eq(*r, std::string{"a b"});
}
Void::Testing::Test url_decode_malformed_fails()
{
Void::Testing::expect(!Void::Encoding::Url::decode("a%2").has_value());
Void::Testing::expect(!Void::Encoding::Url::decode("a%zz").has_value());
}
Void::Testing::Test url_round_trip_utf8()
{
const std::string original = "café/naïve?x=1 2";
const auto enc = Void::Encoding::Url::encode(original);
const auto dec = Void::Encoding::Url::decode(enc);
Void::Testing::expect(dec.has_value());
Void::Testing::expect_eq(*dec, original);
}
// ── HTML entity decoding ───────────────────────────────────────────────────────
Void::Testing::Test html_named_entities()
{
Void::Testing::expect_eq(
Void::Text::html_unescape("a &amp; b &lt; c &gt; d &quot;e&quot;"),
std::string{"a & b < c > d \"e\""});
}
Void::Testing::Test html_numeric_decimal()
{
Void::Testing::expect_eq(Void::Text::html_unescape("&#65;&#66;&#67;"),
std::string{"ABC"});
}
Void::Testing::Test html_numeric_hex_utf8()
{
// U+1F600 grinning face -> 4-byte UTF-8
const auto r = Void::Text::html_unescape("&#x1F600;");
Void::Testing::expect_eq(r.size(), std::size_t{4});
}
Void::Testing::Test html_unknown_left_verbatim()
{
Void::Testing::expect_eq(Void::Text::html_unescape("a &bogus; b"),
std::string{"a &bogus; b"});
Void::Testing::expect_eq(Void::Text::html_unescape("plain & text"),
std::string{"plain & text"});
}
+43
View File
@@ -9,6 +9,49 @@
// ── Stopwatch ─────────────────────────────────────────────────────────────
// ── ISO 8601 interval parsing ─────────────────────────────────────────────
Void::Testing::Test iso8601_interval_hours_minutes()
{
const auto r = Void::Time::parse_iso8601_interval("PT1H30M");
Void::Testing::expect(r.has_value());
Void::Testing::expect_eq(r->count(), static_cast<long long>(5400));
}
Void::Testing::Test iso8601_interval_days_and_time()
{
const auto r = Void::Time::parse_iso8601_interval("P1DT2H");
Void::Testing::expect(r.has_value());
Void::Testing::expect_eq(r->count(), static_cast<long long>(93600));
}
Void::Testing::Test iso8601_interval_seconds_only()
{
const auto r = Void::Time::parse_iso8601_interval("PT45S");
Void::Testing::expect(r.has_value());
Void::Testing::expect_eq(r->count(), static_cast<long long>(45));
}
Void::Testing::Test iso8601_interval_zero_and_negative()
{
const auto z = Void::Time::parse_iso8601_interval("PT0S");
Void::Testing::expect(z.has_value());
Void::Testing::expect_eq(z->count(), static_cast<long long>(0));
const auto n = Void::Time::parse_iso8601_interval("-PT1M");
Void::Testing::expect(n.has_value());
Void::Testing::expect_eq(n->count(), static_cast<long long>(-60));
}
Void::Testing::Test iso8601_interval_malformed()
{
Void::Testing::expect(!Void::Time::parse_iso8601_interval("").has_value());
Void::Testing::expect(
!Void::Time::parse_iso8601_interval("1H30M").has_value());
Void::Testing::expect(!Void::Time::parse_iso8601_interval("PT").has_value());
Void::Testing::expect(
!Void::Time::parse_iso8601_interval("PT5X").has_value());
}
Void::Testing::Test stopwatch_elapsed_is_non_negative()
{
auto sw = Void::Time::Stopwatch::started();