match method

  1. @override
List<List<SequenceMatch>> match(
  1. String password
)
override

Should return the matches for the password.

A synchronous matcher should return a list (usually of length 1) of lists of matches. An asynchronous matcher can return a list of futures that completes with a list of matches.

Implementation

@override
List<List<SequenceMatch>> match(String password) {
  // Identifies sequences by looking for repeated differences in unicode
  // codepoint. This allows skipping, such as 9753, and also matches some
  // extended unicode sequences such as Greek and Cyrillic alphabets.
  //
  // For example, consider the input 'abcdb975zy'
  // password: a   b   c   d   b    9   7   5   z   y
  // index:    0   1   2   3   4    5   6   7   8   9
  // delta:        1   1   1  -2  -41  -2  -2  69   1
  //
  // Expected result:
  // [(start, end, delta), ...] = [(0, 4, 1), (5, 8, -2), (8, 10, 1)]
  final List<SequenceMatch> result = <SequenceMatch>[];
  if (password.length <= 1) return <List<SequenceMatch>>[result];
  int start = 0;
  int? lastDelta;
  for (int end = 1; end < password.length; end++) {
    final int delta = password.codeUnitAt(end) - password.codeUnitAt(end - 1);
    lastDelta ??= delta;
    if (delta != lastDelta) {
      _update(
        result: result,
        password: password,
        start: start,
        end: end,
        delta: lastDelta,
      );
      start = end - 1;
      lastDelta = delta;
    }
  }
  if (lastDelta != null) {
    _update(
      result: result,
      password: password,
      start: start,
      end: password.length,
      delta: lastDelta,
    );
  }
  return <List<SequenceMatch>>[result];
}