holdsTyped function

bool holdsTyped(
  1. String before,
  2. String after,
  3. String typed
)

Whether after is before with typed inserted at one place, once iOS smart quotes and dashes are undone.

Implementation

bool holdsTyped(String before, String after, String typed) {
  String plain(String v) => v
      .replaceAll(RegExp('[\u2018\u2019]'), "'")
      .replaceAll(RegExp('[\u201C\u201D]'), '"')
      .replaceAll('\u2014', '--')
      .replaceAll('\u2013', '-');
  final b = plain(before), a = plain(after), t = plain(typed);
  if (a.length != b.length + t.length) return false;
  for (var i = 0; i <= b.length; i++) {
    if (a.substring(0, i) == b.substring(0, i) &&
        a.substring(i, i + t.length) == t &&
        a.substring(i + t.length) == b.substring(i)) {
      return true;
    }
  }
  return false;
}