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 <noreply@anthropic.com>
This commit is contained in:
1 parent
d74a69aa34
commit
00759450ff
4 files changed
+18
-211
No files matched your search
@@ -102,6 +102,9 @@ target_link_libraries(EchoionCore
|
||||
Void::Async
|
||||
Void::Containers
|
||||
Void::Resilience
|
||||
Void::Text
|
||||
Void::Time
|
||||
Void::Math
|
||||
PRIVATE
|
||||
Threads::Threads
|
||||
qjs
|
||||
|
||||
+4
-8
@@ -1,5 +1,6 @@
|
||||
#include <Database.hpp>
|
||||
#include "echoion/core/Logger.hpp"
|
||||
#include <Void/Math/Random.hpp>
|
||||
#include <cstring>
|
||||
#include <format>
|
||||
#include <shared_mutex>
|
||||
@@ -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"(
|
||||
|
||||
+9
-195
@@ -2,6 +2,9 @@
|
||||
#include "echoion/core/YouTubeClient.hpp"
|
||||
#include <LoggerInterface.hpp>
|
||||
#include <Void/Data/Json.hpp>
|
||||
#include <Void/Text/Html.hpp>
|
||||
#include <Void/Text/Url.hpp>
|
||||
#include <Void/Time/Format.hpp>
|
||||
#include <algorithm>
|
||||
#include <atomic>
|
||||
#include <cctype>
|
||||
@@ -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<unsigned char>(c)) || c == '-' ||
|
||||
c == '_' || c == '.' || c == '~') {
|
||||
encoded << c;
|
||||
} else {
|
||||
encoded << std::uppercase;
|
||||
encoded << '%' << std::setw(2)
|
||||
<< static_cast<int>(static_cast<unsigned char>(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<char>(codePoint);
|
||||
} else if (codePoint <= 0x7FF) {
|
||||
result += static_cast<char>(
|
||||
0xC0 | (codePoint >> 6));
|
||||
result += static_cast<char>(
|
||||
0x80 | (codePoint & 0x3F));
|
||||
} else if (codePoint <= 0xFFFF) {
|
||||
result += static_cast<char>(
|
||||
0xE0 | (codePoint >> 12));
|
||||
result += static_cast<char>(
|
||||
0x80 | ((codePoint >> 6) & 0x3F));
|
||||
result += static_cast<char>(
|
||||
0x80 | (codePoint & 0x3F));
|
||||
} else {
|
||||
result += static_cast<char>(
|
||||
0xF0 | (codePoint >> 18));
|
||||
result += static_cast<char>(
|
||||
0x80 | ((codePoint >> 12) & 0x3F));
|
||||
result += static_cast<char>(
|
||||
0x80 | ((codePoint >> 6) & 0x3F));
|
||||
result += static_cast<char>(
|
||||
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<char>(codePoint);
|
||||
} else if (codePoint <= 0x7FF) {
|
||||
result += static_cast<char>(
|
||||
0xC0 | (codePoint >> 6));
|
||||
result += static_cast<char>(
|
||||
0x80 | (codePoint & 0x3F));
|
||||
} else if (codePoint <= 0xFFFF) {
|
||||
result += static_cast<char>(
|
||||
0xE0 | (codePoint >> 12));
|
||||
result += static_cast<char>(
|
||||
0x80 | ((codePoint >> 6) & 0x3F));
|
||||
result += static_cast<char>(
|
||||
0x80 | (codePoint & 0x3F));
|
||||
} else {
|
||||
result += static_cast<char>(
|
||||
0xF0 | (codePoint >> 18));
|
||||
result += static_cast<char>(
|
||||
0x80 | ((codePoint >> 12) & 0x3F));
|
||||
result += static_cast<char>(
|
||||
0x80 | ((codePoint >> 6) & 0x3F));
|
||||
result += static_cast<char>(
|
||||
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> 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<Api::SearchResult> YouTubeClient::searchWithYtDlp(
|
||||
|
||||
@@ -3,9 +3,8 @@
|
||||
#include "echoion/core/state/StateSerializer.hpp"
|
||||
#include <Void/Data/Json.hpp>
|
||||
#include <Void/Data/Reflect.hpp>
|
||||
#include <Void/Time/Format.hpp>
|
||||
#include <format>
|
||||
#include <iomanip>
|
||||
#include <sstream>
|
||||
|
||||
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
|
||||
Reference in new issue
Block a user