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

Unified Diff: sdk/lib/internal/iterable.dart

Issue 2845933002: SkipIterable - ensure field is only initialized to a known int. (Closed)
Patch Set: fix .skip(-1) bug Created 3 years, 8 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | tests/corelib/iterable_skip_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sdk/lib/internal/iterable.dart
diff --git a/sdk/lib/internal/iterable.dart b/sdk/lib/internal/iterable.dart
index 7f6b8917f357d088185aa4d462b139c42d46dd43..25cfe68fc4a2fddea1a2ff3fbca411ce0e69d902 100644
--- a/sdk/lib/internal/iterable.dart
+++ b/sdk/lib/internal/iterable.dart
@@ -584,22 +584,13 @@ class SkipIterable<E> extends Iterable<E> {
if (iterable is EfficientLengthIterable) {
return new EfficientLengthSkipIterable<E>(iterable, count);
}
- return new SkipIterable<E>._(iterable, count);
+ return new SkipIterable<E>._(iterable, _checkCount(count));
}
- SkipIterable._(this._iterable, this._skipCount) {
- if (_skipCount is! int) {
- throw new ArgumentError.value(_skipCount, "count is not an integer");
- }
- RangeError.checkNotNegative(_skipCount, "count");
- }
+ SkipIterable._(this._iterable, this._skipCount);
Iterable<E> skip(int count) {
- if (_skipCount is! int) {
- throw new ArgumentError.value(_skipCount, "count is not an integer");
- }
- RangeError.checkNotNegative(_skipCount, "count");
- return new SkipIterable<E>._(_iterable, _skipCount + count);
+ return new SkipIterable<E>._(_iterable, _skipCount + _checkCount(count));
}
Iterator<E> get iterator {
@@ -609,14 +600,31 @@ class SkipIterable<E> extends Iterable<E> {
class EfficientLengthSkipIterable<E> extends SkipIterable<E>
implements EfficientLengthIterable<E> {
- EfficientLengthSkipIterable(Iterable<E> iterable, int skipCount)
- : super._(iterable, skipCount);
+ factory EfficientLengthSkipIterable(Iterable<E> iterable, int count) {
+ return new EfficientLengthSkipIterable<E>._(iterable, _checkCount(count));
+ }
+
+ EfficientLengthSkipIterable._(Iterable<E> iterable, int count)
+ : super._(iterable, count);
int get length {
int length = _iterable.length - _skipCount;
if (length >= 0) return length;
return 0;
}
+
+ Iterable<E> skip(int count) {
+ return new EfficientLengthSkipIterable<E>._(
+ _iterable, _skipCount + _checkCount(count));
+ }
+}
+
+int _checkCount(int count) {
+ if (count is! int) {
+ throw new ArgumentError.value(count, "count", "is not an integer");
+ }
+ RangeError.checkNotNegative(count, "count");
+ return count;
}
class SkipIterator<E> extends Iterator<E> {
« no previous file with comments | « no previous file | tests/corelib/iterable_skip_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698