diff --git a/libs/base/string_utils.hpp b/libs/base/string_utils.hpp index 071366563..1452425b5 100644 --- a/libs/base/string_utils.hpp +++ b/libs/base/string_utils.hpp @@ -150,25 +150,30 @@ inline std::string DebugPrint(UniString const & s) return ToUtf8(s); } -template class TokenizeIteratorBase { + template + static constexpr bool is_utf8cpp_iterator = false; + + template + static constexpr bool is_utf8cpp_iterator> = true; + public: using difference_type = std::ptrdiff_t; using iterator_category = std::input_iterator_tag; - // Hack to get buffer pointer from any iterator. - // Deliberately made non-static to simplify the call like this->ToCharPtr. - char const * ToCharPtr(char const * p) const { return p; } template - auto ToCharPtr(T const & i) const + static std::string_view ToStringView(T first, T last) { - return ToCharPtr(i.base()); + if constexpr (is_utf8cpp_iterator) + return TokenizeIteratorBase::ToStringView(first.base(), last.base()); + else + return std::string_view(first, last); } }; template -class TokenizeIterator : public TokenizeIteratorBase +class TokenizeIterator : public TokenizeIteratorBase { public: template @@ -185,8 +190,7 @@ public: { ASSERT(m_start != m_finish, ("Dereferencing of empty iterator.")); - auto const baseI = this->ToCharPtr(m_start); - return std::string_view(baseI, std::distance(baseI, this->ToCharPtr(m_end))); + return this->ToStringView(m_start, m_end); } UniString GetUniString() const @@ -246,7 +250,7 @@ private: /// Used in ParseCSVRow for the generator routine. template -class TokenizeIterator : public TokenizeIteratorBase +class TokenizeIterator : public TokenizeIteratorBase { public: template @@ -265,8 +269,7 @@ public: { ASSERT(!m_finished, ("Dereferencing of empty iterator.")); - auto const baseI = this->ToCharPtr(m_start); - return std::string_view(baseI, std::distance(baseI, this->ToCharPtr(m_end))); + return this->ToStringView(m_start, m_end); } explicit operator bool() const { return !m_finished; }