From a3c11e29d494f5a2d51072e16341b0184e88a44a Mon Sep 17 00:00:00 2001 From: Edoardo Lolletti Date: Sun, 1 Sep 2024 01:47:37 +0200 Subject: [PATCH] Bring string view usage in line with c++17 STL implementation Remove constructs working only because of the alternative c++14 implementation --- gframe/deck_con.cpp | 6 +++--- gframe/text_types.h | 9 +++++++++ gframe/utils.h | 26 ++++++++++++-------------- 3 files changed, 24 insertions(+), 17 deletions(-) diff --git a/gframe/deck_con.cpp b/gframe/deck_con.cpp index 8668b1f8b..6a570aa51 100644 --- a/gframe/deck_con.cpp +++ b/gframe/deck_con.cpp @@ -1107,14 +1107,14 @@ void DeckBuilder::FilterCards(bool force_refresh) { std::vector tokens; int modif = 0; if(!subterm.empty()) { - if(subterm.starts_with(L"!!")) { + if(starts_with(subterm, L"!!")) { modif |= SEARCH_MODIFIER_NEGATIVE_LOOKUP; subterm.remove_prefix(2); } - if(subterm.starts_with(L'@')) { + if(starts_with(subterm, L'@')) { modif |= SEARCH_MODIFIER_ARCHETYPE_ONLY; subterm.remove_prefix(1); - } else if(subterm.starts_with(L'$')) { + } else if(starts_with(subterm, L'$')) { modif |= SEARCH_MODIFIER_NAME_ONLY; subterm.remove_prefix(1); } diff --git a/gframe/text_types.h b/gframe/text_types.h index 27379a8d5..01b65d459 100644 --- a/gframe/text_types.h +++ b/gframe/text_types.h @@ -34,5 +34,14 @@ constexpr epro::wstringview CHAR_T_STRINGVIEW(epro::stringview, epro::wstringvie #define CHAR_T_STRINGVIEW(Char, text) epro::Detail::CHAR_T_STRINGVIEW(text ""_sv, L"" text ""_sv) } +template +bool starts_with(const T1& stringview, const T2& token) { + if constexpr(std::is_same_v, typename T1::value_type>) { + return stringview.size() >= 1 && *stringview.begin() == token; + } else { + epro::basic_string_view token_sv{token}; + return stringview.size() >= token_sv.size() && memcmp(stringview.data(), token_sv.data(), token_sv.size()) == 0; + } +}; using namespace nonstd::literals; #endif /* TEXT_TYPES_H_ */ diff --git a/gframe/utils.h b/gframe/utils.h index 46a2940dd..d2b809d96 100644 --- a/gframe/utils.h +++ b/gframe/utils.h @@ -270,28 +270,26 @@ auto Utils::GetFileNameImpl(const epro::basic_string_view& _file, bool keepex template inline std::vector Utils::TokenizeString(epro::basic_string_view input, const T& token) { std::vector res; - typename T::size_type pos1, pos2 = 0; - while((pos1 = input.find(token, pos2)) != T::npos) { - if(pos1 != pos2) - res.emplace_back(input.begin() + pos2, pos1 - pos2); - pos2 = pos1 + token.size(); + typename T::size_type pos; + while((pos = input.find(token)) != T::npos) { + res.emplace_back(input.substr(0, pos)); + input.remove_prefix(pos + token.size()); } - if(pos2 != input.size()) - res.emplace_back(input.begin() + pos2, input.size() - pos2); + if(!input.empty()) + res.emplace_back(input); return res; } template inline std::vector Utils::TokenizeString(epro::basic_string_view input, typename T::value_type token) { std::vector res; - typename T::size_type pos1, pos2 = 0; - while((pos1 = input.find(token, pos2)) != T::npos) { - if(pos1 != pos2) - res.emplace_back(input.begin() + pos2, pos1 - pos2); - pos2 = pos1 + 1; + typename T::size_type pos; + while((pos = input.find(token)) != T::npos) { + res.emplace_back(input.substr(0, pos)); + input.remove_prefix(pos + 1); } - if(pos2 != input.size()) - res.emplace_back(input.begin() + pos2, input.size() - pos2); + if(!input.empty()) + res.emplace_back(input); return res; }