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

Side by Side Diff: frog/lib/string_implementation.dart

Issue 8907042: Support replaceAll for regexps. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 9 years 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) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2011, 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 //String.prototype.get$length = function() { 5 //String.prototype.get$length = function() {
6 // return this.length; 6 // return this.length;
7 //} 7 //}
8 8
9 // TODO(jimhug): Unify with code from compiler/lib/implementation. 9 // TODO(jimhug): Unify with code from compiler/lib/implementation.
10 class StringImplementation implements String native "String" { 10 class StringImplementation implements String native "String" {
(...skipping 27 matching lines...) Expand all
38 38
39 String trim() native; 39 String trim() native;
40 40
41 // TODO(jmesserly): should support pattern too. 41 // TODO(jmesserly): should support pattern too.
42 bool contains(Pattern pattern, [int startIndex]) native 42 bool contains(Pattern pattern, [int startIndex]) native
43 "return this.indexOf(pattern, startIndex) >= 0;"; 43 "return this.indexOf(pattern, startIndex) >= 0;";
44 44
45 String _replaceFirst(String from, String to) native 45 String _replaceFirst(String from, String to) native
46 "return this.replace(from, to);"; 46 "return this.replace(from, to);";
47 47
48 String _replaceFirstRegExp(RegExp from, String to) native 48 String _replaceRegExp(RegExp from, String to) native
49 "return this.replace(from.re, to);"; 49 "return this.replace(from.re, to);";
50 50
51 String replaceFirst(Pattern from, String to) { 51 String replaceFirst(Pattern from, String to) {
52 if (from is String) return _replaceFirst(from, to); 52 if (from is String) return _replaceFirst(from, to);
53 if (from is RegExp) return _replaceFirstRegExp(from, to); 53 if (from is RegExp) return _replaceRegExp(from, to);
54 for (match in from.allMatches(this)) { 54 for (var match in from.allMatches(this)) {
Jennifer Messerly 2011/12/14 20:36:56 Thanks for making this more consistent with the re
55 // We just care about the first match 55 // We just care about the first match
56 return substring(0, match.start()) + to + substring(match.end()); 56 return substring(0, match.start()) + to + substring(match.end());
57 } 57 }
58 } 58 }
59 59
60 String replaceAll(Pattern from, String to) native @""" 60 String _replaceAll(String from, String to) native @"""
61 if (typeof(from) == 'string' || from instanceof String) { 61 from = new RegExp(from.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"), 'g');
62 from = new RegExp(from.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"), 'g'); 62 to = to.replace(/\$/g, '$$$$'); // Escape sequences are fun!
63 to = to.replace(/\$/g, '$$$$'); // Escape sequences are fun!
64 }
65 return this.replace(from, to);"""; 63 return this.replace(from, to);""";
66 64
65 String replaceAll(Pattern from, String to) {
66 if (from is String) return _replaceAll(from, to);
67 if (from is RegExp) return _replaceRegExp(from.dynamic._global, to);
68 var buffer = new StringBuffer();
69 var lastMatchEnd = 0;
70 for (var match in from.allMatches(this)) {
71 buffer.add(substring(lastMatchEnd, match.start()));
72 buffer.add(to);
73 lastMatchEnd = match.end();
74 }
75 buffer.add(substring(lastMatchEnd));
76 }
77
67 List<String> split(Pattern pattern) native; 78 List<String> split(Pattern pattern) native;
68 79
69 /* 80 /*
70 Iterable<Match> allMatches(String str) { 81 Iterable<Match> allMatches(String str) {
71 List<Match> result = []; 82 List<Match> result = [];
72 if (this.isEmpty()) return result; 83 if (this.isEmpty()) return result;
73 int length = this.length; 84 int length = this.length;
74 85
75 int ix = 0; 86 int ix = 0;
76 while (ix < str.length) { 87 while (ix < str.length) {
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
142 result.add(group(g)); 153 result.add(group(g));
143 } 154 }
144 return result; 155 return result;
145 } 156 }
146 157
147 final int _start; 158 final int _start;
148 final String str; 159 final String str;
149 final String pattern; 160 final String pattern;
150 } 161 }
151 */ 162 */
OLDNEW
« frog/lib/corelib_impl.dart ('K') | « frog/lib/corelib_impl.dart ('k') | frog/minfrog » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698