OLD | NEW |
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 class JSRegExpMatch implements Match { | 5 class JSRegExpMatch implements Match { |
6 JSRegExpMatch(this.regexp, this.str, this._match); | 6 JSRegExpMatch(this.regexp, this.str, this._match); |
7 | 7 |
8 int start() { | 8 int start() { |
9 return _start(0); | 9 return _start(0); |
10 } | 10 } |
11 | 11 |
12 int end() { | 12 int end() { |
13 return _end(0); | 13 return _end(0); |
14 } | 14 } |
15 | 15 |
16 int _start(int group) { | 16 int _start(int group) { |
17 return _match[(group * _kMatchPair)]; | 17 return _match[(group * MATCH_PAIR)]; |
18 } | 18 } |
19 | 19 |
20 int _end(int group) { | 20 int _end(int group) { |
21 return _match[(group * _kMatchPair) + 1]; | 21 return _match[(group * MATCH_PAIR) + 1]; |
22 } | 22 } |
23 | 23 |
24 String group(int group) { | 24 String group(int group) { |
25 return str.substringUnchecked_(_start(group), _end(group)); | 25 if (group < 0 || group > regexp._groupCount) { |
| 26 throw new IndexOutOfRangeException(group); |
| 27 } |
| 28 int startIndex = _start(group); |
| 29 int endIndex = _end(group); |
| 30 if (startIndex == -1) { |
| 31 assert(endIndex == -1); |
| 32 return null; |
| 33 } |
| 34 return str.substringUnchecked_(startIndex, endIndex); |
26 } | 35 } |
27 | 36 |
28 String operator [](int group) { | 37 String operator [](int group) { |
29 return str.substringUnchecked_(_start(group), _end(group)); | 38 return this.group(group); |
30 } | 39 } |
31 | 40 |
32 List<String> groups(List<int> groups) { | 41 List<String> groups(List<int> groups) { |
33 var groupsList = new List<String>(groups.length); | 42 var groupsList = new List<String>(groups.length); |
34 for (int i = 0; i < groups.length; i++) { | 43 for (int i = 0; i < groups.length; i++) { |
35 int grp_idx = groups[i]; | 44 groupsList[i] = group(groups[i]); |
36 groupsList[i] = str.substringUnchecked_(_start(grp_idx), _end(grp_idx)); | |
37 } | 45 } |
38 return groupsList; | 46 return groupsList; |
39 } | 47 } |
40 | 48 |
41 int groupCount() { | 49 int groupCount() { |
42 return regexp._groupCount; | 50 return regexp._groupCount; |
43 } | 51 } |
44 | 52 |
45 final RegExp regexp; | 53 final RegExp regexp; |
46 final String str; | 54 final String str; |
47 final List<int> _match; | 55 final List<int> _match; |
48 static final int _kMatchPair = 2; | 56 static final int MATCH_PAIR = 2; |
49 } | 57 } |
50 | 58 |
51 | 59 |
52 class JSSyntaxRegExp implements RegExp { | 60 class JSSyntaxRegExp implements RegExp { |
53 const factory JSSyntaxRegExp( | 61 const factory JSSyntaxRegExp( |
54 String pattern, | 62 String pattern, |
55 [bool multiLine = false, | 63 [bool multiLine = false, |
56 bool ignoreCase = false]) native "JSSyntaxRegExp_factory"; | 64 bool ignoreCase = false]) native "JSSyntaxRegExp_factory"; |
57 | 65 |
58 Match firstMatch(String str) { | 66 Match firstMatch(String str) { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 | 104 |
97 bool get multiLine() native "JSSyntaxRegExp_multiLine"; | 105 bool get multiLine() native "JSSyntaxRegExp_multiLine"; |
98 | 106 |
99 bool get ignoreCase() native "JSSyntaxRegExp_ignoreCase"; | 107 bool get ignoreCase() native "JSSyntaxRegExp_ignoreCase"; |
100 | 108 |
101 int get _groupCount() native "JSSyntaxRegExp_getGroupCount"; | 109 int get _groupCount() native "JSSyntaxRegExp_getGroupCount"; |
102 | 110 |
103 List _ExecuteMatch(String str, int start_index) | 111 List _ExecuteMatch(String str, int start_index) |
104 native "JSSyntaxRegExp_ExecuteMatch"; | 112 native "JSSyntaxRegExp_ExecuteMatch"; |
105 } | 113 } |
OLD | NEW |