aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/yson_string/string.cpp
blob: 39302fbb4f415804d888135879c42875dc5f20bd (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
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
#include "string.h"

#include <library/cpp/yt/assert/assert.h>
 
#include <library/cpp/yt/misc/variant.h>

#include <library/cpp/yt/memory/new.h>

namespace NYT::NYson {

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

TYsonStringBuf::TYsonStringBuf()
{
    Type_ = EYsonType::Node; // fake
    Null_ = true;
}

TYsonStringBuf::TYsonStringBuf(const TYsonString& ysonString)
{
    if (ysonString) {
        Data_ = ysonString.AsStringBuf();
        Type_ = ysonString.GetType();
        Null_ = false;
    } else {
        Type_ = EYsonType::Node; // fake
        Null_ = true;
    }
}

TYsonStringBuf::TYsonStringBuf(const TString& data, EYsonType type)
    : TYsonStringBuf(TStringBuf(data), type)
{ }

TYsonStringBuf::TYsonStringBuf(TStringBuf data, EYsonType type)
    : Data_(data)
    , Type_(type)
    , Null_(false)
{ }

TYsonStringBuf::TYsonStringBuf(const char* data, EYsonType type)
    : TYsonStringBuf(TStringBuf(data), type)
{ }

TYsonStringBuf::operator bool() const
{
    return !Null_;
}

TStringBuf TYsonStringBuf::AsStringBuf() const
{
    YT_VERIFY(*this);
    return Data_;
}

EYsonType TYsonStringBuf::GetType() const
{
    YT_VERIFY(*this);
    return Type_;
}

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

TYsonString::TYsonString()
{
    Begin_ = nullptr;
    Size_ = 0;
    Type_ = EYsonType::Node; // fake
}

TYsonString::TYsonString(const TYsonStringBuf& ysonStringBuf)
{
    if (ysonStringBuf) {
        struct TCapturedYsonStringPayload
            : public TRefCounted
            , public TWithExtraSpace<TCapturedYsonStringPayload>
        {
            char* GetData()
            {
                return static_cast<char*>(GetExtraSpacePtr());
            }
        };

        auto data = ysonStringBuf.AsStringBuf();
        auto payload = NewWithExtraSpace<TCapturedYsonStringPayload>(data.length());
        ::memcpy(payload->GetData(), data.data(), data.length());
        Payload_ = payload;
        Begin_ = payload->GetData();
        Size_ = data.Size();
        Type_ = ysonStringBuf.GetType();
    } else {
        Begin_ = nullptr;
        Size_ = 0;
        Type_ = EYsonType::Node; // fake
    }
}

TYsonString::TYsonString(
    TStringBuf data,
    EYsonType type)
    : TYsonString(TYsonStringBuf(data, type))
{ }

#ifdef TSTRING_IS_STD_STRING
TYsonString::TYsonString(
    const TString& data,
    EYsonType type)
    : TYsonString(TYsonStringBuf(data, type))
{ }
#else
TYsonString::TYsonString(
    const TString& data,
    EYsonType type)
{
    // NOTE: CoW TString implementation is assumed
    // Moving the payload MUST NOT invalidate its internal pointers
    Payload_ = data;
    Begin_ = data.data();
    Size_ = data.length();
    Type_ = type;
}
#endif

TYsonString::TYsonString(
    const TSharedRef& data,
    EYsonType type)
{
    Payload_ = data.GetHolder();
    Begin_ = data.Begin();
    Size_ = data.Size();
    Type_ = type;
}

TYsonString::operator bool() const
{
    return !std::holds_alternative<TNullPayload>(Payload_);
}

EYsonType TYsonString::GetType() const
{
    YT_VERIFY(*this);
    return Type_;
}

TStringBuf TYsonString::AsStringBuf() const
{
    YT_VERIFY(*this);
    return TStringBuf(Begin_, Begin_ + Size_);
}

TString TYsonString::ToString() const
{
    return Visit(
        Payload_,
        [] (const TNullPayload&) -> TString {
            YT_ABORT();
        },
        [&] (const TRefCountedPtr&) {
            return TString(AsStringBuf());
        },
        [] (const TString& payload) {
            return payload;
        });
}

size_t TYsonString::ComputeHash() const
{
    return THash<TStringBuf>()(TStringBuf(Begin_, Begin_ + Size_));
}

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

TString ToString(const TYsonString& yson)
{
    return yson.ToString();
}

TString ToString(const TYsonStringBuf& yson)
{
    return TString(yson.AsStringBuf());
}

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

} // namespace NYT::NYson