withUtf8<T> function

T withUtf8<T>(
  1. String s,
  2. T fn(
    1. Pointer<Utf8>
    )
)

Run fn with a temporary native UTF-8 copy of s; always frees it.

Implementation

T withUtf8<T>(String s, T Function(Pointer<Utf8>) fn) {
  final ptr = s.toNativeUtf8();
  try {
    return fn(ptr);
  } finally {
    calloc.free(ptr);
  }
}