readFromBufferAsString function
Read a specified piece of the buffer and convert it to a string, interpreting the bytes as UTF-8.
If byteCnt
is not specified, it gets auto-calculated
by looking for the null terminator at position offset
or later in the buffer
.
allowMalformed
is passed directly to utf8.decode
,
allowing or not an invalid UTF-8 sequence to occur in the buffer.
Implementation
String readFromBufferAsString(Pointer<Uint8> buffer, int offset,
[int? byteCnt, bool allowMalformed = true]) {
if (byteCnt == null) {
// auto-detect the end of string
int c = 0;
while (buffer[offset + c++] != 0) {}
byteCnt = c;
}
List<int> codeUnits = Pointer<Uint8>.fromAddress(buffer.address + offset)
.asTypedList(byteCnt)
.takeWhile((value) => value != 0)
.toList();
return utf8.decode(codeUnits, allowMalformed: allowMalformed);
}