Fix view creation for byte-size types.

We had forgotten to subtract the start byte offset from the length
when not passing in an explicit length.

Additionally, fix a couple of checked mode issues.

R=asiva@google.com
BUG=

Review URL: https://codereview.chromium.org//13529005

git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@20867 260f80e4-7a28-3924-810f-c04153c831b5
This commit is contained in:
ager@google.com 2013-04-03 15:29:11 +00:00
parent b8d6302597
commit 6003fdfe6e
2 changed files with 42 additions and 6 deletions

View file

@ -278,7 +278,7 @@ patch class ByteData {
/* patch */ factory ByteData.view(ByteBuffer buffer,
[int offsetInBytes = 0, int length]) {
if (length == null) {
length = buffer.lengthInBytes;
length = buffer.lengthInBytes - offsetInBytes;
}
return new _ByteDataView(buffer, offsetInBytes, length);
}
@ -312,7 +312,7 @@ abstract class _TypedListBase {
return IterableMixinWorkaround.reduce(this, initialValue, combine);
}
Collection where(bool f(int element)) {
Iterable where(bool f(int element)) {
return IterableMixinWorkaround.where(this, f);
}
@ -578,7 +578,7 @@ class _Int8Array extends _TypedList implements Int8List {
factory _Int8Array.view(ByteBuffer buffer,
[int offsetInBytes = 0, int length]) {
if (length == null) {
length = buffer.lengthInBytes;
length = buffer.lengthInBytes - offsetInBytes;
}
return new _Int8ArrayView(buffer, offsetInBytes, length);
}
@ -636,7 +636,7 @@ class _Uint8Array extends _TypedList implements Uint8List {
factory _Uint8Array.view(ByteBuffer buffer,
[int offsetInBytes = 0, int length]) {
if (length == null) {
length = buffer.lengthInBytes;
length = buffer.lengthInBytes - offsetInBytes;
}
return new _Uint8ArrayView(buffer, offsetInBytes, length);
}
@ -692,7 +692,7 @@ class _Uint8ClampedArray extends _TypedList implements Uint8ClampedList {
factory _Uint8ClampedArray.view(ByteBuffer buffer,
[int offsetInBytes = 0, int length]) {
if (length == null) {
length = buffer.lengthInBytes;
length = buffer.lengthInBytes - offsetInBytes;
}
return new _Uint8ClampedArrayView(buffer, offsetInBytes, length);
}
@ -2322,7 +2322,7 @@ class _Uint8ArrayView extends _TypedListView implements Uint8List {
}
class _Uint8ClampedArrayView extends _TypedListView implements Uint8List {
class _Uint8ClampedArrayView extends _TypedListView implements Uint8ClampedList {
// Constructor.
_Uint8ClampedArrayView(ByteBuffer buffer,
[int _offsetInBytes = 0, int _length])

View file

@ -279,6 +279,40 @@ void testSetAtIndex(TypedData list,
validate(false);
}
testViewCreation() {
var bytes = new Uint8List(1024);
var view = new ByteData.view(bytes, 24);
Expect.equals(1000, view.lengthInBytes);
view = new Uint8List.view(bytes, 24);
Expect.equals(1000, view.lengthInBytes);
view = new Int8List.view(bytes, 24);
Expect.equals(1000, view.lengthInBytes);
view = new Uint8ClampedList.view(bytes, 24);
Expect.equals(1000, view.lengthInBytes);
view = new Uint16List.view(bytes, 24);
Expect.equals(1000, view.lengthInBytes);
view = new Int16List.view(bytes, 24);
Expect.equals(1000, view.lengthInBytes);
view = new Uint32List.view(bytes, 24);
Expect.equals(1000, view.lengthInBytes);
view = new Int32List.view(bytes, 24);
Expect.equals(1000, view.lengthInBytes);
view = new Uint64List.view(bytes, 24);
Expect.equals(1000, view.lengthInBytes);
view = new Int64List.view(bytes, 24);
Expect.equals(1000, view.lengthInBytes);
view = new Float32List.view(bytes, 24);
Expect.equals(1000, view.lengthInBytes);
view = new Float64List.view(bytes, 24);
Expect.equals(1000, view.lengthInBytes);
}
testWhere() {
var bytes = new Uint8List(13);
bytes.setRange(0, 5, [1, 1, 1, 1, 1]);
Expect.equals(5, bytes.where((v) => v > 0).length);
}
main() {
for (int i = 0; i < 2000; i++) {
testCreateUint8TypedData();
@ -335,5 +369,7 @@ main() {
testTypedDataRange(true);
testUnsignedTypedDataRange(true);
testExternalClampedUnsignedTypedDataRange(true);
testViewCreation();
testWhere();
}