diff options
author | Anton Samokhvalov <[email protected]> | 2022-02-10 16:45:17 +0300 |
---|---|---|
committer | Daniil Cherednik <[email protected]> | 2022-02-10 16:45:17 +0300 |
commit | d3a398281c6fd1d3672036cb2d63f842d2cb28c5 (patch) | |
tree | dd4bd3ca0f36b817e96812825ffaf10d645803f2 /util/string/strip.h | |
parent | 72cb13b4aff9bc9cf22e49251bc8fd143f82538f (diff) |
Restoring authorship annotation for Anton Samokhvalov <[email protected]>. Commit 2 of 2.
Diffstat (limited to 'util/string/strip.h')
-rw-r--r-- | util/string/strip.h | 142 |
1 files changed, 71 insertions, 71 deletions
diff --git a/util/string/strip.h b/util/string/strip.h index f797dcea098..d5ef6da96db 100644 --- a/util/string/strip.h +++ b/util/string/strip.h @@ -1,11 +1,11 @@ -#pragma once - -#include "ascii.h" - +#pragma once + +#include "ascii.h" + #include <util/generic/string.h> -#include <util/generic/strbuf.h> +#include <util/generic/strbuf.h> #include <utility> - + template <class It> struct TIsAsciiSpaceAdapter { bool operator()(const It& it) const noexcept { @@ -15,7 +15,7 @@ struct TIsAsciiSpaceAdapter { template <class It> TIsAsciiSpaceAdapter<It> IsAsciiSpaceAdapter(It) { - return {}; + return {}; } template <class TChar> @@ -24,47 +24,47 @@ struct TEqualsStripAdapter { : Ch(ch) { } - + template <class It> bool operator()(const It& it) const noexcept { return *it == Ch; } - const TChar Ch; + const TChar Ch; }; template <class TChar> TEqualsStripAdapter<TChar> EqualsStripAdapter(TChar ch) { - return {ch}; + return {ch}; } -template <class It, class TStripCriterion> +template <class It, class TStripCriterion> inline void StripRangeBegin(It& b, const It& e, TStripCriterion&& criterion) noexcept { while (b < e && criterion(b)) { - ++b; - } -} - -template <class It> + ++b; + } +} + +template <class It> inline void StripRangeBegin(It& b, const It& e) noexcept { StripRangeBegin(b, e, IsAsciiSpaceAdapter(b)); } -template <class It, class TStripCriterion> +template <class It, class TStripCriterion> inline void StripRangeEnd(const It& b, It& e, TStripCriterion&& criterion) noexcept { while (b < e && criterion(e - 1)) { - --e; - } -} - -template <class It> + --e; + } +} + +template <class It> inline void StripRangeEnd(const It& b, It& e) noexcept { StripRangeEnd(b, e, IsAsciiSpaceAdapter(b)); } template <bool stripBeg, bool stripEnd> struct TStripImpl { - template <class It, class TStripCriterion> + template <class It, class TStripCriterion> static inline bool StripRange(It& b, It& e, TStripCriterion&& criterion) noexcept { const size_t oldLen = e - b; @@ -80,10 +80,10 @@ struct TStripImpl { return newLen != oldLen; } - template <class T, class TStripCriterion> - static inline bool StripString(const T& from, T& to, TStripCriterion&& criterion) { - auto b = from.begin(); - auto e = from.end(); + template <class T, class TStripCriterion> + static inline bool StripString(const T& from, T& to, TStripCriterion&& criterion) { + auto b = from.begin(); + auto e = from.end(); if (StripRange(b, e, criterion)) { to = T(b, e - b); @@ -96,8 +96,8 @@ struct TStripImpl { return false; } - template <class T, class TStripCriterion> - static inline T StripString(const T& from, TStripCriterion&& criterion) { + template <class T, class TStripCriterion> + static inline T StripString(const T& from, TStripCriterion&& criterion) { T ret; StripString(from, ret, criterion); return ret; @@ -109,49 +109,49 @@ struct TStripImpl { } }; -template <class It, class TStripCriterion> +template <class It, class TStripCriterion> inline bool StripRange(It& b, It& e, TStripCriterion&& criterion) noexcept { return TStripImpl<true, true>::StripRange(b, e, criterion); -} - -template <class It> +} + +template <class It> inline bool StripRange(It& b, It& e) noexcept { return StripRange(b, e, IsAsciiSpaceAdapter(b)); } -template <class It, class TStripCriterion> +template <class It, class TStripCriterion> inline bool Strip(It& b, size_t& len, TStripCriterion&& criterion) noexcept { - It e = b + len; - + It e = b + len; + if (StripRange(b, e, criterion)) { - len = e - b; - - return true; - } - - return false; -} - + len = e - b; + + return true; + } + + return false; +} + template <class It> inline bool Strip(It& b, size_t& len) noexcept { return Strip(b, len, IsAsciiSpaceAdapter(b)); } -template <class T, class TStripCriterion> -static inline bool StripString(const T& from, T& to, TStripCriterion&& criterion) { +template <class T, class TStripCriterion> +static inline bool StripString(const T& from, T& to, TStripCriterion&& criterion) { return TStripImpl<true, true>::StripString(from, to, criterion); -} - -template <class T> +} + +template <class T> static inline bool StripString(const T& from, T& to) { return StripString(from, to, IsAsciiSpaceAdapter(from.begin())); } -template <class T, class TStripCriterion> -static inline T StripString(const T& from, TStripCriterion&& criterion) { +template <class T, class TStripCriterion> +static inline T StripString(const T& from, TStripCriterion&& criterion) { return TStripImpl<true, true>::StripString(from, criterion); -} - +} + template <class T> static inline T StripString(const T& from) { return TStripImpl<true, true>::StripString(from); @@ -167,13 +167,13 @@ static inline T StripStringRight(const T& from) { return TStripImpl<false, true>::StripString(from); } -template <class T, class TStripCriterion> -static inline T StripStringLeft(const T& from, TStripCriterion&& criterion) { +template <class T, class TStripCriterion> +static inline T StripStringLeft(const T& from, TStripCriterion&& criterion) { return TStripImpl<true, false>::StripString(from, criterion); } -template <class T, class TStripCriterion> -static inline T StripStringRight(const T& from, TStripCriterion&& criterion) { +template <class T, class TStripCriterion> +static inline T StripStringRight(const T& from, TStripCriterion&& criterion) { return TStripImpl<false, true>::StripString(from, criterion); } @@ -181,21 +181,21 @@ static inline T StripStringRight(const T& from, TStripCriterion&& criterion) { static inline bool Strip(const TString& from, TString& to) { return StripString(from, to); } - + /// Removes leading and trailing spaces from the string. inline TString& StripInPlace(TString& s) { Strip(s, s); return s; } - + /// Returns a copy of the given string with removed leading and trailing spaces. inline TString Strip(const TString& s) Y_WARN_UNUSED_RESULT; inline TString Strip(const TString& s) { TString ret = s; Strip(ret, ret); - return ret; -} - + return ret; +} + template <class TChar, class TWhitespaceFunc> size_t CollapseImpl(TChar* s, size_t n, const TWhitespaceFunc& isWhitespace) { size_t newLen = 0; @@ -231,27 +231,27 @@ bool CollapseImpl(const TStringType& from, TStringType& to, size_t maxLen, const } bool Collapse(const TString& from, TString& to, size_t maxLen = 0); - + /// Replaces several consequtive space symbols with one (processing is limited to maxLen bytes) inline TString& CollapseInPlace(TString& s, size_t maxLen = 0) { Collapse(s, s, maxLen); - return s; -} + return s; +} /// Replaces several consequtive space symbols with one (processing is limited to maxLen bytes) inline TString Collapse(const TString& s, size_t maxLen = 0) Y_WARN_UNUSED_RESULT; inline TString Collapse(const TString& s, size_t maxLen) { TString ret; Collapse(s, ret, maxLen); - return ret; -} - + return ret; +} + void CollapseText(const TString& from, TString& to, size_t maxLen); - + /// The same as Collapse() + truncates the string to maxLen. /// @details An ellipsis is inserted at the end of the truncated line. inline void CollapseText(TString& s, size_t maxLen) { TString to; CollapseText(s, to, maxLen); - s = to; -} + s = to; +} |