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

Side by Side Diff: dart/sdk/lib/_internal/lib/string_helper.dart

Issue 11973018: Improve decoding of JS TypeError. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge
Patch Set: Address comments Created 7 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 part of _js_helper; 5 part of _js_helper;
6 6
7 class StringMatch implements Match { 7 class StringMatch implements Match {
8 const StringMatch(int this.start, 8 const StringMatch(int this.start,
9 String this.str, 9 String this.str,
10 String this.pattern); 10 String this.pattern);
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
70 } 70 }
71 71
72 stringReplaceJS(receiver, replacer, to) { 72 stringReplaceJS(receiver, replacer, to) {
73 // The JavaScript String.replace method recognizes replacement 73 // The JavaScript String.replace method recognizes replacement
74 // patterns in the replacement string. Dart does not have that 74 // patterns in the replacement string. Dart does not have that
75 // behavior. 75 // behavior.
76 to = JS('String', r'#.replace("$", "$$$$")', to); 76 to = JS('String', r'#.replace("$", "$$$$")', to);
77 return JS('String', r'#.replace(#, #)', receiver, replacer, to); 77 return JS('String', r'#.replace(#, #)', receiver, replacer, to);
78 } 78 }
79 79
80 const String ESCAPE_REGEXP = r'[[\]{}()*+?.\\^$|]';
81
80 stringReplaceAllUnchecked(receiver, from, to) { 82 stringReplaceAllUnchecked(receiver, from, to) {
81 checkString(to); 83 checkString(to);
82 if (from is String) { 84 if (from is String) {
83 if (from == "") { 85 if (from == "") {
84 if (receiver == "") { 86 if (receiver == "") {
85 return to; 87 return to;
86 } else { 88 } else {
87 StringBuffer result = new StringBuffer(); 89 StringBuffer result = new StringBuffer();
88 int length = receiver.length; 90 int length = receiver.length;
89 result.write(to); 91 result.write(to);
90 for (int i = 0; i < length; i++) { 92 for (int i = 0; i < length; i++) {
91 result.write(receiver[i]); 93 result.write(receiver[i]);
92 result.write(to); 94 result.write(to);
93 } 95 }
94 return result.toString(); 96 return result.toString();
95 } 97 }
96 } else { 98 } else {
97 var quoter = JS('', "new RegExp(#, 'g')", r'[[\]{}()*+?.\\^$|]'); 99 var quoter = JS('', "new RegExp(#, 'g')", ESCAPE_REGEXP);
98 var quoted = JS('String', r'#.replace(#, "\\$&")', from, quoter); 100 var quoted = JS('String', r'#.replace(#, "\\$&")', from, quoter);
99 var replacer = JS('', "new RegExp(#, 'g')", quoted); 101 var replacer = JS('', "new RegExp(#, 'g')", quoted);
100 return stringReplaceJS(receiver, replacer, to); 102 return stringReplaceJS(receiver, replacer, to);
101 } 103 }
102 } else if (from is JSSyntaxRegExp) { 104 } else if (from is JSSyntaxRegExp) {
103 var re = regExpGetGlobalNative(from); 105 var re = regExpGetGlobalNative(from);
104 return stringReplaceJS(receiver, re, to); 106 return stringReplaceJS(receiver, re, to);
105 } else { 107 } else {
106 checkNull(from); 108 checkNull(from);
107 // TODO(floitsch): implement generic String.replace (with patterns). 109 // TODO(floitsch): implement generic String.replace (with patterns).
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
192 } else { 194 } else {
193 checkNull(from); 195 checkNull(from);
194 // TODO(floitsch): implement generic String.replace (with patterns). 196 // TODO(floitsch): implement generic String.replace (with patterns).
195 throw "String.replace(Pattern) UNIMPLEMENTED"; 197 throw "String.replace(Pattern) UNIMPLEMENTED";
196 } 198 }
197 } 199 }
198 200
199 stringJoinUnchecked(array, separator) { 201 stringJoinUnchecked(array, separator) {
200 return JS('String', r'#.join(#)', array, separator); 202 return JS('String', r'#.join(#)', array, separator);
201 } 203 }
OLDNEW
« no previous file with comments | « dart/sdk/lib/_internal/lib/js_helper.dart ('k') | dart/tests/compiler/dart2js_native/dart2js_native.status » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698