v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
string-builder-inl.h
Go to the documentation of this file.
1// Copyright 2014 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef V8_STRINGS_STRING_BUILDER_INL_H_
6#define V8_STRINGS_STRING_BUILDER_INL_H_
7
9// Include the non-inl header before the rest of the headers.
10
14
15namespace v8 {
16namespace internal {
17
20
26
27template <typename sinkchar>
28void StringBuilderConcatHelper(Tagged<String> special, sinkchar* sink,
29 Tagged<FixedArray> fixed_array,
30 int array_length);
31
32// Returns the result length of the concatenation.
33// On illegal argument, -1 is returned.
34int StringBuilderConcatLength(int special_length,
35 Tagged<FixedArray> fixed_array, int array_length,
36 bool* one_byte);
37
38// static
40 FixedArrayBuilder* builder, int from, int to) {
41 DCHECK_GE(from, 0);
42 int length = to - from;
43 DCHECK_GT(length, 0);
46 int encoded_slice = StringBuilderSubstringLength::encode(length) |
48 builder->Add(Smi::FromInt(encoded_slice));
49 } else {
50 // Otherwise encode as two smis.
51 builder->Add(Smi::FromInt(-length));
52 builder->Add(Smi::FromInt(from));
53 }
54}
55
56inline void ReplacementStringBuilder::AddSubjectSlice(int from, int to) {
57 EnsureCapacity(2); // Subject slices are encoded with up to two smis.
59 IncrementCharacterCount(to - from);
60}
61
62template <typename SrcChar, typename DestChar>
64 DCHECK_EQ(encoding_ == String::ONE_BYTE_ENCODING, sizeof(DestChar) == 1);
65 if (sizeof(DestChar) == 1) {
68 ->SeqOneByteStringSet(current_index_++, c);
69 } else {
72 ->SeqTwoByteStringSet(current_index_++, c);
73 }
76}
77
85
86template <int N>
88 const char (&literal)[N]) {
89 // Note that the literal contains the zero char.
90 const int length = N - 1;
91 static_assert(length > 0);
92 if (length == 1) return AppendCharacter(literal[0]);
94 const uint8_t* chars = reinterpret_cast<const uint8_t*>(literal);
96 ->SeqOneByteStringSetChars(current_index_, chars, length);
100 return;
101 }
102 return AppendCString(literal);
103}
104
105template <typename SrcChar>
108 while (*s != '\0') Append<SrcChar, uint8_t>(*s++);
109 } else {
110 while (*s != '\0') Append<SrcChar, base::uc16>(*s++);
111 }
112}
113
115 uint32_t length = static_cast<uint32_t>(str.length());
118 ->SeqOneByteStringSetChars(current_index_,
119 reinterpret_cast<const uint8_t*>(str.data()),
120 length);
121 current_index_ += str.length();
124 } else {
125 for (size_t i = 0; i < str.length(); i++) {
126 AppendCharacter(str[i]);
127 }
128 }
129}
130
132 char buffer[kIntToStringViewBufferSize];
133 std::string_view str = IntToStringView(i, base::ArrayVector(buffer));
134 AppendString(str);
135}
136
138 int length) {
139 if (length > kMaxPartLength) return 0;
140 // The worst case length of an escaped character is 6. Shifting the remaining
141 // string length right by 3 is a more pessimistic estimate, but faster to
142 // calculate.
143 static_assert((kMaxPartLength << 3) <= String::kMaxLength);
144 // This shift will not overflow because length is already less than the
145 // maximum part length.
146 int worst_case_length = length << 3;
147 return CurrentPartCanFit(worst_case_length) ? worst_case_length : 0;
148}
149
150// Change encoding to two-byte.
157
161
168
169} // namespace internal
170} // namespace v8
171
172#endif // V8_STRINGS_STRING_BUILDER_INL_H_
static constexpr bool is_valid(T value)
Definition bit-field.h:50
static constexpr U encode(T value)
Definition bit-field.h:55
void Add(Tagged< Object > value)
V8_INLINE void Append(SrcChar c)
V8_INLINE DirectHandle< String > current_part()
V8_INLINE int EscapedLengthIfCurrentPartFits(int length)
V8_INLINE void set_current_part(DirectHandle< String > string)
V8_INLINE void AppendCString(const SrcChar *s)
V8_INLINE void AppendCharacter(uint8_t c)
V8_INLINE void AppendString(std::string_view str)
V8_INLINE void AppendCStringLiteral(const char(&literal)[N])
V8_INLINE bool CurrentPartCanFit(int length)
v8::internal::Factory * factory()
Definition isolate.h:1527
static void AddSubjectSlice(FixedArrayBuilder *builder, int from, int to)
static V8_WARN_UNUSED_RESULT Handle< String > Truncate(Isolate *isolate, Handle< SeqString > string, uint32_t new_length)
Definition string.cc:1924
static constexpr Tagged< Smi > FromInt(int value)
Definition smi.h:38
static const uint32_t kMaxLength
Definition string.h:511
Point from
FunctionLiteral * literal
Definition liveedit.cc:294
constexpr Vector< T > ArrayVector(T(&arr)[N])
Definition vector.h:354
const int kStringBuilderConcatHelperPositionBits
V8_INLINE IndirectHandle< T > indirect_handle(DirectHandle< T > handle)
Definition handles.h:757
std::string_view IntToStringView(int n, base::Vector< char > buffer)
constexpr int N
void StringBuilderConcatHelper(Tagged< String > special, sinkchar *sink, Tagged< FixedArray > fixed_array, int array_length)
int StringBuilderConcatLength(int special_length, Tagged< FixedArray > fixed_array, int array_length, bool *one_byte)
const int kStringBuilderConcatHelperLengthBits
Tagged< To > Cast(Tagged< From > value, const v8::SourceLocation &loc=INIT_SOURCE_LOCATION_IN_DEBUG)
Definition casting.h:150
#define DCHECK_GE(v1, v2)
Definition logging.h:488
#define DCHECK(condition)
Definition logging.h:482
#define DCHECK_EQ(v1, v2)
Definition logging.h:485
#define DCHECK_GT(v1, v2)
Definition logging.h:487
#define V8_INLINE
Definition v8config.h:500