V8 Project
string-builder.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_STRING_BUILDER_H_
6 #define V8_STRING_BUILDER_H_
7 
8 namespace v8 {
9 namespace internal {
10 
13 
19 
20 
21 template <typename sinkchar>
22 static inline void StringBuilderConcatHelper(String* special, sinkchar* sink,
23  FixedArray* fixed_array,
24  int array_length) {
26  int position = 0;
27  for (int i = 0; i < array_length; i++) {
28  Object* element = fixed_array->get(i);
29  if (element->IsSmi()) {
30  // Smi encoding of position and length.
31  int encoded_slice = Smi::cast(element)->value();
32  int pos;
33  int len;
34  if (encoded_slice > 0) {
35  // Position and length encoded in one smi.
36  pos = StringBuilderSubstringPosition::decode(encoded_slice);
37  len = StringBuilderSubstringLength::decode(encoded_slice);
38  } else {
39  // Position and length encoded in two smis.
40  Object* obj = fixed_array->get(++i);
41  DCHECK(obj->IsSmi());
42  pos = Smi::cast(obj)->value();
43  len = -encoded_slice;
44  }
45  String::WriteToFlat(special, sink + position, pos, pos + len);
46  position += len;
47  } else {
48  String* string = String::cast(element);
49  int element_length = string->length();
50  String::WriteToFlat(string, sink + position, 0, element_length);
51  position += element_length;
52  }
53  }
54 }
55 
56 
57 // Returns the result length of the concatenation.
58 // On illegal argument, -1 is returned.
59 static inline int StringBuilderConcatLength(int special_length,
60  FixedArray* fixed_array,
61  int array_length, bool* one_byte) {
63  int position = 0;
64  for (int i = 0; i < array_length; i++) {
65  int increment = 0;
66  Object* elt = fixed_array->get(i);
67  if (elt->IsSmi()) {
68  // Smi encoding of position and length.
69  int smi_value = Smi::cast(elt)->value();
70  int pos;
71  int len;
72  if (smi_value > 0) {
73  // Position and length encoded in one smi.
75  len = StringBuilderSubstringLength::decode(smi_value);
76  } else {
77  // Position and length encoded in two smis.
78  len = -smi_value;
79  // Get the position and check that it is a positive smi.
80  i++;
81  if (i >= array_length) return -1;
82  Object* next_smi = fixed_array->get(i);
83  if (!next_smi->IsSmi()) return -1;
84  pos = Smi::cast(next_smi)->value();
85  if (pos < 0) return -1;
86  }
87  DCHECK(pos >= 0);
88  DCHECK(len >= 0);
89  if (pos > special_length || len > special_length - pos) return -1;
90  increment = len;
91  } else if (elt->IsString()) {
92  String* element = String::cast(elt);
93  int element_length = element->length();
94  increment = element_length;
95  if (*one_byte && !element->HasOnlyOneByteChars()) {
96  *one_byte = false;
97  }
98  } else {
99  return -1;
100  }
101  if (increment > String::kMaxLength - position) {
102  return kMaxInt; // Provoke throw on allocation.
103  }
104  position += increment;
105  }
106  return position;
107 }
108 
109 
111  public:
112  explicit FixedArrayBuilder(Isolate* isolate, int initial_capacity)
113  : array_(isolate->factory()->NewFixedArrayWithHoles(initial_capacity)),
114  length_(0),
116  // Require a non-zero initial size. Ensures that doubling the size to
117  // extend the array will work.
118  DCHECK(initial_capacity > 0);
119  }
120 
121  explicit FixedArrayBuilder(Handle<FixedArray> backing_store)
122  : array_(backing_store), length_(0), has_non_smi_elements_(false) {
123  // Require a non-zero initial size. Ensures that doubling the size to
124  // extend the array will work.
125  DCHECK(backing_store->length() > 0);
126  }
127 
128  bool HasCapacity(int elements) {
129  int length = array_->length();
130  int required_length = length_ + elements;
131  return (length >= required_length);
132  }
133 
134  void EnsureCapacity(int elements) {
135  int length = array_->length();
136  int required_length = length_ + elements;
137  if (length < required_length) {
138  int new_length = length;
139  do {
140  new_length *= 2;
141  } while (new_length < required_length);
142  Handle<FixedArray> extended_array =
143  array_->GetIsolate()->factory()->NewFixedArrayWithHoles(new_length);
144  array_->CopyTo(0, *extended_array, 0, length_);
145  array_ = extended_array;
146  }
147  }
148 
149  void Add(Object* value) {
150  DCHECK(!value->IsSmi());
151  DCHECK(length_ < capacity());
152  array_->set(length_, value);
153  length_++;
154  has_non_smi_elements_ = true;
155  }
156 
157  void Add(Smi* value) {
158  DCHECK(value->IsSmi());
159  DCHECK(length_ < capacity());
160  array_->set(length_, value);
161  length_++;
162  }
163 
165 
166  int length() { return length_; }
167 
168  int capacity() { return array_->length(); }
169 
171  JSArray::SetContent(target_array, array_);
172  target_array->set_length(Smi::FromInt(length_));
173  return target_array;
174  }
175 
176 
177  private:
179  int length_;
181 };
182 
183 
185  public:
187  int estimated_part_count)
188  : heap_(heap),
189  array_builder_(heap->isolate(), estimated_part_count),
190  subject_(subject),
191  character_count_(0),
192  is_one_byte_(subject->IsOneByteRepresentation()) {
193  // Require a non-zero initial size. Ensures that doubling the size to
194  // extend the array will work.
195  DCHECK(estimated_part_count > 0);
196  }
197 
198  static inline void AddSubjectSlice(FixedArrayBuilder* builder, int from,
199  int to) {
200  DCHECK(from >= 0);
201  int length = to - from;
202  DCHECK(length > 0);
205  int encoded_slice = StringBuilderSubstringLength::encode(length) |
207  builder->Add(Smi::FromInt(encoded_slice));
208  } else {
209  // Otherwise encode as two smis.
210  builder->Add(Smi::FromInt(-length));
211  builder->Add(Smi::FromInt(from));
212  }
213  }
214 
215 
216  void EnsureCapacity(int elements) { array_builder_.EnsureCapacity(elements); }
217 
218 
219  void AddSubjectSlice(int from, int to) {
221  IncrementCharacterCount(to - from);
222  }
223 
224 
225  void AddString(Handle<String> string) {
226  int length = string->length();
227  DCHECK(length > 0);
228  AddElement(*string);
229  if (!string->IsOneByteRepresentation()) {
230  is_one_byte_ = false;
231  }
232  IncrementCharacterCount(length);
233  }
234 
235 
237  Isolate* isolate = heap_->isolate();
238  if (array_builder_.length() == 0) {
239  return isolate->factory()->empty_string();
240  }
241 
242  Handle<String> joined_string;
243  if (is_one_byte_) {
246  isolate, seq,
247  isolate->factory()->NewRawOneByteString(character_count_), String);
248 
250  uint8_t* char_buffer = seq->GetChars();
253  joined_string = Handle<String>::cast(seq);
254  } else {
255  // Two-byte.
258  isolate, seq,
259  isolate->factory()->NewRawTwoByteString(character_count_), String);
260 
262  uc16* char_buffer = seq->GetChars();
265  joined_string = Handle<String>::cast(seq);
266  }
267  return joined_string;
268  }
269 
270 
271  void IncrementCharacterCount(int by) {
275  } else {
276  character_count_ += by;
277  }
278  }
279 
280  private:
281  void AddElement(Object* element) {
282  DCHECK(element->IsSmi() || element->IsString());
284  array_builder_.Add(element);
285  }
286 
292 };
293 }
294 } // namespace v8::internal
295 
296 #endif // V8_STRING_BUILDER_H_
Handle< JSArray > ToJSArray(Handle< JSArray > target_array)
FixedArrayBuilder(Isolate *isolate, int initial_capacity)
FixedArrayBuilder(Handle< FixedArray > backing_store)
Handle< FixedArray > array()
void EnsureCapacity(int elements)
Object * get(int index)
Definition: objects-inl.h:2165
static Handle< T > cast(Handle< S > that)
Definition: handles.h:116
Isolate * isolate()
Definition: heap-inl.h:589
Factory * factory()
Definition: isolate.h:982
static void SetContent(Handle< JSArray > array, Handle< FixedArrayBase > storage)
Definition: objects-inl.h:6986
void AddString(Handle< String > string)
ReplacementStringBuilder(Heap *heap, Handle< String > subject, int estimated_part_count)
static void AddSubjectSlice(FixedArrayBuilder *builder, int from, int to)
void AddSubjectSlice(int from, int to)
static Smi * FromInt(int value)
Definition: objects-inl.h:1321
static void WriteToFlat(String *source, sinkchar *sink, int from, int to)
Definition: objects.cc:8370
static const int kMaxLength
Definition: objects.h:8820
enable harmony numeric enable harmony object literal extensions Optimize object Array DOM strings and string trace pretenuring decisions of HAllocate instructions Enables optimizations which favor memory size over execution speed maximum source size in bytes considered for a single inlining maximum cumulative number of AST nodes considered for inlining trace the tracking of allocation sites deoptimize every n garbage collections perform array bounds checks elimination analyze liveness of environment slots and zap dead values flushes the cache of optimized code for closures on every GC allow uint32 values on optimize frames if they are used only in safe operations track concurrent recompilation artificial compilation delay in ms do not emit check maps for constant values that have a leaf deoptimize the optimized code if the layout of the maps changes enable context specialization in TurboFan execution budget before interrupt is triggered max percentage of megamorphic generic ICs to allow optimization enable use of SAHF instruction if enable use of VFP3 instructions if available enable use of NEON instructions if enable use of SDIV and UDIV instructions if enable use of MLS instructions if enable loading bit constant by means of movw movt instruction enable unaligned accesses for enable use of d16 d31 registers on ARM this requires VFP3 force all emitted branches to be in long enable alignment of csp to bytes on platforms which prefer the register to always be expose gc extension under the specified name show built in functions in stack traces use random jit cookie to mask large constants minimum length for automatic enable preparsing CPU profiler sampling interval in microseconds trace out of bounds accesses to external arrays default size of stack region v8 is allowed to maximum length of function source code printed in a stack trace min size of a semi the new space consists of two semi spaces print one trace line following each garbage collection do not print trace line after scavenger collection print cumulative GC statistics in only print modified registers Trace simulator debug messages Implied by trace sim abort randomize hashes to avoid predictable hash Fixed seed to use to hash property Print the time it takes to deserialize the snapshot A filename with extra code to be included in the A file to write the raw snapshot bytes to(mksnapshot only)") DEFINE_STRING(raw_context_file
#define ASSIGN_RETURN_ON_EXCEPTION(isolate, dst, call, T)
Definition: isolate.h:135
#define DCHECK(condition)
Definition: logging.h:205
const int kStringBuilderConcatHelperPositionBits
static void StringBuilderConcatHelper(String *special, sinkchar *sink, FixedArray *fixed_array, int array_length)
BitField< int, kStringBuilderConcatHelperLengthBits, kStringBuilderConcatHelperPositionBits > StringBuilderSubstringPosition
const int kMaxInt
Definition: globals.h:109
STATIC_ASSERT(sizeof(CPURegister)==sizeof(Register))
const int kStringBuilderConcatHelperLengthBits
uint16_t uc16
Definition: globals.h:184
BitField< int, 0, kStringBuilderConcatHelperLengthBits > StringBuilderSubstringLength
static int StringBuilderConcatLength(int special_length, FixedArray *fixed_array, int array_length, bool *one_byte)
Debugger support for the V8 JavaScript engine.
Definition: accessors.cc:20