Skip to content

Add helper methods for NSData #1104

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pkgs/objective_c/lib/objective_c.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// BSD-style license that can be found in the LICENSE file.

export 'src/core.dart';
export 'src/ns_data.dart';
export 'src/ns_string.dart';
export 'src/c_bindings_generated.dart'
show
Expand Down
47 changes: 47 additions & 0 deletions pkgs/objective_c/lib/src/ns_data.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'dart:ffi';
import 'dart:typed_data';

import 'package:ffi/ffi.dart';

import 'objective_c_bindings_generated.dart';

extension NSDataExtensions on NSData {
/// Return the value of [bytes] at the given index.
///
/// The returned value will be in the range 0 to 255.
int operator [](int index) {
IndexError.check(index, length, indexable: this);
return bytes.cast<Uint8>()[index];
}

/// Return a list containing the contents of the [NSData].
Uint8List toList() {
if (bytes.address == 0 || length == 0) {
return Uint8List(0);
} else {
return Uint8List.fromList(bytes.cast<Uint8>().asTypedList(length));
}
}
}

extension NSDataListExtension on List<int> {
/// Return a [NSData] containing the contents of the [List] interpreted as
/// bytes.
///
/// The elements of the [List] should be integers in the range 0 to 255. Any
/// integer, which is not in that range, is converted to a byte as if by
/// `value.toUnsigned(8)`.
NSData toNSData() {
if (length == 0) {
return NSData.new1();
}
final buffer = malloc<Uint8>(length);
buffer.asTypedList(length).setAll(0, this);

return NSData.dataWithBytesNoCopy_length_(buffer.cast(), length);
}
}
88 changes: 88 additions & 0 deletions pkgs/objective_c/test/nsdata_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// Objective C support is only available on mac.
@TestOn('mac-os')

import 'dart:ffi';

import 'package:ffi/ffi.dart';
import 'package:objective_c/objective_c.dart';
import 'package:test/test.dart';

void main() {
group('NSData', () {
group('toNSData', () {
test('empty', () {
final data = <int>[].toNSData();
expect(data.length, 0);
data.release(); // Make sure that dealloc succeeds.
});

test('non empty', () {
final data = [1, 2, 3].toNSData();
expect(data.length, 3);
expect(data.bytes.cast<Uint8>()[0], 1);
expect(data.bytes.cast<Uint8>()[1], 2);
expect(data.bytes.cast<Uint8>()[2], 3);
data.release(); // Make sure that dealloc succeeds.
});

test('non-byte', () {
final data = [257].toNSData();
expect(data.length, 1);
expect(data.bytes.cast<Uint8>().value, 1);
data.release(); // Make sure that dealloc succeeds.
});
});

group('toList', () {
test('empty', () {
final data = NSData.data();
expect(data.toList(), isEmpty);
});

test('non empty', () {
using((arena) {
final bytes = arena<Uint8>(3);
bytes[0] = 1;
bytes[1] = 2;
bytes[2] = 3;

final data = NSData.dataWithBytes_length_(bytes.cast(), 3);
expect(data.toList(), [1, 2, 3]);
});
});
});

group('operator[]', () {
test('in bounds', () {
using((arena) {
final bytes = arena<Uint8>(3);
bytes[0] = 1;
bytes[1] = 2;
bytes[2] = 3;

final data = NSData.dataWithBytes_length_(bytes.cast(), 3);
expect(data[0], 1);
expect(data[1], 2);
expect(data[2], 3);
});
});

test('out of bounds', () {
using((arena) {
final bytes = arena<Uint8>(3);
bytes[0] = 1;
bytes[1] = 2;
bytes[2] = 3;

final data = NSData.dataWithBytes_length_(bytes.cast(), 3);
expect(() => data[3], throwsRangeError);
expect(() => data[-1], throwsRangeError);
});
});
});
});
}
Loading