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

Side by Side Diff: runtime/vm/object.cc

Issue 12568007: Translate raw strings to regular strings during source code generation. This should hopefully avoid… (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 7 years, 9 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 | « runtime/vm/object.h ('k') | no next file » | 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 "include/dart_api.h" 7 #include "include/dart_api.h"
8 #include "platform/assert.h" 8 #include "platform/assert.h"
9 #include "vm/assembler.h" 9 #include "vm/assembler.h"
10 #include "vm/bigint_operations.h" 10 #include "vm/bigint_operations.h"
(...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after
244 244
245 245
246 template<typename type> 246 template<typename type>
247 static bool IsSpecialCharacter(type value) { 247 static bool IsSpecialCharacter(type value) {
248 return ((value == '"') || 248 return ((value == '"') ||
249 (value == '\n') || 249 (value == '\n') ||
250 (value == '\f') || 250 (value == '\f') ||
251 (value == '\b') || 251 (value == '\b') ||
252 (value == '\t') || 252 (value == '\t') ||
253 (value == '\v') || 253 (value == '\v') ||
254 (value == '\r')); 254 (value == '\r') ||
255 (value == '\\') ||
256 (value == '$'));
255 } 257 }
256 258
257 259
258 template<typename type> 260 template<typename type>
259 static type SpecialCharacter(type value) { 261 static type SpecialCharacter(type value) {
260 if (value == '"') { 262 if (value == '"') {
261 return '"'; 263 return '"';
262 } else if (value == '\n') { 264 } else if (value == '\n') {
263 return 'n'; 265 return 'n';
264 } else if (value == '\f') { 266 } else if (value == '\f') {
265 return 'f'; 267 return 'f';
266 } else if (value == '\b') { 268 } else if (value == '\b') {
267 return 'b'; 269 return 'b';
268 } else if (value == '\t') { 270 } else if (value == '\t') {
269 return 't'; 271 return 't';
270 } else if (value == '\v') { 272 } else if (value == '\v') {
271 return 'v'; 273 return 'v';
272 } else if (value == '\r') { 274 } else if (value == '\r') {
273 return 'r'; 275 return 'r';
276 } else if (value == '\\') {
277 return '\\';
278 } else if (value == '$') {
279 return '$';
274 } 280 }
275 UNREACHABLE(); 281 UNREACHABLE();
276 return '\0'; 282 return '\0';
277 } 283 }
278 284
279 285
280 static void DeleteWeakPersistentHandle(Dart_Handle handle) { 286 static void DeleteWeakPersistentHandle(Dart_Handle handle) {
281 ApiState* state = Isolate::Current()->api_state(); 287 ApiState* state = Isolate::Current()->api_state();
282 ASSERT(state != NULL); 288 ASSERT(state != NULL);
283 FinalizablePersistentHandle* weak_ref = 289 FinalizablePersistentHandle* weak_ref =
(...skipping 4553 matching lines...) Expand 10 before | Expand all | Expand 10 after
4837 while (curr != Token::kEOS) { 4843 while (curr != Token::kEOS) {
4838 // Remember current values for this token. 4844 // Remember current values for this token.
4839 obj = iterator.CurrentToken(); 4845 obj = iterator.CurrentToken();
4840 literal = iterator.MakeLiteralToken(obj); 4846 literal = iterator.MakeLiteralToken(obj);
4841 // Advance to be able to use next token kind. 4847 // Advance to be able to use next token kind.
4842 iterator.Advance(); 4848 iterator.Advance();
4843 Token::Kind next = iterator.CurrentTokenKind(); 4849 Token::Kind next = iterator.CurrentTokenKind();
4844 4850
4845 // Handle the current token. 4851 // Handle the current token.
4846 if (curr == Token::kSTRING) { 4852 if (curr == Token::kSTRING) {
4847 bool is_raw_string = false;
4848 bool escape_characters = false; 4853 bool escape_characters = false;
4849 for (intptr_t i = 0; i < literal.Length(); i++) { 4854 for (intptr_t i = 0; i < literal.Length(); i++) {
4850 if (IsSpecialCharacter(literal.CharAt(i))) { 4855 if (IsSpecialCharacter(literal.CharAt(i))) {
4851 escape_characters = true; 4856 escape_characters = true;
4852 } 4857 }
4853 // TODO(4995): Temp solution for raw strings, this will break
4854 // if we saw a string that is not a raw string but has back slashes
4855 // in it.
4856 if ((literal.CharAt(i) == '\\')) {
4857 if ((next != Token::kINTERPOL_VAR) &&
4858 (next != Token::kINTERPOL_START) &&
4859 (prev != Token::kINTERPOL_VAR) &&
4860 (prev != Token::kINTERPOL_END)) {
4861 is_raw_string = true;
4862 } else {
4863 escape_characters = true;
4864 }
4865 }
4866 if ((literal.CharAt(i) == '$')) {
4867 escape_characters = true;
4868 }
4869 } 4858 }
4870 if ((prev != Token::kINTERPOL_VAR) && (prev != Token::kINTERPOL_END)) { 4859 if ((prev != Token::kINTERPOL_VAR) && (prev != Token::kINTERPOL_END)) {
4871 if (is_raw_string) {
4872 literals.Add(Symbols::LowercaseR());
4873 }
4874 literals.Add(Symbols::DoubleQuotes()); 4860 literals.Add(Symbols::DoubleQuotes());
4875 } 4861 }
4876 if (escape_characters) { 4862 if (escape_characters) {
4877 literal = String::EscapeSpecialCharacters(literal, is_raw_string); 4863 literal = String::EscapeSpecialCharacters(literal);
4878 literals.Add(literal); 4864 literals.Add(literal);
4879 } else { 4865 } else {
4880 literals.Add(literal); 4866 literals.Add(literal);
4881 } 4867 }
4882 if ((next != Token::kINTERPOL_VAR) && (next != Token::kINTERPOL_START)) { 4868 if ((next != Token::kINTERPOL_VAR) && (next != Token::kINTERPOL_START)) {
4883 literals.Add(Symbols::DoubleQuotes()); 4869 literals.Add(Symbols::DoubleQuotes());
4884 } 4870 }
4885 } else if (curr == Token::kINTERPOL_VAR) { 4871 } else if (curr == Token::kINTERPOL_VAR) {
4886 literals.Add(Symbols::Dollar()); 4872 literals.Add(Symbols::Dollar());
4887 if (literal.CharAt(0) == Scanner::kPrivateIdentifierStart) { 4873 if (literal.CharAt(0) == Scanner::kPrivateIdentifierStart) {
(...skipping 6241 matching lines...) Expand 10 before | Expand all | Expand 10 after
11129 String::Copy(dst, 11115 String::Copy(dst,
11130 dst_offset, 11116 dst_offset,
11131 ExternalTwoByteString::CharAddr(src, src_offset), 11117 ExternalTwoByteString::CharAddr(src, src_offset),
11132 len); 11118 len);
11133 } 11119 }
11134 } 11120 }
11135 } 11121 }
11136 } 11122 }
11137 11123
11138 11124
11139 RawString* String::EscapeSpecialCharacters(const String& str, bool raw_str) { 11125 RawString* String::EscapeSpecialCharacters(const String& str) {
11140 if (str.IsOneByteString()) { 11126 if (str.IsOneByteString()) {
11141 return OneByteString::EscapeSpecialCharacters(str, raw_str); 11127 return OneByteString::EscapeSpecialCharacters(str);
11142 } 11128 }
11143 ASSERT(str.IsTwoByteString()); 11129 ASSERT(str.IsTwoByteString());
11144 return TwoByteString::EscapeSpecialCharacters(str, raw_str); 11130 return TwoByteString::EscapeSpecialCharacters(str);
11145 } 11131 }
11146 11132
11147 11133
11148 RawString* String::NewFormatted(const char* format, ...) { 11134 RawString* String::NewFormatted(const char* format, ...) {
11149 va_list args; 11135 va_list args;
11150 va_start(args, format); 11136 va_start(args, format);
11151 RawString* result = NewFormattedV(format, args); 11137 RawString* result = NewFormattedV(format, args);
11152 NoGCScope no_gc; 11138 NoGCScope no_gc;
11153 va_end(args); 11139 va_end(args);
11154 return result; 11140 return result;
(...skipping 367 matching lines...) Expand 10 before | Expand all | Expand 10 after
11522 ch_ = Utf16::Decode(ch_, ch2); 11508 ch_ = Utf16::Decode(ch_, ch2);
11523 } 11509 }
11524 } 11510 }
11525 return true; 11511 return true;
11526 } 11512 }
11527 index_ = end_; 11513 index_ = end_;
11528 return false; 11514 return false;
11529 } 11515 }
11530 11516
11531 11517
11532 RawOneByteString* OneByteString::EscapeSpecialCharacters(const String& str, 11518 RawOneByteString* OneByteString::EscapeSpecialCharacters(const String& str) {
11533 bool raw_str) {
11534 intptr_t len = str.Length(); 11519 intptr_t len = str.Length();
11535 if (len > 0) { 11520 if (len > 0) {
11536 intptr_t num_escapes = 0; 11521 intptr_t num_escapes = 0;
11537 intptr_t index = 0; 11522 intptr_t index = 0;
11538 for (intptr_t i = 0; i < len; i++) { 11523 for (intptr_t i = 0; i < len; i++) {
11539 if (IsSpecialCharacter(*CharAddr(str, i)) || 11524 if (IsSpecialCharacter(*CharAddr(str, i))) {
11540 (!raw_str && (*CharAddr(str, i) == '$')) ||
11541 (!raw_str && (*CharAddr(str, i) == '\\'))) {
11542 num_escapes += 1; 11525 num_escapes += 1;
11543 } 11526 }
11544 } 11527 }
11545 const String& dststr = String::Handle( 11528 const String& dststr = String::Handle(
11546 OneByteString::New(len + num_escapes, Heap::kNew)); 11529 OneByteString::New(len + num_escapes, Heap::kNew));
11547 for (intptr_t i = 0; i < len; i++) { 11530 for (intptr_t i = 0; i < len; i++) {
11548 if (IsSpecialCharacter(*CharAddr(str, i))) { 11531 if (IsSpecialCharacter(*CharAddr(str, i))) {
11549 *(CharAddr(dststr, index)) = '\\'; 11532 *(CharAddr(dststr, index)) = '\\';
11550 *(CharAddr(dststr, index + 1)) = SpecialCharacter(*CharAddr(str, i)); 11533 *(CharAddr(dststr, index + 1)) = SpecialCharacter(*CharAddr(str, i));
11551 index += 2; 11534 index += 2;
11552 } else if (!raw_str && (*CharAddr(str, i) == '$')) {
11553 *(CharAddr(dststr, index)) = '\\';
11554 *(CharAddr(dststr, index + 1)) = '$';
11555 index += 2;
11556 } else if (!raw_str && (*CharAddr(str, i) == '\\')) {
11557 *(CharAddr(dststr, index)) = '\\';
11558 *(CharAddr(dststr, index + 1)) = '\\';
11559 index += 2;
11560 } else { 11535 } else {
11561 *(CharAddr(dststr, index)) = *CharAddr(str, i); 11536 *(CharAddr(dststr, index)) = *CharAddr(str, i);
11562 index += 1; 11537 index += 1;
11563 } 11538 }
11564 } 11539 }
11565 return OneByteString::raw(dststr); 11540 return OneByteString::raw(dststr);
11566 } 11541 }
11567 return OneByteString::null(); 11542 return OneByteString::null();
11568 } 11543 }
11569 11544
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
11711 NoGCScope no_gc; 11686 NoGCScope no_gc;
11712 if (length > 0) { 11687 if (length > 0) {
11713 uint8_t* dest = &result->ptr()->data_[0]; 11688 uint8_t* dest = &result->ptr()->data_[0];
11714 uint8_t* src = &raw_ptr(str)->data_[begin_index]; 11689 uint8_t* src = &raw_ptr(str)->data_[begin_index];
11715 memmove(dest, src, length); 11690 memmove(dest, src, length);
11716 } 11691 }
11717 return result; 11692 return result;
11718 } 11693 }
11719 11694
11720 11695
11721 RawTwoByteString* TwoByteString::EscapeSpecialCharacters(const String& str, 11696 RawTwoByteString* TwoByteString::EscapeSpecialCharacters(const String& str) {
11722 bool raw_str) {
11723 intptr_t len = str.Length(); 11697 intptr_t len = str.Length();
11724 if (len > 0) { 11698 if (len > 0) {
11725 intptr_t num_escapes = 0; 11699 intptr_t num_escapes = 0;
11726 intptr_t index = 0; 11700 intptr_t index = 0;
11727 for (intptr_t i = 0; i < len; i++) { 11701 for (intptr_t i = 0; i < len; i++) {
11728 if (IsSpecialCharacter(*CharAddr(str, i)) || 11702 if (IsSpecialCharacter(*CharAddr(str, i))) {
11729 (!raw_str && (*CharAddr(str, i) == '\\'))) {
11730 num_escapes += 1; 11703 num_escapes += 1;
11731 } 11704 }
11732 } 11705 }
11733 const String& dststr = String::Handle( 11706 const String& dststr = String::Handle(
11734 TwoByteString::New(len + num_escapes, Heap::kNew)); 11707 TwoByteString::New(len + num_escapes, Heap::kNew));
11735 for (intptr_t i = 0; i < len; i++) { 11708 for (intptr_t i = 0; i < len; i++) {
11736 if (IsSpecialCharacter(*CharAddr(str, i))) { 11709 if (IsSpecialCharacter(*CharAddr(str, i))) {
11737 *(CharAddr(dststr, index)) = '\\'; 11710 *(CharAddr(dststr, index)) = '\\';
11738 *(CharAddr(dststr, index + 1)) = SpecialCharacter(*CharAddr(str, i)); 11711 *(CharAddr(dststr, index + 1)) = SpecialCharacter(*CharAddr(str, i));
11739 index += 2; 11712 index += 2;
11740 } else if (!raw_str && (*CharAddr(str, i) == '\\')) {
11741 *(CharAddr(dststr, index)) = '\\';
11742 *(CharAddr(dststr, index + 1)) = '\\';
11743 index += 2;
11744 } else { 11713 } else {
11745 *(CharAddr(dststr, index)) = *CharAddr(str, i); 11714 *(CharAddr(dststr, index)) = *CharAddr(str, i);
11746 index += 1; 11715 index += 1;
11747 } 11716 }
11748 } 11717 }
11749 return TwoByteString::raw(dststr); 11718 return TwoByteString::raw(dststr);
11750 } 11719 }
11751 return TwoByteString::null(); 11720 return TwoByteString::null();
11752 } 11721 }
11753 11722
(...skipping 1750 matching lines...) Expand 10 before | Expand all | Expand 10 after
13504 } 13473 }
13505 return result.raw(); 13474 return result.raw();
13506 } 13475 }
13507 13476
13508 13477
13509 const char* WeakProperty::ToCString() const { 13478 const char* WeakProperty::ToCString() const {
13510 return "_WeakProperty"; 13479 return "_WeakProperty";
13511 } 13480 }
13512 13481
13513 } // namespace dart 13482 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/vm/object.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698