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
|
#include "mkql_string_util.h"
namespace NKikimr {
namespace NMiniKQL {
namespace {
ui32 CheckedSum(ui32 one, ui32 two) {
if (ui64(one) + ui64(two) > ui64(std::numeric_limits<ui32>::max()))
ythrow yexception() << "Impossible to concat too large strings " << one << " and " << two << " bytes!";
return one + two;
}
}
NUdf::TUnboxedValuePod AppendString(const NUdf::TUnboxedValuePod value, const NUdf::TStringRef ref)
{
if (!ref.Size())
return value;
const auto& valueRef = value.AsStringRef();
if (!valueRef.Size())
return MakeString(ref);
const auto newSize = CheckedSum(valueRef.Size(), ref.Size());
if (newSize <= NUdf::TUnboxedValuePod::InternalBufferSize) {
auto result = NUdf::TUnboxedValuePod::Embedded(newSize);
const auto buf = result.AsStringRef().Data();
std::memcpy(buf, valueRef.Data(), valueRef.Size());
std::memcpy(buf + valueRef.Size(), ref.Data(), ref.Size());
return result;
} else {
if (value.IsString()) {
auto str = value.AsStringValue();
const ui32 offset = ref.Data() - str.Data();
if (str.Size() == valueRef.Size() + offset) {
if (str.TryExpandOn(ref.Size())) {
std::memcpy(str.Data() + offset + valueRef.Size(), ref.Data(), ref.Size());
return NUdf::TUnboxedValuePod(std::move(str), newSize, offset);
}
}
}
auto data = NUdf::TStringValue::AllocateData(newSize, newSize + newSize / 2);
NUdf::TStringValue str(data);
data->UnRef();
std::memcpy(str.Data(), valueRef.Data(), valueRef.Size());
std::memcpy(str.Data() + valueRef.Size(), ref.Data(), ref.Size());
return NUdf::TUnboxedValuePod(std::move(str));
}
}
NUdf::TUnboxedValuePod PrependString(const NUdf::TStringRef ref, const NUdf::TUnboxedValuePod value)
{
if (!ref.Size())
return value;
const auto& valueRef = value.AsStringRef();
if (!valueRef.Size())
return MakeString(ref);
const auto newSize = CheckedSum(valueRef.Size(), ref.Size());
if (newSize <= NUdf::TUnboxedValuePod::InternalBufferSize) {
auto result = NUdf::TUnboxedValuePod::Embedded(newSize);
const auto buf = result.AsStringRef().Data();
std::memcpy(buf, ref.Data(), ref.Size());
std::memcpy(buf + ref.Size(), valueRef.Data(), valueRef.Size());
return result;
} else {
auto data = NUdf::TStringValue::AllocateData(newSize, newSize + newSize / 2);
NUdf::TStringValue str(data);
data->UnRef();
std::memcpy(str.Data(), ref.Data(), ref.Size());
std::memcpy(str.Data() + ref.Size(), valueRef.Data(), valueRef.Size());
value.DeleteUnreferenced();
return NUdf::TUnboxedValuePod(std::move(str));
}
}
NUdf::TUnboxedValuePod ConcatStrings(const NUdf::TUnboxedValuePod first, const NUdf::TUnboxedValuePod second)
{
const auto& leftRef = first.AsStringRef();
if (!leftRef.Size())
return second;
const auto& rightRef = second.AsStringRef();
if (!rightRef.Size())
return first;
const auto newSize = CheckedSum(leftRef.Size(), rightRef.Size());
if (newSize <= NUdf::TUnboxedValuePod::InternalBufferSize) {
auto result = NUdf::TUnboxedValuePod::Embedded(newSize);
const auto buf = result.AsStringRef().Data();
std::memcpy(buf, leftRef.Data(), leftRef.Size());
std::memcpy(buf + leftRef.Size(), rightRef.Data(), rightRef.Size());
return result;
} else {
if (first.IsString()) {
auto str = first.AsStringValue();
const ui32 offset = leftRef.Data() - str.Data();
if (str.Size() == leftRef.Size() + offset) {
if (str.TryExpandOn(rightRef.Size())) {
std::memcpy(str.Data() + offset + leftRef.Size(), rightRef.Data(), rightRef.Size());
second.DeleteUnreferenced();
return NUdf::TUnboxedValuePod(std::move(str), newSize, offset);
}
}
}
auto data = NUdf::TStringValue::AllocateData(newSize, newSize + newSize / 2);
NUdf::TStringValue str(data);
data->UnRef();
std::memcpy(str.Data(), leftRef.Data(), leftRef.Size());
std::memcpy(str.Data() + leftRef.Size(), rightRef.Data(), rightRef.Size());
second.DeleteUnreferenced();
return NUdf::TUnboxedValuePod(std::move(str));
}
}
NUdf::TUnboxedValuePod SubString(const NUdf::TUnboxedValuePod value, ui32 offset, ui32 size)
{
const auto& ref = value.AsStringRef();
if (size == 0U || ref.Size() <= offset) {
value.DeleteUnreferenced();
return NUdf::TUnboxedValuePod::Zero();
}
if (offset == 0U && ref.Size() <= size)
return value;
if (const auto newSize = std::min(ref.Size() - offset, size); newSize <= NUdf::TUnboxedValuePod::InternalBufferSize) {
auto result = NUdf::TUnboxedValuePod::Embedded(newSize);
std::memcpy(result.AsStringRef().Data(), ref.Data() + offset, newSize);
value.DeleteUnreferenced();
return result;
} else {
auto old = value.AsStringValue();
if (const auto newOffset = ui32(ref.Data() - old.Data()) + offset; NUdf::TUnboxedValuePod::OffsetLimit > newOffset)
return NUdf::TUnboxedValuePod(std::move(old), newSize, newOffset);
auto data = NUdf::TStringValue::AllocateData(newSize, newSize + (newSize >> 1U));
NUdf::TStringValue str(data);
data->UnRef();
std::memcpy(str.Data(), ref.Data() + offset, newSize);
return NUdf::TUnboxedValuePod(std::move(str));
}
}
NUdf::TUnboxedValuePod MakeString(const NUdf::TStringRef ref)
{
if (ref.Size() <= NUdf::TUnboxedValuePod::InternalBufferSize)
return NUdf::TUnboxedValuePod::Embedded(ref);
NUdf::TStringValue str(ref.Size());
std::memcpy(str.Data(), ref.Data(), ref.Size());
return NUdf::TUnboxedValuePod(std::move(str));
}
NUdf::TUnboxedValuePod MakeStringNotFilled(ui32 size, ui32 pad)
{
const auto fullSize = size + pad;
if (fullSize <= NUdf::TUnboxedValuePod::InternalBufferSize)
return NUdf::TUnboxedValuePod::Embedded(size);
return NUdf::TUnboxedValuePod(NUdf::TStringValue(fullSize), size);
}
}
}
|