toUtf8 static method

Implementation

static Pointer<Utf8NullTerminated> toUtf8(String s) {
  final bytes = Utf8Encoder().convert(s);
  final ptr = calloc<Utf8NullTerminated>(bytes.length + 1);
  // [Performance Fix] Use bulk copy (memcpy) instead of slow loop
  // Convert Pointer<Utf8NullTerminated> to Pointer<Uint8> then to list
  final buffer = ptr.cast<Uint8>().asTypedList(bytes.length + 1);
  buffer.setAll(0, bytes);
  // Add the terminator '\0'
  ptr.elementAt(bytes.length).ref.char = 0;
  return ptr;
}