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 return new _JSSyntaxRegExp(pattern, |
| 10 multiLine: multiLine, |
| 11 ignoreCase: ignoreCase); |
| 12 } |
| 13 } |
| 14 |
5 class _JSRegExpMatch implements Match { | 15 class _JSRegExpMatch implements Match { |
6 _JSRegExpMatch(this.regexp, this.str, this._match); | 16 _JSRegExpMatch(this.regexp, this.str, this._match); |
7 | 17 |
8 int get start => _start(0); | 18 int get start => _start(0); |
9 int get end => _end(0); | 19 int get end => _end(0); |
10 | 20 |
11 int _start(int groupIdx) { | 21 int _start(int groupIdx) { |
12 return _match[(groupIdx * MATCH_PAIR)]; | 22 return _match[(groupIdx * MATCH_PAIR)]; |
13 } | 23 } |
14 | 24 |
15 int _end(int groupIdx) { | 25 int _end(int groupIdx) { |
16 return _match[(groupIdx * MATCH_PAIR) + 1]; | 26 return _match[(groupIdx * MATCH_PAIR) + 1]; |
17 } | 27 } |
18 | 28 |
19 String group(int groupIdx) { | 29 String group(int groupIdx) { |
20 if (groupIdx < 0 || groupIdx > regexp._groupCount) { | 30 if (groupIdx < 0 || groupIdx > regexp._groupCount) { |
21 throw new RangeError.value(groupIdx); | 31 throw new RangeError.value(groupIdx); |
22 } | 32 } |
23 int startIndex = _start(groupIdx); | 33 int startIndex = _start(groupIdx); |
24 int endIndex = _end(groupIdx); | 34 int endIndex = _end(groupIdx); |
25 if (startIndex == -1) { | 35 if (startIndex == -1) { |
26 assert(endIndex == -1); | 36 assert(endIndex == -1); |
27 return null; | 37 return null; |
28 } | 38 } |
29 // TODO(ajohnsen): Use _substringUnchecked when regexp is in core. | 39 return str._substringUnchecked(startIndex, endIndex); |
30 return str.substring(startIndex, endIndex); | |
31 } | 40 } |
32 | 41 |
33 String operator [](int groupIdx) { | 42 String operator [](int groupIdx) { |
34 return this.group(groupIdx); | 43 return this.group(groupIdx); |
35 } | 44 } |
36 | 45 |
37 List<String> groups(List<int> groupsSpec) { | 46 List<String> groups(List<int> groupsSpec) { |
38 var groupsList = new List<String>(groupsSpec.length); | 47 var groupsList = new List<String>(groupsSpec.length); |
39 for (int i = 0; i < groupsSpec.length; i++) { | 48 for (int i = 0; i < groupsSpec.length; i++) { |
40 groupsList[i] = group(groupsSpec[i]); | 49 groupsList[i] = group(groupsSpec[i]); |
41 } | 50 } |
42 return groupsList; | 51 return groupsList; |
43 } | 52 } |
44 | 53 |
45 int get groupCount => regexp._groupCount; | 54 int get groupCount => regexp._groupCount; |
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 implements RegExp { |
57 /* patch */ 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 return str._substringUnchecked(match[0], match[1]); |
103 return str.substring(match[0], match[1]); | |
104 } | 112 } |
105 | 113 |
106 /* patch */ String get pattern native "JSSyntaxRegExp_getPattern"; | 114 String get pattern native "JSSyntaxRegExp_getPattern"; |
107 | 115 |
108 /* patch */ bool get multiLine native "JSSyntaxRegExp_multiLine"; | 116 bool get multiLine native "JSSyntaxRegExp_multiLine"; |
109 | 117 |
110 /* patch */ bool get ignoreCase native "JSSyntaxRegExp_ignoreCase"; | 118 bool get ignoreCase native "JSSyntaxRegExp_ignoreCase"; |
111 | 119 |
112 int get _groupCount native "JSSyntaxRegExp_getGroupCount"; | 120 int get _groupCount native "JSSyntaxRegExp_getGroupCount"; |
113 | 121 |
114 List _ExecuteMatch(String str, int start_index) | 122 List _ExecuteMatch(String str, int start_index) |
115 native "JSSyntaxRegExp_ExecuteMatch"; | 123 native "JSSyntaxRegExp_ExecuteMatch"; |
116 } | 124 } |
OLD | NEW |