mirror of
https://github.com/dart-lang/sdk
synced 2024-09-16 00:29:48 +00:00
0b8091e0cd
Addressing follow up comments on https://dart-review.googlesource.com/c/sdk/+/179180 before merging it into `package:ffi` in https://github.com/dart-lang/ffi/pull/103. Also adds the tests from the PR. Change-Id: I9f9e7bdef00c758f0496e73c36e23116c65d95d8 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/201186 Commit-Queue: Daco Harkes <dacoharkes@google.com> Reviewed-by: Lasse R.H. Nielsen <lrn@google.com>
53 lines
1.7 KiB
Dart
53 lines
1.7 KiB
Dart
// Copyright (c) 2021, 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:convert';
|
|
import 'dart:ffi';
|
|
import 'dart:typed_data';
|
|
|
|
import 'package:ffi/ffi.dart';
|
|
|
|
extension Utf8InArena on String {
|
|
/// Convert a [String] to a Utf8-encoded null-terminated C string.
|
|
///
|
|
/// If 'string' contains NULL bytes, the converted string will be truncated
|
|
/// prematurely. See [Utf8Encoder] for details on encoding.
|
|
///
|
|
/// Returns a [allocator]-allocated pointer to the result.
|
|
Pointer<Utf8> toUtf8(Allocator allocator) {
|
|
final units = utf8.encode(this);
|
|
final Pointer<Uint8> result = allocator<Uint8>(units.length + 1);
|
|
final Uint8List nativeString = result.asTypedList(units.length + 1);
|
|
nativeString.setAll(0, units);
|
|
nativeString[units.length] = 0;
|
|
return result.cast();
|
|
}
|
|
}
|
|
|
|
extension Utf8Helpers on Pointer<Utf8> {
|
|
/// The length of a null-terminated string — the number of (one-byte)
|
|
/// characters before the first null byte.
|
|
int get strlen {
|
|
final Pointer<Uint8> array = this.cast<Uint8>();
|
|
int length = 0;
|
|
while (array[length] != 0) {
|
|
length++;
|
|
}
|
|
return length;
|
|
}
|
|
|
|
/// Creates a [String] containing the characters UTF-8 encoded in [this].
|
|
///
|
|
/// [this] must be a zero-terminated byte sequence of valid UTF-8
|
|
/// encodings of Unicode code points. See [Utf8Decoder] for details on
|
|
/// decoding.
|
|
///
|
|
/// Returns a Dart string containing the decoded code points.
|
|
String contents() {
|
|
final int length = strlen;
|
|
return utf8.decode(Uint8List.view(
|
|
this.cast<Uint8>().asTypedList(length).buffer, 0, length));
|
|
}
|
|
}
|