MCPcopy Create free account
hub / github.com/bytedance/bolt / StringView

Class StringView

bolt/type/StringView.h:61–316  ·  view source on GitHub ↗

Variable length string or binary type for use in vectors. This has semantics similar to std::string_view or folly::StringPiece and exposes a subset of the interface. If the string is 12 characters or less, it is inlined and no reference is held. If it is longer, a reference to the string is held and the 4 first characters are cached in the StringView. This allows failing comparisons early and redu

Source from the content-addressed store, hash-verified

59//
60// TODO: Extend the interface to parity with folly::StringPiece as needed.
61struct StringView {
62 public:
63 using value_type = char;
64
65 static constexpr size_t kPrefixSize = 4 * sizeof(char);
66 static constexpr size_t kInlineSize = 12;
67
68 StringView() {
69 static_assert(sizeof(StringView) == 16);
70 memset(this, 0, sizeof(StringView));
71 }
72
73 StringView(const char* data, int32_t len) {
74 set(data, len);
75 }
76
77 void set(const char* data, int32_t len) {
78 BOLT_CHECK_GE(len, 0);
79 BOLT_DCHECK(data || len == 0);
80 size_ = len;
81 if (isInline()) {
82 // Zero the inline part.
83 // this makes sure that inline strings can be compared for equality with 2
84 // int64 compares.
85 memset(prefix_, 0, kPrefixSize);
86 if (size_ == 0) {
87 return;
88 }
89 // small string: inlined. Zero the last 8 bytes first to allow for whole
90 // word comparison.
91 value_.data = nullptr;
92 memcpy(prefix_, data, size_);
93 } else {
94 // large string: store pointer
95 memcpy(prefix_, data, kPrefixSize);
96 value_.data = data;
97 }
98 }
99
100 static StringView makeInline(std::string str) {
101 BOLT_DCHECK(isInline(str.size()));
102 return StringView{str};
103 }
104
105 // Making StringView implicitly constructible/convertible from char* and
106 // string literals, in order to allow for a more flexible API and optional
107 // interoperability. E.g:
108 //
109 // StringView sv = "literal";
110 // std::optional<StringView> osv = "literal";
111 //
112 /* implicit */ StringView(const char* data)
113 : StringView(data, strlen(data)) {}
114
115 explicit StringView(const folly::fbstring& value)
116 : StringView(value.data(), value.size()) {}
117 explicit StringView(folly::fbstring&& value) = delete;
118

Callers 15

operator"" _svFunction · 0.70
OwnedVariantValueClass · 0.70
Timestamp.hFile · 0.70
toFunction · 0.70
tmToStringViewMethod · 0.70
genTpchOrdersFunction · 0.50
genTpchLineItemFunction · 0.50
genTpchPartFunction · 0.50
genTpchSupplierFunction · 0.50
genTpchPartSuppFunction · 0.50
genTpchCustomerFunction · 0.50
genTpchNationFunction · 0.50

Calls 4

sizeFunction · 0.85
writeMethod · 0.45
dataMethod · 0.45
sizeMethod · 0.45

Tested by 15

TEST_FFunction · 0.40
testArrowImportMethod · 0.40
TEST_FFunction · 0.40
TEST_FFunction · 0.40
cardValueOfMethod · 0.40
nullValueOfMethod · 0.40
TEST_FFunction · 0.40
testFlatMethod · 0.40
testValueMethod · 0.40
TEST_FFunction · 0.40
TEST_FFunction · 0.40
TEST_FFunction · 0.40