Chromium Code Reviews| Index: src/objects.cc |
| diff --git a/src/objects.cc b/src/objects.cc |
| index 724a734a2145d6b682725a6b9b7e9d5b7c2f26af..00b391edcb7963a3e78202c9d569b4b45e0e5fc5 100644 |
| --- a/src/objects.cc |
| +++ b/src/objects.cc |
| @@ -5182,7 +5182,7 @@ uint16_t ConsString::ConsStringGet(int index) { |
| // Check for a flattened cons string |
| if (second()->length() == 0) { |
| String* left = first(); |
| - return left->Get(index); |
| + return FlatString::cast(left)->FlatStringGet(index); |
| } |
| String* string = String::cast(this); |
| @@ -5198,7 +5198,7 @@ uint16_t ConsString::ConsStringGet(int index) { |
| string = cons_string->second(); |
| } |
| } else { |
| - return string->Get(index); |
| + return FlatString::cast(string)->FlatStringGet(index); |
| } |
| } |
| @@ -5207,6 +5207,44 @@ uint16_t ConsString::ConsStringGet(int index) { |
| } |
| +MaybeObject* ConsString::ConsStringGetMayFlatten(int index) { |
| + ASSERT(index >= 0); |
| + ASSERT(index < this->length()); |
| + |
| + String* string = this; |
| + if (this->GetIsolate()->ShouldFlattenString()) { |
| + MaybeObject* flatten_result = this->TryFlatten(); |
| + if (flatten_result->IsFailure()) return flatten_result; |
| + string = String::cast(flatten_result->ToObjectUnchecked()); |
| + } else { |
| + int recurse_limit = 7; |
|
Vitaly Repeshko
2011/05/12 15:54:53
Make this a (documented) constant in ConsString.
|
| + String* cursor = string; |
| + int index_in_cursor = index; |
| + while (cursor->IsConsString()) { |
| + ConsString* cons = ConsString::cast(cursor); |
| + String* first = cons->first(); |
| + int first_length = first->length(); |
| + if (index_in_cursor < first_length) { |
| + cursor = first; |
| + } else { |
| + cursor = cons->second(); |
| + index_in_cursor -= first_length; |
| + } |
| + if (--recurse_limit <= 0) { |
| + MaybeObject* flatten_result = this->TryFlatten(); |
| + if (flatten_result->IsFailure()) return flatten_result; |
| + cursor = String::cast(flatten_result->ToObjectUnchecked()); |
|
Vitaly Repeshko
2011/05/12 15:54:53
It's more straightforward to simply return Smi::F
|
| + index_in_cursor = index; |
| + break; |
| + } |
| + } |
| + string = cursor; |
| + index = index_in_cursor; |
| + } |
| + return Smi::FromInt(string->Get(index)); |
| +} |
| + |
| + |
| template <typename sinkchar> |
| void String::WriteToFlat(String* src, |
| sinkchar* sink, |