метод convert
override
Преобразует string в его кодовые единицы UTF-8 (список беззнаковых 8-битных целых чисел).
Если указаны start и end, преобразуется только подстрока string.substring(start, end).
Любой непарный суррогатный символ (U+D800-U+DFFF) во входной строке кодируется символом замены Unicode U+FFFD (�).
Реализация
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);
}
© 2012 the Dart project authors
Licensed under the BSD 3-Clause "New" or "Revised" License.
https://api.dart.dev/stable/2.19.6/dart-convert/Utf8Encoder/convert.html