Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(128)

Unified Diff: src/runtime.cc

Issue 669060: Add runtime function for string to array conversion. (Closed)
Patch Set: Created 10 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« src/factory.cc ('K') | « src/runtime.h ('k') | src/string.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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);
« src/factory.cc ('K') | « src/runtime.h ('k') | src/string.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698