Skip to content

Commit c0061ef

Browse files
committed
Implement missing functions on ListBase.
Review URL: https://codereview.chromium.org//12380086 git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@19382 260f80e4-7a28-3924-810f-c04153c831b5
1 parent 4d30b1b commit c0061ef

File tree

3 files changed

+65
-14
lines changed

3 files changed

+65
-14
lines changed

sdk/lib/_collection_dev/list.dart

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,31 @@ abstract class ListBase<E> extends ListIterable<E> implements List<E> {
2424
E elementAt(int index) {
2525
return this[index];
2626
}
27+
28+
List<E> getRange(int start, int length) {
29+
if (start < 0 || start > this.length) {
30+
throw new RangeError.range(start, 0, this.length);
31+
}
32+
if (length < 0 || start + length > this.length) {
33+
throw new RangeError.range(length, 0, this.length - start);
34+
}
35+
List<E> result = new List<E>(length);
36+
for (int i = 0; i < length; i++) {
37+
result[i] = this[start + i];
38+
}
39+
return result;
40+
}
41+
42+
int indexOf(E element, [int start = 0]) {
43+
return Arrays.indexOf(this, element, start, this.length);
44+
}
45+
46+
int lastIndexOf(E element, [int start = null]) {
47+
if (start == null) start = length - 1;
48+
return Arrays.lastIndexOf(this, element, start);
49+
}
50+
51+
Iterable<E> get reversed => new ReversedListIterable(this);
2752
}
2853

2954
/**
@@ -86,6 +111,11 @@ abstract class FixedLengthListBase<E> extends ListBase<E> {
86111
"Cannot remove from a fixed-length list");
87112
}
88113

114+
void retainMatching(bool test(E element)) {
115+
throw new UnsupportedError(
116+
"Cannot remove from a fixed-length list");
117+
}
118+
89119
void clear() {
90120
throw new UnsupportedError(
91121
"Cannot clear a fixed-length list");
@@ -162,6 +192,11 @@ abstract class UnmodifiableListBase<E> extends ListBase<E> {
162192
"Cannot remove from an unmodifiable list");
163193
}
164194

195+
void retainMatching(bool test(E element)) {
196+
throw new UnsupportedError(
197+
"Cannot remove from an unmodifiable list");
198+
}
199+
165200
void sort([Comparator<E> compare]) {
166201
throw new UnsupportedError(
167202
"Cannot modify an unmodifiable list");
@@ -196,20 +231,6 @@ abstract class UnmodifiableListBase<E> extends ListBase<E> {
196231
throw new UnsupportedError(
197232
"Cannot insert range in an unmodifiable list");
198233
}
199-
200-
List<E> getRange(int start, int length) {
201-
if (start < 0 || start > this.length) {
202-
throw new RangeError.range(start, 0, this.length);
203-
}
204-
if (length < 0 || start + length > this.length) {
205-
throw new RangeError.range(length, 0, this.length - start);
206-
}
207-
List<E> result = new List<E>(length);
208-
for (int i = 0; i < length; i++) {
209-
result[i] = this[start + i];
210-
}
211-
return result;
212-
}
213234
}
214235

215236
/** An empty fixed-length list. */

tests/corelib/corelib.status

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ string_base_vm_test: Fail, OK # VM specific test.
4141

4242
string_replace_func_test: Skip # Bug 6554 - doesn't terminate.
4343

44+
string_codeunits_test: Fail # Issue 8904
45+
4446
[ $compiler == dart2js && $runtime == none ]
4547
*: Fail, Pass # TODO(ahe): Triage these tests.
4648

tests/corelib/string_codeunits_test.dart

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,36 @@ main() {
2525
// .map
2626
Expect.listEquals(expectedUnits.map((x) => x.toRadixString(16)).toList(),
2727
units.map((x) => x.toRadixString(16)).toList());
28+
29+
if (s == "") {
30+
Expect.throws(() => units.first, (e) => e is StateError);
31+
Expect.throws(() => units.last, (e) => e is StateError);
32+
Expect.throws(() => units[0], (e) => e is RangeError);
33+
Expect.throws(() => units[0] = 499, (e) => e is UnsupportedError);
34+
Expect.listEquals([], units.getRange(0, 0));
35+
Expect.equals(-1, units.indexOf(42));
36+
Expect.equals(-1, units.lastIndexOf(499));
37+
} else {
38+
Expect.equals(s.codeUnitAt(0), units.first);
39+
Expect.equals(s.codeUnitAt(s.length - 1), units.last);
40+
Expect.equals(s.codeUnitAt(0), units[0]);
41+
Expect.throws(() { units[0] = 499; }, (e) => e is UnsupportedError);
42+
List<int> sub = units.getRange(1, units.length - 1);
43+
Expect.listEquals(s.substring(1, s.length).codeUnits, sub);
44+
Expect.equals(-1, units.indexOf(-1));
45+
Expect.equals(0, units.indexOf(units[0]));
46+
Expect.equals(-1, units.lastIndexOf(-1));
47+
Expect.equals(units.length - 1, units.lastIndexOf(units[units.length - 1]));
48+
}
49+
50+
Iterable reversed = units.reversed;
51+
int i = units.length - 1;
52+
for (int codeUnit in reversed) {
53+
Expect.equals(units[i--], codeUnit);
54+
}
2855
}
2956

57+
test("");
3058
test("abc");
3159
test("\x00\u0000\u{000000}");
3260
test("\u{ffff}\u{10000}\u{10ffff}");

0 commit comments

Comments
 (0)