From 6c4b711fb4178bb9565388c11bcf2fa9177d6f7d Mon Sep 17 00:00:00 2001 From: Sergey Magidovich Date: Tue, 15 Dec 2015 20:53:36 +0300 Subject: [PATCH] Adjust TokenizeIterator interface so it can be used with stl collections. --- base/base_tests/string_utils_test.cpp | 6 ++++++ base/string_utils.hpp | 29 ++++++++++++++++++++++++--- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/base/base_tests/string_utils_test.cpp b/base/base_tests/string_utils_test.cpp index 9342daf3e0..1c22d9df55 100644 --- a/base/base_tests/string_utils_test.cpp +++ b/base/base_tests/string_utils_test.cpp @@ -349,6 +349,12 @@ UNIT_TEST(SimpleTokenizer) tokens.assign(&s[0], &s[0] + ARRAY_SIZE(s)); TestIter("/1/2/", "/", tokens); } + { + using strings::SimpleTokenizer; + string const str("a,b,c"); + TEST_EQUAL(vector(SimpleTokenizer(str, ","), SimpleTokenizer()), + (vector{"a", "b", "c"}), ()); + } } UNIT_TEST(LastUniChar) diff --git a/base/string_utils.hpp b/base/string_utils.hpp index e00f9320ed..723d72689e 100644 --- a/base/string_utils.hpp +++ b/base/string_utils.hpp @@ -75,9 +75,6 @@ class TokenizeIterator UniCharIterT m_beg, m_end, m_finish; DelimFuncT m_delimFunc; - /// Explicitly disabled, because we're storing iterators for string - TokenizeIterator(char const *, DelimFuncT const &); - void move() { m_beg = m_end; @@ -113,6 +110,12 @@ public: move(); } + /// Use default-constructed iterator for operator == to determin an end of a token stream. + TokenizeIterator() = default; + + /// Explicitly disabled, because we're storing iterators for string + TokenizeIterator(char const *, DelimFuncT const &) = delete; + string operator*() const { ASSERT( m_beg != m_finish, ("dereferencing of empty iterator") ); @@ -141,6 +144,11 @@ public: { return UniString(m_beg, m_end); } + + /// Same as operator bool() in expression it == end(...) + bool operator==(TokenizeIterator const &) { return !(*this); } + /// Same as operator bool() in expression it != end(...) + bool operator!=(TokenizeIterator const &) { return (*this); } }; class SimpleDelimiter @@ -148,6 +156,8 @@ class SimpleDelimiter UniString m_delims; public: SimpleDelimiter(char const * delimChars); + // Used in TokenizeIterator to allow past the end iterator construction. + SimpleDelimiter() = default; /// @return true if c is delimiter bool operator()(UniChar c) const; }; @@ -349,3 +359,16 @@ size_t EditDistance(TIter const & b1, TIter const & e1, TIter const & b2, TIter return prev[m]; } } // namespace strings + +namespace std +{ +template +struct iterator_traits> +{ + using difference_type = std::ptrdiff_t; + using value_type = string; + using pointer = void; + using reference = string; + using iterator_category = std::input_iterator_tag; +}; +} // namespace std