readCString method
Decodes a NUL-terminated C string at ptr (chunked scan — avoids a JS
call per byte). Preserves a leading BOM, tolerates malformed UTF-8.
Implementation
String readCString(int ptr) {
if (ptr == 0) return '';
const chunk = 64;
final collected = BytesBuilder(copy: false);
var base = ptr;
while (true) {
final bytes = readBytes(base, chunk);
// Past the end of linear memory, `slice` returns fewer bytes than asked
// for — and nothing at all at/after the end. Treating that as "no NUL
// yet" spun forever; an unterminated string is a bug in the producer,
// so surface it instead of burning CPU.
if (bytes.isEmpty) {
throw StateError(
'readCString: unterminated C string at $ptr — scanned to the end of '
'linear memory (${base - ptr} bytes) without a NUL terminator.',
);
}
final nul = bytes.indexOf(0);
if (nul >= 0) {
collected.add(Uint8List.sublistView(bytes, 0, nul));
break;
}
collected.add(bytes);
base += bytes.length;
}
return decodeUtf8NoBomStrip(collected.takeBytes());
}