string-builder.cc 10.9 KB
Newer Older
1 2 3 4
// Copyright 2014 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.

5
#include "src/strings/string-builder-inl.h"
6

7
#include "src/execution/isolate-inl.h"
8
#include "src/objects/fixed-array-inl.h"
9
#include "src/objects/js-array-inl.h"
10

11 12 13
namespace v8 {
namespace internal {

14
template <typename sinkchar>
15
void StringBuilderConcatHelper(String special, sinkchar* sink,
16
                               FixedArray fixed_array, int array_length) {
17 18 19
  DisallowHeapAllocation no_gc;
  int position = 0;
  for (int i = 0; i < array_length; i++) {
20 21
    Object element = fixed_array.get(i);
    if (element.IsSmi()) {
22 23 24 25 26 27 28 29 30 31
      // Smi encoding of position and length.
      int encoded_slice = Smi::ToInt(element);
      int pos;
      int len;
      if (encoded_slice > 0) {
        // Position and length encoded in one smi.
        pos = StringBuilderSubstringPosition::decode(encoded_slice);
        len = StringBuilderSubstringLength::decode(encoded_slice);
      } else {
        // Position and length encoded in two smis.
32 33
        Object obj = fixed_array.get(++i);
        DCHECK(obj.IsSmi());
34 35 36 37 38 39
        pos = Smi::ToInt(obj);
        len = -encoded_slice;
      }
      String::WriteToFlat(special, sink + position, pos, pos + len);
      position += len;
    } else {
40
      String string = String::cast(element);
41
      int element_length = string.length();
42 43 44 45 46 47
      String::WriteToFlat(string, sink + position, 0, element_length);
      position += element_length;
    }
  }
}

48
template void StringBuilderConcatHelper<uint8_t>(String special, uint8_t* sink,
49
                                                 FixedArray fixed_array,
50 51
                                                 int array_length);

52
template void StringBuilderConcatHelper<uc16>(String special, uc16* sink,
53
                                              FixedArray fixed_array,
54 55
                                              int array_length);

56
int StringBuilderConcatLength(int special_length, FixedArray fixed_array,
57 58 59 60 61
                              int array_length, bool* one_byte) {
  DisallowHeapAllocation no_gc;
  int position = 0;
  for (int i = 0; i < array_length; i++) {
    int increment = 0;
62 63
    Object elt = fixed_array.get(i);
    if (elt.IsSmi()) {
64 65 66 67 68 69 70 71 72 73 74 75 76 77
      // Smi encoding of position and length.
      int smi_value = Smi::ToInt(elt);
      int pos;
      int len;
      if (smi_value > 0) {
        // Position and length encoded in one smi.
        pos = StringBuilderSubstringPosition::decode(smi_value);
        len = StringBuilderSubstringLength::decode(smi_value);
      } else {
        // Position and length encoded in two smis.
        len = -smi_value;
        // Get the position and check that it is a positive smi.
        i++;
        if (i >= array_length) return -1;
78 79
        Object next_smi = fixed_array.get(i);
        if (!next_smi.IsSmi()) return -1;
80 81 82 83 84 85 86
        pos = Smi::ToInt(next_smi);
        if (pos < 0) return -1;
      }
      DCHECK_GE(pos, 0);
      DCHECK_GE(len, 0);
      if (pos > special_length || len > special_length - pos) return -1;
      increment = len;
87
    } else if (elt.IsString()) {
88
      String element = String::cast(elt);
89
      int element_length = element.length();
90
      increment = element_length;
91
      if (*one_byte && !element.IsOneByteRepresentation()) {
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
        *one_byte = false;
      }
    } else {
      return -1;
    }
    if (increment > String::kMaxLength - position) {
      return kMaxInt;  // Provoke throw on allocation.
    }
    position += increment;
  }
  return position;
}

FixedArrayBuilder::FixedArrayBuilder(Isolate* isolate, int initial_capacity)
    : array_(isolate->factory()->NewFixedArrayWithHoles(initial_capacity)),
      length_(0),
      has_non_smi_elements_(false) {
  // Require a non-zero initial size. Ensures that doubling the size to
  // extend the array will work.
  DCHECK_GT(initial_capacity, 0);
}

FixedArrayBuilder::FixedArrayBuilder(Handle<FixedArray> backing_store)
    : array_(backing_store), length_(0), has_non_smi_elements_(false) {
  // Require a non-zero initial size. Ensures that doubling the size to
  // extend the array will work.
  DCHECK_GT(backing_store->length(), 0);
}

bool FixedArrayBuilder::HasCapacity(int elements) {
  int length = array_->length();
  int required_length = length_ + elements;
  return (length >= required_length);
}

void FixedArrayBuilder::EnsureCapacity(Isolate* isolate, int elements) {
  int length = array_->length();
  int required_length = length_ + elements;
  if (length < required_length) {
    int new_length = length;
    do {
      new_length *= 2;
    } while (new_length < required_length);
    Handle<FixedArray> extended_array =
        isolate->factory()->NewFixedArrayWithHoles(new_length);
    array_->CopyTo(0, *extended_array, 0, length_);
    array_ = extended_array;
  }
}

142
void FixedArrayBuilder::Add(Object value) {
143
  DCHECK(!value.IsSmi());
144 145 146 147 148
  array_->set(length_, value);
  length_++;
  has_non_smi_elements_ = true;
}

149
void FixedArrayBuilder::Add(Smi value) {
150
  DCHECK(value.IsSmi());
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
  array_->set(length_, value);
  length_++;
}

int FixedArrayBuilder::capacity() { return array_->length(); }

Handle<JSArray> FixedArrayBuilder::ToJSArray(Handle<JSArray> target_array) {
  JSArray::SetContent(target_array, array_);
  target_array->set_length(Smi::FromInt(length_));
  return target_array;
}

ReplacementStringBuilder::ReplacementStringBuilder(Heap* heap,
                                                   Handle<String> subject,
                                                   int estimated_part_count)
    : heap_(heap),
167
      array_builder_(Isolate::FromHeap(heap), estimated_part_count),
168 169 170 171 172 173 174 175 176
      subject_(subject),
      character_count_(0),
      is_one_byte_(subject->IsOneByteRepresentation()) {
  // Require a non-zero initial size. Ensures that doubling the size to
  // extend the array will work.
  DCHECK_GT(estimated_part_count, 0);
}

void ReplacementStringBuilder::EnsureCapacity(int elements) {
177
  array_builder_.EnsureCapacity(Isolate::FromHeap(heap_), elements);
178 179 180 181 182
}

void ReplacementStringBuilder::AddString(Handle<String> string) {
  int length = string->length();
  DCHECK_GT(length, 0);
183
  AddElement(string);
184 185 186 187 188 189
  if (!string->IsOneByteRepresentation()) {
    is_one_byte_ = false;
  }
  IncrementCharacterCount(length);
}

190
MaybeHandle<String> ReplacementStringBuilder::ToString() {
191
  Isolate* isolate = Isolate::FromHeap(heap_);
192 193 194 195 196 197 198 199 200 201 202 203
  if (array_builder_.length() == 0) {
    return isolate->factory()->empty_string();
  }

  Handle<String> joined_string;
  if (is_one_byte_) {
    Handle<SeqOneByteString> seq;
    ASSIGN_RETURN_ON_EXCEPTION(
        isolate, seq, isolate->factory()->NewRawOneByteString(character_count_),
        String);

    DisallowHeapAllocation no_gc;
204
    uint8_t* char_buffer = seq->GetChars(no_gc);
205 206 207 208 209 210 211 212 213 214 215
    StringBuilderConcatHelper(*subject_, char_buffer, *array_builder_.array(),
                              array_builder_.length());
    joined_string = Handle<String>::cast(seq);
  } else {
    // Two-byte.
    Handle<SeqTwoByteString> seq;
    ASSIGN_RETURN_ON_EXCEPTION(
        isolate, seq, isolate->factory()->NewRawTwoByteString(character_count_),
        String);

    DisallowHeapAllocation no_gc;
216
    uc16* char_buffer = seq->GetChars(no_gc);
217 218 219 220 221 222 223
    StringBuilderConcatHelper(*subject_, char_buffer, *array_builder_.array(),
                              array_builder_.length());
    joined_string = Handle<String>::cast(seq);
  }
  return joined_string;
}

224
void ReplacementStringBuilder::AddElement(Handle<Object> element) {
225
  DCHECK(element->IsSmi() || element->IsString());
226
  EnsureCapacity(1);
227 228
  DisallowHeapAllocation no_gc;
  array_builder_.Add(*element);
229
}
230 231 232 233 234 235 236 237

IncrementalStringBuilder::IncrementalStringBuilder(Isolate* isolate)
    : isolate_(isolate),
      encoding_(String::ONE_BYTE_ENCODING),
      overflowed_(false),
      part_length_(kInitialPartLength),
      current_index_(0) {
  // Create an accumulator handle starting with the empty string.
238 239
  accumulator_ =
      Handle<String>::New(ReadOnlyRoots(isolate).empty_string(), isolate);
240 241 242 243
  current_part_ =
      factory()->NewRawOneByteString(part_length_).ToHandleChecked();
}

244 245 246
int IncrementalStringBuilder::Length() const {
  return accumulator_->length() + current_index_;
}
247

248
void IncrementalStringBuilder::Accumulate(Handle<String> new_part) {
249
  Handle<String> new_accumulator;
250
  if (accumulator()->length() + new_part->length() > String::kMaxLength) {
251 252 253 254
    // Set the flag and carry on. Delay throwing the exception till the end.
    new_accumulator = factory()->empty_string();
    overflowed_ = true;
  } else {
255 256
    new_accumulator =
        factory()->NewConsString(accumulator(), new_part).ToHandleChecked();
257 258 259 260 261
  }
  set_accumulator(new_accumulator);
}

void IncrementalStringBuilder::Extend() {
262 263
  DCHECK_EQ(current_index_, current_part()->length());
  Accumulate(current_part());
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
  if (part_length_ <= kMaxPartLength / kPartLengthGrowthFactor) {
    part_length_ *= kPartLengthGrowthFactor;
  }
  Handle<String> new_part;
  if (encoding_ == String::ONE_BYTE_ENCODING) {
    new_part = factory()->NewRawOneByteString(part_length_).ToHandleChecked();
  } else {
    new_part = factory()->NewRawTwoByteString(part_length_).ToHandleChecked();
  }
  // Reuse the same handle to avoid being invalidated when exiting handle scope.
  set_current_part(new_part);
  current_index_ = 0;
}

MaybeHandle<String> IncrementalStringBuilder::Finish() {
  ShrinkCurrentPart();
280
  Accumulate(current_part());
281 282 283 284 285 286
  if (overflowed_) {
    THROW_NEW_ERROR(isolate_, NewInvalidStringLengthError(), String);
  }
  return accumulator();
}

287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
// Short strings can be copied directly to {current_part_}.
// Requires the IncrementalStringBuilder to either have two byte encoding or
// the incoming string to have one byte representation "underneath" (The
// one byte check requires the string to be flat).
bool IncrementalStringBuilder::CanAppendByCopy(Handle<String> string) {
  constexpr int kMaxStringLengthForCopy = 16;
  const bool representation_ok =
      encoding_ == String::TWO_BYTE_ENCODING ||
      (string->IsFlat() && String::IsOneByteRepresentationUnderneath(*string));

  return representation_ok && string->length() <= kMaxStringLengthForCopy &&
         CurrentPartCanFit(string->length());
}

void IncrementalStringBuilder::AppendStringByCopy(Handle<String> string) {
  DCHECK(CanAppendByCopy(string));

  Handle<SeqOneByteString> part =
      Handle<SeqOneByteString>::cast(current_part());
  {
    DisallowHeapAllocation no_gc;
    String::WriteToFlat(*string, part->GetChars(no_gc) + current_index_, 0,
                        string->length());
  }
  current_index_ += string->length();
  DCHECK(current_index_ <= part_length_);
  if (current_index_ == part_length_) Extend();
}

316
void IncrementalStringBuilder::AppendString(Handle<String> string) {
317 318 319 320 321
  if (CanAppendByCopy(string)) {
    AppendStringByCopy(string);
    return;
  }

322 323 324
  ShrinkCurrentPart();
  part_length_ = kInitialPartLength;  // Allocate conservatively.
  Extend();  // Attach current part and allocate new part.
325
  Accumulate(string);
326
}
327 328
}  // namespace internal
}  // namespace v8