Chromium Code Reviews| 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 * _kMatchPair)]; |
| 18 } | 18 } |
| 19 | 19 |
| 20 int _end(int group) { | 20 int _end(int group) { |
| 21 return _match[(group * _kMatchPair) + 1]; | 21 return _match[(group * _kMatchPair) + 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 if ((group * _kMatchPair) < _match.length ) { | |
| 29 return str.substringUnchecked_(_start(group), _end(group)); | |
| 30 } | |
| 31 return null; | |
| 26 } | 32 } |
| 27 | 33 |
| 28 String operator [](int group) { | 34 String operator [](int group) { |
| 29 return str.substringUnchecked_(_start(group), _end(group)); | 35 return this.group(group); |
| 30 } | 36 } |
| 31 | 37 |
| 32 List<String> groups(List<int> groups) { | 38 List<String> groups(List<int> groups) { |
| 33 var groupsList = new List<String>(groups.length); | 39 var groupsList = new List<String>(groups.length); |
| 34 for (int i = 0; i < groups.length; i++) { | 40 for (int i = 0; i < groups.length; i++) { |
| 35 int grp_idx = groups[i]; | 41 int grp_idx = groups[i]; |
| 36 groupsList[i] = str.substringUnchecked_(_start(grp_idx), _end(grp_idx)); | 42 groupsList[i] = str.substringUnchecked_(_start(grp_idx), _end(grp_idx)); |
| 37 } | 43 } |
| 38 return groupsList; | 44 return groupsList; |
| 39 } | 45 } |
| 40 | 46 |
| 41 int groupCount() { | 47 int groupCount() { |
| 42 return regexp._groupCount; | 48 return regexp._groupCount; |
| 43 } | 49 } |
| 44 | 50 |
| 45 final RegExp regexp; | 51 final RegExp regexp; |
| 46 final String str; | 52 final String str; |
| 47 final List<int> _match; | 53 final List<int> _match; |
| 48 static final int _kMatchPair = 2; | 54 static final int _kMatchPair = 2; |
|
ahe
2012/03/19 20:59:56
Nit: we use HACKER_STYLE for constants in Dart.
| |
| 49 } | 55 } |
| 50 | 56 |
| 51 | 57 |
| 52 class JSSyntaxRegExp implements RegExp { | 58 class JSSyntaxRegExp implements RegExp { |
| 53 const factory JSSyntaxRegExp( | 59 const factory JSSyntaxRegExp( |
| 54 String pattern, | 60 String pattern, |
| 55 [bool multiLine = false, | 61 [bool multiLine = false, |
| 56 bool ignoreCase = false]) native "JSSyntaxRegExp_factory"; | 62 bool ignoreCase = false]) native "JSSyntaxRegExp_factory"; |
| 57 | 63 |
| 58 Match firstMatch(String str) { | 64 Match firstMatch(String str) { |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 96 | 102 |
| 97 bool get multiLine() native "JSSyntaxRegExp_multiLine"; | 103 bool get multiLine() native "JSSyntaxRegExp_multiLine"; |
| 98 | 104 |
| 99 bool get ignoreCase() native "JSSyntaxRegExp_ignoreCase"; | 105 bool get ignoreCase() native "JSSyntaxRegExp_ignoreCase"; |
| 100 | 106 |
| 101 int get _groupCount() native "JSSyntaxRegExp_getGroupCount"; | 107 int get _groupCount() native "JSSyntaxRegExp_getGroupCount"; |
| 102 | 108 |
| 103 List _ExecuteMatch(String str, int start_index) | 109 List _ExecuteMatch(String str, int start_index) |
| 104 native "JSSyntaxRegExp_ExecuteMatch"; | 110 native "JSSyntaxRegExp_ExecuteMatch"; |
| 105 } | 111 } |
| OLD | NEW |