v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
string-builder.h
Go to the documentation of this file.
1// Copyright 2024 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_H_
6#define V8_STRINGS_STRING_BUILDER_H_
7
10#include "src/objects/string.h"
11
12namespace v8 {
13namespace internal {
14
16 public:
17 explicit FixedArrayBuilder(Isolate* isolate, int initial_capacity);
18 explicit FixedArrayBuilder(DirectHandle<FixedArray> backing_store);
19
20 // Creates a FixedArrayBuilder which allocates its backing store lazily when
21 // EnsureCapacity is called.
22 static FixedArrayBuilder Lazy(Isolate* isolate);
23
24 bool HasCapacity(int elements);
25 void EnsureCapacity(Isolate* isolate, int elements);
26
27 void Add(Tagged<Object> value);
28 void Add(Tagged<Smi> value);
29
31
32 int length() { return length_; }
33
34 int capacity();
35
36 private:
37 explicit FixedArrayBuilder(Isolate* isolate);
38
42};
43
45 public:
47 int estimated_part_count);
48
49 // Caution: Callers must ensure the builder has enough capacity.
50 static inline void AddSubjectSlice(FixedArrayBuilder* builder, int from,
51 int to);
52
53 inline void AddSubjectSlice(int from, int to);
54
56
58
59 void IncrementCharacterCount(uint32_t by) {
61 static_assert(String::kMaxLength < kMaxInt);
63 } else {
64 character_count_ += by;
65 }
66 }
67
68 private:
69 void AddElement(DirectHandle<Object> element);
70 void EnsureCapacity(int elements);
71
77};
78
80 public:
81 explicit IncrementalStringBuilder(Isolate* isolate);
82
84
85 template <typename SrcChar, typename DestChar>
86 V8_INLINE void Append(SrcChar c);
87
88 V8_INLINE void AppendCharacter(uint8_t c);
89
90 template <int N>
91 V8_INLINE void AppendCStringLiteral(const char (&literal)[N]);
92
93 template <typename SrcChar>
94 V8_INLINE void AppendCString(const SrcChar* s);
95 V8_INLINE void AppendString(std::string_view str);
96
97 V8_INLINE void AppendInt(int i);
98
99 V8_INLINE bool CurrentPartCanFit(int length) {
101 }
102
103 // We make a rough estimate to find out if the current string can be
104 // serialized without allocating a new string part.
106
108
110
111 V8_INLINE bool HasOverflowed() const { return overflowed_; }
112
113 int Length() const;
114
115 // Change encoding to two-byte.
117
118 Isolate* isolate() { return isolate_; }
119
120 private:
122
124
126 accumulator_.SetValue(*string);
127 }
128
130
132 current_part_.SetValue(*string);
133 }
134
135 // Add the current part to the accumulator.
136 void Accumulate(DirectHandle<String> new_part);
137
138 // Finish the current part and allocate a new part.
139 void Extend();
140
141 bool HasValidCurrentIndex() const;
142
143 // Shrink current part to the right size.
145
148
149 static const int kInitialPartLength = 32;
150 static const int kMaxPartLength = 16 * 1024;
151 static const int kPartLengthGrowthFactor = 2;
152 // sizeof(string) includes \0.
153 static const int kIntToStringViewBufferSize = sizeof("-2147483648") - 1;
154
162};
163
164} // namespace internal
165} // namespace v8
166
167#endif // V8_STRINGS_STRING_BUILDER_H_
void Add(Tagged< Object > value)
void EnsureCapacity(Isolate *isolate, int elements)
FixedArrayBuilder(Isolate *isolate, int initial_capacity)
DirectHandle< FixedArray > array()
DirectHandle< FixedArray > array_
static FixedArrayBuilder Lazy(Isolate *isolate)
void Accumulate(DirectHandle< String > new_part)
V8_INLINE void Append(SrcChar c)
bool CanAppendByCopy(DirectHandle< String > string)
V8_INLINE DirectHandle< String > accumulator()
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)
void AppendStringByCopy(DirectHandle< String > string)
MaybeDirectHandle< String > Finish()
V8_INLINE void AppendCharacter(uint8_t c)
V8_INLINE void AppendString(std::string_view str)
V8_INLINE void set_accumulator(DirectHandle< String > string)
V8_INLINE String::Encoding CurrentEncoding()
V8_INLINE void AppendCStringLiteral(const char(&literal)[N])
V8_INLINE bool CurrentPartCanFit(int length)
ReplacementStringBuilder(Heap *heap, DirectHandle< String > subject, int estimated_part_count)
static void AddSubjectSlice(FixedArrayBuilder *builder, int from, int to)
MaybeDirectHandle< String > ToString()
void AddString(DirectHandle< String > string)
void AddElement(DirectHandle< Object > element)
static const uint32_t kMaxLength
Definition string.h:511
FunctionLiteral * literal
Definition liveedit.cc:294
constexpr int N
constexpr int kMaxInt
Definition globals.h:374
#define V8_INLINE
Definition v8config.h:500