findMatches function

List<int> findMatches(
  1. String text,
  2. String needle
)

Start offsets of every case-insensitive, non-overlapping match of needle (already lowercase) in text.

Implementation

List<int> findMatches(String text, String needle) {
  if (needle.isEmpty) return const [];
  final haystack = text.toLowerCase();
  final starts = <int>[];
  var from = 0;
  while (true) {
    final index = haystack.indexOf(needle, from);
    if (index < 0) return starts;
    starts.add(index);
    from = index + needle.length;
  }
}