Index: src/runtime.cc |
=================================================================== |
--- src/runtime.cc (revision 6322) |
+++ src/runtime.cc (working copy) |
@@ -4621,12 +4621,12 @@ |
} |
-template <typename Char, typename StringType> |
+template <typename Char, typename StringType, bool comma> |
static MaybeObject* SlowQuoteJsonString(Vector<const Char> characters) { |
Erik Corry
2011/01/14 14:42:57
I think it would be nice to know what the performa
sandholm
2011/01/16 21:08:43
Having this as a normal argument makes the change
|
int length = characters.length(); |
const Char* read_cursor = characters.start(); |
const Char* end = read_cursor + length; |
- const int kSpaceForQuotes = 2; |
+ const int kSpaceForQuotes = 2 + (comma ? 1 :0); |
int quoted_length = kSpaceForQuotes; |
while (read_cursor < end) { |
Char c = *(read_cursor++); |
@@ -4645,8 +4645,9 @@ |
Char* write_cursor = reinterpret_cast<Char*>( |
new_string->address() + SeqAsciiString::kHeaderSize); |
+ if (comma) *(write_cursor++) = ','; |
*(write_cursor++) = '"'; |
- |
+ |
read_cursor = characters.start(); |
while (read_cursor < end) { |
Char c = *(read_cursor++); |
@@ -4666,14 +4667,14 @@ |
} |
-template <typename Char, typename StringType> |
+template <typename Char, typename StringType, bool comma> |
static MaybeObject* QuoteJsonString(Vector<const Char> characters) { |
int length = characters.length(); |
Counters::quote_json_char_count.Increment(length); |
- const int kSpaceForQuotes = 2; |
+ const int kSpaceForQuotes = 2 + (comma ? 1 :0); |
int worst_case_length = length * kJsonQuoteWorstCaseBlowup + kSpaceForQuotes; |
if (worst_case_length > kMaxGuaranteedNewSpaceString) { |
- return SlowQuoteJsonString<Char, StringType>(characters); |
+ return SlowQuoteJsonString<Char, StringType, comma>(characters); |
} |
MaybeObject* new_alloc = AllocateRawString<StringType>(worst_case_length); |
@@ -4686,7 +4687,7 @@ |
// handle it being allocated in old space as may happen in the third |
// attempt. See CALL_AND_RETRY in heap-inl.h and similar code in |
// CEntryStub::GenerateCore. |
- return SlowQuoteJsonString<Char, StringType>(characters); |
+ return SlowQuoteJsonString<Char, StringType, comma>(characters); |
} |
StringType* new_string = StringType::cast(new_object); |
ASSERT(Heap::new_space()->Contains(new_string)); |
@@ -4694,6 +4695,7 @@ |
STATIC_ASSERT(SeqTwoByteString::kHeaderSize == SeqAsciiString::kHeaderSize); |
Char* write_cursor = reinterpret_cast<Char*>( |
new_string->address() + SeqAsciiString::kHeaderSize); |
+ if (comma) *(write_cursor++) = ','; |
*(write_cursor++) = '"'; |
const Char* read_cursor = characters.start(); |
@@ -4744,14 +4746,33 @@ |
ASSERT(str->IsFlat()); |
} |
if (str->IsTwoByteRepresentation()) { |
- return QuoteJsonString<uc16, SeqTwoByteString>(str->ToUC16Vector()); |
+ return QuoteJsonString<uc16, SeqTwoByteString, false>(str->ToUC16Vector()); |
} else { |
- return QuoteJsonString<char, SeqAsciiString>(str->ToAsciiVector()); |
+ return QuoteJsonString<char, SeqAsciiString, false>(str->ToAsciiVector()); |
} |
} |
+static MaybeObject* Runtime_QuoteJSONStringComma(Arguments args) { |
+ NoHandleAllocation ha; |
+ CONVERT_CHECKED(String, str, args[0]); |
+ if (!str->IsFlat()) { |
+ MaybeObject* try_flatten = str->TryFlatten(); |
+ Object* flat; |
+ if (!try_flatten->ToObject(&flat)) { |
+ return try_flatten; |
+ } |
+ str = String::cast(flat); |
+ ASSERT(str->IsFlat()); |
+ } |
+ if (str->IsTwoByteRepresentation()) { |
+ return QuoteJsonString<uc16, SeqTwoByteString, true>(str->ToUC16Vector()); |
+ } else { |
+ return QuoteJsonString<char, SeqAsciiString, true>(str->ToAsciiVector()); |
+ } |
+} |
+ |
static MaybeObject* Runtime_StringParseInt(Arguments args) { |
NoHandleAllocation ha; |