convert method
Converts input to the byte encoding in this code page.
If invalidCharacter
is supplied, it must be a byte value
(in the range 0..255).
If input
contains characters that are not available
in this code page, they are replaced by the invalidCharacter
byte,
and then invalidCharacter
must have been supplied.
Implementation
@override
Uint8List convert(String input, {int? invalidCharacter}) {
if (invalidCharacter != null) {
RangeError.checkValueInInterval(
invalidCharacter, 0, 255, 'invalidCharacter');
}
var count = input.length;
var result = Uint8List(count);
var j = 0;
for (var i = 0; i < count; i++) {
var char = input.codeUnitAt(i);
var byte = _encoding[char];
nullCheck:
if (byte == null) {
// Check for surrogate.
var offset = i;
if (char & 0xFC00 == 0xD800 && i + 1 < count) {
var next = input.codeUnitAt(i + 1);
if ((next & 0xFC00) == 0xDC00) {
i = i + 1;
char = 0x10000 + ((char & 0x3ff) << 10) + (next & 0x3ff);
byte = _encoding[char];
if (byte != null) break nullCheck;
}
}
byte = invalidCharacter ??
(throw FormatException(
'Not a character in this code page', input, offset));
}
result[j++] = byte;
}
return Uint8List.sublistView(result, 0, j);
}