From 00759450ff38136c78f8c6a6f557b64d339b6c33 Mon Sep 17 00:00:00 2001 From: faumaray Date: Mon, 22 Jun 2026 16:32:08 +0300 Subject: [PATCH] Swap hand-rolled utils for Void primitives Replace bespoke implementations with the new universal Void utilities: - YouTubeClient: urlEncode -> Void::Encoding::Url::encode; ~140-line decodeHtmlEntities -> Void::Text::html_unescape; parseIso8601Duration -> Void::Time::parse_iso8601_interval. - StateSerializer: getCurrentTimestampISO8601 -> Void::Time::utc_now_iso8601. - Database: generateRpcToken's rand() loop -> Void::Random::random_string (seeded from std::random_device). Link Void::Text / Void::Time / Void::Math. Co-Authored-By: Claude Opus 4.8 --- CMakeLists.txt | 3 + src/Database.cpp | 12 +- src/YouTubeClient.cpp | 204 ++-------------------------------- src/state/StateSerializer.cpp | 10 +- 4 files changed, 18 insertions(+), 211 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b4a27a6..42c5cc3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -102,6 +102,9 @@ target_link_libraries(EchoionCore Void::Async Void::Containers Void::Resilience + Void::Text + Void::Time + Void::Math PRIVATE Threads::Threads qjs diff --git a/src/Database.cpp b/src/Database.cpp index ff978e0..11cf49c 100644 --- a/src/Database.cpp +++ b/src/Database.cpp @@ -1,5 +1,6 @@ #include #include "echoion/core/Logger.hpp" +#include #include #include #include @@ -1026,14 +1027,9 @@ namespace Echoion::Api Error(ErrorCode::NotFound, "Subscriber not found")); } - // Generate a random token (32 characters) - const char charset[] = - "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; - std::string token; - token.reserve(32); - for (int i = 0; i < 32; ++i) { - token += charset[rand() % (sizeof(charset) - 1)]; - } + // Generate a random 32-character alphanumeric token (Void::Random is + // seeded from std::random_device, unlike the former rand()). + const std::string token = Void::Random::random_string(32); // Insert token const char* sql = R"( diff --git a/src/YouTubeClient.cpp b/src/YouTubeClient.cpp index e0ef765..3b22897 100644 --- a/src/YouTubeClient.cpp +++ b/src/YouTubeClient.cpp @@ -2,6 +2,9 @@ #include "echoion/core/YouTubeClient.hpp" #include #include +#include +#include +#include #include #include #include @@ -58,168 +61,13 @@ namespace Echoion::Core // URL encode helper function std::string YouTubeClient::urlEncode(std::string_view value) { - std::ostringstream encoded; - encoded.fill('0'); - encoded << std::hex; - - for (char c : value) { - if (std::isalnum(static_cast(c)) || c == '-' || - c == '_' || c == '.' || c == '~') { - encoded << c; - } else { - encoded << std::uppercase; - encoded << '%' << std::setw(2) - << static_cast(static_cast(c)); - encoded << std::nouppercase; - } - } - - return encoded.str(); + return Void::Encoding::Url::encode(value); } // HTML entity decode helper function std::string YouTubeClient::decodeHtmlEntities(std::string_view value) { - std::string result; - result.reserve(value.size()); - - for (size_t i = 0; i < value.size(); ++i) { - if (value[i] == '&' && i + 1 < value.size()) { - // Check for numeric entities &#nnnn; or &#xhhhh; - if (value[i + 1] == '#') { - size_t endPos = value.find(';', i + 2); - if (endPos != std::string_view::npos) { - try { - if (i + 2 < value.size() && value[i + 2] == 'x') { - // Hexadecimal: &#xhhhh; - std::string hex( - value.substr(i + 3, endPos - i - 3)); - char32_t codePoint = - std::stoul(hex, nullptr, 16); - // Convert to UTF-8 - if (codePoint <= 0x7F) { - result += static_cast(codePoint); - } else if (codePoint <= 0x7FF) { - result += static_cast( - 0xC0 | (codePoint >> 6)); - result += static_cast( - 0x80 | (codePoint & 0x3F)); - } else if (codePoint <= 0xFFFF) { - result += static_cast( - 0xE0 | (codePoint >> 12)); - result += static_cast( - 0x80 | ((codePoint >> 6) & 0x3F)); - result += static_cast( - 0x80 | (codePoint & 0x3F)); - } else { - result += static_cast( - 0xF0 | (codePoint >> 18)); - result += static_cast( - 0x80 | ((codePoint >> 12) & 0x3F)); - result += static_cast( - 0x80 | ((codePoint >> 6) & 0x3F)); - result += static_cast( - 0x80 | (codePoint & 0x3F)); - } - } else { - // Decimal: &#nnnn; - std::string num( - value.substr(i + 2, endPos - i - 2)); - char32_t codePoint = std::stoul(num); - // Convert to UTF-8 - if (codePoint <= 0x7F) { - result += static_cast(codePoint); - } else if (codePoint <= 0x7FF) { - result += static_cast( - 0xC0 | (codePoint >> 6)); - result += static_cast( - 0x80 | (codePoint & 0x3F)); - } else if (codePoint <= 0xFFFF) { - result += static_cast( - 0xE0 | (codePoint >> 12)); - result += static_cast( - 0x80 | ((codePoint >> 6) & 0x3F)); - result += static_cast( - 0x80 | (codePoint & 0x3F)); - } else { - result += static_cast( - 0xF0 | (codePoint >> 18)); - result += static_cast( - 0x80 | ((codePoint >> 12) & 0x3F)); - result += static_cast( - 0x80 | ((codePoint >> 6) & 0x3F)); - result += static_cast( - 0x80 | (codePoint & 0x3F)); - } - } - i = endPos; - continue; - } catch (...) { - // Fall through to append '&' - } - } - } - - // Check for named entities - size_t endPos = value.find(';', i + 1); - if (endPos != std::string_view::npos && endPos - i <= 10) { - std::string_view entity = - value.substr(i + 1, endPos - i - 1); - if (entity == "quot") { - result += '"'; - i = endPos; - continue; - } else if (entity == "amp") { - result += '&'; - i = endPos; - continue; - } else if (entity == "apos") { - result += '\''; - i = endPos; - continue; - } else if (entity == "lt") { - result += '<'; - i = endPos; - continue; - } else if (entity == "gt") { - result += '>'; - i = endPos; - continue; - } else if (entity == "nbsp") { - result += ' '; - i = endPos; - continue; - } else if (entity == "copy") { - result += '\xC2\xA9'; - i = endPos; - continue; - } else if (entity == "reg") { - result += '\xC2\xAE'; - i = endPos; - continue; - } else if (entity == "trade") { - result += '\xE2\x84\xA2'; - i = endPos; - continue; - } else if (entity == "mdash") { - result += '\xE2\x80\x94'; - i = endPos; - continue; - } else if (entity == "ndash") { - result += '\xE2\x80\x93'; - i = endPos; - continue; - } else if (entity == "hellip") { - result += '\xE2\x80\xA6'; - i = endPos; - continue; - } - } - } - result += value[i]; - } - - return result; + return Void::Text::html_unescape(value); } YouTubeClient::YouTubeClient(std::shared_ptr networkClient) @@ -900,45 +748,11 @@ namespace Echoion::Core std::chrono::seconds YouTubeClient::parseIso8601Duration(const std::string& duration) { - // Parse ISO 8601 duration format (PT#M#S) - // Examples: PT3M30S, PT1H2M3S, PT45S - std::chrono::seconds result{0}; - - if (duration.empty() || duration[0] != 'P' || duration[1] != 'T') { - return result; + // Delegate to Void; an unparseable value yields zero (prior behaviour). + if (auto d = Void::Time::parse_iso8601_interval(duration)) { + return *d; } - - size_t pos = 2; - int hours = 0, minutes = 0, seconds = 0; - - while (pos < duration.size()) { - size_t numEnd = pos; - while (numEnd < duration.size() && std::isdigit(duration[numEnd])) { - ++numEnd; - } - - if (numEnd == pos) { - break; - } - - int value = std::stoi(duration.substr(pos, numEnd - pos)); - - if (numEnd < duration.size()) { - char unit = duration[numEnd]; - if (unit == 'H') { - hours = value; - } else if (unit == 'M') { - minutes = value; - } else if (unit == 'S') { - seconds = value; - } - pos = numEnd + 1; - } else { - break; - } - } - - return std::chrono::seconds(hours * 3600 + minutes * 60 + seconds); + return std::chrono::seconds{0}; } Api::Result YouTubeClient::searchWithYtDlp( diff --git a/src/state/StateSerializer.cpp b/src/state/StateSerializer.cpp index 66c8e3b..12bb51f 100644 --- a/src/state/StateSerializer.cpp +++ b/src/state/StateSerializer.cpp @@ -3,9 +3,8 @@ #include "echoion/core/state/StateSerializer.hpp" #include #include +#include #include -#include -#include namespace Echoion::Core { @@ -133,12 +132,7 @@ namespace Echoion::Core std::string StateSerializer::getCurrentTimestampISO8601() { - auto now = std::chrono::system_clock::now(); - auto time_t_now = std::chrono::system_clock::to_time_t(now); - - std::stringstream ss; - ss << std::put_time(std::gmtime(&time_t_now), "%Y-%m-%dT%H:%M:%SZ"); - return ss.str(); + return Void::Time::utc_now_iso8601(); } } // namespace Echoion::Core