aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/yson_string/string-inl.h
blob: 09db98881f0a87ef98297c39e704a1ab52222dcf (plain) (blame)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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
#ifndef STRING_INL_H_
#error "Direct inclusion of this file is not allowed, include string.h"
// For the sake of sane code completion.
#include "string.h"
#endif

namespace NYT::NYson {

////////////////////////////////////////////////////////////////////////////////

namespace NDetail {

template <typename TLeft, typename TRight>
bool Equals(const TLeft& lhs, const TRight& rhs)
{
    auto lhsNull = !lhs.operator bool();
    auto rhsNull = !rhs.operator bool();
    if (lhsNull != rhsNull) {
        return false;
    }
    if (lhsNull && rhsNull) {
        return true;
    }
    return
        lhs.AsStringBuf() == rhs.AsStringBuf() &&
        lhs.GetType() == rhs.GetType();
}

class TYsonStringHolder
    : public TSharedRangeHolder
    , public TWithExtraSpace<TYsonStringHolder>
{
public:
    explicit TYsonStringHolder(size_t size)
        : Size_(size)
    { }

    char* GetData()
    {
        return static_cast<char*>(GetExtraSpacePtr());
    }

    // TSharedRangeHolder overrides.
    std::optional<size_t> GetTotalByteSize() const override
    {
        return Size_ + sizeof(TYsonStringHolder);
    }

    static TIntrusivePtr<TYsonStringHolder> Allocate(size_t size)
    {
        return NewWithExtraSpace<TYsonStringHolder>(size, size);
    }

private:
    const size_t Size_;
};

} // namespace NDetail

inline bool operator == (const TYsonString& lhs, const TYsonString& rhs)
{
    return NDetail::Equals(lhs, rhs);
}

inline bool operator == (const TYsonString& lhs, const TYsonStringBuf& rhs)
{
    return NDetail::Equals(lhs, rhs);
}

inline bool operator == (const TYsonStringBuf& lhs, const TYsonString& rhs)
{
    return NDetail::Equals(lhs, rhs);
}

inline bool operator == (const TYsonStringBuf& lhs, const TYsonStringBuf& rhs)
{
    return NDetail::Equals(lhs, rhs);
}

inline bool operator != (const TYsonString& lhs, const TYsonString& rhs)
{
    return !(lhs == rhs);
}

inline bool operator != (const TYsonString& lhs, const TYsonStringBuf& rhs)
{
    return !(lhs == rhs);
}

inline bool operator != (const TYsonStringBuf& lhs, const TYsonString& rhs)
{
    return !(lhs == rhs);
}

inline bool operator != (const TYsonStringBuf& lhs, const TYsonStringBuf& rhs)
{
    return !(lhs == rhs);
}

////////////////////////////////////////////////////////////////////////////////

} // namespace NYT::NYson

//! A hasher for TYsonString
template <>
struct THash<NYT::NYson::TYsonString>
{
    size_t operator () (const NYT::NYson::TYsonString& str) const
    {
        return str.ComputeHash();
    }
};

//! A hasher for TYsonStringBuf
template <>
struct THash<NYT::NYson::TYsonStringBuf>
{
    size_t operator () (const NYT::NYson::TYsonStringBuf& str) const
    {
        return THash<TStringBuf>()(str.AsStringBuf());
    }
};