OLD | NEW |
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 library utf.list_range; | 5 library utf.list_range; |
6 | 6 |
7 import 'dart:collection'; | 7 import 'dart:collection'; |
8 | 8 |
9 /** | 9 /** |
10 * _ListRange in an internal type used to create a lightweight Interable on a | 10 * _ListRange in an internal type used to create a lightweight Interable on a |
11 * range within a source list. DO NOT MODIFY the underlying list while | 11 * range within a source list. DO NOT MODIFY the underlying list while |
12 * iterating over it. The results of doing so are undefined. | 12 * iterating over it. The results of doing so are undefined. |
13 */ | 13 */ |
14 // TODO(floitsch): Consider removing the extend and switch to implements since | 14 // TODO(floitsch): Consider removing the extend and switch to implements since |
15 // that's cheaper to allocate. | 15 // that's cheaper to allocate. |
16 class ListRange extends IterableBase { | 16 class ListRange extends IterableBase<int> { |
17 final List _source; | 17 final List<int> _source; |
18 final int _offset; | 18 final int _offset; |
19 final int _length; | 19 final int _length; |
20 | 20 |
21 ListRange(source, [offset = 0, length]) : | 21 ListRange(List<int> source, [offset = 0, length]) |
22 this._source = source, | 22 : this._source = source, |
23 this._offset = offset, | 23 this._offset = offset, |
24 this._length = (length == null ? source.length - offset : length) { | 24 this._length = (length == null ? source.length - offset : length) { |
25 if (_offset < 0 || _offset > _source.length) { | 25 if (_offset < 0 || _offset > _source.length) { |
26 throw new RangeError.value(_offset); | 26 throw new RangeError.value(_offset); |
27 } | 27 } |
28 if (_length != null && (_length < 0)) { | 28 if (_length != null && (_length < 0)) { |
29 throw new RangeError.value(_length); | 29 throw new RangeError.value(_length); |
30 } | 30 } |
31 if (_length + _offset > _source.length) { | 31 if (_length + _offset > _source.length) { |
32 throw new RangeError.value(_length + _offset); | 32 throw new RangeError.value(_length + _offset); |
33 } | 33 } |
34 } | 34 } |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
70 void backup([int by = 1]) { | 70 void backup([int by = 1]) { |
71 _offset -= by; | 71 _offset -= by; |
72 } | 72 } |
73 | 73 |
74 int get remaining => _end - _offset - 1; | 74 int get remaining => _end - _offset - 1; |
75 | 75 |
76 void skip([int count = 1]) { | 76 void skip([int count = 1]) { |
77 _offset += count; | 77 _offset += count; |
78 } | 78 } |
79 } | 79 } |
OLD | NEW |