diff options
author | gleb-kov <gleb-kov@yandex-team.ru> | 2022-02-10 12:28:02 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 15:58:17 +0300 |
commit | 0eef543901a7710e104c6896930f164d24185d6e (patch) | |
tree | 6d314891cc237ed4ecfc3e29b1f606834d36459d | |
parent | 8850c0828a3703fdb4dd75090c8edf8de14b5671 (diff) | |
download | ydb-0eef543901a7710e104c6896930f164d24185d6e.tar.gz |
IGNIETFERRO-1970 TStringSplitter::TIteratorState inherits TStringBufType
ref:d9bd6fa68b77248692551b16e94be52cb6b137c4
-rw-r--r-- | util/string/split.h | 82 |
1 files changed, 34 insertions, 48 deletions
diff --git a/util/string/split.h b/util/string/split.h index 80f8c787dc..bc46d9e64c 100644 --- a/util/string/split.h +++ b/util/string/split.h @@ -651,74 +651,58 @@ namespace NStringSplitPrivate { using TIteratorOf = typename TIteratorOfImpl<String>::type; template <class String> - struct TIterState { + class TStringSplitter; + + template <class String> + struct TIterState: public TStringBufOf<String> { + public: using TStringBufType = TStringBufOf<String>; using TIterator = TIteratorOf<String>; + friend class TStringSplitter<String>; TIterState(const String& string) noexcept - : TokS() - , TokD() + : TStringBufType() + , DelimiterEnd_(std::begin(string)) + , OriginEnd_(std::end(string)) { - Init(string, THasData<String>()); - } - - operator TStringBufType() const noexcept { - return Token(); } template < typename Other, - class = typename std::enable_if< - std::is_convertible<Other, TStringBufType>::value, - void>::type> + typename = std::enable_if_t< + std::is_convertible<Other, TStringBufType>::value>> bool operator==(const Other& toCompare) const { return TStringBufType(*this) == TStringBufType(toCompare); } - explicit operator bool() const { - return !Empty(); - } - TIterator TokenStart() const noexcept { - return TokS; + return this->begin(); } TIterator TokenDelim() const noexcept { - return TokD; - } - - TIterator TokenEnd() const noexcept { - return B; - } - - Y_PURE_FUNCTION bool Empty() const noexcept { - return TokenStart() == TokenDelim(); + return this->end(); } TStringBufType Token() const noexcept { - return MakeStringBuf<TStringBufType>(TokenStart(), TokenDelim()); + return *this; } TStringBufType Delim() const noexcept { - return MakeStringBuf<TStringBufType>(TokenDelim(), TokenEnd()); + return MakeStringBuf<TStringBufType>(TokenDelim(), DelimiterEnd_); } - TIterator B; - TIterator E; - - TIterator TokS; - TIterator TokD; - private: - void Init(const String& string, std::true_type) { - B = string.data(); - E = string.data() + string.size(); + void UpdateParentBuf(TIterator tokenStart, TIterator tokenDelim) noexcept { + *static_cast<TStringBufType*>(this) = MakeStringBuf<TStringBufType>(tokenStart, tokenDelim); } - void Init(const String& string, std::false_type) { - B = string.begin(); - E = string.end(); + bool DelimiterIsEmpty() const noexcept { + return TokenDelim() == DelimiterEnd_; } + + private: + TIterator DelimiterEnd_; + const TIterator OriginEnd_; }; template <class Base> @@ -843,10 +827,10 @@ namespace NStringSplitPrivate { template <class String> class TStringSplitter { using TStringType = String; - using TStringBufType = TStringBufOf<TStringType>; using TChar = typename TStringType::value_type; - using TIterator = TIteratorOf<TStringType>; using TIteratorState = TIterState<TStringType>; + using TStringBufType = typename TIteratorState::TStringBufType; + using TIterator = typename TIteratorState::TIterator; /** * Base class for all split ranges that actually does the splitting. @@ -857,17 +841,18 @@ namespace NStringSplitPrivate { inline TSplitRangeBase(OtherString&& s, Args&&... args) : String_(std::forward<OtherString>(s)) , State_(String_) - , Delim_(std::forward<Args>(args)...) + , Delimiter_(std::forward<Args>(args)...) { } inline TIteratorState* Next() { - if (State_.TokD == State_.B) { + if (State_.DelimiterIsEmpty()) { return nullptr; } - State_.TokS = State_.B; - State_.TokD = Delim_.Ptr()->Find(State_.B, State_.E); + const auto tokenBegin = State_.DelimiterEnd_; + const auto tokenEnd = Delimiter_.Ptr()->Find(State_.DelimiterEnd_, State_.OriginEnd_); + State_.UpdateParentBuf(tokenBegin, tokenEnd); return &State_; } @@ -875,7 +860,7 @@ namespace NStringSplitPrivate { private: TStringType String_; TIteratorState State_; - DelimStorage Delim_; + DelimStorage Delimiter_; }; template <class Base, class Filter> @@ -903,7 +888,7 @@ namespace NStringSplitPrivate { struct TNonEmptyFilter { template <class TToken> inline bool Accept(const TToken* token) noexcept { - return !token->Empty(); + return !token->empty(); } }; @@ -982,7 +967,8 @@ namespace NStringSplitPrivate { --Count; return false; } else if (Count == 1) { - token->TokD = token->B = token->E; + token->DelimiterEnd_ = token->OriginEnd_; + token->UpdateParentBuf(token->TokenStart(), token->DelimiterEnd_); return false; } return true; |