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.
5 #ifndef V8_STRING_BUILDER_H_
6 #define V8_STRING_BUILDER_H_
13 const int kStringBuilderConcatHelperLengthBits = 11;
14 const int kStringBuilderConcatHelperPositionBits = 19;
16 typedef BitField<int, 0, kStringBuilderConcatHelperLengthBits>
17 StringBuilderSubstringLength;
18 typedef BitField<int, kStringBuilderConcatHelperLengthBits,
19 kStringBuilderConcatHelperPositionBits>
20 StringBuilderSubstringPosition;
23 template <typename sinkchar>
24 static inline void StringBuilderConcatHelper(String* special, sinkchar* sink,
25 FixedArray* fixed_array,
27 DisallowHeapAllocation no_gc;
29 for (int i = 0; i < array_length; i++) {
30 Object* element = fixed_array->get(i);
31 if (element->IsSmi()) {
32 // Smi encoding of position and length.
33 int encoded_slice = Smi::cast(element)->value();
36 if (encoded_slice > 0) {
37 // Position and length encoded in one smi.
38 pos = StringBuilderSubstringPosition::decode(encoded_slice);
39 len = StringBuilderSubstringLength::decode(encoded_slice);
41 // Position and length encoded in two smis.
42 Object* obj = fixed_array->get(++i);
44 pos = Smi::cast(obj)->value();
47 String::WriteToFlat(special, sink + position, pos, pos + len);
50 String* string = String::cast(element);
51 int element_length = string->length();
52 String::WriteToFlat(string, sink + position, 0, element_length);
53 position += element_length;
59 // Returns the result length of the concatenation.
60 // On illegal argument, -1 is returned.
61 static inline int StringBuilderConcatLength(int special_length,
62 FixedArray* fixed_array,
63 int array_length, bool* one_byte) {
64 DisallowHeapAllocation no_gc;
66 for (int i = 0; i < array_length; i++) {
68 Object* elt = fixed_array->get(i);
70 // Smi encoding of position and length.
71 int smi_value = Smi::cast(elt)->value();
75 // Position and length encoded in one smi.
76 pos = StringBuilderSubstringPosition::decode(smi_value);
77 len = StringBuilderSubstringLength::decode(smi_value);
79 // Position and length encoded in two smis.
81 // Get the position and check that it is a positive smi.
83 if (i >= array_length) return -1;
84 Object* next_smi = fixed_array->get(i);
85 if (!next_smi->IsSmi()) return -1;
86 pos = Smi::cast(next_smi)->value();
87 if (pos < 0) return -1;
91 if (pos > special_length || len > special_length - pos) return -1;
93 } else if (elt->IsString()) {
94 String* element = String::cast(elt);
95 int element_length = element->length();
96 increment = element_length;
97 if (*one_byte && !element->HasOnlyOneByteChars()) {
103 if (increment > String::kMaxLength - position) {
104 return kMaxInt; // Provoke throw on allocation.
106 position += increment;
112 class FixedArrayBuilder {
114 explicit FixedArrayBuilder(Isolate* isolate, int initial_capacity)
115 : array_(isolate->factory()->NewFixedArrayWithHoles(initial_capacity)),
117 has_non_smi_elements_(false) {
118 // Require a non-zero initial size. Ensures that doubling the size to
119 // extend the array will work.
120 DCHECK(initial_capacity > 0);
123 explicit FixedArrayBuilder(Handle<FixedArray> backing_store)
124 : array_(backing_store), length_(0), has_non_smi_elements_(false) {
125 // Require a non-zero initial size. Ensures that doubling the size to
126 // extend the array will work.
127 DCHECK(backing_store->length() > 0);
130 bool HasCapacity(int elements) {
131 int length = array_->length();
132 int required_length = length_ + elements;
133 return (length >= required_length);
136 void EnsureCapacity(int elements) {
137 int length = array_->length();
138 int required_length = length_ + elements;
139 if (length < required_length) {
140 int new_length = length;
143 } while (new_length < required_length);
144 Handle<FixedArray> extended_array =
145 array_->GetIsolate()->factory()->NewFixedArrayWithHoles(new_length);
146 array_->CopyTo(0, *extended_array, 0, length_);
147 array_ = extended_array;
151 void Add(Object* value) {
152 DCHECK(!value->IsSmi());
153 DCHECK(length_ < capacity());
154 array_->set(length_, value);
156 has_non_smi_elements_ = true;
159 void Add(Smi* value) {
160 DCHECK(value->IsSmi());
161 DCHECK(length_ < capacity());
162 array_->set(length_, value);
166 Handle<FixedArray> array() { return array_; }
168 int length() { return length_; }
170 int capacity() { return array_->length(); }
172 Handle<JSArray> ToJSArray(Handle<JSArray> target_array) {
173 JSArray::SetContent(target_array, array_);
174 target_array->set_length(Smi::FromInt(length_));
180 Handle<FixedArray> array_;
182 bool has_non_smi_elements_;
186 class ReplacementStringBuilder {
188 ReplacementStringBuilder(Heap* heap, Handle<String> subject,
189 int estimated_part_count)
191 array_builder_(heap->isolate(), estimated_part_count),
194 is_one_byte_(subject->IsOneByteRepresentation()) {
195 // Require a non-zero initial size. Ensures that doubling the size to
196 // extend the array will work.
197 DCHECK(estimated_part_count > 0);
200 static inline void AddSubjectSlice(FixedArrayBuilder* builder, int from,
203 int length = to - from;
205 if (StringBuilderSubstringLength::is_valid(length) &&
206 StringBuilderSubstringPosition::is_valid(from)) {
207 int encoded_slice = StringBuilderSubstringLength::encode(length) |
208 StringBuilderSubstringPosition::encode(from);
209 builder->Add(Smi::FromInt(encoded_slice));
211 // Otherwise encode as two smis.
212 builder->Add(Smi::FromInt(-length));
213 builder->Add(Smi::FromInt(from));
218 void EnsureCapacity(int elements) { array_builder_.EnsureCapacity(elements); }
221 void AddSubjectSlice(int from, int to) {
222 AddSubjectSlice(&array_builder_, from, to);
223 IncrementCharacterCount(to - from);
227 void AddString(Handle<String> string) {
228 int length = string->length();
231 if (!string->IsOneByteRepresentation()) {
232 is_one_byte_ = false;
234 IncrementCharacterCount(length);
238 MaybeHandle<String> ToString();
241 void IncrementCharacterCount(int by) {
242 if (character_count_ > String::kMaxLength - by) {
243 STATIC_ASSERT(String::kMaxLength < kMaxInt);
244 character_count_ = kMaxInt;
246 character_count_ += by;
251 void AddElement(Object* element) {
252 DCHECK(element->IsSmi() || element->IsString());
253 DCHECK(array_builder_.capacity() > array_builder_.length());
254 array_builder_.Add(element);
258 FixedArrayBuilder array_builder_;
259 Handle<String> subject_;
260 int character_count_;
265 class IncrementalStringBuilder {
267 explicit IncrementalStringBuilder(Isolate* isolate);
269 INLINE(String::Encoding CurrentEncoding()) { return encoding_; }
271 template <typename SrcChar, typename DestChar>
272 INLINE(void Append(SrcChar c));
274 INLINE(void AppendCharacter(uint8_t c)) {
275 if (encoding_ == String::ONE_BYTE_ENCODING) {
276 Append<uint8_t, uint8_t>(c);
278 Append<uint8_t, uc16>(c);
282 INLINE(void AppendCString(const char* s)) {
283 const uint8_t* u = reinterpret_cast<const uint8_t*>(s);
284 if (encoding_ == String::ONE_BYTE_ENCODING) {
285 while (*u != '\0') Append<uint8_t, uint8_t>(*(u++));
287 while (*u != '\0') Append<uint8_t, uc16>(*(u++));
291 INLINE(bool CurrentPartCanFit(int length)) {
292 return part_length_ - current_index_ > length;
295 void AppendString(Handle<String> string);
297 MaybeHandle<String> Finish();
299 // Change encoding to two-byte.
300 void ChangeEncoding() {
301 DCHECK_EQ(String::ONE_BYTE_ENCODING, encoding_);
303 encoding_ = String::TWO_BYTE_ENCODING;
307 template <typename DestChar>
310 explicit NoExtend(Handle<String> string, int offset) {
311 DCHECK(string->IsSeqOneByteString() || string->IsSeqTwoByteString());
312 if (sizeof(DestChar) == 1) {
313 start_ = reinterpret_cast<DestChar*>(
314 Handle<SeqOneByteString>::cast(string)->GetChars() + offset);
316 start_ = reinterpret_cast<DestChar*>(
317 Handle<SeqTwoByteString>::cast(string)->GetChars() + offset);
322 INLINE(void Append(DestChar c)) { *(cursor_++) = c; }
323 INLINE(void AppendCString(const char* s)) {
324 const uint8_t* u = reinterpret_cast<const uint8_t*>(s);
325 while (*u != '\0') Append(*(u++));
328 int written() { return cursor_ - start_; }
333 DisallowHeapAllocation no_gc_;
336 template <typename DestChar>
337 class NoExtendString : public NoExtend<DestChar> {
339 NoExtendString(Handle<String> string, int required_length)
340 : NoExtend<DestChar>(string, 0), string_(string) {
341 DCHECK(string->length() >= required_length);
345 Handle<SeqString> string = Handle<SeqString>::cast(string_);
346 int length = NoExtend<DestChar>::written();
347 *string_.location() = *SeqString::Truncate(string, length);
351 Handle<String> string_;
354 template <typename DestChar>
355 class NoExtendBuilder : public NoExtend<DestChar> {
357 NoExtendBuilder(IncrementalStringBuilder* builder, int required_length)
358 : NoExtend<DestChar>(builder->current_part(), builder->current_index_),
360 DCHECK(builder->CurrentPartCanFit(required_length));
364 builder_->current_index_ += NoExtend<DestChar>::written();
368 IncrementalStringBuilder* builder_;
372 Factory* factory() { return isolate_->factory(); }
374 INLINE(Handle<String> accumulator()) { return accumulator_; }
376 INLINE(void set_accumulator(Handle<String> string)) {
377 *accumulator_.location() = *string;
380 INLINE(Handle<String> current_part()) { return current_part_; }
382 INLINE(void set_current_part(Handle<String> string)) {
383 *current_part_.location() = *string;
386 // Add the current part to the accumulator.
389 // Finish the current part and allocate a new part.
392 // Shrink current part to the right size.
393 void ShrinkCurrentPart() {
394 DCHECK(current_index_ < part_length_);
395 set_current_part(SeqString::Truncate(
396 Handle<SeqString>::cast(current_part()), current_index_));
399 static const int kInitialPartLength = 32;
400 static const int kMaxPartLength = 16 * 1024;
401 static const int kPartLengthGrowthFactor = 2;
404 String::Encoding encoding_;
408 Handle<String> accumulator_;
409 Handle<String> current_part_;
413 template <typename SrcChar, typename DestChar>
414 void IncrementalStringBuilder::Append(SrcChar c) {
415 DCHECK_EQ(encoding_ == String::ONE_BYTE_ENCODING, sizeof(DestChar) == 1);
416 if (sizeof(DestChar) == 1) {
417 DCHECK_EQ(String::ONE_BYTE_ENCODING, encoding_);
418 SeqOneByteString::cast(*current_part_)
419 ->SeqOneByteStringSet(current_index_++, c);
421 DCHECK_EQ(String::TWO_BYTE_ENCODING, encoding_);
422 SeqTwoByteString::cast(*current_part_)
423 ->SeqTwoByteStringSet(current_index_++, c);
425 if (current_index_ == part_length_) Extend();
428 } // namespace v8::internal
430 #endif // V8_STRING_BUILDER_H_