truncatedMismatchSample static method

  1. @visibleForTesting
String truncatedMismatchSample(
  1. String sample
)

Shortens a mismatch sample to at most maxSampleLength code points: first part + "…" + last part (16 + 1 + 15). A cut never lands inside a grapheme cluster, so it may come out shorter. iOS and Android apply the same rule; the shared sample table in the tests pins all three.

Implementation

@visibleForTesting
static String truncatedMismatchSample(String sample) {
  final runes = sample.runes.toList();
  if (runes.length <= maxSampleLength) return sample;
  final boundaries = <int>{0};
  var offset = 0;
  for (final cluster in sample.characters) {
    offset += cluster.runes.length;
    boundaries.add(offset);
  }
  var head = maxSampleLength ~/ 2;
  while (!boundaries.contains(head)) {
    head--;
  }
  var tail = runes.length - (maxSampleLength - 1) ~/ 2;
  while (!boundaries.contains(tail)) {
    tail++;
  }
  return '${String.fromCharCodes(runes.sublist(0, head))}\u2026'
      '${String.fromCharCodes(runes.sublist(tail))}';
}