Chromium Code Reviews| Index: src/runtime.cc |
| diff --git a/src/runtime.cc b/src/runtime.cc |
| index 8bb7ae6caaa5b119e13ec017db12a552d4e3a42a..b6be2b9ee07a7883fafc0179a07586ab7497374b 100644 |
| --- a/src/runtime.cc |
| +++ b/src/runtime.cc |
| @@ -4213,6 +4213,73 @@ static Object* Runtime_StringTrim(Arguments args) { |
| return s->SubString(left, right); |
| } |
| + |
| +// Copies ascii characters to the given fixed array looking up |
| +// one-char strings in the cache. Gives up on the first char that is |
| +// not in the cache. Returns the length of the successfully copied |
| +// prefix. |
| +static int CopyCachedAsciiCharsToArray(const char* chars, |
| + FixedArray* elements, |
| + int length) { |
| + AssertNoAllocation nogc; |
| + FixedArray* ascii_cache = Heap::single_character_string_cache(); |
| + Object* undefined = Heap::undefined_value(); |
| + for (int i = 0; i < length; ++i) { |
| + Object* value = ascii_cache->get(chars[i]); |
| + if (value == undefined) return i; |
| + ASSERT(!Heap::InNewSpace(value)); |
| + elements->set(i, value, SKIP_WRITE_BARRIER); |
| + } |
| + return length; |
| +} |
| + |
| + |
| +// Converts a String to JSArray. |
| +// For example, "foo" => ["f", "o", "o"]. |
| +static Object* Runtime_StringToArray(Arguments args) { |
| + HandleScope scope; |
| + ASSERT(args.length() == 1); |
| + CONVERT_ARG_CHECKED(String, s, 0); |
| + |
| + s->TryFlatten(); |
| + const int length = s->length(); |
| + |
| + Handle<FixedArray> elements = Factory::NewUninitializedFixedArray(length); |
| + if (s->IsFlat()) { |
| + if (s->IsAsciiRepresentation()) { |
| + Vector<const char> chars = s->ToAsciiVector(); |
| + int num_copied_from_cache = CopyCachedAsciiCharsToArray(chars.start(), |
| + *elements, |
| + length); |
| + for (int i = num_copied_from_cache; i < length; ++i) { |
| + elements->set(i, *LookupSingleCharacterStringFromCode(chars[i]), |
| + UPDATE_WRITE_BARRIER); |
|
Mads Ager (chromium)
2010/03/04 13:30:56
Just omit the WriteBarrierMode here since the defa
Vitaly Repeshko
2010/03/04 14:03:12
Done.
|
| + } |
| + } else { |
| + ASSERT(s->IsTwoByteRepresentation()); |
| + Vector<const uc16> chars = s->ToUC16Vector(); |
| + for (int i = 0; i < length; ++i) { |
| + elements->set(i, *LookupSingleCharacterStringFromCode(chars[i]), |
| + UPDATE_WRITE_BARRIER); |
| + } |
| + } |
| + } else { |
| + for (int i = 0; i < length; ++i) { |
| + elements->set(i, *LookupSingleCharacterStringFromCode(s->Get(i)), |
| + UPDATE_WRITE_BARRIER); |
| + } |
| + } |
| + |
| +#ifdef DEBUG |
| + for (int i = 0; i < length; ++i) { |
| + ASSERT(String::cast(elements->get(i))->length() == 1); |
| + } |
| +#endif |
| + |
| + return *Factory::NewJSArrayWithElements(elements); |
| +} |
| + |
| + |
| bool Runtime::IsUpperCaseChar(uint16_t ch) { |
| unibrow::uchar chars[unibrow::ToUppercase::kMaxWidth]; |
| int char_length = to_upper_mapping.get(ch, 0, chars); |