Skip to content

Commit 448ce9d

Browse files
Remove references to asExternalTypedData (flutter#13)
* asTypedList * changelog
1 parent 630570e commit 448ce9d

File tree

5 files changed

+15
-20
lines changed

5 files changed

+15
-20
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 0.1.3-dev.3
4+
5+
Replace use of deprecated `asExternalTypedData` with `asTypedList`.
6+
37
## 0.1.3-dev.2
48

59
Incorporate struct API changes, drop type argument of structs.

lib/src/utf16.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ class Utf16 extends Struct {
2424
static Pointer<Utf16> toUtf16(String s) {
2525
final units = s.codeUnits;
2626
final Pointer<Uint16> result = allocate<Uint16>(count: units.length + 1);
27-
final Uint16List nativeString =
28-
result.asExternalTypedData(count: units.length + 1);
27+
final Uint16List nativeString = result.asTypedList(units.length + 1);
2928
nativeString.setAll(0, units);
3029
nativeString[units.length] = 0;
3130
return result.cast();

lib/src/utf8.dart

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ final int _maxSize = sizeOf<IntPtr>() == 8 ? _kMaxSmi64 : _kMaxSmi32;
1919
/// native function signatures.
2020
//
2121
// TODO(https://github.com/dart-lang/ffi/issues/4): No need to use
22-
// 'asExternalTypedData' when Pointer operations are performant.
22+
// 'asTypedList' when Pointer operations are performant.
2323
class Utf8 extends Struct {
2424
/// Returns the length of a null-terminated string -- the number of (one-byte)
2525
/// characters before the first null byte.
2626
static int strlen(Pointer<Utf8> string) {
2727
final Pointer<Uint8> array = string.cast<Uint8>();
28-
final Uint8List nativeString = array.asExternalTypedData(count: _maxSize);
28+
final Uint8List nativeString = array.asTypedList(_maxSize);
2929
return nativeString.indexWhere((char) => char == 0);
3030
}
3131

@@ -41,9 +41,7 @@ class Utf8 extends Struct {
4141
static String fromUtf8(Pointer<Utf8> string) {
4242
final int length = strlen(string);
4343
return utf8.decode(Uint8List.view(
44-
string.cast<Uint8>().asExternalTypedData(count: length).buffer,
45-
0,
46-
length));
44+
string.cast<Uint8>().asTypedList(length).buffer, 0, length));
4745
}
4846

4947
/// Convert a [String] to a Utf8-encoded null-terminated C string.
@@ -56,8 +54,7 @@ class Utf8 extends Struct {
5654
static Pointer<Utf8> toUtf8(String string) {
5755
final units = utf8.encode(string);
5856
final Pointer<Uint8> result = allocate<Uint8>(count: units.length + 1);
59-
final Uint8List nativeString =
60-
result.asExternalTypedData(count: units.length + 1);
57+
final Uint8List nativeString = result.asTypedList(units.length + 1);
6158
nativeString.setAll(0, units);
6259
nativeString[units.length] = 0;
6360
return result.cast();

test/utf16_test.dart

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ main() {
1212
test("toUtf16 ASCII", () {
1313
final String start = "Hello World!\n";
1414
final Pointer<Uint16> converted = Utf16.toUtf16(start).cast();
15-
final Uint16List end =
16-
converted.asExternalTypedData(count: start.codeUnits.length + 1);
15+
final Uint16List end = converted.asTypedList(start.codeUnits.length + 1);
1716
final matcher = equals(start.codeUnits.toList()..add(0));
1817
expect(end, matcher);
1918
free(converted);
@@ -23,8 +22,7 @@ main() {
2322
final String start = "😎";
2423
final Pointer<Utf16> converted = Utf16.toUtf16(start).cast();
2524
final int length = start.codeUnits.length;
26-
final Uint16List end =
27-
converted.cast<Uint16>().asExternalTypedData(count: length + 1);
25+
final Uint16List end = converted.cast<Uint16>().asTypedList(length + 1);
2826
final matcher = equals(start.codeUnits.toList()..add(0));
2927
expect(end, matcher);
3028
free(converted);

test/utf8_test.dart

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import 'package:ffi/ffi.dart';
1010

1111
Pointer<Uint8> _bytesFromList(List<int> ints) {
1212
final Pointer<Uint8> ptr = allocate(count: ints.length);
13-
final Uint8List list = ptr.asExternalTypedData(count: ints.length);
13+
final Uint8List list = ptr.asTypedList(ints.length);
1414
list.setAll(0, ints);
1515
return ptr;
1616
}
@@ -19,8 +19,7 @@ main() {
1919
test("toUtf8 ASCII", () {
2020
final String start = "Hello World!\n";
2121
final Pointer<Uint8> converted = Utf8.toUtf8(start).cast();
22-
final Uint8List end =
23-
converted.asExternalTypedData(count: start.length + 1);
22+
final Uint8List end = converted.asTypedList(start.length + 1);
2423
final matcher =
2524
equals([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33, 10, 0]);
2625
expect(end, matcher);
@@ -38,8 +37,7 @@ main() {
3837
final String start = "😎👿💬";
3938
final Pointer<Utf8> converted = Utf8.toUtf8(start).cast();
4039
final int length = Utf8.strlen(converted);
41-
final Uint8List end =
42-
converted.cast<Uint8>().asExternalTypedData(count: length + 1);
40+
final Uint8List end = converted.cast<Uint8>().asTypedList(length + 1);
4341
final matcher =
4442
equals([240, 159, 152, 142, 240, 159, 145, 191, 240, 159, 146, 172, 0]);
4543
expect(end, matcher);
@@ -57,8 +55,7 @@ main() {
5755
final String start = String.fromCharCodes([0xD800, 0x1000]);
5856
final Pointer<Utf8> converted = Utf8.toUtf8(start).cast();
5957
final int length = Utf8.strlen(converted);
60-
final Uint8List end =
61-
converted.cast<Uint8>().asExternalTypedData(count: length + 1);
58+
final Uint8List end = converted.cast<Uint8>().asTypedList(length + 1);
6259
expect(end, equals([237, 160, 128, 225, 128, 128, 0]));
6360
free(converted);
6461
});

0 commit comments

Comments
 (0)