diff --git a/source/include/Utils/StringMisc.hpp b/source/include/Utils/StringMisc.hpp index 1eae8fcd6..1f4909768 100644 --- a/source/include/Utils/StringMisc.hpp +++ b/source/include/Utils/StringMisc.hpp @@ -1,91 +1,38 @@ #pragma once -#include -#include -#include -#include -#include -#include -#include #include #include #include -#include +#include #include "Ranges.hpp" MAA_NS_BEGIN -namespace string_detail -{ -template -using sv_type = std::basic_string_view::value_type, - typename std::remove_reference_t::traits_type>; - template -using sv_pair = std::pair, sv_type>; -} // namespace string_detail - -template > -concept IsSomeKindOfString = std::same_as || std::same_as; - -template > -concept IsSomeKindOfStringArray = IsSomeKindOfString; +concept IsSomeKindOfString = std::same_as || std::same_as; template -requires IsSomeKindOfString -inline constexpr void string_replace_all_(StringT& str, string_detail::sv_type from, - string_detail::sv_type to) + requires IsSomeKindOfString +inline void string_replace_all_(StringT& str, const StringT& from, const StringT& to) { - for (size_t pos(0);; pos += to.length()) { - if ((pos = str.find(from, pos)) == StringT::npos) return; - str.replace(pos, from.length(), to); + for (size_t pos = str.find(from); pos != StringT::npos; pos = str.find(from, pos + to.size())) { + str.replace(pos, from.size(), to); } } template -requires IsSomeKindOfString -inline constexpr void string_replace_all_(StringT& str, const MapT& replace_map) + requires IsSomeKindOfString +inline void string_replace_all_(StringT& str, const MapT& replace_map) { for (const auto& [from, to] : replace_map) { string_replace_all_(str, from, to); } } -#ifdef MAA_USE_RANGES_RANGE_V3 -// workaround for P2210R2 -template -requires(requires(Rng rng) { std::basic_string_view(std::addressof(*rng.begin()), MAA_RNS::ranges::distance(rng)); }) -inline auto make_string_view(Rng rng) -{ - return std::basic_string_view(std::addressof(*rng.begin()), MAA_RNS::ranges::distance(rng)); -} - -template End> -requires(requires(It beg, End end) { std::basic_string_view(std::addressof(*beg), std::distance(beg, end)); }) -inline auto make_string_view(It beg, End end) -{ - return std::basic_string_view(std::addressof(*beg), std::distance(beg, end)); -} -#else -template -inline auto make_string_view(Rng rng) -{ - return std::basic_string_view(rng.begin(), rng.end()); -} - -template End> -requires(requires(It beg, End end) { std::basic_string_view(beg, end); }) -inline auto make_string_view(It beg, End end) -{ - return std::basic_string_view(beg, end); -} -#endif - template -requires IsSomeKindOfString -[[nodiscard]] inline constexpr auto string_replace_all(StringT&& str, string_detail::sv_type from, - string_detail::sv_type to) + requires IsSomeKindOfString +[[nodiscard]] inline auto string_replace_all(StringT&& str, const StringT& from, const StringT& to) { std::decay_t result = std::forward(str); string_replace_all_(result, from, to); @@ -93,44 +40,50 @@ requires IsSomeKindOfString } template -requires IsSomeKindOfString -[[nodiscard]] inline constexpr auto string_replace_all(const StringT& str, const MapT& replace_map) + requires IsSomeKindOfString +[[nodiscard]] inline auto string_replace_all(const StringT& str, const MapT& replace_map) { StringT result = str; string_replace_all_(result, replace_map); return result; } -template , - typename Pred = decltype([](CharT c) -> bool { return c != ' '; })> -requires IsSomeKindOfString -inline void string_trim_(StringT& str, Pred not_space = Pred {}) +template bool { return c != ' '; }) > + requires IsSomeKindOfString +inline void string_trim_(StringT& str, Pred not_space = Pred{}) { str.erase(MAA_RNS::ranges::find_if(str | MAA_RNS::views::reverse, not_space).base(), str.end()); str.erase(str.begin(), MAA_RNS::ranges::find_if(str, not_space)); } -template > -requires IsSomeKindOfString +template + requires IsSomeKindOfString inline void tolowers_(StringT& str) { - MAA_RNS::ranges::for_each(str, [](CharT& ch) -> void { ch = static_cast(std::tolower(ch)); }); + using CharT = typename StringT::value_type; + for (auto& ch : str) { + ch = static_cast(std::tolower(ch)); + } } -template > -requires IsSomeKindOfString +template + requires IsSomeKindOfString inline void touppers_(StringT& str) { - MAA_RNS::ranges::for_each(str, [](CharT& ch) -> void { ch = static_cast(std::toupper(ch)); }); + using CharT = typename StringT::value_type; + for (auto& ch : str) { + ch = static_cast(std::toupper(ch)); + } } template -requires IsSomeKindOfString -[[nodiscard]] inline constexpr auto string_split(StringT&& str, DelimT&& delim) + requires IsSomeKindOfString +[[nodiscard]] inline std::vector string_split(const StringT& str, const DelimT& delim) { - std::vector> result; + std::vector result; auto views = str | MAA_RNS::views::split(delim) | - MAA_RNS::views::transform([](auto&& rng) { return make_string_view(rng); }); + MAA_RNS::views::transform([](auto&& rng) { return std::basic_string_view(rng.begin(), rng.end()); }); for (auto v : views) { result.emplace_back(v); }