Skip to content

Commit e02da07

Browse files
authored
[ffi] Tweak the docs (dart-archive/ffi#63)
Be consistent about zero- vs. null-terminated (the latter was more commonly used, but using the former was suggested in dart-archive/ffi#61). Furthermore, align UTF-8/16 spelling and fix a couple of minor typos and formatting issues.
1 parent d7d5dc7 commit e02da07

File tree

4 files changed

+16
-16
lines changed

4 files changed

+16
-16
lines changed

pkgs/ffi/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[![Build Status](https://travis-ci.org/dart-lang/ffi.svg?branch=master)](https://travis-ci.org/dart-lang/ffi)
22

33
Utilities for working with Foreign Function Interface (FFI) code, incl.
4-
converting between Dart strings and C strings encoded with utf8 and utf16.
4+
converting between Dart strings and C strings encoded with UTF-8 and UTF-16.
55

66
For additional details about Dart FFI (`dart:ffi`), see
77
https://dart.dev/guides/libraries/c-interop.

pkgs/ffi/example/main.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ void main() {
99
print(pointer.value);
1010
free(pointer);
1111

12-
// Use the Utf8 helper to encode null-terminated Utf8 strings in native memory.
12+
// Use the Utf8 helper to encode zero-terminated UTF-8 strings in native memory.
1313
final String myString = '😎👿💬';
1414
final Pointer<Utf8> charPointer = Utf8.toUtf8(myString);
1515
print('First byte is: ${charPointer.cast<Uint8>().value}');

pkgs/ffi/lib/src/utf16.dart

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,21 @@ import 'dart:typed_data';
88

99
import 'package:ffi/ffi.dart';
1010

11-
/// [Utf16] implements conversion between Dart strings and null-terminated
11+
/// [Utf16] implements conversion between Dart strings and zero-terminated
1212
/// UTF-16 encoded "char*" strings in C.
1313
///
14-
/// [Utf16] is respresented as a struct so that `Pointer<Utf16>` can be used in
14+
/// [Utf16] is represented as a struct so that `Pointer<Utf16>` can be used in
1515
/// native function signatures.
1616
class Utf16 extends Struct {
17-
/// Convert a [String] to a Utf16-encoded null-terminated C string.
17+
/// Convert a [String] to a UTF-16 encoded zero-terminated C string.
1818
///
19-
/// If 'string' contains NULL bytes, the converted string will be truncated
19+
/// If [string] contains NULL characters, the converted string will be truncated
2020
/// prematurely. Unpaired surrogate code points in [string] will be preserved
21-
/// in the UTF-8 encoded result. See [Utf8Encoder] for details on encoding.
21+
/// in the UTF-16 encoded result. See [Utf16Encoder] for details on encoding.
2222
///
2323
/// Returns a malloc-allocated pointer to the result.
24-
static Pointer<Utf16> toUtf16(String s) {
25-
final units = s.codeUnits;
24+
static Pointer<Utf16> toUtf16(String string) {
25+
final units = string.codeUnits;
2626
final Pointer<Uint16> result = allocate<Uint16>(count: units.length + 1);
2727
final Uint16List nativeString = result.asTypedList(units.length + 1);
2828
nativeString.setAll(0, units);

pkgs/ffi/lib/src/utf8.dart

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@ const int _kMaxSmi64 = (1 << 62) - 1;
1212
const int _kMaxSmi32 = (1 << 30) - 1;
1313
final int _maxSize = sizeOf<IntPtr>() == 8 ? _kMaxSmi64 : _kMaxSmi32;
1414

15-
/// [Utf8] implements conversion between Dart strings and null-terminated
16-
/// Utf8-encoded "char*" strings in C.
15+
/// [Utf8] implements conversion between Dart strings and zero-terminated
16+
/// UTF-8 encoded "char*" strings in C.
1717
///
18-
/// [Utf8] is respresented as a struct so that `Pointer<Utf8>` can be used in
18+
/// [Utf8] is represented as a struct so that `Pointer<Utf8>` can be used in
1919
/// native function signatures.
2020
//
2121
// TODO(https://github.com/dart-lang/ffi/issues/4): No need to use
2222
// 'asTypedList' when Pointer operations are performant.
2323
class Utf8 extends Struct {
24-
/// Returns the length of a null-terminated string -- the number of (one-byte)
25-
/// characters before the first null byte.
24+
/// Returns the length of a zero-terminated string &mdash; the number of
25+
/// bytes before the first zero byte.
2626
static int strlen(Pointer<Utf8> string) {
2727
final Pointer<Uint8> array = string.cast<Uint8>();
2828
final Uint8List nativeString = array.asTypedList(_maxSize);
@@ -42,9 +42,9 @@ class Utf8 extends Struct {
4242
string.cast<Uint8>().asTypedList(length).buffer, 0, length));
4343
}
4444

45-
/// Convert a [String] to a Utf8-encoded null-terminated C string.
45+
/// Convert a [String] to a UTF-8 encoded zero-terminated C string.
4646
///
47-
/// If 'string' contains NULL bytes, the converted string will be truncated
47+
/// If [string] contains NULL characters, the converted string will be truncated
4848
/// prematurely. Unpaired surrogate code points in [string] will be encoded
4949
/// as replacement characters (U+FFFD, encoded as the bytes 0xEF 0xBF 0xBD)
5050
/// in the UTF-8 encoded result. See [Utf8Encoder] for details on encoding.

0 commit comments

Comments
 (0)