forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring-builder.h
More file actions
167 lines (118 loc) Β· 4.46 KB
/
string-builder.h
File metadata and controls
167 lines (118 loc) Β· 4.46 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
// Copyright 2024 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef V8_STRINGS_STRING_BUILDER_H_
#define V8_STRINGS_STRING_BUILDER_H_
#include "src/common/assert-scope.h"
#include "src/handles/handles.h"
#include "src/objects/string.h"
namespace v8 {
namespace internal {
class FixedArrayBuilder {
public:
explicit FixedArrayBuilder(Isolate* isolate, int initial_capacity);
explicit FixedArrayBuilder(DirectHandle<FixedArray> backing_store);
// Creates a FixedArrayBuilder which allocates its backing store lazily when
// EnsureCapacity is called.
static FixedArrayBuilder Lazy(Isolate* isolate);
bool HasCapacity(int elements);
void EnsureCapacity(Isolate* isolate, int elements);
void Add(Tagged<Object> value);
void Add(Tagged<Smi> value);
DirectHandle<FixedArray> array() { return array_; }
int length() { return length_; }
int capacity();
private:
explicit FixedArrayBuilder(Isolate* isolate);
DirectHandle<FixedArray> array_;
int length_;
bool has_non_smi_elements_;
};
class ReplacementStringBuilder {
public:
ReplacementStringBuilder(Heap* heap, DirectHandle<String> subject,
int estimated_part_count);
// Caution: Callers must ensure the builder has enough capacity.
static inline void AddSubjectSlice(FixedArrayBuilder* builder, int from,
int to);
inline void AddSubjectSlice(int from, int to);
void AddString(DirectHandle<String> string);
MaybeDirectHandle<String> ToString();
void IncrementCharacterCount(uint32_t by) {
if (character_count_ > String::kMaxLength - by) {
static_assert(String::kMaxLength < kMaxInt);
character_count_ = kMaxInt;
} else {
character_count_ += by;
}
}
private:
void AddElement(DirectHandle<Object> element);
void EnsureCapacity(int elements);
Heap* heap_;
FixedArrayBuilder array_builder_;
DirectHandle<String> subject_;
uint32_t character_count_;
bool is_one_byte_;
};
class IncrementalStringBuilder {
public:
explicit IncrementalStringBuilder(Isolate* isolate);
V8_INLINE String::Encoding CurrentEncoding() { return encoding_; }
template <typename SrcChar, typename DestChar>
V8_INLINE void Append(SrcChar c);
V8_INLINE void AppendCharacter(uint8_t c);
template <int N>
V8_INLINE void AppendCStringLiteral(const char (&literal)[N]);
template <typename SrcChar>
V8_INLINE void AppendCString(const SrcChar* s);
V8_INLINE void AppendString(std::string_view str);
V8_INLINE void AppendInt(int i);
V8_INLINE bool CurrentPartCanFit(int length) {
return part_length_ - current_index_ > length;
}
// We make a rough estimate to find out if the current string can be
// serialized without allocating a new string part.
V8_INLINE int EscapedLengthIfCurrentPartFits(int length);
void AppendString(DirectHandle<String> string);
MaybeDirectHandle<String> Finish();
V8_INLINE bool HasOverflowed() const { return overflowed_; }
int Length() const;
// Change encoding to two-byte.
V8_INLINE void ChangeEncoding();
Isolate* isolate() { return isolate_; }
private:
V8_INLINE Factory* factory();
V8_INLINE DirectHandle<String> accumulator() { return accumulator_; }
V8_INLINE void set_accumulator(DirectHandle<String> string) {
accumulator_.SetValue(*string);
}
V8_INLINE DirectHandle<String> current_part() { return current_part_; }
V8_INLINE void set_current_part(DirectHandle<String> string) {
current_part_.SetValue(*string);
}
// Add the current part to the accumulator.
void Accumulate(DirectHandle<String> new_part);
// Finish the current part and allocate a new part.
void Extend();
bool HasValidCurrentIndex() const;
// Shrink current part to the right size.
V8_INLINE void ShrinkCurrentPart();
void AppendStringByCopy(DirectHandle<String> string);
bool CanAppendByCopy(DirectHandle<String> string);
static const int kInitialPartLength = 32;
static const int kMaxPartLength = 16 * 1024;
static const int kPartLengthGrowthFactor = 2;
// sizeof(string) includes \0.
static const int kIntToStringViewBufferSize = sizeof("-2147483648") - 1;
Isolate* isolate_;
String::Encoding encoding_;
bool overflowed_;
int part_length_;
int current_index_;
DirectHandle<String> accumulator_;
DirectHandle<String> current_part_;
};
} // namespace internal
} // namespace v8
#endif // V8_STRINGS_STRING_BUILDER_H_