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
@@ -198,4 +198,82 @@ namespace Void::Time
|
||||
tm.tm_hour, tm.tm_min, tm.tm_sec, ms.count());
|
||||
}
|
||||
|
||||
/// Parse an ISO 8601 *duration/interval* of the form
|
||||
/// `[-]P[nD]T[nH][nM][n[.n]S]` (also accepts week form `PnW`) into a
|
||||
/// std::chrono::seconds. Examples: "PT1H30M" -> 5400s, "P1DT2H" -> 93600s,
|
||||
/// "PT45S" -> 45s, "PT0S" -> 0s. Fractional seconds are truncated.
|
||||
/// Returns nullopt on malformed input. Distinct from parse_duration, which
|
||||
/// handles unit-suffixed values like "250ms".
|
||||
[[nodiscard]] inline std::optional<std::chrono::seconds>
|
||||
parse_iso8601_interval(std::string_view s)
|
||||
{
|
||||
if (s.empty()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
bool negative = false;
|
||||
std::size_t i = 0;
|
||||
if (s[i] == '+' || s[i] == '-') {
|
||||
negative = (s[i] == '-');
|
||||
++i;
|
||||
}
|
||||
if (i >= s.size() || s[i] != 'P') {
|
||||
return std::nullopt;
|
||||
}
|
||||
++i; // consume 'P'
|
||||
|
||||
long long total = 0;
|
||||
bool in_time = false; // past the 'T' separator
|
||||
bool any_field = false;
|
||||
|
||||
while (i < s.size()) {
|
||||
if (s[i] == 'T') {
|
||||
in_time = true;
|
||||
++i;
|
||||
continue;
|
||||
}
|
||||
// Read a (optionally fractional) number.
|
||||
const std::size_t start = i;
|
||||
while (i < s.size() &&
|
||||
((s[i] >= '0' && s[i] <= '9') || s[i] == '.')) {
|
||||
++i;
|
||||
}
|
||||
if (i == start || i >= s.size()) {
|
||||
return std::nullopt; // number without a unit
|
||||
}
|
||||
double value = 0.0;
|
||||
{
|
||||
// from_chars on the [start, i) numeric span.
|
||||
const auto* b = s.data() + start;
|
||||
const auto* e = s.data() + i;
|
||||
const auto [ptr, ec] = std::from_chars(b, e, value);
|
||||
if (ec != std::errc{} || ptr != e) {
|
||||
return std::nullopt;
|
||||
}
|
||||
}
|
||||
const char unit = s[i++];
|
||||
long long secs = 0;
|
||||
if (!in_time) {
|
||||
switch (unit) {
|
||||
case 'W': secs = static_cast<long long>(value) * 604800; break;
|
||||
case 'D': secs = static_cast<long long>(value) * 86400; break;
|
||||
default: return std::nullopt; // Y/M (calendar) unsupported
|
||||
}
|
||||
} else {
|
||||
switch (unit) {
|
||||
case 'H': secs = static_cast<long long>(value) * 3600; break;
|
||||
case 'M': secs = static_cast<long long>(value) * 60; break;
|
||||
case 'S': secs = static_cast<long long>(value); break;
|
||||
default: return std::nullopt;
|
||||
}
|
||||
}
|
||||
total += secs;
|
||||
any_field = true;
|
||||
}
|
||||
|
||||
if (!any_field) {
|
||||
return std::nullopt;
|
||||
}
|
||||
return std::chrono::seconds{negative ? -total : total};
|
||||
}
|
||||
|
||||
} // namespace Void::Time
|
||||
Reference in new issue
Block a user