Skip to content

Commit

Permalink
Bring string view usage in line with c++17 STL implementation
Browse files Browse the repository at this point in the history
Remove constructs working only because of the alternative c++14 implementation
  • Loading branch information
edo9300 committed Aug 31, 2024
1 parent dad306b commit a3c11e2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
6 changes: 3 additions & 3 deletions gframe/deck_con.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1107,14 +1107,14 @@ void DeckBuilder::FilterCards(bool force_refresh) {
std::vector<epro::wstringview> 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);
}
Expand Down
9 changes: 9 additions & 0 deletions gframe/text_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<Char>(text ""_sv, L"" text ""_sv)

}
template<typename T1, typename T2>
bool starts_with(const T1& stringview, const T2& token) {
if constexpr(std::is_same_v<std::remove_cv_t<T2>, 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_ */
26 changes: 12 additions & 14 deletions gframe/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -270,28 +270,26 @@ auto Utils::GetFileNameImpl(const epro::basic_string_view<T>& _file, bool keepex
template<typename T>
inline std::vector<T> Utils::TokenizeString(epro::basic_string_view<typename T::value_type> input, const T& token) {
std::vector<T> 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<typename T>
inline std::vector<T> Utils::TokenizeString(epro::basic_string_view<typename T::value_type> input, typename T::value_type token) {
std::vector<T> 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;
}

Expand Down

0 comments on commit a3c11e2

Please sign in to comment.