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

Unified Diff: vm/object.cc

Issue 10765017: Add quotes around strings when generating source from the token stream. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/runtime/
Patch Set: Created 8 years, 5 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
« no previous file with comments | « vm/object.h ('k') | vm/snapshot_test.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: vm/object.cc
===================================================================
--- vm/object.cc (revision 9534)
+++ vm/object.cc (working copy)
@@ -4456,10 +4456,29 @@
String& literal = String::Handle();
String& blank = String::Handle(String::New(" "));
String& newline = String::Handle(String::New("\n"));
+ String& double_quotes = String::Handle(String::New("\""));
for (intptr_t i = 0; i < Length(); i++) {
Token::Kind kind = KindAt(i);
literal = LiteralAt(i);
- literals.Add(literal);
+ if (kind == Token::kSTRING) {
+ bool escape_quotes = false;
+ for (intptr_t i = 0; i < literal.Length(); i++) {
+ if (literal.CharAt(i) == '"') {
+ escape_quotes = true;
+ break;
+ }
+ }
+ literals.Add(double_quotes);
+ if (escape_quotes) {
+ literal = String::EscapeDoubleQuotes(literal);
+ literals.Add(literal);
+ } else {
+ literals.Add(literal);
+ }
+ literals.Add(double_quotes);
+ } else {
+ literals.Add(literal);
+ }
if (kind == Token::kLBRACE) {
literals.Add(newline);
} else {
@@ -8311,6 +8330,21 @@
}
+RawString* String::EscapeDoubleQuotes(const String& str) {
+ if (str.IsOneByteString()) {
+ const OneByteString& onestr = OneByteString::Cast(str);
+ return onestr.EscapeDoubleQuotes();
+ }
+ if (str.IsTwoByteString()) {
+ const TwoByteString& twostr = TwoByteString::Cast(str);
+ return twostr.EscapeDoubleQuotes();
+ }
+ ASSERT(str.IsFourByteString());
+ const FourByteString& fourstr = FourByteString::Cast(str);
+ return fourstr.EscapeDoubleQuotes();
+}
+
+
static void GrowSymbolTable(const Array& symbol_table, intptr_t table_size) {
// TODO(iposva): Avoid exponential growth.
intptr_t new_table_size = table_size * 2;
@@ -8628,6 +8662,34 @@
}
+RawOneByteString* OneByteString::EscapeDoubleQuotes() const {
+ intptr_t len = Length();
+ if (len > 0) {
+ intptr_t num_quotes = 0;
+ intptr_t index = 0;
+ for (intptr_t i = 0; i < len; i++) {
+ if (*CharAddr(i) == '"') {
+ num_quotes += 1;
+ }
+ }
+ const OneByteString& dststr = OneByteString::Handle(
+ OneByteString::New(len + num_quotes, Heap::kNew));
+ for (intptr_t i = 0; i < len; i++) {
+ if (*CharAddr(i) == '"') {
+ *(dststr.CharAddr(index)) = '\\';
+ *(dststr.CharAddr(index + 1)) = '"';
+ index += 2;
+ } else {
+ *(dststr.CharAddr(index)) = *CharAddr(i);
+ index += 1;
+ }
+ }
+ return dststr.raw();
+ }
+ return OneByteString::null();
+}
+
+
RawOneByteString* OneByteString::New(intptr_t len,
Heap::Space space) {
Isolate* isolate = Isolate::Current();
@@ -8741,6 +8803,34 @@
}
+RawTwoByteString* TwoByteString::EscapeDoubleQuotes() const {
+ intptr_t len = Length();
+ if (len > 0) {
+ intptr_t num_quotes = 0;
+ intptr_t index = 0;
+ for (intptr_t i = 0; i < len; i++) {
+ if (*CharAddr(i) == '"') {
+ num_quotes += 1;
+ }
+ }
+ const TwoByteString& dststr = TwoByteString::Handle(
+ TwoByteString::New(len + num_quotes, Heap::kNew));
+ for (intptr_t i = 0; i < len; i++) {
+ if (*CharAddr(i) == '"') {
+ *(dststr.CharAddr(index)) = '\\';
+ *(dststr.CharAddr(index + 1)) = '"';
+ index += 2;
+ } else {
+ *(dststr.CharAddr(index)) = *CharAddr(i);
+ index += 1;
+ }
+ }
+ return dststr.raw();
+ }
+ return TwoByteString::null();
+}
+
+
RawTwoByteString* TwoByteString::New(intptr_t len,
Heap::Space space) {
Isolate* isolate = Isolate::Current();
@@ -8844,6 +8934,34 @@
}
+RawFourByteString* FourByteString::EscapeDoubleQuotes() const {
+ intptr_t len = Length();
+ if (len > 0) {
+ intptr_t num_quotes = 0;
+ intptr_t index = 0;
+ for (intptr_t i = 0; i < len; i++) {
+ if (*CharAddr(i) == '"') {
+ num_quotes += 1;
+ }
+ }
+ const FourByteString& dststr = FourByteString::Handle(
+ FourByteString::New(len + num_quotes, Heap::kNew));
+ for (intptr_t i = 0; i < len; i++) {
+ if (*CharAddr(i) == '"') {
+ *(dststr.CharAddr(index)) = '\\';
+ *(dststr.CharAddr(index + 1)) = '"';
+ index += 2;
+ } else {
+ *(dststr.CharAddr(index)) = *CharAddr(i);
+ index += 1;
+ }
+ }
+ return dststr.raw();
+ }
+ return FourByteString::null();
+}
+
+
RawFourByteString* FourByteString::New(intptr_t len,
Heap::Space space) {
Isolate* isolate = Isolate::Current();
« no previous file with comments | « vm/object.h ('k') | vm/snapshot_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698