linkTarget static method

Uint8List linkTarget(
  1. String uri
)

uri as the target of an OSC 8 hyperlink: printable ASCII, the other bytes of its UTF-8 form (non-ASCII text, spaces, controls) percent-encoded, which keeps the target and cannot end the sequence early.

final target = LineEncoder.linkTarget('https://x.dev/é');
print(String.fromCharCodes(target)); // https://x.dev/%C3%A9

Implementation

static Uint8List linkTarget(String uri) {
  const hex = '0123456789ABCDEF';
  final b = BytesBuilder(copy: false);
  for (final c in utf8.encode(uri)) {
    if (c > 0x20 && c < 0x7F) {
      b.addByte(c);
    } else {
      b
        ..addByte(0x25) // %
        ..addByte(hex.codeUnitAt(c >> 4))
        ..addByte(hex.codeUnitAt(c & 0xF));
    }
  }
  return b.takeBytes();
}