123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- #pragma once
- #include "udf_type_size_check.h"
- #include <util/generic/strbuf.h>
- #include <algorithm>
- #include <string_view>
- #include <type_traits>
- namespace NYql {
- namespace NUdf {
- //////////////////////////////////////////////////////////////////////////////
- // TStringRefBase
- //////////////////////////////////////////////////////////////////////////////
- template<bool Const>
- class TStringRefBase
- {
- public:
- typedef std::conditional_t<Const, const char*, char*> TDataType;
- protected:
- inline constexpr TStringRefBase() noexcept = default;
- inline constexpr TStringRefBase(TDataType data, ui32 size) noexcept
- : Data_(data)
- , Size_(size)
- {}
- public:
- inline constexpr operator std::string_view() const noexcept { return { Data_, Size_ }; }
- inline constexpr operator TStringBuf() const noexcept { return { Data_, Size_ }; }
- inline constexpr TDataType Data() const noexcept { return Data_; }
- inline constexpr ui32 Size() const noexcept { return Size_; }
- inline constexpr bool Empty() const noexcept { return Size_ == 0; }
- inline constexpr TDataType data() const noexcept { return Data_; }
- inline constexpr ui32 size() const noexcept { return Size_; }
- inline constexpr bool empty() const noexcept { return Size_ == 0; }
- protected:
- TDataType Data_ = nullptr;
- ui32 Size_ = 0U;
- ui8 Reserved_[4] = {};
- };
- //////////////////////////////////////////////////////////////////////////////
- // TMutableStringRef
- //////////////////////////////////////////////////////////////////////////////
- class TMutableStringRef : public TStringRefBase<false>
- {
- public:
- typedef TStringRefBase<false> TBase;
- inline constexpr TMutableStringRef(TDataType data, ui32 size) noexcept
- : TBase(data, size)
- {}
- };
- UDF_ASSERT_TYPE_SIZE(TMutableStringRef, 16);
- //////////////////////////////////////////////////////////////////////////////
- // TStringRef
- //////////////////////////////////////////////////////////////////////////////
- class TStringRef : public TStringRefBase<true>
- {
- public:
- typedef TStringRefBase<true> TBase;
- inline constexpr TStringRef() noexcept = default;
- inline constexpr TStringRef(TDataType data, ui32 size) noexcept
- : TBase(data, size)
- {}
- template<size_t Size>
- inline constexpr TStringRef(const char (&data)[Size]) noexcept
- : TBase(data, Size - 1)
- {}
- inline constexpr TStringRef(const TMutableStringRef& buf) noexcept
- : TBase(buf.Data(), buf.Size())
- {}
- template <typename TStringType>
- inline constexpr TStringRef(const TStringType& buf) noexcept
- : TBase(TGetData<TStringType>::Get(buf), TGetSize<TStringType>::Get(buf))
- {}
- template <size_t size>
- inline static constexpr TStringRef Of(const char(&str)[size]) noexcept {
- return TStringRef(str);
- }
- inline constexpr TStringRef& Trunc(ui32 len) noexcept {
- if (Size_ > len) {
- Size_ = len;
- }
- return *this;
- }
- inline constexpr TStringRef Substring(ui32 start, ui32 count) const noexcept {
- start = std::min(start, Size_);
- count = std::min(count, Size_ - start);
- return TStringRef(Data_ + start, count);
- }
- inline constexpr bool operator==(const TStringRef& rhs) const noexcept {
- return Compare(*this, rhs) == 0;
- }
- inline constexpr bool operator!=(const TStringRef& rhs) const noexcept {
- return Compare(*this, rhs) != 0;
- }
- inline constexpr bool operator<(const TStringRef& rhs) const noexcept {
- return Compare(*this, rhs) < 0;
- }
- inline constexpr bool operator<=(const TStringRef& rhs) const noexcept {
- return Compare(*this, rhs) <= 0;
- }
- inline constexpr bool operator>(const TStringRef& rhs) const noexcept {
- return Compare(*this, rhs) > 0;
- }
- inline constexpr bool operator>=(const TStringRef& rhs) const noexcept {
- return Compare(*this, rhs) >= 0;
- }
- inline constexpr i64 Compare(const TStringRef& rhs) const noexcept {
- return Compare(*this, rhs);
- }
- private:
- inline static constexpr i64 Compare(const TStringRef& s1, const TStringRef& s2) noexcept {
- auto minSize = std::min(s1.Size(), s2.Size());
- if (const auto result = minSize > 0 ? std::memcmp(s1.Data(), s2.Data(), minSize) : 0)
- return result;
- return i64(s1.Size()) - i64(s2.Size());
- }
- Y_HAS_MEMBER(Data);
- Y_HAS_MEMBER(Size);
- template<typename TStringType>
- struct TByData {
- static constexpr auto Get(const TStringType& buf) noexcept {
- return buf.data();
- }
- };
- template<typename TStringType>
- struct TBySize {
- static constexpr auto Get(const TStringType& buf) noexcept {
- return buf.size();
- }
- };
- template<typename TStringType>
- struct TBydata {
- static constexpr auto Get(const TStringType& buf) noexcept {
- return buf.data();
- }
- };
- template<typename TStringType>
- struct TBysize {
- static constexpr auto Get(const TStringType& buf) noexcept {
- return buf.size();
- }
- };
- template<typename TStringType>
- using TGetData = std::conditional_t<THasData<TStringType>::value, TByData<TStringType>, TBydata<TStringType>>;
- template<typename TStringType>
- using TGetSize = std::conditional_t<THasSize<TStringType>::value, TBySize<TStringType>, TBysize<TStringType>>;
- };
- UDF_ASSERT_TYPE_SIZE(TStringRef, 16);
- } // namspace NUdf
- } // namspace NYql
|