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

Side by Side Diff: runtime/lib/regexp_patch.dart

Issue 16206008: Fix-up RegExp implementations. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Renaming startIndex to index. Add comment. Created 7 years, 6 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 | « no previous file | sdk/lib/_internal/compiler/implementation/lib/js_string.dart » ('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 patch class RegExp { 5 patch class RegExp {
6 /* patch */ factory RegExp(String pattern, 6 /* patch */ factory RegExp(String pattern,
7 {bool multiLine: false, 7 {bool multiLine: false,
8 bool caseSensitive: true}) { 8 bool caseSensitive: true}) {
9 return new _JSSyntaxRegExp(pattern, 9 return new _JSSyntaxRegExp(pattern,
10 multiLine: multiLine, 10 multiLine: multiLine,
11 caseSensitive: caseSensitive); 11 caseSensitive: caseSensitive);
12 } 12 }
13 } 13 }
14 14
15 class _JSRegExpMatch implements Match { 15 class _JSRegExpMatch implements Match {
16 _JSRegExpMatch(this.regexp, this.str, this._match); 16 _JSRegExpMatch(this._regexp, this.str, this._match);
17 17
18 int get start => _start(0); 18 int get start => _start(0);
19 int get end => _end(0); 19 int get end => _end(0);
20 20
21 int _start(int groupIdx) { 21 int _start(int groupIdx) {
22 return _match[(groupIdx * MATCH_PAIR)]; 22 return _match[(groupIdx * _MATCH_PAIR)];
23 } 23 }
24 24
25 int _end(int groupIdx) { 25 int _end(int groupIdx) {
26 return _match[(groupIdx * MATCH_PAIR) + 1]; 26 return _match[(groupIdx * _MATCH_PAIR) + 1];
27 } 27 }
28 28
29 String group(int groupIdx) { 29 String group(int groupIdx) {
30 if (groupIdx < 0 || groupIdx > regexp._groupCount) { 30 if (groupIdx < 0 || groupIdx > _regexp._groupCount) {
31 throw new RangeError.value(groupIdx); 31 throw new RangeError.value(groupIdx);
32 } 32 }
33 int startIndex = _start(groupIdx); 33 int startIndex = _start(groupIdx);
34 int endIndex = _end(groupIdx); 34 int endIndex = _end(groupIdx);
35 if (startIndex == -1) { 35 if (startIndex == -1) {
36 assert(endIndex == -1); 36 assert(endIndex == -1);
37 return null; 37 return null;
38 } 38 }
39 return str._substringUnchecked(startIndex, endIndex); 39 return str._substringUnchecked(startIndex, endIndex);
40 } 40 }
41 41
42 String operator [](int groupIdx) { 42 String operator [](int groupIdx) {
43 return this.group(groupIdx); 43 return this.group(groupIdx);
44 } 44 }
45 45
46 List<String> groups(List<int> groupsSpec) { 46 List<String> groups(List<int> groupsSpec) {
47 var groupsList = new List<String>(groupsSpec.length); 47 var groupsList = new List<String>(groupsSpec.length);
48 for (int i = 0; i < groupsSpec.length; i++) { 48 for (int i = 0; i < groupsSpec.length; i++) {
49 groupsList[i] = group(groupsSpec[i]); 49 groupsList[i] = group(groupsSpec[i]);
50 } 50 }
51 return groupsList; 51 return groupsList;
52 } 52 }
53 53
54 int get groupCount => regexp._groupCount; 54 int get groupCount => _regexp._groupCount;
55 55
56 String get pattern => regexp.pattern; 56 Pattern get pattern => _regexp;
57 57
58 final RegExp regexp; 58 final RegExp _regexp;
59 final String str; 59 final String str;
60 final List<int> _match; 60 final List<int> _match;
61 static const int MATCH_PAIR = 2; 61 static const int _MATCH_PAIR = 2;
62 } 62 }
63 63
64 64
65 class _JSSyntaxRegExp implements RegExp { 65 class _JSSyntaxRegExp implements RegExp {
66 factory _JSSyntaxRegExp( 66 factory _JSSyntaxRegExp(
67 String pattern, 67 String pattern,
68 {bool multiLine: false, 68 {bool multiLine: false,
69 bool caseSensitive: true}) native "JSSyntaxRegExp_factory"; 69 bool caseSensitive: true}) native "JSSyntaxRegExp_factory";
70 70
71 Match firstMatch(String str) { 71 Match firstMatch(String str) {
72 List match = _ExecuteMatch(str, 0); 72 List match = _ExecuteMatch(str, 0);
73 if (match == null) { 73 if (match == null) {
74 return null; 74 return null;
75 } 75 }
76 return new _JSRegExpMatch(this, str, match); 76 return new _JSRegExpMatch(this, str, match);
77 } 77 }
78 78
79 Iterable<Match> allMatches(String str) { 79 Iterable<Match> allMatches(String str) {
80 if (str is! String) throw new ArgumentError(str); 80 if (str is! String) throw new ArgumentError(str);
81 List<Match> result = new List<Match>(); 81 List<Match> result = new List<Match>();
82 int length = str.length; 82 int length = str.length;
83 int startIndex = 0; 83 int index = 0;
84 while (true) { 84 while (true) {
85 List match = _ExecuteMatch(str, startIndex); 85 List match = _ExecuteMatch(str, index);
86 if (match == null) { 86 if (match == null) {
87 break; 87 break;
88 } 88 }
89 result.add(new _JSRegExpMatch(this, str, match)); 89 result.add(new _JSRegExpMatch(this, str, match));
90 int endIndex = match[1]; 90 // Find the index at which to start searching for the next match.
91 if (endIndex == length) { 91 index = match[1];
92 break; 92 if (match[0] == index) {
93 } else if (match[0] == endIndex) { 93 // Zero-length match.
94 ++startIndex; // empty match, advance and restart 94 if (index == length) {
95 } else { 95 break;
96 startIndex = endIndex; 96 }
97 index++;
97 } 98 }
98 } 99 }
99 return result; 100 return result;
100 } 101 }
101 102
102 bool hasMatch(String str) { 103 bool hasMatch(String str) {
103 List match = _ExecuteMatch(str, 0); 104 List match = _ExecuteMatch(str, 0);
104 return (match == null) ? false : true; 105 return (match == null) ? false : true;
105 } 106 }
106 107
107 String stringMatch(String str) { 108 String stringMatch(String str) {
108 List match = _ExecuteMatch(str, 0); 109 List match = _ExecuteMatch(str, 0);
109 if (match == null) { 110 if (match == null) {
110 return null; 111 return null;
111 } 112 }
112 return str._substringUnchecked(match[0], match[1]); 113 return str._substringUnchecked(match[0], match[1]);
113 } 114 }
114 115
115 String get pattern native "JSSyntaxRegExp_getPattern"; 116 String get pattern native "JSSyntaxRegExp_getPattern";
116 117
117 bool get isMultiLine native "JSSyntaxRegExp_getIsMultiLine"; 118 bool get isMultiLine native "JSSyntaxRegExp_getIsMultiLine";
118 119
119 bool get isCaseSensitive native "JSSyntaxRegExp_getIsCaseSensitive"; 120 bool get isCaseSensitive native "JSSyntaxRegExp_getIsCaseSensitive";
120 121
121 int get _groupCount native "JSSyntaxRegExp_getGroupCount"; 122 int get _groupCount native "JSSyntaxRegExp_getGroupCount";
122 123
123 List _ExecuteMatch(String str, int start_index) 124 List _ExecuteMatch(String str, int start_index)
124 native "JSSyntaxRegExp_ExecuteMatch"; 125 native "JSSyntaxRegExp_ExecuteMatch";
125 } 126 }
OLDNEW
« no previous file with comments | « no previous file | sdk/lib/_internal/compiler/implementation/lib/js_string.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698