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

Side by Side Diff: dart/frog/leg/lib/mockimpl.dart

Issue 9426040: Improve String and RegExp implementations. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge
Patch Set: changes Created 8 years, 10 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 // Mocks of classes and interfaces that Leg cannot read directly. 5 // Mocks of classes and interfaces that Leg cannot read directly.
6 6
7 // TODO(ahe): Remove this file. 7 // TODO(ahe): Remove this file.
8 8
9 class JSSyntaxRegExp implements RegExp { 9 class JSSyntaxRegExp implements RegExp {
10 final String pattern; 10 final String pattern;
11 final bool multiLine; 11 final bool multiLine;
12 final bool ignoreCase; 12 final bool ignoreCase;
13 final RegExpWrapper _re; 13 final RegExpWrapper _re;
14 14
15 const JSSyntaxRegExp(String pattern, [bool multiLine, bool ignoreCase]) 15 const JSSyntaxRegExp(String pattern,
16 [bool multiLine = false, bool ignoreCase = false])
16 // TODO(ahe): Redirect to _internal when that is supported. 17 // TODO(ahe): Redirect to _internal when that is supported.
ngeoffray 2012/02/21 12:15:53 I believe that is implemented now.
17 : this.pattern = pattern, 18 : this.pattern = pattern,
18 this.multiLine = multiLine, 19 this.multiLine = multiLine,
19 this.ignoreCase = ignoreCase, 20 this.ignoreCase = ignoreCase,
20 this._re = const RegExpWrapper(pattern, multiLine, ignoreCase, false); 21 this._re = const RegExpWrapper(pattern, multiLine, ignoreCase, false);
21 22
22 const JSSyntaxRegExp._internal(String pattern, bool multiLine, 23 const JSSyntaxRegExp._internal(String pattern, bool multiLine,
23 bool ignoreCase, bool global) 24 bool ignoreCase, bool global)
24 : this.pattern = pattern, 25 : this.pattern = pattern,
25 this.multiLine = multiLine, 26 this.multiLine = multiLine,
26 this.ignoreCase = ignoreCase, 27 this.ignoreCase = ignoreCase,
27 this._re = const RegExpWrapper(pattern, multiLine, ignoreCase, global); 28 this._re = const RegExpWrapper(pattern, multiLine, ignoreCase, global);
28 29
29 Match firstMatch(String str) { 30 Match firstMatch(String str) {
30 List<String> m = _re.exec(str); 31 List<String> m = _re.exec(str);
31 if (m === null) return null; 32 if (m === null) return null;
32 var matchStart = RegExpWrapper.matchStart(m); 33 var matchStart = RegExpWrapper.matchStart(m);
33 return new MatchImplementation(pattern, str, matchStart, 34 // m.lastIndex only works with flag 'g'.
34 _re.lastIndex(), m); 35 var matchEnd = matchStart + m[0].length;
36 return new MatchImplementation(pattern, str, matchStart, matchEnd, m);
35 } 37 }
36 38
37 bool hasMatch(String str) => _re.test(str); 39 bool hasMatch(String str) => _re.test(str);
38 40
39 String stringMatch(String str) { 41 String stringMatch(String str) {
40 var match = firstMatch(str); 42 var match = firstMatch(str);
41 return match === null ? null : match.group(0); 43 return match === null ? null : match.group(0);
42 } 44 }
43 45
44 Iterable<Match> allMatches(String str) => new _AllMatchesIterable(this, str); 46 Iterable<Match> allMatches(String str) {
47 checkString(str);
48 return new _AllMatchesIterable(this, str);
49 }
45 50
46 /** 51 /**
47 * Returns a new RegExp with the same pattern as this one and with the 52 * Returns a new RegExp with the same pattern as this one and with the
48 * "global" flag set. This allows us to match this RegExp against a string 53 * "global" flag set. This allows us to match this RegExp against a string
49 * multiple times, to support things like [allMatches] and 54 * multiple times, to support things like [allMatches] and
50 * [String.replaceAll]. 55 * [String.replaceAll].
51 * 56 *
52 * Note that the returned RegExp disobeys the normal API in that it maintains 57 * Note that the returned RegExp disobeys the normal API in that it maintains
53 * state about the location of the last match. 58 * state about the location of the last match.
54 */ 59 */
(...skipping 11 matching lines...) Expand all
66 List<String> this._groups); 71 List<String> this._groups);
67 72
68 final String pattern; 73 final String pattern;
69 final String str; 74 final String str;
70 final int _start; 75 final int _start;
71 final int _end; 76 final int _end;
72 final List<String> _groups; 77 final List<String> _groups;
73 78
74 int start() => _start; 79 int start() => _start;
75 int end() => _end; 80 int end() => _end;
76 String group(int group) => _groups[group]; 81 String group(int index) => stringify(_groups[index]);
ngeoffray 2012/02/21 12:15:53 stringify is a weird name for here. I would implem
77 String operator [](int group) => _groups[group]; 82 String operator [](int index) => group(index);
78 int groupCount() => _groups.length; 83 int groupCount() => _groups.length - 1;
79 84
80 List<String> groups(List<int> groups) { 85 List<String> groups(List<int> groups) {
81 List<String> out = []; 86 List<String> out = [];
82 for (int group in groups) { 87 for (int i in groups) {
83 out.add(_groups[group]); 88 out.add(group(i));
84 } 89 }
85 return out; 90 return out;
86 } 91 }
87 } 92 }
88 93
89 class _AllMatchesIterable implements Iterable<Match> { 94 class _AllMatchesIterable implements Iterable<Match> {
90 final JSSyntaxRegExp _re; 95 final JSSyntaxRegExp _re;
91 final String _str; 96 final String _str;
92 97
93 const _AllMatchesIterable(this._re, this._str); 98 const _AllMatchesIterable(this._re, this._str);
(...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after
384 } 389 }
385 String twoDigitMinutes = 390 String twoDigitMinutes =
386 twoDigits(inMinutes.remainder(Duration.MINUTES_PER_HOUR)); 391 twoDigits(inMinutes.remainder(Duration.MINUTES_PER_HOUR));
387 String twoDigitSeconds = 392 String twoDigitSeconds =
388 twoDigits(inSeconds.remainder(Duration.SECONDS_PER_MINUTE)); 393 twoDigits(inSeconds.remainder(Duration.SECONDS_PER_MINUTE));
389 String threeDigitMs = 394 String threeDigitMs =
390 threeDigits(inMilliseconds.remainder(Duration.MILLISECONDS_PER_SECOND)); 395 threeDigits(inMilliseconds.remainder(Duration.MILLISECONDS_PER_SECOND));
391 return "${inHours}:${twoDigitMinutes}:${twoDigitSeconds}.${threeDigitMs}"; 396 return "${inHours}:${twoDigitMinutes}:${twoDigitSeconds}.${threeDigitMs}";
392 } 397 }
393 } 398 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698