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

Side by Side Diff: sdk/lib/_collection_dev/iterable.dart

Issue 17020004: State that skip/take does not accept negative arguments. (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
« no previous file with comments | « no previous file | sdk/lib/core/iterable.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2011, 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 part of dart._collection.dev; 5 part of dart._collection.dev;
6 6
7 /** 7 /**
8 * An [Iterable] for classes that have efficient [length] and [elementAt]. 8 * An [Iterable] for classes that have efficient [length] and [elementAt].
9 * 9 *
10 * All other methods are implemented in terms of [length] and [elementAt], 10 * All other methods are implemented in terms of [length] and [elementAt],
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after
209 return result; 209 return result;
210 } 210 }
211 } 211 }
212 212
213 class SubListIterable<E> extends ListIterable<E> { 213 class SubListIterable<E> extends ListIterable<E> {
214 final Iterable<E> _iterable; 214 final Iterable<E> _iterable;
215 final int _start; 215 final int _start;
216 /** If null, represents the length of the iterable. */ 216 /** If null, represents the length of the iterable. */
217 final int _endOrLength; 217 final int _endOrLength;
218 218
219 SubListIterable(this._iterable, this._start, this._endOrLength); 219 SubListIterable(this._iterable, this._start, this._endOrLength) {
220 if (_start < 0) {
221 throw new RangeError.value(_start);
222 }
223 if (_endOrLength != null) {
224 if (_endOrLength < 0) {
225 throw new RangeError.value(_endOrLength);
226 }
227 if (_start > _endOrLength) {
228 throw new RangeError.range(_start, 0, _endOrLength);
229 }
230 }
231 }
220 232
221 int get _endIndex { 233 int get _endIndex {
222 int length = _iterable.length; 234 int length = _iterable.length;
223 if (_endOrLength == null || _endOrLength > length) return length; 235 if (_endOrLength == null || _endOrLength > length) return length;
224 return _endOrLength; 236 return _endOrLength;
225 } 237 }
226 238
227 int get _startIndex { 239 int get _startIndex {
228 int length = _iterable.length; 240 int length = _iterable.length;
229 if (_start > length) return length; 241 if (_start > length) return length;
(...skipping 11 matching lines...) Expand all
241 253
242 E elementAt(int index) { 254 E elementAt(int index) {
243 int realIndex = _startIndex + index; 255 int realIndex = _startIndex + index;
244 if (index < 0 || realIndex >= _endIndex) { 256 if (index < 0 || realIndex >= _endIndex) {
245 throw new RangeError.range(index, 0, length); 257 throw new RangeError.range(index, 0, length);
246 } 258 }
247 return _iterable.elementAt(realIndex); 259 return _iterable.elementAt(realIndex);
248 } 260 }
249 261
250 Iterable<E> skip(int count) { 262 Iterable<E> skip(int count) {
251 if (count < 0) throw new ArgumentError(count); 263 if (count < 0) throw new RangeError.value(count);
252 return new SubListIterable(_iterable, _start + count, _endOrLength); 264 return new SubListIterable(_iterable, _start + count, _endOrLength);
253 } 265 }
254 266
255 Iterable<E> take(int count) { 267 Iterable<E> take(int count) {
256 if (count < 0) throw new ArgumentError(count); 268 if (count < 0) throw new RangeError.value(count);
257 if (_endOrLength == null) { 269 if (_endOrLength == null) {
258 return new SubListIterable(_iterable, _start, _start + count); 270 return new SubListIterable(_iterable, _start, _start + count);
259 } else { 271 } else {
260 int newEnd = _start + count; 272 int newEnd = _start + count;
261 if (_endOrLength < newEnd) return this; 273 if (_endOrLength < newEnd) return this;
262 return new SubListIterable(_iterable, _start, newEnd); 274 return new SubListIterable(_iterable, _start, newEnd);
263 } 275 }
264 } 276 }
265 } 277 }
266 278
(...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after
511 return _iterator.current; 523 return _iterator.current;
512 } 524 }
513 } 525 }
514 526
515 class SkipIterable<E> extends IterableBase<E> { 527 class SkipIterable<E> extends IterableBase<E> {
516 final Iterable<E> _iterable; 528 final Iterable<E> _iterable;
517 final int _skipCount; 529 final int _skipCount;
518 530
519 SkipIterable(this._iterable, this._skipCount) { 531 SkipIterable(this._iterable, this._skipCount) {
520 if (_skipCount is! int || _skipCount < 0) { 532 if (_skipCount is! int || _skipCount < 0) {
521 throw new ArgumentError(_skipCount); 533 throw new RangeError(_skipCount);
522 } 534 }
523 } 535 }
524 536
525 Iterable<E> skip(int n) { 537 Iterable<E> skip(int n) {
526 if (n is! int || n < 0) { 538 if (n is! int || n < 0) {
527 throw new ArgumentError(n); 539 throw new RangeError.value(n);
528 } 540 }
529 return new SkipIterable<E>(_iterable, _skipCount + n); 541 return new SkipIterable<E>(_iterable, _skipCount + n);
530 } 542 }
531 543
532 Iterator<E> get iterator { 544 Iterator<E> get iterator {
533 return new SkipIterator<E>(_iterable.iterator, _skipCount); 545 return new SkipIterator<E>(_iterable.iterator, _skipCount);
534 } 546 }
535 } 547 }
536 548
537 class SkipIterator<E> extends Iterator<E> { 549 class SkipIterator<E> extends Iterator<E> {
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
636 Iterable map(f(E element)) => const EmptyIterable(); 648 Iterable map(f(E element)) => const EmptyIterable();
637 649
638 E reduce(E combine(E value, E element)) { 650 E reduce(E combine(E value, E element)) {
639 throw new StateError("No elements"); 651 throw new StateError("No elements");
640 } 652 }
641 653
642 fold(var initialValue, combine(var previousValue, E element)) { 654 fold(var initialValue, combine(var previousValue, E element)) {
643 return initialValue; 655 return initialValue;
644 } 656 }
645 657
646 Iterable<E> skip(int count) => this; 658 Iterable<E> skip(int count) {
659 if (count < 0) throw new RangeError.value(count);
660 return this;
661 }
647 662
648 Iterable<E> skipWhile(bool test(E element)) => this; 663 Iterable<E> skipWhile(bool test(E element)) => this;
649 664
650 Iterable<E> take(int count) => this; 665 Iterable<E> take(int count) {
666 if (count < 0) throw new RangeError.value(count);
667 this;
668 }
651 669
652 Iterable<E> takeWhile(bool test(E element)) => this; 670 Iterable<E> takeWhile(bool test(E element)) => this;
653 671
654 List toList({ bool growable: true }) => growable ? <E>[] : new List<E>(0); 672 List toList({ bool growable: true }) => growable ? <E>[] : new List<E>(0);
655 673
656 Set toSet() => new Set<E>(); 674 Set toSet() => new Set<E>();
657 } 675 }
658 676
659 /** The always empty iterator. */ 677 /** The always empty iterator. */
660 class EmptyIterator<E> implements Iterator<E> { 678 class EmptyIterator<E> implements Iterator<E> {
(...skipping 375 matching lines...) Expand 10 before | Expand all | Expand 10 after
1036 1054
1037 static Set setDifference(Set set, Set other, Set result) { 1055 static Set setDifference(Set set, Set other, Set result) {
1038 for (var element in set) { 1056 for (var element in set) {
1039 if (!other.contains(element)) { 1057 if (!other.contains(element)) {
1040 result.add(element); 1058 result.add(element);
1041 } 1059 }
1042 } 1060 }
1043 return result; 1061 return result;
1044 } 1062 }
1045 } 1063 }
OLDNEW
« no previous file with comments | « no previous file | sdk/lib/core/iterable.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698