Skip to content

Commit

Permalink
Remove references to asExternalTypedData (flutter#13)
Browse files Browse the repository at this point in the history
* asTypedList

* changelog
  • Loading branch information
sjindel-google authored Oct 15, 2019
1 parent 630570e commit 448ce9d
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 20 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 0.1.3-dev.3

Replace use of deprecated `asExternalTypedData` with `asTypedList`.

## 0.1.3-dev.2

Incorporate struct API changes, drop type argument of structs.
Expand Down
3 changes: 1 addition & 2 deletions lib/src/utf16.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ class Utf16 extends Struct {
static Pointer<Utf16> toUtf16(String s) {
final units = s.codeUnits;
final Pointer<Uint16> result = allocate<Uint16>(count: units.length + 1);
final Uint16List nativeString =
result.asExternalTypedData(count: units.length + 1);
final Uint16List nativeString = result.asTypedList(units.length + 1);
nativeString.setAll(0, units);
nativeString[units.length] = 0;
return result.cast();
Expand Down
11 changes: 4 additions & 7 deletions lib/src/utf8.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ final int _maxSize = sizeOf<IntPtr>() == 8 ? _kMaxSmi64 : _kMaxSmi32;
/// native function signatures.
//
// TODO(https://github.com/dart-lang/ffi/issues/4): No need to use
// 'asExternalTypedData' when Pointer operations are performant.
// 'asTypedList' when Pointer operations are performant.
class Utf8 extends Struct {
/// Returns the length of a null-terminated string -- the number of (one-byte)
/// characters before the first null byte.
static int strlen(Pointer<Utf8> string) {
final Pointer<Uint8> array = string.cast<Uint8>();
final Uint8List nativeString = array.asExternalTypedData(count: _maxSize);
final Uint8List nativeString = array.asTypedList(_maxSize);
return nativeString.indexWhere((char) => char == 0);
}

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

/// Convert a [String] to a Utf8-encoded null-terminated C string.
Expand All @@ -56,8 +54,7 @@ class Utf8 extends Struct {
static Pointer<Utf8> toUtf8(String string) {
final units = utf8.encode(string);
final Pointer<Uint8> result = allocate<Uint8>(count: units.length + 1);
final Uint8List nativeString =
result.asExternalTypedData(count: units.length + 1);
final Uint8List nativeString = result.asTypedList(units.length + 1);
nativeString.setAll(0, units);
nativeString[units.length] = 0;
return result.cast();
Expand Down
6 changes: 2 additions & 4 deletions test/utf16_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ main() {
test("toUtf16 ASCII", () {
final String start = "Hello World!\n";
final Pointer<Uint16> converted = Utf16.toUtf16(start).cast();
final Uint16List end =
converted.asExternalTypedData(count: start.codeUnits.length + 1);
final Uint16List end = converted.asTypedList(start.codeUnits.length + 1);
final matcher = equals(start.codeUnits.toList()..add(0));
expect(end, matcher);
free(converted);
Expand All @@ -23,8 +22,7 @@ main() {
final String start = "😎";
final Pointer<Utf16> converted = Utf16.toUtf16(start).cast();
final int length = start.codeUnits.length;
final Uint16List end =
converted.cast<Uint16>().asExternalTypedData(count: length + 1);
final Uint16List end = converted.cast<Uint16>().asTypedList(length + 1);
final matcher = equals(start.codeUnits.toList()..add(0));
expect(end, matcher);
free(converted);
Expand Down
11 changes: 4 additions & 7 deletions test/utf8_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import 'package:ffi/ffi.dart';

Pointer<Uint8> _bytesFromList(List<int> ints) {
final Pointer<Uint8> ptr = allocate(count: ints.length);
final Uint8List list = ptr.asExternalTypedData(count: ints.length);
final Uint8List list = ptr.asTypedList(ints.length);
list.setAll(0, ints);
return ptr;
}
Expand All @@ -19,8 +19,7 @@ main() {
test("toUtf8 ASCII", () {
final String start = "Hello World!\n";
final Pointer<Uint8> converted = Utf8.toUtf8(start).cast();
final Uint8List end =
converted.asExternalTypedData(count: start.length + 1);
final Uint8List end = converted.asTypedList(start.length + 1);
final matcher =
equals([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33, 10, 0]);
expect(end, matcher);
Expand All @@ -38,8 +37,7 @@ main() {
final String start = "😎👿💬";
final Pointer<Utf8> converted = Utf8.toUtf8(start).cast();
final int length = Utf8.strlen(converted);
final Uint8List end =
converted.cast<Uint8>().asExternalTypedData(count: length + 1);
final Uint8List end = converted.cast<Uint8>().asTypedList(length + 1);
final matcher =
equals([240, 159, 152, 142, 240, 159, 145, 191, 240, 159, 146, 172, 0]);
expect(end, matcher);
Expand All @@ -57,8 +55,7 @@ main() {
final String start = String.fromCharCodes([0xD800, 0x1000]);
final Pointer<Utf8> converted = Utf8.toUtf8(start).cast();
final int length = Utf8.strlen(converted);
final Uint8List end =
converted.cast<Uint8>().asExternalTypedData(count: length + 1);
final Uint8List end = converted.cast<Uint8>().asTypedList(length + 1);
expect(end, equals([237, 160, 128, 225, 128, 128, 0]));
free(converted);
});
Expand Down

0 comments on commit 448ce9d

Please sign in to comment.