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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #include "vm/object.h" 5 #include "vm/object.h"
6 6
7 #include "platform/assert.h" 7 #include "platform/assert.h"
8 #include "vm/assembler.h" 8 #include "vm/assembler.h"
9 #include "vm/bigint_operations.h" 9 #include "vm/bigint_operations.h"
10 #include "vm/bootstrap.h" 10 #include "vm/bootstrap.h"
(...skipping 4405 matching lines...) Expand 10 before | Expand all | Expand 10 after
4416 } 4416 }
4417 } 4417 }
4418 4418
4419 4419
4420 RawString* TokenStream::GenerateSource() const { 4420 RawString* TokenStream::GenerateSource() const {
4421 GrowableObjectArray& literals = 4421 GrowableObjectArray& literals =
4422 GrowableObjectArray::Handle(GrowableObjectArray::New(Length())); 4422 GrowableObjectArray::Handle(GrowableObjectArray::New(Length()));
4423 String& literal = String::Handle(); 4423 String& literal = String::Handle();
4424 String& blank = String::Handle(String::New(" ")); 4424 String& blank = String::Handle(String::New(" "));
4425 String& newline = String::Handle(String::New("\n")); 4425 String& newline = String::Handle(String::New("\n"));
4426 String& double_quotes = String::Handle(String::New("\""));
4426 for (intptr_t i = 0; i < Length(); i++) { 4427 for (intptr_t i = 0; i < Length(); i++) {
4427 Token::Kind kind = KindAt(i); 4428 Token::Kind kind = KindAt(i);
4428 literal = LiteralAt(i); 4429 literal = LiteralAt(i);
4429 literals.Add(literal); 4430 if (kind == Token::kSTRING) {
4431 bool escape_quotes = false;
4432 for (intptr_t i = 0; i < literal.Length(); i++) {
4433 if (literal.CharAt(i) == '"') {
4434 escape_quotes = true;
4435 break;
4436 }
4437 }
4438 literals.Add(double_quotes);
4439 if (escape_quotes) {
4440 literal = String::EscapeQuotes(literal);
4441 literals.Add(literal);
4442 } else {
4443 literals.Add(literal);
4444 }
4445 literals.Add(double_quotes);
4446 } else {
4447 literals.Add(literal);
4448 }
4430 if (kind == Token::kLBRACE) { 4449 if (kind == Token::kLBRACE) {
4431 literals.Add(newline); 4450 literals.Add(newline);
4432 } else { 4451 } else {
4433 literals.Add(blank); 4452 literals.Add(blank);
4434 } 4453 }
4435 } 4454 }
4436 const Array& source = Array::Handle(Array::MakeArray(literals)); 4455 const Array& source = Array::Handle(Array::MakeArray(literals));
4437 return String::ConcatAll(source); 4456 return String::ConcatAll(source);
4438 } 4457 }
4439 4458
(...skipping 3826 matching lines...) Expand 10 before | Expand all | Expand 10 after
8266 const ExternalFourByteString& fourstr = 8285 const ExternalFourByteString& fourstr =
8267 ExternalFourByteString::Cast(src); 8286 ExternalFourByteString::Cast(src);
8268 NoGCScope no_gc; 8287 NoGCScope no_gc;
8269 String::Copy(dst, dst_offset, fourstr.CharAddr(0) + src_offset, len); 8288 String::Copy(dst, dst_offset, fourstr.CharAddr(0) + src_offset, len);
8270 } 8289 }
8271 } 8290 }
8272 } 8291 }
8273 } 8292 }
8274 8293
8275 8294
8295 RawString* String::EscapeQuotes(const String& str) {
8296 intptr_t len = str.Length();
8297 if (len > 0) {
8298 intptr_t num_quotes = 0;
8299 intptr_t index = 0;
8300 for (intptr_t i = 0; i < len; i++) {
8301 if (str.CharAt(i) == '"') {
cshapiro 2012/07/10 19:03:27 This entails one virtual function call per charact
siva 2012/07/10 22:59:10 Done.
8302 num_quotes += 1;
8303 }
8304 }
8305 if (str.IsOneByteString()) {
8306 const OneByteString& dststr = OneByteString::Handle(
8307 OneByteString::New(len + num_quotes, Heap::kNew));
8308 for (intptr_t i = 0; i < len; i++) {
8309 if (str.CharAt(i) == '"') {
8310 *(dststr.CharAddr(index)) = '\\';
8311 *(dststr.CharAddr(index + 1)) = '"';
8312 index += 2;
8313 } else {
8314 *(dststr.CharAddr(index)) = str.CharAt(i);
8315 index += 1;
8316 }
8317 }
8318 return dststr.raw();
8319 }
8320 if (str.IsTwoByteString()) {
8321 const TwoByteString& dststr = TwoByteString::Handle(
8322 TwoByteString::New(len + num_quotes, Heap::kNew));
8323 for (intptr_t i = 0; i < len; i++) {
8324 if (str.CharAt(i) == '"') {
8325 *(dststr.CharAddr(index)) = '\\';
8326 *(dststr.CharAddr(index + 1)) = '"';
8327 index += 2;
8328 } else {
8329 *(dststr.CharAddr(index)) = str.CharAt(i);
8330 index += 1;
8331 }
8332 }
8333 return dststr.raw();
8334 }
8335 ASSERT(str.IsFourByteString());
8336 const FourByteString& dststr = FourByteString::Handle(
8337 FourByteString::New(len + num_quotes, Heap::kNew));
8338 for (intptr_t i = 0; i < len; i++) {
8339 if (str.CharAt(i) == '"') {
8340 *(dststr.CharAddr(index)) = '\\';
8341 *(dststr.CharAddr(index + 1)) = '"';
8342 index += 2;
8343 } else {
8344 *(dststr.CharAddr(index)) = str.CharAt(i);
8345 index += 1;
8346 }
8347 }
8348 return dststr.raw();
8349 }
8350 return String::null();
8351 }
8352
8353
8276 static void GrowSymbolTable(const Array& symbol_table, intptr_t table_size) { 8354 static void GrowSymbolTable(const Array& symbol_table, intptr_t table_size) {
8277 // TODO(iposva): Avoid exponential growth. 8355 // TODO(iposva): Avoid exponential growth.
8278 intptr_t new_table_size = table_size * 2; 8356 intptr_t new_table_size = table_size * 2;
8279 Array& new_symbol_table = Array::Handle(Array::New(new_table_size + 1)); 8357 Array& new_symbol_table = Array::Handle(Array::New(new_table_size + 1));
8280 // Copy all elements from the original symbol table to the newly allocated 8358 // Copy all elements from the original symbol table to the newly allocated
8281 // array. 8359 // array.
8282 String& element = String::Handle(); 8360 String& element = String::Handle();
8283 Object& new_element = Object::Handle(); 8361 Object& new_element = Object::Handle();
8284 for (intptr_t i = 0; i < table_size; i++) { 8362 for (intptr_t i = 0; i < table_size; i++) {
8285 element ^= symbol_table.At(i); 8363 element ^= symbol_table.At(i);
(...skipping 1922 matching lines...) Expand 10 before | Expand all | Expand 10 after
10208 const String& str = String::Handle(pattern()); 10286 const String& str = String::Handle(pattern());
10209 const char* format = "JSRegExp: pattern=%s flags=%s"; 10287 const char* format = "JSRegExp: pattern=%s flags=%s";
10210 intptr_t len = OS::SNPrint(NULL, 0, format, str.ToCString(), Flags()); 10288 intptr_t len = OS::SNPrint(NULL, 0, format, str.ToCString(), Flags());
10211 char* chars = reinterpret_cast<char*>( 10289 char* chars = reinterpret_cast<char*>(
10212 Isolate::Current()->current_zone()->Allocate(len + 1)); 10290 Isolate::Current()->current_zone()->Allocate(len + 1));
10213 OS::SNPrint(chars, (len + 1), format, str.ToCString(), Flags()); 10291 OS::SNPrint(chars, (len + 1), format, str.ToCString(), Flags());
10214 return chars; 10292 return chars;
10215 } 10293 }
10216 10294
10217 } // namespace dart 10295 } // namespace dart
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698