AppFlowy/frontend/appflowy_flutter/lib/util/levenshtein.dart

27 lines
568 B
Dart
Raw Normal View History

[feat] Allow user to select any Google Font (#2895) * chore: add label for font selection drop down * chore: add method to set font family * feat: add drop down to setting appearance view * feat: add fontFamily to document appearance cubit * feat: add bloc provider to root for document appearance style * feat: syncFont family from setting appearance dialog * feat: plumbing for font style in editor * fix: add blocprovider before pushing overlay * chore: add kv_keys * fix: use fontFamily in document appearance cubit * fix: remove bloc providers because bloc is supplied in ancestor * fix: remove unecessary bloc provider * chore: add constraints to popover * chore: add translation for search box * feat: add levenshtein for string sort * feat: add search bar view * refactor: levenshtein * chore: add tests for levenshtein algorithm * feat: add unit tests for appearance cubit * fix: analyzer warnings * feat: sort by ascending if query is empty * chore: add test for the font family setting widget * feat: make comparison case insensitive * feat: lazy load with listview.builder Co-authored-by: Yijing Huang <hyj891204@gmail.com> * fix: fonts loaded on open application * fix: checkmark doesn't show * fix: try catch before getFont * fix: clear text editing value on close * fix: remove autofocus for search text field * chore: add tests * feat: use sliver protocol Co-authored-by: Yijing Huang <hyj891204@gmail.com> * fix: avoid using intrinsic height Co-authored-by: Yijing Huang <hyj891204@gmail.com> * fix: extra paren caused build failure * feat: switch order of font family setting --------- Co-authored-by: Yijing Huang <hyj891204@gmail.com>
2023-07-04 21:30:38 +00:00
import 'dart:math';
int levenshtein(String s, String t, {bool caseSensitive = true}) {
if (!caseSensitive) {
s = s.toLowerCase();
t = t.toLowerCase();
}
if (s == t) return 0;
final v0 = List<int>.generate(t.length + 1, (i) => i);
final v1 = List<int>.filled(t.length + 1, 0);
for (var i = 0; i < s.length; i++) {
v1[0] = i + 1;
for (var j = 0; j < t.length; j++) {
final cost = (s[i] == t[j]) ? 0 : 1;
v1[j + 1] = min(v1[j] + 1, min(v0[j + 1] + 1, v0[j] + cost));
}
v0.setAll(0, v1);
}
return v1[t.length];
}