getBiDiSegmentsInRange method

List<BiDiSegment> getBiDiSegmentsInRange(
  1. int start,
  2. int end
)

Splits [start, end) into runs of one strong direction. Neutral characters attach to the run before them (leading neutrals join the first run). Costs O(log n + range length).

Implementation

List<BiDiSegment> getBiDiSegmentsInRange(int start, int end) {
  final from = start.clamp(0, length);
  final to = end.clamp(from, length);
  if (from == to) return const [];

  final text = slice(from, to);
  final segments = <BiDiSegment>[];
  TextDirection? current;
  var runStart = 0;

  for (var i = 0; i < text.length; i++) {
    final direction = _U.strongDirection(text.codeUnitAt(i));
    if (direction == null) continue;
    if (current == null) {
      current = direction;
    } else if (direction != current) {
      segments.add(
        BiDiSegment(
          start: from + runStart,
          end: from + i,
          direction: current,
        ),
      );
      runStart = i;
      current = direction;
    }
  }
  segments.add(
    BiDiSegment(
      start: from + runStart,
      end: to,
      direction: current ?? TextDirection.ltr,
    ),
  );
  return segments;
}