toNativeUtf8 method

Pointer<Utf8> toNativeUtf8({
  1. Allocator allocator = malloc,
})

Creates a zero-terminated Utf8 code-unit array from this String.

If this String contains NUL characters, converting it back to a string using Utf8Pointer.toDartString will truncate the result if a length is not passed.

Unpaired surrogate code points in this String will be encoded as replacement characters (U+FFFD, encoded as the bytes 0xEF 0xBF 0xBD) in the UTF-8 encoded result. See Utf8Encoder for details on encoding.

Returns an allocator-allocated pointer to the result.

Implementation

Pointer<Utf8> toNativeUtf8({Allocator allocator = malloc}) {
  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();
}