Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(510)

Side by Side Diff: dart/frog/leg/lib/string_helper.dart

Issue 9426040: Improve String and RegExp implementations. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge
Patch Set: changes Created 8 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 allMatchesInStringUnchecked(receiver, str) {
36 var result = new List();
37 var length = receiver.length;
38 if (length === 0) {
39 return result;
40 }
41
42 var strLength = str.length;
43 for (var i = 0; i < strLength;) {
44 var index = str.indexOf(receiver, i);
45 if (index < 0) {
46 return result;
47 }
48 result.add(new StringMatch(index, str, receiver));
49 i = index + length;
50 }
51 return result;
52 }
53
54 stringContainsUnchecked(receiver, other, startIndex) {
55 if (other is String) {
56 return receiver.indexOf(other, startIndex) !== -1;
57 } else if (other is RegExp) {
58 return other.hasMatch(receiver.substring(startIndex));
59 } else {
60 var substr = receiver.substring(startIndex);
61 return other.allMatches(substr).iterator().hasNext();
62 }
63 }
64
65 stringReplaceAllUnchecked(receiver, from, to) {
66 if (from is String) {
67 if (from == "") {
68 if (receiver == "") {
69 return to;
70 } else {
71 StringBuffer result = new StringBuffer();
72 int length = receiver.length;
73 result.add(to);
74 for (int i = 0; i < length; i++) {
75 result.add(receiver[i]);
76 result.add(to);
77 }
78 return result.toString();
79 }
80 } else {
81 var quoter =
82 new RegExpWrapper(@'[-[\]{}()*+?.,\\^$|#\s]', false, false, true).re;
ngeoffray 2012/02/21 12:15:53 Add this constant as a static final of this class?
83 var quoted = JS('String', @'$0.replace($1, "\\$&")', from, quoter);
84 var re = new RegExpWrapper(quoted, false, false, true).re;
85 return JS('String', @'$0.replace($1, $2)', receiver, re, to);
86 }
87 } else if (from is RegExp) {
88 var re = new RegExpWrapper.fromRegExp(from, true).re;
89 return JS('String', @'$0.replace($1, $2)', receiver, re, to);
90 } else {
91 // TODO(floitsch): implement generic String.replace (with patterns).
92 throw "StringImplementation.replaceAll(Pattern) UNIMPLEMENTED";
93 }
94 }
95
96 stringReplaceFirstUnchecked(receiver, from, to) {
97 if (from is String) {
98 return JS('String', @'$0.replace($1, $2)', receiver, from, to);
99 } else if (from is RegExp) {
100 var re = new RegExpWrapper.fromRegExp(from, false).re;
101 return JS('String', @'$0.replace($1, $2)', receiver, re, to);
102 } else {
103 // TODO(floitsch): implement generic String.replace (with patterns).
104 throw "StringImplementation.replace(Pattern) UNIMPLEMENTED";
105 }
106 }
107
108 stringSplitUnchecked(receiver, pattern) {
109 if (pattern is String) {
110 return JS('List', @'$0.split($1)', receiver, pattern);
111 } else if (pattern is RegExp) {
112 var re = new RegExpWrapper.fromRegExp(pattern, false).re;
113 return JS('List', @'$0.split($1)', receiver, re);
114 } else {
115 throw "StringImplementation.split(Pattern) UNIMPLEMENTED";
116 }
117 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698