-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathbyte_string.h
More file actions
833 lines (644 loc) · 24.4 KB
/
byte_string.h
File metadata and controls
833 lines (644 loc) · 24.4 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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
// Copyright 2024 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.
#ifndef THIRD_PARTY_CEL_CPP_COMMON_INTERNAL_BYTE_STRING_H_
#define THIRD_PARTY_CEL_CPP_COMMON_INTERNAL_BYTE_STRING_H_
#include <cstddef>
#include <cstdint>
#include <functional>
#include <new>
#include <ostream>
#include <string>
#include <type_traits>
#include <utility>
#include "absl/base/attributes.h"
#include "absl/base/nullability.h"
#include "absl/base/optimization.h"
#include "absl/hash/hash.h"
#include "absl/log/absl_check.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/metadata.h"
#include "common/internal/reference_count.h"
#include "common/memory.h"
#include "google/protobuf/arena.h"
namespace cel {
class BytesValueInputStream;
class BytesValueOutputStream;
class StringValue;
namespace common_internal {
// absl::Cord is trivially relocatable IFF we are not using ASan or MSan. When
// using ASan or MSan absl::Cord will poison/unpoison its inline storage.
#if defined(ABSL_HAVE_ADDRESS_SANITIZER) || defined(ABSL_HAVE_MEMORY_SANITIZER)
#define CEL_COMMON_INTERNAL_BYTE_STRING_TRIVIAL_ABI
#else
#define CEL_COMMON_INTERNAL_BYTE_STRING_TRIVIAL_ABI ABSL_ATTRIBUTE_TRIVIAL_ABI
#endif
class CEL_COMMON_INTERNAL_BYTE_STRING_TRIVIAL_ABI [[nodiscard]] ByteString;
class ABSL_ATTRIBUTE_TRIVIAL_ABI ByteStringView;
struct ByteStringTestFriend;
struct ByteStringViewTestFriend;
enum class ByteStringKind : unsigned int {
kSmall = 0,
kMedium,
kLarge,
};
inline std::ostream& operator<<(std::ostream& out, ByteStringKind kind) {
switch (kind) {
case ByteStringKind::kSmall:
return out << "SMALL";
case ByteStringKind::kMedium:
return out << "MEDIUM";
case ByteStringKind::kLarge:
return out << "LARGE";
}
}
// Representation of small strings in ByteString, which are stored in place.
struct CEL_COMMON_INTERNAL_BYTE_STRING_TRIVIAL_ABI SmallByteStringRep final {
#ifdef _MSC_VER
#pragma push(pack, 1)
#endif
struct ABSL_ATTRIBUTE_PACKED CEL_COMMON_INTERNAL_BYTE_STRING_TRIVIAL_ABI {
ByteStringKind kind : 2;
size_t size : 6;
};
#ifdef _MSC_VER
#pragma pop(pack)
#endif
char data[23 - sizeof(google::protobuf::Arena*)];
google::protobuf::Arena* arena;
};
inline constexpr size_t kSmallByteStringCapacity =
sizeof(SmallByteStringRep::data);
inline constexpr size_t kMediumByteStringSizeBits = sizeof(size_t) * 8 - 2;
inline constexpr size_t kMediumByteStringMaxSize =
(size_t{1} << kMediumByteStringSizeBits) - 1;
inline constexpr size_t kByteStringViewSizeBits = sizeof(size_t) * 8 - 1;
inline constexpr size_t kByteStringViewMaxSize =
(size_t{1} << kByteStringViewSizeBits) - 1;
// Representation of medium strings in ByteString. These are either owned by an
// arena or managed by a reference count. This is encoded in `owner` following
// the same semantics as `cel::Owner`.
struct CEL_COMMON_INTERNAL_BYTE_STRING_TRIVIAL_ABI MediumByteStringRep final {
#ifdef _MSC_VER
#pragma push(pack, 1)
#endif
struct ABSL_ATTRIBUTE_PACKED CEL_COMMON_INTERNAL_BYTE_STRING_TRIVIAL_ABI {
ByteStringKind kind : 2;
size_t size : kMediumByteStringSizeBits;
};
#ifdef _MSC_VER
#pragma pop(pack)
#endif
const char* data;
uintptr_t owner;
};
// Representation of large strings in ByteString. These are stored as
// `absl::Cord` and never owned by an arena.
struct CEL_COMMON_INTERNAL_BYTE_STRING_TRIVIAL_ABI LargeByteStringRep final {
#ifdef _MSC_VER
#pragma push(pack, 1)
#endif
struct ABSL_ATTRIBUTE_PACKED CEL_COMMON_INTERNAL_BYTE_STRING_TRIVIAL_ABI {
ByteStringKind kind : 2;
size_t padding : kMediumByteStringSizeBits;
};
#ifdef _MSC_VER
#pragma pop(pack)
#endif
alignas(absl::Cord) char data[sizeof(absl::Cord)];
};
// Representation of ByteString.
union CEL_COMMON_INTERNAL_BYTE_STRING_TRIVIAL_ABI ByteStringRep final {
#ifdef _MSC_VER
#pragma push(pack, 1)
#endif
struct ABSL_ATTRIBUTE_PACKED CEL_COMMON_INTERNAL_BYTE_STRING_TRIVIAL_ABI {
ByteStringKind kind : 2;
} header;
#ifdef _MSC_VER
#pragma pop(pack)
#endif
SmallByteStringRep small;
MediumByteStringRep medium;
LargeByteStringRep large;
};
// Returns a `absl::string_view` from `ByteString`, using `arena` to make memory
// allocations if necessary. `stable` indicates whether `cel::Value` is in a
// location where it will not be moved, so that inline string/bytes storage can
// be referenced.
absl::string_view LegacyByteString(const ByteString& string, bool stable,
absl::Nonnull<google::protobuf::Arena*> arena);
// `ByteString` is an vocabulary type capable of representing copy-on-write
// strings efficiently for arenas and reference counting. The contents of the
// byte string are owned by an arena or managed by a reference count. All byte
// strings have an associated allocator specified at construction, once the byte
// string is constructed the allocator will not and cannot change. Copying and
// moving between different allocators is supported and dealt with
// transparently by copying.
class CEL_COMMON_INTERNAL_BYTE_STRING_TRIVIAL_ABI [[nodiscard]]
ByteString final {
public:
static ByteString Concat(const ByteString& lhs, const ByteString& rhs,
absl::Nonnull<google::protobuf::Arena*> arena);
ByteString() : ByteString(NewDeleteAllocator()) {}
explicit ByteString(absl::Nullable<const char*> string)
: ByteString(NewDeleteAllocator(), string) {}
explicit ByteString(absl::string_view string)
: ByteString(NewDeleteAllocator(), string) {}
explicit ByteString(const std::string& string)
: ByteString(NewDeleteAllocator(), string) {}
explicit ByteString(std::string&& string)
: ByteString(NewDeleteAllocator(), std::move(string)) {}
explicit ByteString(const absl::Cord& cord)
: ByteString(NewDeleteAllocator(), cord) {}
explicit ByteString(ByteStringView other);
ByteString(const ByteString& other) noexcept {
Construct(other, /*allocator=*/absl::nullopt);
}
ByteString(ByteString&& other) noexcept {
Construct(other, /*allocator=*/absl::nullopt);
}
explicit ByteString(Allocator<> allocator) {
SetSmallEmpty(allocator.arena());
}
ByteString(Allocator<> allocator, absl::Nullable<const char*> string)
: ByteString(allocator, absl::NullSafeStringView(string)) {}
ByteString(Allocator<> allocator, absl::string_view string);
ByteString(Allocator<> allocator, const std::string& string);
ByteString(Allocator<> allocator, std::string&& string);
ByteString(Allocator<> allocator, const absl::Cord& cord);
ByteString(Allocator<> allocator, ByteStringView other);
ByteString(Allocator<> allocator, const ByteString& other) {
Construct(other, allocator);
}
ByteString(Allocator<> allocator, ByteString&& other) {
Construct(other, allocator);
}
ByteString(Borrower borrower,
absl::Nullable<const char*> string ABSL_ATTRIBUTE_LIFETIME_BOUND)
: ByteString(borrower, absl::NullSafeStringView(string)) {}
ByteString(Borrower borrower,
absl::string_view string ABSL_ATTRIBUTE_LIFETIME_BOUND)
: ByteString(Borrowed(borrower, string)) {}
ByteString(Borrower borrower,
const absl::Cord& cord ABSL_ATTRIBUTE_LIFETIME_BOUND)
: ByteString(Borrowed(borrower, cord)) {}
~ByteString() { Destroy(); }
ByteString& operator=(const ByteString& other) noexcept {
if (ABSL_PREDICT_TRUE(this != &other)) {
CopyFrom(other);
}
return *this;
}
ByteString& operator=(ByteString&& other) noexcept {
if (ABSL_PREDICT_TRUE(this != &other)) {
MoveFrom(other);
}
return *this;
}
ByteString& operator=(ByteStringView other);
bool empty() const;
size_t size() const;
size_t max_size() const { return kByteStringViewMaxSize; }
absl::string_view Flatten() ABSL_ATTRIBUTE_LIFETIME_BOUND;
absl::optional<absl::string_view> TryFlat() const
ABSL_ATTRIBUTE_LIFETIME_BOUND;
bool Equals(ByteStringView rhs) const;
int Compare(ByteStringView rhs) const;
bool StartsWith(ByteStringView rhs) const;
bool EndsWith(ByteStringView rhs) const;
void RemovePrefix(size_t n);
void RemoveSuffix(size_t n);
std::string ToString() const;
void CopyToString(absl::Nonnull<std::string*> out) const;
void AppendToString(absl::Nonnull<std::string*> out) const;
absl::Cord ToCord() const&;
absl::Cord ToCord() &&;
void CopyToCord(absl::Nonnull<absl::Cord*> out) const;
void AppendToCord(absl::Nonnull<absl::Cord*> out) const;
absl::string_view ToStringView(
absl::Nonnull<std::string*> scratch
ABSL_ATTRIBUTE_LIFETIME_BOUND) const ABSL_ATTRIBUTE_LIFETIME_BOUND;
absl::string_view AsStringView() const ABSL_ATTRIBUTE_LIFETIME_BOUND;
absl::Nullable<google::protobuf::Arena*> GetArena() const;
ByteString Clone(absl::Nonnull<google::protobuf::Arena*> arena) const;
void HashValue(absl::HashState state) const;
template <typename Visitor>
decltype(auto) Visit(Visitor&& visitor) const {
switch (GetKind()) {
case ByteStringKind::kSmall:
return std::forward<Visitor>(visitor)(GetSmall());
case ByteStringKind::kMedium:
return std::forward<Visitor>(visitor)(GetMedium());
case ByteStringKind::kLarge:
return std::forward<Visitor>(visitor)(GetLarge());
}
}
friend void swap(ByteString& lhs, ByteString& rhs) {
if (&lhs != &rhs) {
lhs.Swap(rhs);
}
}
template <typename H>
friend H AbslHashValue(H state, const ByteString& byte_string) {
byte_string.HashValue(absl::HashState::Create(&state));
return state;
}
private:
friend class ByteStringView;
friend struct ByteStringTestFriend;
friend class cel::BytesValueInputStream;
friend class cel::BytesValueOutputStream;
friend class cel::StringValue;
friend absl::string_view LegacyByteString(
const ByteString& string, bool stable,
absl::Nonnull<google::protobuf::Arena*> arena);
friend struct cel::ArenaTraits<ByteString>;
static ByteString Borrowed(Borrower borrower,
absl::string_view string
ABSL_ATTRIBUTE_LIFETIME_BOUND);
static ByteString Borrowed(
Borrower borrower, const absl::Cord& cord ABSL_ATTRIBUTE_LIFETIME_BOUND);
ByteString(absl::Nonnull<const ReferenceCount*> refcount,
absl::string_view string);
constexpr ByteStringKind GetKind() const { return rep_.header.kind; }
absl::string_view GetSmall() const {
ABSL_DCHECK_EQ(GetKind(), ByteStringKind::kSmall);
return GetSmall(rep_.small);
}
static absl::string_view GetSmall(const SmallByteStringRep& rep) {
return absl::string_view(rep.data, rep.size);
}
absl::string_view GetMedium() const {
ABSL_DCHECK_EQ(GetKind(), ByteStringKind::kMedium);
return GetMedium(rep_.medium);
}
static absl::string_view GetMedium(const MediumByteStringRep& rep) {
return absl::string_view(rep.data, rep.size);
}
absl::Nullable<google::protobuf::Arena*> GetSmallArena() const {
ABSL_DCHECK_EQ(GetKind(), ByteStringKind::kSmall);
return GetSmallArena(rep_.small);
}
static absl::Nullable<google::protobuf::Arena*> GetSmallArena(
const SmallByteStringRep& rep) {
return rep.arena;
}
absl::Nullable<google::protobuf::Arena*> GetMediumArena() const {
ABSL_DCHECK_EQ(GetKind(), ByteStringKind::kMedium);
return GetMediumArena(rep_.medium);
}
static absl::Nullable<google::protobuf::Arena*> GetMediumArena(
const MediumByteStringRep& rep);
absl::Nullable<const ReferenceCount*> GetMediumReferenceCount() const {
ABSL_DCHECK_EQ(GetKind(), ByteStringKind::kMedium);
return GetMediumReferenceCount(rep_.medium);
}
static absl::Nullable<const ReferenceCount*> GetMediumReferenceCount(
const MediumByteStringRep& rep);
uintptr_t GetMediumOwner() const {
ABSL_DCHECK_EQ(GetKind(), ByteStringKind::kMedium);
return rep_.medium.owner;
}
absl::Cord& GetLarge() ABSL_ATTRIBUTE_LIFETIME_BOUND {
ABSL_DCHECK_EQ(GetKind(), ByteStringKind::kLarge);
return GetLarge(rep_.large);
}
static absl::Cord& GetLarge(
LargeByteStringRep& rep ABSL_ATTRIBUTE_LIFETIME_BOUND) {
return *std::launder(reinterpret_cast<absl::Cord*>(&rep.data[0]));
}
const absl::Cord& GetLarge() const ABSL_ATTRIBUTE_LIFETIME_BOUND {
ABSL_DCHECK_EQ(GetKind(), ByteStringKind::kLarge);
return GetLarge(rep_.large);
}
static const absl::Cord& GetLarge(
const LargeByteStringRep& rep ABSL_ATTRIBUTE_LIFETIME_BOUND) {
return *std::launder(reinterpret_cast<const absl::Cord*>(&rep.data[0]));
}
void SetSmallEmpty(absl::Nullable<google::protobuf::Arena*> arena) {
rep_.header.kind = ByteStringKind::kSmall;
rep_.small.size = 0;
rep_.small.arena = arena;
}
void SetSmall(absl::Nullable<google::protobuf::Arena*> arena, absl::string_view string);
void SetSmall(absl::Nullable<google::protobuf::Arena*> arena, const absl::Cord& cord);
void SetMedium(absl::Nullable<google::protobuf::Arena*> arena,
absl::string_view string);
void SetMedium(absl::Nullable<google::protobuf::Arena*> arena, std::string&& string);
void SetMedium(absl::Nonnull<google::protobuf::Arena*> arena, const absl::Cord& cord);
void SetMedium(absl::string_view string, uintptr_t owner);
void SetLarge(const absl::Cord& cord);
void SetLarge(absl::Cord&& cord);
void Swap(ByteString& other);
void Construct(const ByteString& other,
absl::optional<Allocator<>> allocator);
void Construct(ByteStringView other, absl::optional<Allocator<>> allocator);
void Construct(ByteString& other, absl::optional<Allocator<>> allocator);
void CopyFrom(const ByteString& other);
void CopyFrom(ByteStringView other);
void MoveFrom(ByteString& other);
void Destroy();
void DestroyMedium() {
ABSL_DCHECK_EQ(GetKind(), ByteStringKind::kMedium);
DestroyMedium(rep_.medium);
}
static void DestroyMedium(const MediumByteStringRep& rep) {
StrongUnref(GetMediumReferenceCount(rep));
}
void DestroyLarge() {
ABSL_DCHECK_EQ(GetKind(), ByteStringKind::kLarge);
DestroyLarge(rep_.large);
}
static void DestroyLarge(LargeByteStringRep& rep) { GetLarge(rep).~Cord(); }
void CopyToArray(absl::Nonnull<char*> out) const;
ByteStringRep rep_;
};
enum class ByteStringViewKind : unsigned int {
kString = 0,
kCord,
};
inline std::ostream& operator<<(std::ostream& out, ByteStringViewKind kind) {
switch (kind) {
case ByteStringViewKind::kString:
return out << "STRING";
case ByteStringViewKind::kCord:
return out << "CORD";
}
}
struct ABSL_ATTRIBUTE_TRIVIAL_ABI StringByteStringViewRep final {
#ifdef _MSC_VER
#pragma push(pack, 1)
#endif
struct ABSL_ATTRIBUTE_TRIVIAL_ABI ABSL_ATTRIBUTE_PACKED {
ByteStringViewKind kind : 1;
size_t size : kByteStringViewSizeBits;
};
#ifdef _MSC_VER
#pragma pop(pack)
#endif
const char* data;
uintptr_t owner;
};
struct ABSL_ATTRIBUTE_TRIVIAL_ABI CordByteStringViewRep final {
#ifdef _MSC_VER
#pragma push(pack, 1)
#endif
struct ABSL_ATTRIBUTE_TRIVIAL_ABI ABSL_ATTRIBUTE_PACKED {
ByteStringViewKind kind : 1;
size_t size : kByteStringViewSizeBits;
};
#ifdef _MSC_VER
#pragma pop(pack)
#endif
const absl::Cord* data;
size_t pos;
};
union ABSL_ATTRIBUTE_TRIVIAL_ABI ByteStringViewRep final {
#ifdef _MSC_VER
#pragma push(pack, 1)
#endif
struct ABSL_ATTRIBUTE_TRIVIAL_ABI ABSL_ATTRIBUTE_PACKED {
ByteStringViewKind kind : 1;
size_t size : kByteStringViewSizeBits;
} header;
#ifdef _MSC_VER
#pragma pop(pack)
#endif
StringByteStringViewRep string;
CordByteStringViewRep cord;
};
// `ByteStringView` is to `ByteString` what `std::string_view` is to
// `std::string`. While it is capable of being a view over the underlying data
// of `ByteStringView`, it is also capable of being a view over `std::string`,
// `std::string_view`, and `absl::Cord`.
class ABSL_ATTRIBUTE_TRIVIAL_ABI ByteStringView final {
public:
ByteStringView() {
rep_.header.kind = ByteStringViewKind::kString;
rep_.string.size = 0;
rep_.string.data = "";
rep_.string.owner = 0;
}
ByteStringView(const ByteStringView&) = default;
ByteStringView& operator=(const ByteStringView&) = default;
// NOLINTNEXTLINE(google-explicit-constructor)
ByteStringView(
absl::Nullable<const char*> string ABSL_ATTRIBUTE_LIFETIME_BOUND)
: ByteStringView(absl::NullSafeStringView(string)) {}
// NOLINTNEXTLINE(google-explicit-constructor)
ByteStringView(absl::string_view string ABSL_ATTRIBUTE_LIFETIME_BOUND) {
ABSL_DCHECK_LE(string.size(), max_size());
rep_.header.kind = ByteStringViewKind::kString;
rep_.string.size = string.size();
rep_.string.data = string.data();
rep_.string.owner = 0;
}
// NOLINTNEXTLINE(google-explicit-constructor)
ByteStringView(const std::string& string ABSL_ATTRIBUTE_LIFETIME_BOUND)
: ByteStringView(absl::string_view(string)) {}
// NOLINTNEXTLINE(google-explicit-constructor)
ByteStringView(const absl::Cord& cord ABSL_ATTRIBUTE_LIFETIME_BOUND) {
ABSL_DCHECK_LE(cord.size(), max_size());
rep_.header.kind = ByteStringViewKind::kCord;
rep_.cord.size = cord.size();
rep_.cord.data = &cord;
rep_.cord.pos = 0;
}
// NOLINTNEXTLINE(google-explicit-constructor)
ByteStringView(const ByteString& other ABSL_ATTRIBUTE_LIFETIME_BOUND);
// NOLINTNEXTLINE(google-explicit-constructor)
ByteStringView& operator=(
absl::Nullable<const char*> string ABSL_ATTRIBUTE_LIFETIME_BOUND) {
return *this = ByteStringView(string);
}
// NOLINTNEXTLINE(google-explicit-constructor)
ByteStringView& operator=(
absl::string_view string ABSL_ATTRIBUTE_LIFETIME_BOUND) {
return *this = ByteStringView(string);
}
// NOLINTNEXTLINE(google-explicit-constructor)
ByteStringView& operator=(
const std::string& string ABSL_ATTRIBUTE_LIFETIME_BOUND) {
return *this = ByteStringView(string);
}
ByteStringView& operator=(std::string&&) = delete;
// NOLINTNEXTLINE(google-explicit-constructor)
ByteStringView& operator=(
const absl::Cord& cord ABSL_ATTRIBUTE_LIFETIME_BOUND) {
return *this = ByteStringView(cord);
}
ByteStringView& operator=(absl::Cord&&) = delete;
// NOLINTNEXTLINE(google-explicit-constructor)
ByteStringView& operator=(
const ByteString& other ABSL_ATTRIBUTE_LIFETIME_BOUND) {
return *this = ByteStringView(other);
}
ByteStringView& operator=(ByteString&&) = delete;
bool empty() const;
size_t size() const;
size_t max_size() const { return kByteStringViewMaxSize; }
absl::optional<absl::string_view> TryFlat() const
ABSL_ATTRIBUTE_LIFETIME_BOUND;
bool Equals(ByteStringView rhs) const;
int Compare(ByteStringView rhs) const;
bool StartsWith(ByteStringView rhs) const;
bool EndsWith(ByteStringView rhs) const;
void RemovePrefix(size_t n);
void RemoveSuffix(size_t n);
std::string ToString() const;
void CopyToString(absl::Nonnull<std::string*> out) const;
void AppendToString(absl::Nonnull<std::string*> out) const;
absl::Cord ToCord() const;
void CopyToCord(absl::Nonnull<absl::Cord*> out) const;
void AppendToCord(absl::Nonnull<absl::Cord*> out) const;
absl::Nullable<google::protobuf::Arena*> GetArena() const;
void HashValue(absl::HashState state) const;
template <typename Visitor>
decltype(auto) Visit(Visitor&& visitor) const {
switch (GetKind()) {
case ByteStringViewKind::kString:
return std::forward<Visitor>(visitor)(GetString());
case ByteStringViewKind::kCord:
return std::forward<Visitor>(visitor)(
static_cast<const absl::Cord&>(GetSubcord()));
}
}
template <typename H>
friend H AbslHashValue(H state, ByteStringView byte_string_view) {
byte_string_view.HashValue(absl::HashState::Create(&state));
return state;
}
private:
friend class ByteString;
friend struct ByteStringViewTestFriend;
constexpr ByteStringViewKind GetKind() const { return rep_.header.kind; }
absl::string_view GetString() const {
ABSL_DCHECK_EQ(GetKind(), ByteStringViewKind::kString);
return absl::string_view(rep_.string.data, rep_.string.size);
}
absl::Nullable<google::protobuf::Arena*> GetStringArena() const {
ABSL_DCHECK_EQ(GetKind(), ByteStringViewKind::kString);
if ((rep_.string.owner & kMetadataOwnerBits) == kMetadataOwnerArenaBit) {
return reinterpret_cast<google::protobuf::Arena*>(rep_.string.owner &
kMetadataOwnerPointerMask);
}
return nullptr;
}
absl::Nullable<const ReferenceCount*> GetStringReferenceCount() const {
ABSL_DCHECK_EQ(GetKind(), ByteStringViewKind::kString);
return GetStringReferenceCount(rep_.string);
}
static absl::Nullable<const ReferenceCount*> GetStringReferenceCount(
const StringByteStringViewRep& rep) {
if ((rep.owner & kMetadataOwnerBits) == kMetadataOwnerReferenceCountBit) {
return reinterpret_cast<const ReferenceCount*>(rep.owner &
kMetadataOwnerPointerMask);
}
return nullptr;
}
uintptr_t GetStringOwner() const {
ABSL_DCHECK_EQ(GetKind(), ByteStringViewKind::kString);
return rep_.string.owner;
}
const absl::Cord& GetCord() const {
ABSL_DCHECK_EQ(GetKind(), ByteStringViewKind::kCord);
return *rep_.cord.data;
}
absl::Cord GetSubcord() const {
ABSL_DCHECK_EQ(GetKind(), ByteStringViewKind::kCord);
return GetCord().Subcord(rep_.cord.pos, rep_.cord.size);
}
ByteStringViewRep rep_;
};
inline bool operator==(const ByteString& lhs, const ByteString& rhs) {
return lhs.Equals(rhs);
}
inline bool operator!=(const ByteString& lhs, const ByteString& rhs) {
return !operator==(lhs, rhs);
}
inline bool operator<(const ByteString& lhs, const ByteString& rhs) {
return lhs.Compare(rhs) < 0;
}
inline bool operator<=(const ByteString& lhs, const ByteString& rhs) {
return lhs.Compare(rhs) <= 0;
}
inline bool operator>(const ByteString& lhs, const ByteString& rhs) {
return lhs.Compare(rhs) > 0;
}
inline bool operator>=(const ByteString& lhs, const ByteString& rhs) {
return lhs.Compare(rhs) >= 0;
}
inline bool ByteString::Equals(ByteStringView rhs) const {
return ByteStringView(*this).Equals(rhs);
}
inline int ByteString::Compare(ByteStringView rhs) const {
return ByteStringView(*this).Compare(rhs);
}
inline bool ByteString::StartsWith(ByteStringView rhs) const {
return ByteStringView(*this).StartsWith(rhs);
}
inline bool ByteString::EndsWith(ByteStringView rhs) const {
return ByteStringView(*this).EndsWith(rhs);
}
inline bool operator==(ByteStringView lhs, ByteStringView rhs) {
return lhs.Equals(rhs);
}
inline bool operator!=(ByteStringView lhs, ByteStringView rhs) {
return !operator==(lhs, rhs);
}
inline bool operator<(ByteStringView lhs, ByteStringView rhs) {
return lhs.Compare(rhs) < 0;
}
inline bool operator<=(ByteStringView lhs, ByteStringView rhs) {
return lhs.Compare(rhs) <= 0;
}
inline bool operator>(ByteStringView lhs, ByteStringView rhs) {
return lhs.Compare(rhs) > 0;
}
inline bool operator>=(ByteStringView lhs, ByteStringView rhs) {
return lhs.Compare(rhs) >= 0;
}
inline ByteString::ByteString(ByteStringView other) {
Construct(other, /*allocator=*/absl::nullopt);
}
inline ByteString::ByteString(Allocator<> allocator, ByteStringView other) {
Construct(other, allocator);
}
inline ByteString& ByteString::operator=(ByteStringView other) {
CopyFrom(other);
return *this;
}
#undef CEL_COMMON_INTERNAL_BYTE_STRING_TRIVIAL_ABI
} // namespace common_internal
template <>
struct ArenaTraits<common_internal::ByteString> {
using constructible = std::true_type;
static bool trivially_destructible(
const common_internal::ByteString& byte_string) {
switch (byte_string.GetKind()) {
case common_internal::ByteStringKind::kSmall:
return true;
case common_internal::ByteStringKind::kMedium:
return byte_string.GetMediumReferenceCount() == nullptr;
case common_internal::ByteStringKind::kLarge:
return false;
}
}
};
} // namespace cel
#endif // THIRD_PARTY_CEL_CPP_COMMON_INTERNAL_BYTE_STRING_H_