diff options
author | Shauren <shauren.trinity@gmail.com> | 2025-06-02 18:29:47 +0200 |
---|---|---|
committer | Shauren <shauren.trinity@gmail.com> | 2025-06-02 18:29:47 +0200 |
commit | 2d984fcade155bf860ed20713a95251ad0103318 (patch) | |
tree | fe36ed3d4e99541ef591319cc2a959b7a9ce894f | |
parent | d2d0995097bc0c748c3ee6f8a92f66919c5697a3 (diff) |
Core/Misc: Add end iterator/sentinel to make `char const*` work with std::ranges algorithms
-rw-r--r-- | src/common/Utilities/Util.h | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/src/common/Utilities/Util.h b/src/common/Utilities/Util.h index 9ee4d1d62c0..8e78797caef 100644 --- a/src/common/Utilities/Util.h +++ b/src/common/Utilities/Util.h @@ -372,6 +372,37 @@ TC_COMMON_API void wstrToLower(std::wstring& str); TC_COMMON_API void strToUpper(std::string& str); TC_COMMON_API void strToLower(std::string& str); +/** + * End sentinel for std::ranges algorithms to use a char const* as begin iterator where end bound is known (for example std::array storage) + */ +template <typename Iterator> +struct CStringBoundedSentinel +{ + Iterator End; + constexpr bool operator==(Iterator itr) const; +}; + +/** + * End sentinel for std::ranges algorithms to use a char const* as begin iterator + */ +struct CStringSentinel_T +{ + template <typename Iterator> + inline constexpr bool operator==(Iterator itr) const { return *itr == static_cast<std::iter_value_t<Iterator>>(0); } + + /** + * End sentinel for std::ranges algorithms to use a char const* as begin iterator where end of valid memory is known (for example std::array partially filled by the string) + */ + template <typename Iterator> + inline constexpr CStringBoundedSentinel<Iterator> Checked(Iterator end) const { return { .End = end }; } +} inline constexpr CStringSentinel; + +template <typename Iterator> +inline constexpr bool CStringBoundedSentinel<Iterator>::operator==(Iterator itr) const +{ + return itr == End || itr == CStringSentinel; +} + TC_COMMON_API std::wstring wstrCaseAccentInsensitiveParse(std::wstring_view wstr, LocaleConstant locale); TC_COMMON_API std::wstring GetMainPartOfName(std::wstring const& wname, uint32 declension); |