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

+136
View File
@@ -0,0 +1,136 @@
#pragma once
#include <Void/Functional/Result.hpp>
#include <Void/Log/Log.hpp>
#include <atomic>
#include <chrono>
#include <cstdint>
#include <filesystem>
#include <format>
#include <fstream>
#include <string>
#include <string_view>
#include <system_error>
#include <thread>
#include <utility>
namespace Void::FileSystem
{
/// RAII scoped *file* under `std::filesystem::temp_directory_path()`.
///
/// A uniquely-named empty file is created on construction and removed on
/// destruction, unless `keep()` was called. Move-only. Useful for staging
/// downloads, copying a shared library to a fresh inode, etc.
///
/// Construction throws `std::system_error` if the file cannot be created;
/// use `TempFile::open(prefix, ext)` for a non-throwing factory.
class TempFile
{
public:
explicit TempFile(std::string_view prefix = "void_",
std::string_view extension = "")
: m_path(make_unique_file(prefix, extension))
{
}
~TempFile() noexcept
{
if (!m_path.empty() && !m_keep) {
std::error_code ignore;
std::filesystem::remove(m_path, ignore);
Void::Log::debug("FileSystem", "TempFile: removed '{}'",
m_path.string());
}
}
TempFile(const TempFile&) = delete;
TempFile& operator=(const TempFile&) = delete;
TempFile(TempFile&& other) noexcept
: m_path(std::exchange(other.m_path, {})),
m_keep(std::exchange(other.m_keep, true))
{
}
TempFile& operator=(TempFile&& other) noexcept
{
if (this != &other) {
if (!m_path.empty() && !m_keep) {
std::error_code ignore;
std::filesystem::remove(m_path, ignore);
}
m_path = std::exchange(other.m_path, {});
m_keep = std::exchange(other.m_keep, true);
}
return *this;
}
[[nodiscard]] const std::filesystem::path& path() const noexcept
{
return m_path;
}
/// Persist the file beyond this object's lifetime (no removal on
/// destruction). Returns the path for convenience.
std::filesystem::path keep() noexcept
{
m_keep = true;
return m_path;
}
/// Non-throwing factory.
[[nodiscard]] static Void::Result::Result<TempFile,
Void::Result::Error>
open(std::string_view prefix = "void_",
std::string_view extension = "")
{
try {
return Void::Result::ok(TempFile{prefix, extension});
} catch (const std::exception& e) {
return Void::Result::fail(std::string{e.what()});
}
}
private:
std::filesystem::path m_path;
bool m_keep = false;
static std::filesystem::path
make_unique_file(std::string_view prefix,
std::string_view extension)
{
static std::atomic<std::uint64_t> counter{0};
const auto base = std::filesystem::temp_directory_path();
for (int attempt = 0; attempt < 16; ++attempt) {
const auto seed =
static_cast<std::uint64_t>(
std::chrono::steady_clock::now()
.time_since_epoch()
.count()) ^
(counter.fetch_add(1, std::memory_order_relaxed) << 16) ^
static_cast<std::uint64_t>(
std::hash<std::thread::id>{}(
std::this_thread::get_id()));
auto candidate =
base / std::format("{}{:016x}{}", prefix, seed, extension);
std::error_code ec;
if (!std::filesystem::exists(candidate, ec)) {
std::ofstream f(candidate);
if (f) {
Void::Log::debug("FileSystem",
"TempFile: created '{}'",
candidate.string());
return candidate;
}
}
}
throw std::system_error(
std::make_error_code(std::errc::file_exists),
"TempFile: failed to create unique file after 16 attempts");
}
};
} // namespace Void::FileSystem