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

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
« no previous file with comments | « vm/object.h ('k') | vm/snapshot_test.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 4438 matching lines...) Expand 10 before | Expand all | Expand 10 after
4449 } 4449 }
4450 } 4450 }
4451 4451
4452 4452
4453 RawString* TokenStream::GenerateSource() const { 4453 RawString* TokenStream::GenerateSource() const {
4454 GrowableObjectArray& literals = 4454 GrowableObjectArray& literals =
4455 GrowableObjectArray::Handle(GrowableObjectArray::New(Length())); 4455 GrowableObjectArray::Handle(GrowableObjectArray::New(Length()));
4456 String& literal = String::Handle(); 4456 String& literal = String::Handle();
4457 String& blank = String::Handle(String::New(" ")); 4457 String& blank = String::Handle(String::New(" "));
4458 String& newline = String::Handle(String::New("\n")); 4458 String& newline = String::Handle(String::New("\n"));
4459 String& double_quotes = String::Handle(String::New("\""));
4459 for (intptr_t i = 0; i < Length(); i++) { 4460 for (intptr_t i = 0; i < Length(); i++) {
4460 Token::Kind kind = KindAt(i); 4461 Token::Kind kind = KindAt(i);
4461 literal = LiteralAt(i); 4462 literal = LiteralAt(i);
4462 literals.Add(literal); 4463 if (kind == Token::kSTRING) {
4464 bool escape_quotes = false;
4465 for (intptr_t i = 0; i < literal.Length(); i++) {
4466 if (literal.CharAt(i) == '"') {
4467 escape_quotes = true;
4468 break;
4469 }
4470 }
4471 literals.Add(double_quotes);
4472 if (escape_quotes) {
4473 literal = String::EscapeDoubleQuotes(literal);
4474 literals.Add(literal);
4475 } else {
4476 literals.Add(literal);
4477 }
4478 literals.Add(double_quotes);
4479 } else {
4480 literals.Add(literal);
4481 }
4463 if (kind == Token::kLBRACE) { 4482 if (kind == Token::kLBRACE) {
4464 literals.Add(newline); 4483 literals.Add(newline);
4465 } else { 4484 } else {
4466 literals.Add(blank); 4485 literals.Add(blank);
4467 } 4486 }
4468 } 4487 }
4469 const Array& source = Array::Handle(Array::MakeArray(literals)); 4488 const Array& source = Array::Handle(Array::MakeArray(literals));
4470 return String::ConcatAll(source); 4489 return String::ConcatAll(source);
4471 } 4490 }
4472 4491
(...skipping 3831 matching lines...) Expand 10 before | Expand all | Expand 10 after
8304 const ExternalFourByteString& fourstr = 8323 const ExternalFourByteString& fourstr =
8305 ExternalFourByteString::Cast(src); 8324 ExternalFourByteString::Cast(src);
8306 NoGCScope no_gc; 8325 NoGCScope no_gc;
8307 String::Copy(dst, dst_offset, fourstr.CharAddr(0) + src_offset, len); 8326 String::Copy(dst, dst_offset, fourstr.CharAddr(0) + src_offset, len);
8308 } 8327 }
8309 } 8328 }
8310 } 8329 }
8311 } 8330 }
8312 8331
8313 8332
8333 RawString* String::EscapeDoubleQuotes(const String& str) {
8334 if (str.IsOneByteString()) {
8335 const OneByteString& onestr = OneByteString::Cast(str);
8336 return onestr.EscapeDoubleQuotes();
8337 }
8338 if (str.IsTwoByteString()) {
8339 const TwoByteString& twostr = TwoByteString::Cast(str);
8340 return twostr.EscapeDoubleQuotes();
8341 }
8342 ASSERT(str.IsFourByteString());
8343 const FourByteString& fourstr = FourByteString::Cast(str);
8344 return fourstr.EscapeDoubleQuotes();
8345 }
8346
8347
8314 static void GrowSymbolTable(const Array& symbol_table, intptr_t table_size) { 8348 static void GrowSymbolTable(const Array& symbol_table, intptr_t table_size) {
8315 // TODO(iposva): Avoid exponential growth. 8349 // TODO(iposva): Avoid exponential growth.
8316 intptr_t new_table_size = table_size * 2; 8350 intptr_t new_table_size = table_size * 2;
8317 Array& new_symbol_table = Array::Handle(Array::New(new_table_size + 1)); 8351 Array& new_symbol_table = Array::Handle(Array::New(new_table_size + 1));
8318 // Copy all elements from the original symbol table to the newly allocated 8352 // Copy all elements from the original symbol table to the newly allocated
8319 // array. 8353 // array.
8320 String& element = String::Handle(); 8354 String& element = String::Handle();
8321 Object& new_element = Object::Handle(); 8355 Object& new_element = Object::Handle();
8322 for (intptr_t i = 0; i < table_size; i++) { 8356 for (intptr_t i = 0; i < table_size; i++) {
8323 element ^= symbol_table.At(i); 8357 element ^= symbol_table.At(i);
(...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after
8621 return Transform(CaseMapping::ToUpper, str, space); 8655 return Transform(CaseMapping::ToUpper, str, space);
8622 } 8656 }
8623 8657
8624 8658
8625 RawString* String::ToLowerCase(const String& str, Heap::Space space) { 8659 RawString* String::ToLowerCase(const String& str, Heap::Space space) {
8626 // TODO(cshapiro): create a fast-path for OneByteString instances. 8660 // TODO(cshapiro): create a fast-path for OneByteString instances.
8627 return Transform(CaseMapping::ToLower, str, space); 8661 return Transform(CaseMapping::ToLower, str, space);
8628 } 8662 }
8629 8663
8630 8664
8665 RawOneByteString* OneByteString::EscapeDoubleQuotes() const {
8666 intptr_t len = Length();
8667 if (len > 0) {
8668 intptr_t num_quotes = 0;
8669 intptr_t index = 0;
8670 for (intptr_t i = 0; i < len; i++) {
8671 if (*CharAddr(i) == '"') {
8672 num_quotes += 1;
8673 }
8674 }
8675 const OneByteString& dststr = OneByteString::Handle(
8676 OneByteString::New(len + num_quotes, Heap::kNew));
8677 for (intptr_t i = 0; i < len; i++) {
8678 if (*CharAddr(i) == '"') {
8679 *(dststr.CharAddr(index)) = '\\';
8680 *(dststr.CharAddr(index + 1)) = '"';
8681 index += 2;
8682 } else {
8683 *(dststr.CharAddr(index)) = *CharAddr(i);
8684 index += 1;
8685 }
8686 }
8687 return dststr.raw();
8688 }
8689 return OneByteString::null();
8690 }
8691
8692
8631 RawOneByteString* OneByteString::New(intptr_t len, 8693 RawOneByteString* OneByteString::New(intptr_t len,
8632 Heap::Space space) { 8694 Heap::Space space) {
8633 Isolate* isolate = Isolate::Current(); 8695 Isolate* isolate = Isolate::Current();
8634 8696
8635 const Class& cls = 8697 const Class& cls =
8636 Class::Handle(isolate->object_store()->one_byte_string_class()); 8698 Class::Handle(isolate->object_store()->one_byte_string_class());
8637 OneByteString& result = OneByteString::Handle(); 8699 OneByteString& result = OneByteString::Handle();
8638 { 8700 {
8639 RawObject* raw = Object::Allocate(cls, 8701 RawObject* raw = Object::Allocate(cls,
8640 OneByteString::InstanceSize(len), 8702 OneByteString::InstanceSize(len),
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
8734 } 8796 }
8735 return result.raw(); 8797 return result.raw();
8736 } 8798 }
8737 8799
8738 8800
8739 const char* OneByteString::ToCString() const { 8801 const char* OneByteString::ToCString() const {
8740 return String::ToCString(); 8802 return String::ToCString();
8741 } 8803 }
8742 8804
8743 8805
8806 RawTwoByteString* TwoByteString::EscapeDoubleQuotes() const {
8807 intptr_t len = Length();
8808 if (len > 0) {
8809 intptr_t num_quotes = 0;
8810 intptr_t index = 0;
8811 for (intptr_t i = 0; i < len; i++) {
8812 if (*CharAddr(i) == '"') {
8813 num_quotes += 1;
8814 }
8815 }
8816 const TwoByteString& dststr = TwoByteString::Handle(
8817 TwoByteString::New(len + num_quotes, Heap::kNew));
8818 for (intptr_t i = 0; i < len; i++) {
8819 if (*CharAddr(i) == '"') {
8820 *(dststr.CharAddr(index)) = '\\';
8821 *(dststr.CharAddr(index + 1)) = '"';
8822 index += 2;
8823 } else {
8824 *(dststr.CharAddr(index)) = *CharAddr(i);
8825 index += 1;
8826 }
8827 }
8828 return dststr.raw();
8829 }
8830 return TwoByteString::null();
8831 }
8832
8833
8744 RawTwoByteString* TwoByteString::New(intptr_t len, 8834 RawTwoByteString* TwoByteString::New(intptr_t len,
8745 Heap::Space space) { 8835 Heap::Space space) {
8746 Isolate* isolate = Isolate::Current(); 8836 Isolate* isolate = Isolate::Current();
8747 8837
8748 const Class& cls = 8838 const Class& cls =
8749 Class::Handle(isolate->object_store()->two_byte_string_class()); 8839 Class::Handle(isolate->object_store()->two_byte_string_class());
8750 TwoByteString& result = TwoByteString::Handle(); 8840 TwoByteString& result = TwoByteString::Handle();
8751 { 8841 {
8752 RawObject* raw = Object::Allocate(cls, 8842 RawObject* raw = Object::Allocate(cls,
8753 TwoByteString::InstanceSize(len), 8843 TwoByteString::InstanceSize(len),
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
8837 } 8927 }
8838 return result.raw(); 8928 return result.raw();
8839 } 8929 }
8840 8930
8841 8931
8842 const char* TwoByteString::ToCString() const { 8932 const char* TwoByteString::ToCString() const {
8843 return String::ToCString(); 8933 return String::ToCString();
8844 } 8934 }
8845 8935
8846 8936
8937 RawFourByteString* FourByteString::EscapeDoubleQuotes() const {
8938 intptr_t len = Length();
8939 if (len > 0) {
8940 intptr_t num_quotes = 0;
8941 intptr_t index = 0;
8942 for (intptr_t i = 0; i < len; i++) {
8943 if (*CharAddr(i) == '"') {
8944 num_quotes += 1;
8945 }
8946 }
8947 const FourByteString& dststr = FourByteString::Handle(
8948 FourByteString::New(len + num_quotes, Heap::kNew));
8949 for (intptr_t i = 0; i < len; i++) {
8950 if (*CharAddr(i) == '"') {
8951 *(dststr.CharAddr(index)) = '\\';
8952 *(dststr.CharAddr(index + 1)) = '"';
8953 index += 2;
8954 } else {
8955 *(dststr.CharAddr(index)) = *CharAddr(i);
8956 index += 1;
8957 }
8958 }
8959 return dststr.raw();
8960 }
8961 return FourByteString::null();
8962 }
8963
8964
8847 RawFourByteString* FourByteString::New(intptr_t len, 8965 RawFourByteString* FourByteString::New(intptr_t len,
8848 Heap::Space space) { 8966 Heap::Space space) {
8849 Isolate* isolate = Isolate::Current(); 8967 Isolate* isolate = Isolate::Current();
8850 8968
8851 const Class& cls = 8969 const Class& cls =
8852 Class::Handle(isolate->object_store()->four_byte_string_class()); 8970 Class::Handle(isolate->object_store()->four_byte_string_class());
8853 FourByteString& result = FourByteString::Handle(); 8971 FourByteString& result = FourByteString::Handle();
8854 { 8972 {
8855 RawObject* raw = Object::Allocate(cls, 8973 RawObject* raw = Object::Allocate(cls,
8856 FourByteString::InstanceSize(len), 8974 FourByteString::InstanceSize(len),
(...skipping 1389 matching lines...) Expand 10 before | Expand all | Expand 10 after
10246 const String& str = String::Handle(pattern()); 10364 const String& str = String::Handle(pattern());
10247 const char* format = "JSRegExp: pattern=%s flags=%s"; 10365 const char* format = "JSRegExp: pattern=%s flags=%s";
10248 intptr_t len = OS::SNPrint(NULL, 0, format, str.ToCString(), Flags()); 10366 intptr_t len = OS::SNPrint(NULL, 0, format, str.ToCString(), Flags());
10249 char* chars = reinterpret_cast<char*>( 10367 char* chars = reinterpret_cast<char*>(
10250 Isolate::Current()->current_zone()->Allocate(len + 1)); 10368 Isolate::Current()->current_zone()->Allocate(len + 1));
10251 OS::SNPrint(chars, (len + 1), format, str.ToCString(), Flags()); 10369 OS::SNPrint(chars, (len + 1), format, str.ToCString(), Flags());
10252 return chars; 10370 return chars;
10253 } 10371 }
10254 10372
10255 } // namespace dart 10373 } // namespace dart
OLDNEW
« 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