isolateOppositeRuns function

String isolateOppositeRuns(
  1. String line, {
  2. bool? rtl,
})

Wraps the runs that read the other way in Unicode isolates, so song titles, numbers, times and percentages keep their order inside a sentence.

The paragraph direction comes from its first strong letter, or from rtl when you pass it. Use it on one paragraph at a time.

Implementation

String isolateOppositeRuns(String line, {bool? rtl}) {
  if (line.isEmpty) return line;
  final isR = rtl ?? isRtl(line);
  if (isR) {
    if (!_ltrRun.hasMatch(line)) return line;
    return line.replaceAllMapped(_ltrRun, (m) => '$_lri${m[0]}$_pdi');
  }
  if (!_rtlRun.hasMatch(line)) return line;
  return line.replaceAllMapped(_rtlRun, (m) => '$_rli${m[0]}$_pdi');
}