From 05f0e1474a8400b7a7bed900f6f75638c2484047 Mon Sep 17 00:00:00 2001 From: Patrick Wuttke Date: Tue, 20 Aug 2024 00:35:19 +0200 Subject: [PATCH] Fixed URLs with non-default char types. --- source/mijin/net/url.hpp | 15 ++++++++------- source/mijin/util/string.hpp | 18 +++++++++++++----- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/source/mijin/net/url.hpp b/source/mijin/net/url.hpp index 37b57b8..2127349 100644 --- a/source/mijin/net/url.hpp +++ b/source/mijin/net/url.hpp @@ -79,7 +79,7 @@ constexpr void URLBase::parse() noexcept } string_view_t toParse = base_; - typename string_view_t::size_type pos = toParse.find(':'); + typename string_view_t::size_type pos = toParse.find(TChar(':')); if (pos == string_t::npos) { clear(); @@ -88,7 +88,8 @@ constexpr void URLBase::parse() noexcept scheme_ = toParse.substr(0, pos); toParse = toParse.substr(pos + 1); - if (!toParse.starts_with("//")) + const TChar DOUBLE_SLASH[] = {TChar('/'), TChar('/'), TChar(0)}; + if (!toParse.starts_with(DOUBLE_SLASH)) { userinfo_ = host_ = {}; port_ = 0; @@ -96,7 +97,7 @@ constexpr void URLBase::parse() noexcept else { toParse = toParse.substr(2); // skip the slashes - pos = toParse.find('/'); + pos = toParse.find(TChar('/')); if (!parseAuthority(toParse.substr(0, pos))) { clear(); @@ -110,7 +111,7 @@ constexpr void URLBase::parse() noexcept toParse = toParse.substr(pos); } pathQueryFragment_ = toParse; - pos = toParse.find('#'); + pos = toParse.find(TChar('#')); if (pos == string_view_t::npos) { fragment_ = {}; @@ -120,7 +121,7 @@ constexpr void URLBase::parse() noexcept fragment_ = toParse.substr(pos + 1); toParse = toParse.substr(0, pos); } - pos = toParse.find('?'); + pos = toParse.find(TChar('?')); if (pos == string_view_t::npos) { query_ = {}; @@ -137,7 +138,7 @@ template constexpr bool URLBase::parseAuthority(string_view_t authority) noexcept { string_view_t toParse = authority; - typename string_view_t::size_type pos = toParse.find('@'); + typename string_view_t::size_type pos = toParse.find(TChar('@')); if (pos == string_view_t::npos) { userinfo_ = {}; @@ -147,7 +148,7 @@ constexpr bool URLBase::parseAuthority(string_view_t userinfo_ = toParse.substr(0, pos); toParse = toParse.substr(pos + 1); } - pos = toParse.find(':'); // TODO: IPv6 + pos = toParse.find(TChar(':')); // TODO: IPv6 if (pos == string_view_t::npos) { port_ = 0; diff --git a/source/mijin/util/string.hpp b/source/mijin/util/string.hpp index f41a719..5189f9a 100644 --- a/source/mijin/util/string.hpp +++ b/source/mijin/util/string.hpp @@ -249,7 +249,7 @@ template } template -void makeLower(std::basic_string& string) +constexpr void makeLower(std::basic_string& string) { std::transform(string.begin(), string.end(), string.begin(), [locale = std::locale()](TChar chr) { @@ -258,7 +258,7 @@ void makeLower(std::basic_string& string) } template -void makeUpper(std::basic_string& string) +constexpr void makeUpper(std::basic_string& string) { std::transform(string.begin(), string.end(), string.begin(), [locale = std::locale()](TChar chr) { @@ -268,7 +268,7 @@ void makeUpper(std::basic_string& string) template [[nodiscard]] -auto toLower(TArgs&&... args) +constexpr auto toLower(TArgs&&... args) { std::basic_string string(std::forward(args)...); makeLower(string); @@ -277,7 +277,7 @@ auto toLower(TArgs&&... args) template [[nodiscard]] -auto toUpper(TArgs&&... args) +constexpr auto toUpper(TArgs&&... args) { std::basic_string string(std::forward(args)...); makeUpper(string); @@ -287,7 +287,7 @@ auto toUpper(TArgs&&... args) template [[nodiscard]] -bool toNumber(std::string_view stringView, TNumber& outNumber, int base = 10) noexcept +constexpr bool toNumber(std::string_view stringView, TNumber& outNumber, int base = 10) noexcept { const char* start = &*stringView.begin(); const char* end = start + stringView.size(); @@ -295,6 +295,14 @@ bool toNumber(std::string_view stringView, TNumber& outNumber, int base = 10) no return res.ec == std::errc{} && res.ptr == end; } +template +[[nodiscard]] +constexpr bool toNumber(std::basic_string_view stringView, TNumber& outNumber, int base = 10) noexcept requires (!std::is_same_v) +{ + std::string asString(stringView.begin(), stringView.end()); + return toNumber(asString, outNumber, base); +} + namespace pipe { struct Join