2024年2月27日 星期二

用 dart 語法, 針對 Uint8List 資料流(stream): 內容是 vt100 字串, 僅把 ESC[ ... m 移除並轉換成 utf8 字串

Uint8List remain = Uint8List(0);
String utf8Log = "";
void stream2utf8(Uint8List data) {
  final findLF = data.lastIndexWhere((e) => e == 0xa);// find position of the last \n
  final endLF = findLF + 1; // to include LF
  final len = remain.length + (findLF < 0 ? data.length : endLF);
  final resemble = Uint8List(len);
  if (remain.isNotEmpty) resemble.setAll(0, remain);// copy remain to front of resemble
  resemble.setRange(remain.length, len, data); // append sublist of data to resemble
  if (findLF < 0) { // keep in memory
    remain = resemble;
  } else { // line feed found, time to flush out
    remain = Uint8List.sublistView(data, endLF);
    Uint8List afterESC = Uint8List.sublistView(resemble);
    while (afterESC.isNotEmpty) { // until empty
      final findESC = afterESC.indexOf(0x1b); // find ESC position
      utf8Log += utf8.decode(Uint8List.sublistView(afterESC, 0,
        findESC < 0 ? afterESC.length : findESC
      ));
      if (findESC < 0) break;
      var lenESC = 1; // sequence ^[ ... m seek
      if (afterESC[findESC + lenESC] == 0x5b) { // found sequence start '['
        do {
          lenESC ++;  // one byte advance
          if (afterESC[findESC + lenESC] == 0x6d) { // found end of character 'm'
            lenESC ++;// one byte advance
            break;
          }
        } while (findESC + lenESC < afterESC.length);
        if (findESC + lenESC >= afterESC.length) { // todo: other ^[ sequence not yet implement!
          afterESC[findESC] = 0x5E; // change ESC from 0x1b to '^' to prevent infinit loop
          continue;// backoff to show this sequence
        }
      } else { // todo: other ^ sequence not yet implement!
        afterESC[findESC] = 0x5E; // change ESC from 0x1b to '^' to prevent infinit loop
        continue;// backoff to show this sequence
      }
      afterESC = Uint8List.sublistView(afterESC, findESC + lenESC);// ok,  go ahead
    }
    setState((){}); // update screen if necessary
}

沒有留言:

張貼留言

使用 pcie 轉接器連接 nvme SSD

之前 AM4 主機板使用 pcie ssd, 但主機板故障了沒辦法上網, 只好翻出以前買的 FM2 舊主機板, 想辦法讓老主機復活, 但舊主機板沒有 nvme 的界面, 因此上網買了 pcie 轉接器用來連接 nvme ssd, 遺憾的是 grub2 bootloader 無法識...