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

Side by Side Diff: runtime/lib/string_patch.dart

Issue 16957002: Use Pattern.matchAsPrefix to let String.indexOf/lastIndexOf/startsWith accept Pattern. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 6 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
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 String { 5 patch class String {
6 /* patch */ factory String.fromCharCodes(Iterable<int> charCodes) { 6 /* patch */ factory String.fromCharCodes(Iterable<int> charCodes) {
7 return _StringBase.createFromCharCodes(charCodes); 7 return _StringBase.createFromCharCodes(charCodes);
8 } 8 }
9 } 9 }
10 10
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
106 return 1; 106 return 1;
107 } 107 }
108 } 108 }
109 if (thisLength < otherLength) return -1; 109 if (thisLength < otherLength) return -1;
110 if (thisLength > otherLength) return 1; 110 if (thisLength > otherLength) return 1;
111 return 0; 111 return 0;
112 } 112 }
113 113
114 bool _substringMatches(int start, String other) { 114 bool _substringMatches(int start, String other) {
115 if (other.isEmpty) return true; 115 if (other.isEmpty) return true;
116 if ((start < 0) || (start >= this.length)) {
117 return false;
118 }
119 final int len = other.length; 116 final int len = other.length;
120 if ((start + len) > this.length) { 117 if ((start < 0) || (start + len > this.length)) {
121 return false; 118 return false;
122 } 119 }
123 for (int i = 0; i < len; i++) { 120 for (int i = 0; i < len; i++) {
124 if (this.codeUnitAt(i + start) != other.codeUnitAt(i)) { 121 if (this.codeUnitAt(i + start) != other.codeUnitAt(i)) {
125 return false; 122 return false;
126 } 123 }
127 } 124 }
128 return true; 125 return true;
129 } 126 }
130 127
131 bool endsWith(String other) { 128 bool endsWith(String other) {
132 return _substringMatches(this.length - other.length, other); 129 return _substringMatches(this.length - other.length, other);
133 } 130 }
134 131
135 bool startsWith(String other) { 132 bool startsWith(Pattern pattern) {
136 return _substringMatches(0, other); 133 if (pattern is String) {
134 return _substringMatches(0, pattern);
135 }
136 return pattern.matchAsPrefix(this, 0) != null;
137 } 137 }
138 138
139 int indexOf(String other, [int start = 0]) { 139 int indexOf(Pattern pattern, [int start = 0]) {
140 if (other.isEmpty) { 140 if (start < 0 || start > this.length) {
141 return start < this.length ? start : this.length; 141 throw new RangeError.range(start, 0, this.length);
142 } 142 }
143 if ((start < 0) || (start >= this.length)) { 143 if (pattern is String) {
144 String other = pattern;
145 int maxIndex = this.length - other.length;
146 // Consider using an efficient string search (e.g. BMH).
floitsch 2013/06/13 11:54:58 Make this a TODO.
Lasse Reichstein Nielsen 2013/06/13 12:37:49 Done.
147 for (int index = start; index <= maxIndex; index++) {
148 if (_substringMatches(index, other)) {
149 return index;
150 }
151 }
144 return -1; 152 return -1;
145 } 153 }
146 int len = this.length - other.length + 1; 154 for (int i = start; i <= this.length; i++) {
147 for (int index = start; index < len; index++) { 155 if (pattern.matchAsPrefix(this, i) != null) return i;
148 if (_substringMatches(index, other)) {
149 return index;
150 }
151 } 156 }
152 return -1; 157 return -1;
153 } 158 }
154 159
155 int lastIndexOf(String other, [int start = null]) { 160 int lastIndexOf(Pattern pattern, [int start = null]) {
156 if (start == null) start = length - 1; 161 if (start == null) {
157 if (other.isEmpty) { 162 start = this.length;
158 return min(this.length, start); 163 } else if (start < 0 || start > this.length) {
164 throw new RangeError.range(start, 0, this.length);
159 } 165 }
160 if (start >= this.length) { 166 if (pattern is String) {
161 start = this.length - 1; 167 String other = pattern;
168 int maxIndex = this.length - other.length;
169 if (maxIndex < start) start = maxIndex;
170 for (int index = start; index >= 0; index--) {
171 if (_substringMatches(index, other)) {
172 return index;
173 }
174 }
175 return -1;
162 } 176 }
163 for (int index = start; index >= 0; index--) { 177 for (int i = start; i >= 0; i--) {
164 if (_substringMatches(index, other)) { 178 // This has quadratic behavior because matchAsPrefix tries to find a later
floitsch 2013/06/13 11:54:58 TODO(..): currently this has quadratic behavior be
Lasse Reichstein Nielsen 2013/06/13 12:37:49 Done.
165 return index; 179 // match too. Optimize matchAsPrefix to avoid this.
166 } 180 if (pattern.matchAsPrefix(this, i) != null) return i;
167 } 181 }
168 return -1; 182 return -1;
169 } 183 }
170 184
171 String substring(int startIndex, [int endIndex]) { 185 String substring(int startIndex, [int endIndex]) {
172 if (endIndex == null) endIndex = this.length; 186 if (endIndex == null) endIndex = this.length;
173 187
174 if ((startIndex < 0) || (startIndex > this.length)) { 188 if ((startIndex < 0) || (startIndex > this.length)) {
175 throw new RangeError.value(startIndex); 189 throw new RangeError.value(startIndex);
176 } 190 }
(...skipping 437 matching lines...) Expand 10 before | Expand all | Expand 10 after
614 class _CodeUnits extends Object with ListMixin<int>, 628 class _CodeUnits extends Object with ListMixin<int>,
615 UnmodifiableListMixin<int> { 629 UnmodifiableListMixin<int> {
616 /** The string that this is the code units of. */ 630 /** The string that this is the code units of. */
617 String _string; 631 String _string;
618 632
619 _CodeUnits(this._string); 633 _CodeUnits(this._string);
620 634
621 int get length => _string.length; 635 int get length => _string.length;
622 int operator[](int i) => _string.codeUnitAt(i); 636 int operator[](int i) => _string.codeUnitAt(i);
623 } 637 }
OLDNEW
« no previous file with comments | « no previous file | sdk/lib/_internal/compiler/implementation/lib/interceptors.dart » ('j') | tests/corelib/string_test.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698