-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathstring_value.h
More file actions
389 lines (301 loc) · 12.5 KB
/
string_value.h
File metadata and controls
389 lines (301 loc) · 12.5 KB
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
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// IWYU pragma: private, include "common/value.h"
// IWYU pragma: friend "common/value.h"
#ifndef THIRD_PARTY_CEL_CPP_COMMON_VALUES_STRING_VALUE_H_
#define THIRD_PARTY_CEL_CPP_COMMON_VALUES_STRING_VALUE_H_
#include <cstddef>
#include <ostream>
#include <string>
#include <type_traits>
#include <utility>
#include "absl/base/attributes.h"
#include "absl/base/nullability.h"
#include "absl/log/absl_check.h"
#include "absl/status/status.h"
#include "absl/strings/cord.h"
#include "absl/strings/string_view.h"
#include "absl/types/optional.h"
#include "common/allocator.h"
#include "common/arena.h"
#include "common/internal/byte_string.h"
#include "common/memory.h"
#include "common/type.h"
#include "common/value_kind.h"
#include "common/values/values.h"
#include "google/protobuf/arena.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/message.h"
namespace cel {
class Value;
class StringValue;
class TypeManager;
namespace common_internal {
absl::string_view LegacyStringValue(const StringValue& value, bool stable,
absl::Nonnull<google::protobuf::Arena*> arena);
} // namespace common_internal
// `StringValue` represents values of the primitive `string` type.
class StringValue final : private common_internal::ValueMixin<StringValue> {
public:
static constexpr ValueKind kKind = ValueKind::kString;
static StringValue From(absl::Nullable<const char*> value,
absl::Nonnull<google::protobuf::Arena*> arena
ABSL_ATTRIBUTE_LIFETIME_BOUND);
static StringValue From(absl::string_view value,
absl::Nonnull<google::protobuf::Arena*> arena
ABSL_ATTRIBUTE_LIFETIME_BOUND);
static StringValue From(const absl::Cord& value);
static StringValue From(std::string&& value,
absl::Nonnull<google::protobuf::Arena*> arena
ABSL_ATTRIBUTE_LIFETIME_BOUND);
static StringValue Wrap(absl::string_view value,
absl::Nullable<google::protobuf::Arena*> arena
ABSL_ATTRIBUTE_LIFETIME_BOUND);
static StringValue Wrap(absl::string_view value);
static StringValue Wrap(const absl::Cord& value);
static StringValue Wrap(std::string&& value) = delete;
static StringValue Wrap(std::string&& value,
absl::Nullable<google::protobuf::Arena*> arena
ABSL_ATTRIBUTE_LIFETIME_BOUND) = delete;
static StringValue Concat(const StringValue& lhs, const StringValue& rhs,
absl::Nonnull<google::protobuf::Arena*> arena
ABSL_ATTRIBUTE_LIFETIME_BOUND);
ABSL_DEPRECATED("Use From")
explicit StringValue(absl::Nullable<const char*> value) : value_(value) {}
ABSL_DEPRECATED("Use From")
explicit StringValue(absl::string_view value) : value_(value) {}
ABSL_DEPRECATED("Use From")
explicit StringValue(const absl::Cord& value) : value_(value) {}
ABSL_DEPRECATED("Use From")
explicit StringValue(std::string&& value) : value_(std::move(value)) {}
ABSL_DEPRECATED("Use From")
StringValue(Allocator<> allocator, absl::Nullable<const char*> value)
: value_(allocator, value) {}
ABSL_DEPRECATED("Use From")
StringValue(Allocator<> allocator, absl::string_view value)
: value_(allocator, value) {}
ABSL_DEPRECATED("Use From")
StringValue(Allocator<> allocator, const absl::Cord& value)
: value_(allocator, value) {}
ABSL_DEPRECATED("Use From")
StringValue(Allocator<> allocator, std::string&& value)
: value_(allocator, std::move(value)) {}
ABSL_DEPRECATED("Use Wrap")
StringValue(Borrower borrower, absl::string_view value)
: value_(borrower, value) {}
ABSL_DEPRECATED("Use Wrap")
StringValue(Borrower borrower, const absl::Cord& value)
: value_(borrower, value) {}
StringValue() = default;
StringValue(const StringValue&) = default;
StringValue(StringValue&&) = default;
StringValue& operator=(const StringValue&) = default;
StringValue& operator=(StringValue&&) = default;
constexpr ValueKind kind() const { return kKind; }
absl::string_view GetTypeName() const { return StringType::kName; }
std::string DebugString() const;
// See Value::SerializeTo().
absl::Status SerializeTo(
absl::Nonnull<const google::protobuf::DescriptorPool*> descriptor_pool,
absl::Nonnull<google::protobuf::MessageFactory*> message_factory,
absl::Nonnull<absl::Cord*> value) const;
// See Value::ConvertToJson().
absl::Status ConvertToJson(
absl::Nonnull<const google::protobuf::DescriptorPool*> descriptor_pool,
absl::Nonnull<google::protobuf::MessageFactory*> message_factory,
absl::Nonnull<google::protobuf::Message*> json) const;
absl::Status Equal(
const Value& other,
absl::Nonnull<const google::protobuf::DescriptorPool*> descriptor_pool,
absl::Nonnull<google::protobuf::MessageFactory*> message_factory,
absl::Nonnull<google::protobuf::Arena*> arena, absl::Nonnull<Value*> result) const;
using ValueMixin::Equal;
StringValue Clone(absl::Nonnull<google::protobuf::Arena*> arena) const;
bool IsZeroValue() const {
return NativeValue([](const auto& value) -> bool { return value.empty(); });
}
ABSL_DEPRECATED("Use ToString()")
std::string NativeString() const { return value_.ToString(); }
ABSL_DEPRECATED("Use ToStringView()")
absl::string_view NativeString(
std::string& scratch
ABSL_ATTRIBUTE_LIFETIME_BOUND) const ABSL_ATTRIBUTE_LIFETIME_BOUND {
return value_.ToStringView(&scratch);
}
ABSL_DEPRECATED("Use ToCord()")
absl::Cord NativeCord() const { return value_.ToCord(); }
template <typename Visitor>
ABSL_DEPRECATED("Use TryFlat()")
std::common_type_t<
std::invoke_result_t<Visitor, absl::string_view>,
std::invoke_result_t<Visitor, const absl::Cord&>> NativeValue(Visitor&&
visitor)
const {
return value_.Visit(std::forward<Visitor>(visitor));
}
void swap(StringValue& other) noexcept {
using std::swap;
swap(value_, other.value_);
}
size_t Size() const;
bool IsEmpty() const;
bool Equals(absl::string_view string) const;
bool Equals(const absl::Cord& string) const;
bool Equals(const StringValue& string) const;
int Compare(absl::string_view string) const;
int Compare(const absl::Cord& string) const;
int Compare(const StringValue& string) const;
bool StartsWith(absl::string_view string) const;
bool StartsWith(const absl::Cord& string) const;
bool StartsWith(const StringValue& string) const;
bool EndsWith(absl::string_view string) const;
bool EndsWith(const absl::Cord& string) const;
bool EndsWith(const StringValue& string) const;
bool Contains(absl::string_view string) const;
bool Contains(const absl::Cord& string) const;
bool Contains(const StringValue& string) const;
absl::optional<absl::string_view> TryFlat() const
ABSL_ATTRIBUTE_LIFETIME_BOUND {
return value_.TryFlat();
}
std::string ToString() const { return value_.ToString(); }
void CopyToString(absl::Nonnull<std::string*> out) const {
value_.CopyToString(out);
}
void AppendToString(absl::Nonnull<std::string*> out) const {
value_.AppendToString(out);
}
absl::Cord ToCord() const { return value_.ToCord(); }
void CopyToCord(absl::Nonnull<absl::Cord*> out) const {
value_.CopyToCord(out);
}
void AppendToCord(absl::Nonnull<absl::Cord*> out) const {
value_.AppendToCord(out);
}
absl::string_view ToStringView(
absl::Nonnull<std::string*> scratch
ABSL_ATTRIBUTE_LIFETIME_BOUND) const ABSL_ATTRIBUTE_LIFETIME_BOUND {
return value_.ToStringView(scratch);
}
template <typename H>
friend H AbslHashValue(H state, const StringValue& string) {
return H::combine(std::move(state), string.value_);
}
friend bool operator==(const StringValue& lhs, const StringValue& rhs) {
return lhs.value_ == rhs.value_;
}
friend bool operator<(const StringValue& lhs, const StringValue& rhs) {
return lhs.value_ < rhs.value_;
}
private:
friend class common_internal::ValueMixin<StringValue>;
friend absl::string_view common_internal::LegacyStringValue(
const StringValue& value, bool stable,
absl::Nonnull<google::protobuf::Arena*> arena);
friend struct ArenaTraits<StringValue>;
explicit StringValue(common_internal::ByteString value) noexcept
: value_(std::move(value)) {}
common_internal::ByteString value_;
};
inline void swap(StringValue& lhs, StringValue& rhs) noexcept { lhs.swap(rhs); }
inline bool operator==(const StringValue& lhs, absl::string_view rhs) {
return lhs.Equals(rhs);
}
inline bool operator==(absl::string_view lhs, const StringValue& rhs) {
return rhs == lhs;
}
inline bool operator==(const StringValue& lhs, const absl::Cord& rhs) {
return lhs.Equals(rhs);
}
inline bool operator==(const absl::Cord& lhs, const StringValue& rhs) {
return rhs == lhs;
}
inline bool operator!=(const StringValue& lhs, absl::string_view rhs) {
return !operator==(lhs, rhs);
}
inline bool operator!=(absl::string_view lhs, const StringValue& rhs) {
return !operator==(lhs, rhs);
}
inline bool operator!=(const StringValue& lhs, const absl::Cord& rhs) {
return !operator==(lhs, rhs);
}
inline bool operator!=(const absl::Cord& lhs, const StringValue& rhs) {
return !operator==(lhs, rhs);
}
inline bool operator!=(const StringValue& lhs, const StringValue& rhs) {
return !operator==(lhs, rhs);
}
inline bool operator<(const StringValue& lhs, absl::string_view rhs) {
return lhs.Compare(rhs) < 0;
}
inline bool operator<(absl::string_view lhs, const StringValue& rhs) {
return rhs.Compare(lhs) > 0;
}
inline bool operator<(const StringValue& lhs, const absl::Cord& rhs) {
return lhs.Compare(rhs) < 0;
}
inline bool operator<(const absl::Cord& lhs, const StringValue& rhs) {
return rhs.Compare(lhs) > 0;
}
inline std::ostream& operator<<(std::ostream& out, const StringValue& value) {
return out << value.DebugString();
}
inline StringValue StringValue::From(absl::Nullable<const char*> value,
absl::Nonnull<google::protobuf::Arena*> arena
ABSL_ATTRIBUTE_LIFETIME_BOUND) {
return From(absl::NullSafeStringView(value), arena);
}
inline StringValue StringValue::From(absl::string_view value,
absl::Nonnull<google::protobuf::Arena*> arena
ABSL_ATTRIBUTE_LIFETIME_BOUND) {
ABSL_DCHECK(arena != nullptr);
return StringValue(arena, value);
}
inline StringValue StringValue::From(const absl::Cord& value) {
return StringValue(value);
}
inline StringValue StringValue::From(std::string&& value,
absl::Nonnull<google::protobuf::Arena*> arena
ABSL_ATTRIBUTE_LIFETIME_BOUND) {
ABSL_DCHECK(arena != nullptr);
return StringValue(arena, std::move(value));
}
inline StringValue StringValue::Wrap(absl::string_view value,
absl::Nullable<google::protobuf::Arena*> arena
ABSL_ATTRIBUTE_LIFETIME_BOUND) {
ABSL_DCHECK(arena != nullptr);
return StringValue(Borrower::Arena(arena), value);
}
inline StringValue StringValue::Wrap(absl::string_view value) {
return Wrap(value, nullptr);
}
inline StringValue StringValue::Wrap(const absl::Cord& value) {
return StringValue(value);
}
namespace common_internal {
inline absl::string_view LegacyStringValue(
const StringValue& value, bool stable,
absl::Nonnull<google::protobuf::Arena*> arena) {
return LegacyByteString(value.value_, stable, arena);
}
} // namespace common_internal
template <>
struct ArenaTraits<StringValue> {
using constructible = std::true_type;
static bool trivially_destructible(const StringValue& value) {
return ArenaTraits<>::trivially_destructible(value.value_);
}
};
} // namespace cel
#endif // THIRD_PARTY_CEL_CPP_COMMON_VALUES_STRING_VALUE_H_