toNativeUtf16 method

Pointer<Utf16> toNativeUtf16(
  1. {Allocator allocator = malloc}
)

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

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

Returns an allocator-allocated pointer to the result.

Implementation

Pointer<Utf16> toNativeUtf16({Allocator allocator = malloc}) {
  final units = codeUnits;
  final Pointer<Uint16> result = allocator<Uint16>(units.length + 1);
  final Uint16List nativeString = result.asTypedList(units.length + 1);
  nativeString.setRange(0, units.length, units);
  nativeString[units.length] = 0;
  return result.cast();
}