Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 { | |
| 6 /* patch */ factory RegExp(String pattern, | |
| 7 {bool multiLine: false, | |
| 8 bool ignoreCase: false}) | |
| 9 => new _JSSyntaxRegExp(pattern, | |
|
Mads Ager (google)
2012/11/12 12:28:10
I don't like the arrow when the body takes up this
Anders Johnsen
2012/11/12 12:30:32
Sure, done!
| |
| 10 multiLine: multiLine, | |
| 11 ignoreCase: ignoreCase); | |
| 12 } | |
| 13 | |
| 5 class _JSRegExpMatch implements Match { | 14 class _JSRegExpMatch implements Match { |
| 6 _JSRegExpMatch(this.regexp, this.str, this._match); | 15 _JSRegExpMatch(this.regexp, this.str, this._match); |
| 7 | 16 |
| 8 int get start => _start(0); | 17 int get start => _start(0); |
| 9 int get end => _end(0); | 18 int get end => _end(0); |
| 10 | 19 |
| 11 int _start(int groupIdx) { | 20 int _start(int groupIdx) { |
| 12 return _match[(groupIdx * MATCH_PAIR)]; | 21 return _match[(groupIdx * MATCH_PAIR)]; |
| 13 } | 22 } |
| 14 | 23 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 46 | 55 |
| 47 String get pattern => regexp.pattern; | 56 String get pattern => regexp.pattern; |
| 48 | 57 |
| 49 final RegExp regexp; | 58 final RegExp regexp; |
| 50 final String str; | 59 final String str; |
| 51 final List<int> _match; | 60 final List<int> _match; |
| 52 static const int MATCH_PAIR = 2; | 61 static const int MATCH_PAIR = 2; |
| 53 } | 62 } |
| 54 | 63 |
| 55 | 64 |
| 56 patch class JSSyntaxRegExp { | 65 class _JSSyntaxRegExp extends RegExp { |
| 57 /* patch */ const factory JSSyntaxRegExp( | 66 factory _JSSyntaxRegExp( |
| 58 String pattern, | 67 String pattern, |
| 59 {bool multiLine: false, | 68 {bool multiLine: false, |
| 60 bool ignoreCase: false}) native "JSSyntaxRegExp_factory"; | 69 bool ignoreCase: false}) native "JSSyntaxRegExp_factory"; |
| 61 | 70 |
| 62 /* patch */ Match firstMatch(String str) { | 71 Match firstMatch(String str) { |
| 63 List match = _ExecuteMatch(str, 0); | 72 List match = _ExecuteMatch(str, 0); |
| 64 if (match === null) { | 73 if (match === null) { |
| 65 return null; | 74 return null; |
| 66 } | 75 } |
| 67 return new _JSRegExpMatch(this, str, match); | 76 return new _JSRegExpMatch(this, str, match); |
| 68 } | 77 } |
| 69 | 78 |
| 70 /* patch */ Iterable<Match> allMatches(String str) { | 79 Iterable<Match> allMatches(String str) { |
| 71 List<Match> result = new List<Match>(); | 80 List<Match> result = new List<Match>(); |
| 72 int length = str.length; | 81 int length = str.length; |
| 73 int startIndex = 0; | 82 int startIndex = 0; |
| 74 while (true) { | 83 while (true) { |
| 75 List match = _ExecuteMatch(str, startIndex); | 84 List match = _ExecuteMatch(str, startIndex); |
| 76 if (match == null) { | 85 if (match == null) { |
| 77 break; | 86 break; |
| 78 } | 87 } |
| 79 result.add(new _JSRegExpMatch(this, str, match)); | 88 result.add(new _JSRegExpMatch(this, str, match)); |
| 80 int endIndex = match[1]; | 89 int endIndex = match[1]; |
| 81 if (endIndex == length) { | 90 if (endIndex == length) { |
| 82 break; | 91 break; |
| 83 } else if (match[0] == endIndex) { | 92 } else if (match[0] == endIndex) { |
| 84 ++startIndex; // empty match, advance and restart | 93 ++startIndex; // empty match, advance and restart |
| 85 } else { | 94 } else { |
| 86 startIndex = endIndex; | 95 startIndex = endIndex; |
| 87 } | 96 } |
| 88 } | 97 } |
| 89 return result; | 98 return result; |
| 90 } | 99 } |
| 91 | 100 |
| 92 /* patch */ bool hasMatch(String str) { | 101 bool hasMatch(String str) { |
| 93 List match = _ExecuteMatch(str, 0); | 102 List match = _ExecuteMatch(str, 0); |
| 94 return (match === null) ? false : true; | 103 return (match === null) ? false : true; |
| 95 } | 104 } |
| 96 | 105 |
| 97 /* patch */ String stringMatch(String str) { | 106 String stringMatch(String str) { |
| 98 List match = _ExecuteMatch(str, 0); | 107 List match = _ExecuteMatch(str, 0); |
| 99 if (match === null) { | 108 if (match === null) { |
| 100 return null; | 109 return null; |
| 101 } | 110 } |
| 102 // TODO(ajohnsen): Use _substringUnchecked when regexp is in core. | 111 // TODO(ajohnsen): Use _substringUnchecked when regexp is in core. |
| 103 return str.substring(match[0], match[1]); | 112 return str.substring(match[0], match[1]); |
| 104 } | 113 } |
| 105 | 114 |
| 106 /* patch */ String get pattern native "JSSyntaxRegExp_getPattern"; | 115 String get pattern native "JSSyntaxRegExp_getPattern"; |
| 107 | 116 |
| 108 /* patch */ bool get multiLine native "JSSyntaxRegExp_multiLine"; | 117 bool get multiLine native "JSSyntaxRegExp_multiLine"; |
| 109 | 118 |
| 110 /* patch */ bool get ignoreCase native "JSSyntaxRegExp_ignoreCase"; | 119 bool get ignoreCase native "JSSyntaxRegExp_ignoreCase"; |
| 111 | 120 |
| 112 int get _groupCount native "JSSyntaxRegExp_getGroupCount"; | 121 int get _groupCount native "JSSyntaxRegExp_getGroupCount"; |
| 113 | 122 |
| 114 List _ExecuteMatch(String str, int start_index) | 123 List _ExecuteMatch(String str, int start_index) |
| 115 native "JSSyntaxRegExp_ExecuteMatch"; | 124 native "JSSyntaxRegExp_ExecuteMatch"; |
| 116 } | 125 } |
| OLD | NEW |