convert method
override
Converts string
to its UTF-8 code units (a list of
unsigned 8-bit integers).
If start
and end
are provided, only the substring
string.substring(start, end)
is converted.
Any unpaired surrogate character (U+D800
-U+DFFF
) in the input string
is encoded as a Unicode Replacement character U+FFFD
(�).
Implementation
Uint8List convert(String string, [int start = 0, int? end]) {
var stringLength = string.length;
end = RangeError.checkValidRange(start, end, stringLength);
var length = end - start;
if (length == 0) return Uint8List(0);
// Create a new encoder with a length that is guaranteed to be big enough.
// A single code unit uses at most 3 bytes, a surrogate pair at most 4.
var encoder = _Utf8Encoder.withBufferSize(length * 3);
var endPosition = encoder._fillBuffer(string, start, end);
assert(endPosition >= end - 1);
if (endPosition != end) {
// Encoding skipped the last code unit.
// That can only happen if the last code unit is a leadsurrogate.
// Force encoding of the lead surrogate by itself.
var lastCodeUnit = string.codeUnitAt(end - 1);
assert(_isLeadSurrogate(lastCodeUnit));
// Write a replacement character to represent the unpaired surrogate.
encoder._writeReplacementCharacter();
}
return encoder._buffer.sublist(0, encoder._bufferIndex);
}