locate method

(int, int)? locate(
  1. String needle, {
  2. int near = -1,
})

Finds needle on the page, first exactly, then Persian/Arabic-normalised.

Implementation

(int, int)? locate(String needle, {int near = -1}) {
  final String trimmed = needle.trim();
  if (trimmed.isEmpty || text.isEmpty) return null;
  final List<int> exact = UDocText.findAll(text, trimmed);
  if (exact.isNotEmpty) {
    final int pick = _closest(exact, near);
    return (pick, pick + trimmed.length);
  }
  final UDocNormalizedText normalized = UDocNormalizedText.build(text);
  final String target = UDocText.normalize(trimmed);
  if (target.isEmpty) return null;
  final List<int> hits = UDocText.findAll(normalized.text, target);
  if (hits.isEmpty) return null;
  final int pick = _closest(hits, near < 0 ? -1 : near);
  return (normalized.sourceAt(pick), normalized.sourceAt(pick + target.length - 1) + 1);
}