OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 class StringMatch implements Match { | |
6 const StringMatch(int this._start, | |
7 String this.str, | |
8 String this.pattern); | |
9 | |
10 int start() => _start; | |
11 int end() => _start + pattern.length; | |
12 String operator[](int g) => group(g); | |
13 int groupCount() => 0; | |
14 | |
15 String group(int group_) { | |
16 if (group_ != 0) { | |
17 throw new IndexOutOfRangeException(group_); | |
18 } | |
19 return pattern; | |
20 } | |
21 | |
22 List<String> groups(List<int> groups_) { | |
23 List<String> result = new List<String>(); | |
24 for (int g in groups_) { | |
25 result.add(group(g)); | |
26 } | |
27 return result; | |
28 } | |
29 | |
30 final int _start; | |
31 final String str; | |
32 final String pattern; | |
33 } | |
34 | |
35 List<Match> allMatchesInStringUnchecked(String needle, String haystack) { | |
36 // Copied from StringBase.allMatches in | |
37 // ../../../runtime/lib/string.dart | |
38 List<Match> result = new List<Match>(); | |
39 int length = haystack.length; | |
40 int patternLength = needle.length; | |
41 int startIndex = 0; | |
42 while (true) { | |
43 int position = haystack.indexOf(needle, startIndex); | |
44 if (position == -1) { | |
45 break; | |
46 } | |
47 result.add(new StringMatch(position, haystack, needle)); | |
48 int endIndex = position + patternLength; | |
49 if (endIndex == length) { | |
50 break; | |
51 } else if (position == endIndex) { | |
52 ++startIndex; // empty match, advance and restart | |
53 } else { | |
54 startIndex = endIndex; | |
55 } | |
56 } | |
57 return result; | |
58 } | |
59 | |
60 stringContainsUnchecked(receiver, other, startIndex) { | |
61 if (other is String) { | |
62 return receiver.indexOf(other, startIndex) !== -1; | |
63 } else if (other is JSSyntaxRegExp) { | |
64 return other.hasMatch(receiver.substring(startIndex)); | |
65 } else { | |
66 var substr = receiver.substring(startIndex); | |
67 return other.allMatches(substr).iterator().hasNext(); | |
68 } | |
69 } | |
70 | |
71 stringReplaceAllUnchecked(receiver, from, to) { | |
72 if (from is String) { | |
73 if (from == "") { | |
74 if (receiver == "") { | |
75 return to; | |
76 } else { | |
77 StringBuffer result = new StringBuffer(); | |
78 int length = receiver.length; | |
79 result.add(to); | |
80 for (int i = 0; i < length; i++) { | |
81 result.add(receiver[i]); | |
82 result.add(to); | |
83 } | |
84 return result.toString(); | |
85 } | |
86 } else { | |
87 RegExp quoteRegExp = | |
88 const JSSyntaxRegExp(@'[-[\]{}()*+?.,\\^$|#\s]', false, false); | |
89 var quoter = regExpMakeNative(quoteRegExp, global: true); | |
90 var quoted = JS('String', @'#.replace(#, "\\$&")', from, quoter); | |
91 RegExp replaceRegExp = new JSSyntaxRegExp(quoted, false, false); | |
92 var replacer = regExpMakeNative(replaceRegExp, global: true); | |
93 return JS('String', @'#.replace(#, #)', receiver, replacer, to); | |
94 } | |
95 } else if (from is JSSyntaxRegExp) { | |
96 var re = regExpMakeNative(from, global: true); | |
97 return JS('String', @'#.replace(#, #)', receiver, re, to); | |
98 } else { | |
99 checkNull(from); | |
100 // TODO(floitsch): implement generic String.replace (with patterns). | |
101 throw "StringImplementation.replaceAll(Pattern) UNIMPLEMENTED"; | |
102 } | |
103 } | |
104 | |
105 stringReplaceFirstUnchecked(receiver, from, to) { | |
106 if (from is String) { | |
107 return JS('String', @'#.replace(#, #)', receiver, from, to); | |
108 } else if (from is JSSyntaxRegExp) { | |
109 var re = regExpGetNative(from); | |
110 return JS('String', @'#.replace(#, #)', receiver, re, to); | |
111 } else { | |
112 checkNull(from); | |
113 // TODO(floitsch): implement generic String.replace (with patterns). | |
114 throw "StringImplementation.replace(Pattern) UNIMPLEMENTED"; | |
115 } | |
116 } | |
117 | |
118 stringSplitUnchecked(receiver, pattern) { | |
119 if (pattern is String) { | |
120 return JS('List', @'#.split(#)', receiver, pattern); | |
121 } else if (pattern is JSSyntaxRegExp) { | |
122 var re = regExpGetNative(pattern); | |
123 return JS('List', @'#.split(#)', receiver, re); | |
124 } else { | |
125 throw "StringImplementation.split(Pattern) UNIMPLEMENTED"; | |
126 } | |
127 } | |
OLD | NEW |