diff options
Diffstat (limited to 'util/string')
| -rw-r--r-- | util/string/ascii.cpp | 40 | ||||
| -rw-r--r-- | util/string/ascii.h | 83 | ||||
| -rw-r--r-- | util/string/strip.h | 74 | ||||
| -rw-r--r-- | util/string/strip_ut.cpp | 36 |
4 files changed, 133 insertions, 100 deletions
diff --git a/util/string/ascii.cpp b/util/string/ascii.cpp index c9779c9dfaf..c18997b4a2c 100644 --- a/util/string/ascii.cpp +++ b/util/string/ascii.cpp @@ -3,46 +3,6 @@ #include <util/system/yassert.h> #include <util/system/compat.h> -// clang-format off -extern const unsigned char NPrivate::ASCII_CLASS[256] = { - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x01, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, - 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, - 0x80, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, - 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x80, 0x80, 0x80, 0x80, 0x80, - 0x80, 0x74, 0x74, 0x74, 0x74, 0x74, 0x74, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, - 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x80, 0x80, 0x80, 0x80, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, -}; - -extern const unsigned char NPrivate::ASCII_LOWER[256] = { - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, - 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, - 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, - 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, - 64, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, - 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 91, 92, 93, 94, 95, - 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, - 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, - 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, - 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, - 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, - 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, - 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, - 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, - 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, - 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, -}; -// clang-format on - int AsciiCompareIgnoreCase(const TStringBuf s1, const TStringBuf s2) noexcept { if (s1.size() <= s2.size()) { if (int cmp = ::strnicmp(s1.data(), s2.data(), s1.size())) { diff --git a/util/string/ascii.h b/util/string/ascii.h index 2d199958665..3cda6b40f16 100644 --- a/util/string/ascii.h +++ b/util/string/ascii.h @@ -23,8 +23,45 @@ namespace NPrivate { CC_PUNCT = 128, }; - extern const unsigned char ASCII_CLASS[256]; - extern const unsigned char ASCII_LOWER[256]; + // clang-format off + inline constexpr unsigned char ASCII_CLASS[256] = { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x01, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, + 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x68, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, + 0x80, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, + 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x80, 0x80, 0x80, 0x80, 0x80, + 0x80, 0x74, 0x74, 0x74, 0x74, 0x74, 0x74, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, + 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x80, 0x80, 0x80, 0x80, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + }; + + inline constexpr unsigned char ASCII_LOWER[256] = { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, + 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, + 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, + 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, + 64, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, + 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 91, 92, 93, 94, 95, + 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, + 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, + 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, + 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, + 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, + 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, + 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, + 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, + 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, + 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, + }; + // clang-format on template <class T> struct TDereference { @@ -42,7 +79,7 @@ namespace NPrivate { using TDereferenced = typename TDereference<T>::type; template <class T> - bool RangeOk(T c) noexcept { + constexpr bool RangeOk(T c) noexcept { static_assert(std::is_integral<T>::value, "Integral type character expected"); if (sizeof(T) == 1) { @@ -64,96 +101,96 @@ constexpr bool IsAscii(const int c) noexcept { return !(c & ~0x7f); } -inline bool IsAsciiSpace(unsigned char c) { +constexpr bool IsAsciiSpace(unsigned char c) { return ::NPrivate::ASCII_CLASS[c] & ::NPrivate::CC_SPACE; } -inline bool IsAsciiUpper(unsigned char c) { +constexpr bool IsAsciiUpper(unsigned char c) { return ::NPrivate::ASCII_CLASS[c] & ::NPrivate::CC_UPPER; } -inline bool IsAsciiLower(unsigned char c) { +constexpr bool IsAsciiLower(unsigned char c) { return ::NPrivate::ASCII_CLASS[c] & ::NPrivate::CC_LOWER; } -inline bool IsAsciiDigit(unsigned char c) { +constexpr bool IsAsciiDigit(unsigned char c) { return ::NPrivate::ASCII_CLASS[c] & ::NPrivate::CC_DIGIT; } -inline bool IsAsciiAlpha(unsigned char c) { +constexpr bool IsAsciiAlpha(unsigned char c) { return ::NPrivate::ASCII_CLASS[c] & ::NPrivate::CC_ALPHA; } -inline bool IsAsciiAlnum(unsigned char c) { +constexpr bool IsAsciiAlnum(unsigned char c) { return ::NPrivate::ASCII_CLASS[c] & ::NPrivate::CC_ALNUM; } -inline bool IsAsciiHex(unsigned char c) { +constexpr bool IsAsciiHex(unsigned char c) { return ::NPrivate::ASCII_CLASS[c] & ::NPrivate::CC_ISHEX; } -inline bool IsAsciiPunct(unsigned char c) { +constexpr bool IsAsciiPunct(unsigned char c) { return ::NPrivate::ASCII_CLASS[c] & ::NPrivate::CC_PUNCT; } // some overloads template <class T> -inline bool IsAsciiSpace(T c) { +constexpr bool IsAsciiSpace(T c) { return ::NPrivate::RangeOk(c) && IsAsciiSpace(static_cast<unsigned char>(c)); } template <class T> -inline bool IsAsciiUpper(T c) { +constexpr bool IsAsciiUpper(T c) { return ::NPrivate::RangeOk(c) && IsAsciiUpper(static_cast<unsigned char>(c)); } template <class T> -inline bool IsAsciiLower(T c) { +constexpr bool IsAsciiLower(T c) { return ::NPrivate::RangeOk(c) && IsAsciiLower(static_cast<unsigned char>(c)); } template <class T> -inline bool IsAsciiDigit(T c) { +constexpr bool IsAsciiDigit(T c) { return ::NPrivate::RangeOk(c) && IsAsciiDigit(static_cast<unsigned char>(c)); } template <class T> -inline bool IsAsciiAlpha(T c) { +constexpr bool IsAsciiAlpha(T c) { return ::NPrivate::RangeOk(c) && IsAsciiAlpha(static_cast<unsigned char>(c)); } template <class T> -inline bool IsAsciiAlnum(T c) { +constexpr bool IsAsciiAlnum(T c) { return ::NPrivate::RangeOk(c) && IsAsciiAlnum(static_cast<unsigned char>(c)); } template <class T> -inline bool IsAsciiHex(T c) { +constexpr bool IsAsciiHex(T c) { return ::NPrivate::RangeOk(c) && IsAsciiHex(static_cast<unsigned char>(c)); } template <class T> -inline bool IsAsciiPunct(T c) { +constexpr bool IsAsciiPunct(T c) { return ::NPrivate::RangeOk(c) && IsAsciiPunct(static_cast<unsigned char>(c)); } // some extra helpers -inline ui8 AsciiToLower(ui8 c) noexcept { +constexpr ui8 AsciiToLower(ui8 c) noexcept { return ::NPrivate::ASCII_LOWER[c]; } -inline char AsciiToLower(char c) noexcept { +constexpr char AsciiToLower(char c) noexcept { return (char)AsciiToLower((ui8)c); } template <class T> -inline ::NPrivate::TDereferenced<T> AsciiToLower(T c) noexcept { +constexpr ::NPrivate::TDereferenced<T> AsciiToLower(T c) noexcept { return (c >= 0 && c <= 127) ? (::NPrivate::TDereferenced<T>)AsciiToLower((ui8)c) : c; } template <class T> -inline ::NPrivate::TDereferenced<T> AsciiToUpper(T c) noexcept { +constexpr ::NPrivate::TDereferenced<T> AsciiToUpper(T c) noexcept { return IsAsciiLower(c) ? (c + ('A' - 'a')) : c; } diff --git a/util/string/strip.h b/util/string/strip.h index f4aecd4703f..c50cb3023ea 100644 --- a/util/string/strip.h +++ b/util/string/strip.h @@ -9,25 +9,25 @@ template <class It> struct TIsAsciiSpaceAdapter { - bool operator()(const It& it) const noexcept { + constexpr bool operator()(const It& it) const noexcept { return IsAsciiSpace(*it); } }; template <class It> -TIsAsciiSpaceAdapter<It> IsAsciiSpaceAdapter(It) { +constexpr TIsAsciiSpaceAdapter<It> IsAsciiSpaceAdapter(It) { return {}; } template <class TChar> struct TEqualsStripAdapter { - TEqualsStripAdapter(TChar ch) + constexpr TEqualsStripAdapter(TChar ch) : Ch(ch) { } template <class It> - bool operator()(const It& it) const noexcept { + constexpr bool operator()(const It& it) const noexcept { return *it == Ch; } @@ -35,38 +35,38 @@ struct TEqualsStripAdapter { }; template <class TChar> -TEqualsStripAdapter<TChar> EqualsStripAdapter(TChar ch) { +constexpr TEqualsStripAdapter<TChar> EqualsStripAdapter(TChar ch) { return {ch}; } template <class It, class TStripCriterion> -inline void StripRangeBegin(It& b, const It& e, TStripCriterion&& criterion) noexcept { +constexpr void StripRangeBegin(It& b, const It& e, TStripCriterion&& criterion) noexcept { while (b < e && criterion(b)) { ++b; } } template <class It> -inline void StripRangeBegin(It& b, const It& e) noexcept { +constexpr void StripRangeBegin(It& b, const It& e) noexcept { StripRangeBegin(b, e, IsAsciiSpaceAdapter(b)); } template <class It, class TStripCriterion> -inline void StripRangeEnd(const It& b, It& e, TStripCriterion&& criterion) noexcept { +constexpr void StripRangeEnd(const It& b, It& e, TStripCriterion&& criterion) noexcept { while (b < e && criterion(e - 1)) { --e; } } template <class It> -inline void StripRangeEnd(const It& b, It& e) noexcept { +constexpr 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> - static inline bool StripRange(It& b, It& e, TStripCriterion&& criterion) noexcept { + static constexpr bool StripRange(It& b, It& e, TStripCriterion&& criterion) noexcept { const size_t oldLen = e - b; if (stripBeg) { @@ -82,7 +82,7 @@ struct TStripImpl { } template <class T, class TStripCriterion> - static inline bool StripString(const T& from, T& to, TStripCriterion&& criterion) { + static constexpr bool StripString(const T& from, T& to, TStripCriterion&& criterion) { auto b = from.begin(); auto e = from.end(); @@ -102,30 +102,30 @@ struct TStripImpl { } template <class T, class TStripCriterion> - [[nodiscard]] static inline T StripString(const T& from, TStripCriterion&& criterion) { + [[nodiscard]] static constexpr T StripString(const T& from, TStripCriterion&& criterion) { T ret; StripString(from, ret, criterion); return ret; } template <class T> - [[nodiscard]] static inline T StripString(const T& from) { + [[nodiscard]] static constexpr T StripString(const T& from) { return StripString(from, IsAsciiSpaceAdapter(from.begin())); } }; template <class It, class TStripCriterion> -inline bool StripRange(It& b, It& e, TStripCriterion&& criterion) noexcept { +constexpr bool StripRange(It& b, It& e, TStripCriterion&& criterion) noexcept { return TStripImpl<true, true>::StripRange(b, e, criterion); } template <class It> -inline bool StripRange(It& b, It& e) noexcept { +constexpr bool StripRange(It& b, It& e) noexcept { return StripRange(b, e, IsAsciiSpaceAdapter(b)); } template <class It, class TStripCriterion> -inline bool Strip(It& b, size_t& len, TStripCriterion&& criterion) noexcept { +constexpr bool Strip(It& b, size_t& len, TStripCriterion&& criterion) noexcept { It e = b + len; if (StripRange(b, e, criterion)) { @@ -138,107 +138,107 @@ inline bool Strip(It& b, size_t& len, TStripCriterion&& criterion) noexcept { } template <class It> -inline bool Strip(It& b, size_t& len) noexcept { +constexpr 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) { +static constexpr bool StripString(const T& from, T& to, TStripCriterion&& criterion) { return TStripImpl<true, true>::StripString(from, to, criterion); } template <class T> -static inline bool StripString(const T& from, T& to) { +static constexpr bool StripString(const T& from, T& to) { return StripString(from, to, IsAsciiSpaceAdapter(from.begin())); } template <class T, class TStripCriterion> -[[nodiscard]] static inline T StripString(const T& from, TStripCriterion&& criterion) { +[[nodiscard]] static constexpr T StripString(const T& from, TStripCriterion&& criterion) { return TStripImpl<true, true>::StripString(from, criterion); } template <typename TChar, typename TTraits, class TStripCriterion> -[[nodiscard]] static inline std::enable_if_t<std::is_invocable_v<TStripCriterion, typename TBasicStringBuf<TChar, TTraits>::iterator>, TBasicStringBuf<TChar, TTraits>> StripString(const TBasicStringBuf<TChar, TTraits> from Y_LIFETIME_BOUND, TStripCriterion&& criterion) { +[[nodiscard]] static constexpr std::enable_if_t<std::is_invocable_v<TStripCriterion, typename TBasicStringBuf<TChar, TTraits>::iterator>, TBasicStringBuf<TChar, TTraits>> StripString(const TBasicStringBuf<TChar, TTraits> from Y_LIFETIME_BOUND, TStripCriterion&& criterion) { return TStripImpl<true, true>::StripString(from, criterion); } template <typename TChar, typename TTraits, class TStripCriterion> -[[nodiscard]] static inline std::enable_if_t<std::is_invocable_v<TStripCriterion, typename std::basic_string_view<TChar, TTraits>::iterator>, std::basic_string_view<TChar, TTraits>> StripString(const std::basic_string_view<TChar, TTraits> from Y_LIFETIME_BOUND, TStripCriterion&& criterion) { +[[nodiscard]] static constexpr std::enable_if_t<std::is_invocable_v<TStripCriterion, typename std::basic_string_view<TChar, TTraits>::iterator>, std::basic_string_view<TChar, TTraits>> StripString(const std::basic_string_view<TChar, TTraits> from Y_LIFETIME_BOUND, TStripCriterion&& criterion) { return TStripImpl<true, true>::StripString(from, criterion); } template <class T> -[[nodiscard]] static inline T StripString(const T& from) { +[[nodiscard]] static constexpr T StripString(const T& from) { return TStripImpl<true, true>::StripString(from); } template <typename TChar, typename TTraits> -[[nodiscard]] static inline TBasicStringBuf<TChar, TTraits> StripString(const TBasicStringBuf<TChar, TTraits> from Y_LIFETIME_BOUND) { +[[nodiscard]] static constexpr TBasicStringBuf<TChar, TTraits> StripString(const TBasicStringBuf<TChar, TTraits> from Y_LIFETIME_BOUND) { return TStripImpl<true, true>::StripString(from); } template <typename TChar, typename TTraits> -[[nodiscard]] static inline std::basic_string_view<TChar, TTraits> StripString(const std::basic_string_view<TChar, TTraits> from Y_LIFETIME_BOUND) { +[[nodiscard]] static constexpr std::basic_string_view<TChar, TTraits> StripString(const std::basic_string_view<TChar, TTraits> from Y_LIFETIME_BOUND) { return TStripImpl<true, true>::StripString(from); } template <class T> -[[nodiscard]] static inline T StripStringLeft(const T& from) { +[[nodiscard]] static constexpr T StripStringLeft(const T& from) { return TStripImpl<true, false>::StripString(from); } template <typename TChar, typename TTraits> -[[nodiscard]] static inline TBasicStringBuf<TChar, TTraits> StripStringLeft(const TBasicStringBuf<TChar, TTraits> from Y_LIFETIME_BOUND) { +[[nodiscard]] static constexpr TBasicStringBuf<TChar, TTraits> StripStringLeft(const TBasicStringBuf<TChar, TTraits> from Y_LIFETIME_BOUND) { return TStripImpl<true, false>::StripString(from); } template <typename TChar, typename TTraits> -[[nodiscard]] static inline std::basic_string_view<TChar, TTraits> StripStringLeft(const std::basic_string_view<TChar, TTraits> from Y_LIFETIME_BOUND) { +[[nodiscard]] static constexpr std::basic_string_view<TChar, TTraits> StripStringLeft(const std::basic_string_view<TChar, TTraits> from Y_LIFETIME_BOUND) { return TStripImpl<true, false>::StripString(from); } template <class T> -[[nodiscard]] static inline T StripStringRight(const T& from) { +[[nodiscard]] static constexpr T StripStringRight(const T& from) { return TStripImpl<false, true>::StripString(from); } template <typename TChar, typename TTraits> -[[nodiscard]] static inline TBasicStringBuf<TChar, TTraits> StripStringRight(const TBasicStringBuf<TChar, TTraits> from Y_LIFETIME_BOUND) { +[[nodiscard]] static constexpr TBasicStringBuf<TChar, TTraits> StripStringRight(const TBasicStringBuf<TChar, TTraits> from Y_LIFETIME_BOUND) { return TStripImpl<false, true>::StripString(from); } template <typename TChar, typename TTraits> -[[nodiscard]] static inline std::basic_string_view<TChar, TTraits> StripStringRight(const std::basic_string_view<TChar, TTraits> from Y_LIFETIME_BOUND) { +[[nodiscard]] static constexpr std::basic_string_view<TChar, TTraits> StripStringRight(const std::basic_string_view<TChar, TTraits> from Y_LIFETIME_BOUND) { return TStripImpl<false, true>::StripString(from); } template <class T, class TStripCriterion> -[[nodiscard]] static inline T StripStringLeft(const T& from, TStripCriterion&& criterion) { +[[nodiscard]] static constexpr T StripStringLeft(const T& from, TStripCriterion&& criterion) { return TStripImpl<true, false>::StripString(from, criterion); } template <typename TChar, typename TTraits, class TStripCriterion> -[[nodiscard]] static inline std::enable_if_t<std::is_invocable_v<TStripCriterion, typename TBasicStringBuf<TChar, TTraits>::iterator>, TBasicStringBuf<TChar, TTraits>> StripStringLeft(const TBasicStringBuf<TChar, TTraits> from Y_LIFETIME_BOUND, TStripCriterion&& criterion) { +[[nodiscard]] static constexpr std::enable_if_t<std::is_invocable_v<TStripCriterion, typename TBasicStringBuf<TChar, TTraits>::iterator>, TBasicStringBuf<TChar, TTraits>> StripStringLeft(const TBasicStringBuf<TChar, TTraits> from Y_LIFETIME_BOUND, TStripCriterion&& criterion) { return TStripImpl<true, false>::StripString(from, criterion); } template <typename TChar, typename TTraits, class TStripCriterion> -[[nodiscard]] static inline std::enable_if_t<std::is_invocable_v<TStripCriterion, typename std::basic_string_view<TChar, TTraits>::iterator>, std::basic_string_view<TChar, TTraits>> StripStringLeft(const std::basic_string_view<TChar, TTraits> from Y_LIFETIME_BOUND, TStripCriterion&& criterion) { +[[nodiscard]] static constexpr std::enable_if_t<std::is_invocable_v<TStripCriterion, typename std::basic_string_view<TChar, TTraits>::iterator>, std::basic_string_view<TChar, TTraits>> StripStringLeft(const std::basic_string_view<TChar, TTraits> from Y_LIFETIME_BOUND, TStripCriterion&& criterion) { return TStripImpl<true, false>::StripString(from, criterion); } template <class T, class TStripCriterion> -[[nodiscard]] static inline T StripStringRight(const T& from, TStripCriterion&& criterion) { +[[nodiscard]] static constexpr T StripStringRight(const T& from, TStripCriterion&& criterion) { return TStripImpl<false, true>::StripString(from, criterion); } template <typename TChar, typename TTraits, class TStripCriterion> -[[nodiscard]] static inline std::enable_if_t<std::is_invocable_v<TStripCriterion, typename TBasicStringBuf<TChar, TTraits>::iterator>, TBasicStringBuf<TChar, TTraits>> StripStringRight(const TBasicStringBuf<TChar, TTraits> from Y_LIFETIME_BOUND, TStripCriterion&& criterion) { +[[nodiscard]] static constexpr std::enable_if_t<std::is_invocable_v<TStripCriterion, typename TBasicStringBuf<TChar, TTraits>::iterator>, TBasicStringBuf<TChar, TTraits>> StripStringRight(const TBasicStringBuf<TChar, TTraits> from Y_LIFETIME_BOUND, TStripCriterion&& criterion) { return TStripImpl<false, true>::StripString(from, criterion); } template <typename TChar, typename TTraits, class TStripCriterion> -[[nodiscard]] static inline std::enable_if_t<std::is_invocable_v<TStripCriterion, typename std::basic_string_view<TChar, TTraits>::iterator>, std::basic_string_view<TChar, TTraits>> StripStringRight(const std::basic_string_view<TChar, TTraits> from Y_LIFETIME_BOUND, TStripCriterion&& criterion) { +[[nodiscard]] static constexpr std::enable_if_t<std::is_invocable_v<TStripCriterion, typename std::basic_string_view<TChar, TTraits>::iterator>, std::basic_string_view<TChar, TTraits>> StripStringRight(const std::basic_string_view<TChar, TTraits> from Y_LIFETIME_BOUND, TStripCriterion&& criterion) { return TStripImpl<false, true>::StripString(from, criterion); } diff --git a/util/string/strip_ut.cpp b/util/string/strip_ut.cpp index 37e5a4f04a0..1d47e21e23c 100644 --- a/util/string/strip_ut.cpp +++ b/util/string/strip_ut.cpp @@ -4,7 +4,43 @@ #include <util/charset/wide.h> +#include <string_view> + Y_UNIT_TEST_SUITE(TStripStringTest) { + Y_UNIT_TEST(TestConstexprStrip) { + static constexpr std::string_view stripped = StripString<std::string_view>(" \n\t abc def \r\n "); + static_assert(stripped == "abc def"); + + static constexpr std::string_view strippedLeft = StripStringLeft(std::string_view(" \n abc \n ")); + static_assert(strippedLeft == "abc \n "); + + static constexpr std::string_view strippedRight = StripStringRight(std::string_view(" \n abc \n ")); + static_assert(strippedRight == " \n abc"); + + static constexpr std::string_view empty = StripString<std::string_view>(" \n\t\r "); + static_assert(empty.empty()); + + static constexpr std::string_view unchanged = StripString<std::string_view>("abc"); + static_assert(unchanged == "abc"); + + UNIT_ASSERT_VALUES_EQUAL(stripped, "abc def"); + UNIT_ASSERT_VALUES_EQUAL(strippedLeft, "abc \n "); + UNIT_ASSERT_VALUES_EQUAL(strippedRight, " \n abc"); + } + + Y_UNIT_TEST(TestConstexprCustomStrip) { + static constexpr std::string_view stripped = StripString(std::string_view("//abc//"), EqualsStripAdapter('/')); + static_assert(stripped == "abc"); + + static constexpr std::string_view strippedLeft = StripStringLeft(std::string_view("//abc//"), EqualsStripAdapter('/')); + static_assert(strippedLeft == "abc//"); + + static constexpr std::string_view strippedRight = StripStringRight(std::string_view("//abc//"), EqualsStripAdapter('/')); + static_assert(strippedRight == "//abc"); + + UNIT_ASSERT_VALUES_EQUAL(stripped, "abc"); + } + struct TStripTest { TStringBuf Str; TStringBuf StripLeftRes; |
