1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
//! `DamLev` automaton for efficient fuzzy string matching.
//!
//! This implements a `DamLev` NFA that can find all approximate matches
//! of a pattern in a text with bounded edit distance in O(N × m × k) time.
#![allow(
clippy::needless_range_loop,
clippy::items_after_statements,
clippy::similar_names,
clippy::too_many_lines
)]
use super::hash::{FxHashMap, FxHashSet};
/// Reusable buffers for NFA search to avoid allocations.
/// Create once and pass to search methods for reuse.
#[derive(Default, Debug)]
pub struct SearchBuffers {
/// Active states during search
active: Vec<ActiveState>,
/// Next iteration's active states
next_active: Vec<ActiveState>,
/// Seen states for epsilon closure
seen_set: FxHashSet<(State, usize)>,
/// Deduplication map
deduped: FxHashMap<(usize, usize, bool), ActiveState>,
/// Match results
matches: FxHashMap<(usize, usize), DamLevMatch>,
}
impl SearchBuffers {
/// Create new buffers with default capacity.
#[must_use]
pub fn new() -> Self {
SearchBuffers {
active: Vec::with_capacity(32),
next_active: Vec::with_capacity(32),
seen_set: FxHashSet::default(),
deduped: FxHashMap::default(),
matches: FxHashMap::default(),
}
}
/// Clear all buffers for reuse.
pub fn clear(&mut self) {
self.active.clear();
self.next_active.clear();
self.seen_set.clear();
self.deduped.clear();
self.matches.clear();
}
}
/// Edit operation limits.
#[derive(Debug, Clone, Default)]
pub struct EditLimits {
/// Maximum total number of edit operations allowed.
pub max_edits: u8,
/// Maximum number of insertion edits allowed (None = unlimited up to `max_edits`).
pub max_insertions: Option<u8>,
/// Maximum number of deletion edits allowed (None = unlimited up to `max_edits`).
pub max_deletions: Option<u8>,
/// Maximum number of substitution edits allowed (None = unlimited up to `max_edits`).
pub max_substitutions: Option<u8>,
/// Maximum number of transposition edits allowed (None = unlimited up to `max_edits`).
pub max_swaps: Option<u8>,
}
impl EditLimits {
/// Create edit limits with only a maximum total edit count.
#[must_use]
pub fn new(max_edits: u8) -> Self {
EditLimits {
max_edits,
max_insertions: None,
max_deletions: None,
max_substitutions: None,
max_swaps: None,
}
}
/// Create edit limits with specific limits for each operation type.
#[must_use]
pub fn with_limits(
max_edits: u8,
max_insertions: Option<u8>,
max_deletions: Option<u8>,
max_substitutions: Option<u8>,
max_swaps: Option<u8>,
) -> Self {
EditLimits {
max_edits,
max_insertions,
max_deletions,
max_substitutions,
max_swaps,
}
}
}
/// A match found by the `DamLev` automaton.
#[derive(Debug, Clone)]
pub struct DamLevMatch {
/// Start position of the match (byte offset, inclusive).
pub start: usize,
/// End position of the match (byte offset, exclusive).
pub end: usize,
/// Number of insertion edits in this match.
pub insertions: u8,
/// Number of deletion edits in this match.
pub deletions: u8,
/// Number of substitution edits in this match.
pub substitutions: u8,
/// Number of transposition edits in this match.
pub swaps: u8,
/// Similarity score (0.0 to 1.0).
pub similarity: f32,
}
impl DamLevMatch {
/// Returns the total number of edit operations in this match.
#[must_use]
pub fn total_edits(&self) -> u8 {
self.insertions
.saturating_add(self.deletions)
.saturating_add(self.substitutions)
.saturating_add(self.swaps)
}
}
/// State in the `DamLev` NFA.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
struct State {
/// Position in pattern (`0..=pattern_len`).
pos: usize,
/// Number of insertions used.
insertions: u8,
/// Number of deletions used.
deletions: u8,
/// Number of substitutions used.
substitutions: u8,
/// Number of swaps (transpositions) used.
swaps: u8,
/// If true, skip processing on next character (used for transposition which consumes 2 chars).
skip_next: bool,
}
impl State {
fn new(pos: usize) -> Self {
State {
pos,
insertions: 0,
deletions: 0,
substitutions: 0,
swaps: 0,
skip_next: false,
}
}
fn total_edits(&self) -> u8 {
self.insertions
.saturating_add(self.deletions)
.saturating_add(self.substitutions)
.saturating_add(self.swaps)
}
fn advance_match(&self) -> Self {
State {
pos: self.pos + 1,
skip_next: false,
..*self
}
}
fn advance_substitution(&self) -> Self {
State {
pos: self.pos + 1,
substitutions: self.substitutions + 1,
skip_next: false,
..*self
}
}
fn advance_deletion(&self) -> Self {
State {
pos: self.pos + 1,
deletions: self.deletions + 1,
skip_next: false,
..*self
}
}
fn advance_insertion(&self) -> Self {
State {
insertions: self.insertions + 1,
skip_next: false,
..*self
}
}
/// Advance by transposition (swap two adjacent characters).
/// Consumes 2 pattern chars and 2 text chars for 1 edit.
fn advance_swap(&self) -> Self {
State {
pos: self.pos + 2,
swaps: self.swaps + 1,
skip_next: true, // Skip next text char since transposition consumes 2
..*self
}
}
}
/// Active state tracking match start position.
#[derive(Debug, Clone)]
struct ActiveState {
state: State,
start_byte: usize,
start_char: usize,
}
/// `DamLev` NFA for fuzzy pattern matching.
#[derive(Debug)]
pub struct DamLevNfa {
pattern: String,
pattern_chars: Vec<char>,
limits: EditLimits,
case_insensitive: bool,
/// Beam width for state pruning - limits state explosion.
/// Default: `pattern_len` * 4 (adapts to pattern size).
beam_width: usize,
}
impl DamLevNfa {
/// Create a new `DamLev` NFA for the given pattern.
#[must_use]
pub fn new(pattern: &str, limits: EditLimits, case_insensitive: bool) -> Self {
let pattern_chars: Vec<char> = if case_insensitive {
pattern.to_lowercase().chars().collect()
} else {
pattern.chars().collect()
};
// Beam width scales with pattern length and max_edits
// Larger patterns need more states, but we cap it to prevent explosion
let beam_width =
((pattern_chars.len() + 1) * (limits.max_edits as usize + 1) * 2).clamp(32, 256);
DamLevNfa {
pattern: pattern.to_string(),
pattern_chars,
limits,
case_insensitive,
beam_width,
}
}
/// Returns the original pattern string.
#[must_use]
pub fn pattern(&self) -> &str {
&self.pattern
}
/// Check if we can do an insertion.
fn can_insert(&self, state: &State) -> bool {
if state.total_edits() >= self.limits.max_edits {
return false;
}
self.limits
.max_insertions
.is_none_or(|max| state.insertions < max)
}
/// Check if we can do a deletion.
fn can_delete(&self, state: &State) -> bool {
if state.total_edits() >= self.limits.max_edits {
return false;
}
self.limits
.max_deletions
.is_none_or(|max| state.deletions < max)
}
/// Check if we can do a substitution.
fn can_substitute(&self, state: &State) -> bool {
if state.total_edits() >= self.limits.max_edits {
return false;
}
self.limits
.max_substitutions
.is_none_or(|max| state.substitutions < max)
}
/// Check if we can do a swap (transposition).
fn can_swap(&self, state: &State) -> bool {
if state.total_edits() >= self.limits.max_edits {
return false;
}
self.limits.max_swaps.is_none_or(|max| state.swaps < max)
}
/// Add epsilon closure (deletion transitions) to a set of states.
/// Takes a reusable `HashSet` to avoid allocation on every call.
/// The `HashSet` is used internally for deduplication and will be modified.
fn epsilon_closure(&self, states: &mut Vec<ActiveState>, seen: &mut FxHashSet<(State, usize)>) {
self.epsilon_closure_from(states, 0, seen);
}
/// Add epsilon closure starting from a specific index in the states vector.
/// The `HashSet` should be pre-populated with existing states for deduplication.
fn epsilon_closure_from(
&self,
states: &mut Vec<ActiveState>,
start_idx: usize,
seen: &mut FxHashSet<(State, usize)>,
) {
let mut i = start_idx;
while i < states.len() {
let active = &states[i];
let state = active.state;
// Try deletion: skip pattern character without consuming text
if state.pos < self.pattern_chars.len() && self.can_delete(&state) {
let new_state = state.advance_deletion();
let key = (new_state, active.start_byte);
if !seen.contains(&key) {
seen.insert(key);
states.push(ActiveState {
state: new_state,
start_byte: active.start_byte,
start_char: active.start_char,
});
}
}
i += 1;
}
}
/// Calculate similarity score using normalized edit distance.
fn calc_similarity(&self, state: &State) -> f32 {
let pattern_len = self.pattern_chars.len() as f32;
if pattern_len == 0.0 {
return 1.0;
}
let edit_distance = f32::from(state.total_edits());
// Matched text length = pattern_len + insertions - deletions
let matched_len = pattern_len + f32::from(state.insertions) - f32::from(state.deletions);
let max_len = pattern_len.max(matched_len).max(1.0);
// Normalized DamLev similarity
(1.0 - edit_distance / max_len).max(0.0)
}
/// Calculate the maximum possible similarity a state can achieve.
/// Used for early pruning - if max possible < threshold, prune the state.
///
/// This is conservative: we only prune if the state CANNOT reach the threshold.
/// The actual similarity is: (1.0 - edits / max(`pattern_len`, `matched_len`))
/// where `matched_len` = `pattern_len` + insertions - deletions.
///
/// For early pruning, we use the most optimistic denominator (largest possible),
/// which gives the highest possible similarity.
#[inline]
fn max_possible_similarity(&self, state: &State) -> f32 {
let pattern_len = self.pattern_chars.len() as f32;
if pattern_len == 0.0 {
return 1.0;
}
let min_edits = f32::from(state.total_edits());
// Current matched length estimate (can grow with more insertions)
let current_matched_len =
pattern_len + f32::from(state.insertions) - f32::from(state.deletions);
// The max possible denominator is max(pattern_len, current_matched_len + remaining_edits)
// We're conservative: use the largest plausible denominator
let remaining_edits = f32::from(self.limits.max_edits - state.total_edits());
let max_denominator = pattern_len.max(current_matched_len + remaining_edits);
// Best case: no more edits needed, largest denominator
(1.0 - min_edits / max_denominator).max(0.0)
}
/// Apply beam pruning to active states - keep only the best states.
/// Sorts by `total_edits` (lower is better) and truncates to `beam_width`.
#[inline]
fn beam_prune(&self, states: &mut Vec<ActiveState>) {
if states.len() > self.beam_width * 2 {
// Sort by total_edits (ascending) - states with fewer edits are better
states.sort_by_key(|s| s.state.total_edits());
states.truncate(self.beam_width);
}
}
/// Find the first match in the text.
///
/// Returns the earliest match found, or None if no match exists.
#[must_use]
pub fn find_first(&self, text: &str, threshold: f32) -> Option<DamLevMatch> {
// For NFA fallback, we use find_all and take the first result
// This is not optimal but NFA is only used as a fallback for >64 char patterns
self.find_all(text, threshold)
.into_iter()
.min_by_key(|m| m.start)
}
/// Find all matches in the text.
///
/// Returns matches organized by (`start_byte`, `end_byte`) with the best match for each span.
#[must_use]
pub fn find_all(&self, text: &str, threshold: f32) -> Vec<DamLevMatch> {
if self.pattern_chars.is_empty() {
// Empty pattern matches everywhere
return vec![DamLevMatch {
start: 0,
end: 0,
insertions: 0,
deletions: 0,
substitutions: 0,
swaps: 0,
similarity: 1.0,
}];
}
let text_chars: Vec<(usize, char)> = text.char_indices().collect();
let mut matches: FxHashMap<(usize, usize), DamLevMatch> = FxHashMap::default();
// Reusable FxHashSet to avoid allocation per character
let mut seen_set: FxHashSet<(State, usize)> = FxHashSet::default();
// Reusable FxHashMap for deduplication
let mut deduped: FxHashMap<(usize, usize, bool), ActiveState> = FxHashMap::default();
// Active states: each contains (State, start_byte_pos, start_char_pos)
let mut active: Vec<ActiveState> = Vec::new();
// Process text character by character
for (char_idx, &(byte_pos, text_char)) in text_chars.iter().enumerate() {
let text_char = if self.case_insensitive {
text_char.to_lowercase().next().unwrap_or(text_char)
} else {
text_char
};
// Get next text char for transposition detection
let next_text_char = text_chars.get(char_idx + 1).map(|&(_, c)| {
if self.case_insensitive {
c.to_lowercase().next().unwrap_or(c)
} else {
c
}
});
// Start a new potential match at this position
let initial = ActiveState {
state: State::new(0),
start_byte: byte_pos,
start_char: char_idx,
};
active.push(initial);
// Add epsilon closure for new state - run directly on active
{
// Track where new states start
let start_idx = active.len() - 1;
// Populate seen_set with existing states
seen_set.clear();
for a in &active {
seen_set.insert((a.state, a.start_byte));
}
// Run epsilon closure starting from the new state
self.epsilon_closure_from(&mut active, start_idx, &mut seen_set);
}
// Compute next states
let mut next_active: Vec<ActiveState> = Vec::new();
for active_state in &active {
let state = active_state.state;
// If this state is marked skip_next (from a transposition), just clear the flag
if state.skip_next {
let mut continued = state;
continued.skip_next = false;
next_active.push(ActiveState {
state: continued,
start_byte: active_state.start_byte,
start_char: active_state.start_char,
});
continue;
}
if state.pos < self.pattern_chars.len() {
let pattern_char = self.pattern_chars[state.pos];
// Match transition
if text_char == pattern_char {
let new_state = state.advance_match();
next_active.push(ActiveState {
state: new_state,
start_byte: active_state.start_byte,
start_char: active_state.start_char,
});
}
// Substitution transition
if text_char != pattern_char && self.can_substitute(&state) {
let new_state = state.advance_substitution();
next_active.push(ActiveState {
state: new_state,
start_byte: active_state.start_byte,
start_char: active_state.start_char,
});
}
// Transposition transition: pattern[pos:pos+2] matches text[idx:idx+2] in reverse
// e.g., pattern "ab" matches text "ba"
if let Some(next_char) = next_text_char
&& state.pos + 1 < self.pattern_chars.len()
&& self.can_swap(&state)
{
let next_pattern_char = self.pattern_chars[state.pos + 1];
// Check if pattern[pos]=next_text and pattern[pos+1]=current_text (swapped)
if pattern_char == next_char
&& next_pattern_char == text_char
&& pattern_char != next_pattern_char
{
let new_state = state.advance_swap();
next_active.push(ActiveState {
state: new_state,
start_byte: active_state.start_byte,
start_char: active_state.start_char,
});
}
}
}
// Insertion transition (consume text char, stay at same pattern pos)
if self.can_insert(&state) {
let new_state = state.advance_insertion();
next_active.push(ActiveState {
state: new_state,
start_byte: active_state.start_byte,
start_char: active_state.start_char,
});
}
}
// Add epsilon closure (deletions)
seen_set.clear();
self.epsilon_closure(&mut next_active, &mut seen_set);
// Deduplicate: keep best state for each (pos, start_byte, skip_next)
deduped.clear();
for active_state in next_active {
// Early pruning: skip states that can't reach threshold
if self.max_possible_similarity(&active_state.state) < threshold {
continue;
}
let key = (
active_state.state.pos,
active_state.start_byte,
active_state.state.skip_next,
);
deduped
.entry(key)
.and_modify(|existing| {
// Keep state with fewer edits
if active_state.state.total_edits() < existing.state.total_edits() {
*existing = active_state.clone();
}
})
.or_insert(active_state);
}
active.clear();
active.extend(deduped.values().cloned());
// Beam pruning: if too many states, keep only the best ones
self.beam_prune(&mut active);
// Check for accepting states (reached end of pattern)
let end_byte = text_chars.get(char_idx + 1).map_or(text.len(), |(b, _)| *b);
for active_state in &active {
if active_state.state.pos == self.pattern_chars.len()
&& !active_state.state.skip_next
{
let sim = self.calc_similarity(&active_state.state);
if sim >= threshold {
let key = (active_state.start_byte, end_byte);
let m = DamLevMatch {
start: active_state.start_byte,
end: end_byte,
insertions: active_state.state.insertions,
deletions: active_state.state.deletions,
substitutions: active_state.state.substitutions,
swaps: active_state.state.swaps,
similarity: sim,
};
matches
.entry(key)
.and_modify(|existing| {
if m.similarity > existing.similarity {
*existing = m.clone();
}
})
.or_insert(m);
}
}
}
// Remove states that reached the end of pattern (they've been recorded)
// Also prune states that have fallen too far behind (can't possibly match)
let max_window = self.pattern_chars.len() + self.limits.max_edits as usize;
active.retain(|a| {
(a.state.pos < self.pattern_chars.len() || a.state.skip_next)
&& (char_idx + 1 - a.start_char) <= max_window
});
}
// Handle remaining states that might match with deletions at the end
for active_state in &active {
let state = active_state.state;
if state.skip_next {
continue; // Can't complete if waiting for next char
}
let remaining = (self.pattern_chars.len() - state.pos) as u8;
// Can we delete all remaining pattern chars?
if remaining <= self.limits.max_edits - state.total_edits() {
let dels_needed = remaining;
let total_dels = state.deletions + dels_needed;
if self
.limits
.max_deletions
.is_none_or(|max| total_dels <= max)
{
let final_state = State {
pos: self.pattern_chars.len(),
deletions: total_dels,
..state
};
let sim = self.calc_similarity(&final_state);
if sim >= threshold {
let key = (active_state.start_byte, text.len());
let m = DamLevMatch {
start: active_state.start_byte,
end: text.len(),
insertions: final_state.insertions,
deletions: final_state.deletions,
substitutions: final_state.substitutions,
swaps: final_state.swaps,
similarity: sim,
};
matches
.entry(key)
.and_modify(|existing| {
if m.similarity > existing.similarity {
*existing = m.clone();
}
})
.or_insert(m);
}
}
}
}
matches.into_values().collect()
}
/// Find up to `n` non-overlapping matches.
///
/// Note: This is a simple implementation that finds all matches and takes the first `n`.
/// For large texts with many matches, consider using the Bitap-based matcher instead.
#[must_use]
pub fn find_n(&self, text: &str, threshold: f32, n: usize) -> Vec<DamLevMatch> {
if n == 0 {
return Vec::new();
}
let mut all_matches = self.find_all(text, threshold);
// Sort by start position
all_matches.sort_by_key(|m| m.start);
// Take non-overlapping matches up to limit
let mut result = Vec::with_capacity(n.min(all_matches.len()));
let mut last_end = 0;
for m in all_matches {
if m.start >= last_end {
last_end = m.end;
result.push(m);
if result.len() >= n {
break;
}
}
}
result
}
/// Find all matches, but only start new potential matches at candidate positions.
///
/// This is an optimization: instead of starting a new match at every character,
/// we only start at positions identified by a prefilter.
#[must_use]
pub fn find_all_with_candidates(
&self,
text: &str,
threshold: f32,
candidates: &FxHashSet<usize>,
) -> Vec<DamLevMatch> {
let mut buffers = SearchBuffers::new();
self.find_all_with_candidates_buffered(text, threshold, candidates, &mut buffers)
}
/// Find all matches using pre-allocated buffers to avoid allocations.
pub fn find_all_with_candidates_buffered(
&self,
text: &str,
threshold: f32,
candidates: &FxHashSet<usize>,
buffers: &mut SearchBuffers,
) -> Vec<DamLevMatch> {
// Clear buffers for reuse
buffers.clear();
if self.pattern_chars.is_empty() {
return vec![DamLevMatch {
start: 0,
end: 0,
insertions: 0,
deletions: 0,
substitutions: 0,
swaps: 0,
similarity: 1.0,
}];
}
// Stream through chars without collecting - use peekable for lookahead
let mut char_iter = text.char_indices().peekable();
let mut char_idx = 0usize;
while let Some((byte_pos, raw_char)) = char_iter.next() {
let text_char = if self.case_insensitive {
raw_char.to_lowercase().next().unwrap_or(raw_char)
} else {
raw_char
};
// Peek next char for transposition detection and end_byte calculation
let next_info = char_iter.peek().map(|&(next_byte, next_char)| {
let c = if self.case_insensitive {
next_char.to_lowercase().next().unwrap_or(next_char)
} else {
next_char
};
(next_byte, c)
});
let next_text_char = next_info.map(|(_, c)| c);
let end_byte = next_info.map_or(text.len(), |(b, _)| b);
// Only start a new potential match if this is a candidate position
if candidates.contains(&byte_pos) {
let initial = ActiveState {
state: State::new(0),
start_byte: byte_pos,
start_char: char_idx,
};
buffers.active.push(initial);
// Add epsilon closure for new state - run directly on active
{
let start_idx = buffers.active.len() - 1;
buffers.seen_set.clear();
for a in &buffers.active {
buffers.seen_set.insert((a.state, a.start_byte));
}
self.epsilon_closure_from(
&mut buffers.active,
start_idx,
&mut buffers.seen_set,
);
}
}
// Process active states - reuse next_active buffer
buffers.next_active.clear();
for active_state in &buffers.active {
let state = active_state.state;
// If this state is marked skip_next (from a transposition), just clear the flag
if state.skip_next {
let mut continued = state;
continued.skip_next = false;
buffers.next_active.push(ActiveState {
state: continued,
start_byte: active_state.start_byte,
start_char: active_state.start_char,
});
continue;
}
if state.pos < self.pattern_chars.len() {
let pattern_char = self.pattern_chars[state.pos];
if text_char == pattern_char {
buffers.next_active.push(ActiveState {
state: state.advance_match(),
start_byte: active_state.start_byte,
start_char: active_state.start_char,
});
}
if text_char != pattern_char && self.can_substitute(&state) {
buffers.next_active.push(ActiveState {
state: state.advance_substitution(),
start_byte: active_state.start_byte,
start_char: active_state.start_char,
});
}
// Transposition transition
if let Some(next_char) = next_text_char
&& state.pos + 1 < self.pattern_chars.len()
&& self.can_swap(&state)
{
let next_pattern_char = self.pattern_chars[state.pos + 1];
if pattern_char == next_char
&& next_pattern_char == text_char
&& pattern_char != next_pattern_char
{
buffers.next_active.push(ActiveState {
state: state.advance_swap(),
start_byte: active_state.start_byte,
start_char: active_state.start_char,
});
}
}
}
if self.can_insert(&state) {
buffers.next_active.push(ActiveState {
state: state.advance_insertion(),
start_byte: active_state.start_byte,
start_char: active_state.start_char,
});
}
}
buffers.seen_set.clear();
self.epsilon_closure(&mut buffers.next_active, &mut buffers.seen_set);
// Deduplicate with early pruning
buffers.deduped.clear();
for active_state in buffers.next_active.drain(..) {
// Early pruning: skip states that can't reach threshold
if self.max_possible_similarity(&active_state.state) < threshold {
continue;
}
let key = (
active_state.state.pos,
active_state.start_byte,
active_state.state.skip_next,
);
buffers
.deduped
.entry(key)
.and_modify(|existing| {
if active_state.state.total_edits() < existing.state.total_edits() {
*existing = active_state.clone();
}
})
.or_insert(active_state);
}
buffers.active.clear();
buffers.active.extend(buffers.deduped.values().cloned());
// Beam pruning: limit state explosion
self.beam_prune(&mut buffers.active);
// Check for accepting states (end_byte already computed above from peek)
for active_state in &buffers.active {
if active_state.state.pos == self.pattern_chars.len()
&& !active_state.state.skip_next
{
let sim = self.calc_similarity(&active_state.state);
if sim >= threshold {
let key = (active_state.start_byte, end_byte);
let m = DamLevMatch {
start: active_state.start_byte,
end: end_byte,
insertions: active_state.state.insertions,
deletions: active_state.state.deletions,
substitutions: active_state.state.substitutions,
swaps: active_state.state.swaps,
similarity: sim,
};
buffers
.matches
.entry(key)
.and_modify(|existing| {
if m.similarity > existing.similarity {
*existing = m.clone();
}
})
.or_insert(m);
}
}
}
// Prune states
let max_window = self.pattern_chars.len() + self.limits.max_edits as usize;
buffers.active.retain(|a| {
(a.state.pos < self.pattern_chars.len() || a.state.skip_next)
&& (char_idx + 1 - a.start_char) <= max_window
});
char_idx += 1;
}
// Handle remaining states at end of text
for active_state in &buffers.active {
let state = active_state.state;
if state.skip_next {
continue;
}
let remaining = self.pattern_chars.len() - state.pos;
if remaining as u8 <= self.limits.max_edits - state.total_edits() {
let dels_needed = remaining as u8;
let total_dels = state.deletions + dels_needed;
if self
.limits
.max_deletions
.is_none_or(|max| total_dels <= max)
{
let final_state = State {
pos: self.pattern_chars.len(),
deletions: total_dels,
..state
};
let sim = self.calc_similarity(&final_state);
if sim >= threshold {
let key = (active_state.start_byte, text.len());
let m = DamLevMatch {
start: active_state.start_byte,
end: text.len(),
insertions: final_state.insertions,
deletions: final_state.deletions,
substitutions: final_state.substitutions,
swaps: final_state.swaps,
similarity: sim,
};
buffers
.matches
.entry(key)
.and_modify(|existing| {
if m.similarity > existing.similarity {
*existing = m.clone();
}
})
.or_insert(m);
}
}
}
}
buffers.matches.drain().map(|(_, v)| v).collect()
}
/// Find the first match, stopping as soon as one is found.
///
/// This is much faster than `find_all` when we only need the first match,
/// especially when the match is found early in the text.
#[must_use]
pub fn find_first_with_candidates(
&self,
text: &str,
threshold: f32,
candidates: &FxHashSet<usize>,
) -> Option<DamLevMatch> {
if self.pattern_chars.is_empty() {
return Some(DamLevMatch {
start: 0,
end: 0,
insertions: 0,
deletions: 0,
substitutions: 0,
swaps: 0,
similarity: 1.0,
});
}
let text_chars: Vec<(usize, char)> = text.char_indices().collect();
let mut active: Vec<ActiveState> = Vec::new();
// Reusable FxHashSet to avoid allocation per character
let mut seen_set: FxHashSet<(State, usize)> = FxHashSet::default();
// Reusable FxHashMap for deduplication
let mut deduped: FxHashMap<(usize, usize, bool), ActiveState> = FxHashMap::default();
for (char_idx, &(byte_pos, text_char)) in text_chars.iter().enumerate() {
let text_char = if self.case_insensitive {
text_char.to_lowercase().next().unwrap_or(text_char)
} else {
text_char
};
// Get next text char for transposition detection
let next_text_char = text_chars.get(char_idx + 1).map(|&(_, c)| {
if self.case_insensitive {
c.to_lowercase().next().unwrap_or(c)
} else {
c
}
});
// Only start a new potential match if this is a candidate position
if candidates.contains(&byte_pos) {
let initial = ActiveState {
state: State::new(0),
start_byte: byte_pos,
start_char: char_idx,
};
active.push(initial);
// Add epsilon closure for new state - in-place from last element
let start_idx = active.len() - 1;
seen_set.clear();
for a in &active {
seen_set.insert((a.state, a.start_byte));
}
self.epsilon_closure_from(&mut active, start_idx, &mut seen_set);
}
// If no active states, continue to next char
if active.is_empty() {
continue;
}
// Process active states
let mut next_active: Vec<ActiveState> = Vec::new();
for active_state in &active {
let state = active_state.state;
// If this state is marked skip_next (from a transposition), just clear the flag
if state.skip_next {
let mut continued = state;
continued.skip_next = false;
next_active.push(ActiveState {
state: continued,
start_byte: active_state.start_byte,
start_char: active_state.start_char,
});
continue;
}
if state.pos < self.pattern_chars.len() {
let pattern_char = self.pattern_chars[state.pos];
if text_char == pattern_char {
next_active.push(ActiveState {
state: state.advance_match(),
start_byte: active_state.start_byte,
start_char: active_state.start_char,
});
}
if text_char != pattern_char && self.can_substitute(&state) {
next_active.push(ActiveState {
state: state.advance_substitution(),
start_byte: active_state.start_byte,
start_char: active_state.start_char,
});
}
// Transposition transition
if let Some(next_char) = next_text_char
&& state.pos + 1 < self.pattern_chars.len()
&& self.can_swap(&state)
{
let next_pattern_char = self.pattern_chars[state.pos + 1];
if pattern_char == next_char
&& next_pattern_char == text_char
&& pattern_char != next_pattern_char
{
next_active.push(ActiveState {
state: state.advance_swap(),
start_byte: active_state.start_byte,
start_char: active_state.start_char,
});
}
}
}
if self.can_insert(&state) {
next_active.push(ActiveState {
state: state.advance_insertion(),
start_byte: active_state.start_byte,
start_char: active_state.start_char,
});
}
}
seen_set.clear();
self.epsilon_closure(&mut next_active, &mut seen_set);
// Deduplicate (keep best state) with early pruning
deduped.clear();
for active_state in next_active {
// Early pruning: skip states that can't reach threshold
if self.max_possible_similarity(&active_state.state) < threshold {
continue;
}
let key = (
active_state.state.pos,
active_state.start_byte,
active_state.state.skip_next,
);
deduped
.entry(key)
.and_modify(|existing| {
if active_state.state.total_edits() < existing.state.total_edits() {
*existing = active_state.clone();
}
})
.or_insert(active_state);
}
active.clear();
active.extend(deduped.values().cloned());
// Beam pruning: limit state explosion
self.beam_prune(&mut active);
// Check for accepting states - return best match at this position
let end_byte = text_chars.get(char_idx + 1).map_or(text.len(), |(b, _)| *b);
// Find the best accepting state (highest similarity = lowest edits)
let mut best_match: Option<DamLevMatch> = None;
for active_state in &active {
if active_state.state.pos == self.pattern_chars.len()
&& !active_state.state.skip_next
{
let sim = self.calc_similarity(&active_state.state);
if sim >= threshold {
let m = DamLevMatch {
start: active_state.start_byte,
end: end_byte,
insertions: active_state.state.insertions,
deletions: active_state.state.deletions,
substitutions: active_state.state.substitutions,
swaps: active_state.state.swaps,
similarity: sim,
};
if best_match.as_ref().is_none_or(|best| sim > best.similarity) {
best_match = Some(m);
}
}
}
}
if best_match.is_some() {
return best_match;
}
// Prune states
let max_window = self.pattern_chars.len() + self.limits.max_edits as usize;
active.retain(|a| {
(a.state.pos < self.pattern_chars.len() || a.state.skip_next)
&& (char_idx + 1 - a.start_char) <= max_window
});
}
// Check remaining states at end of text
for active_state in &active {
let state = active_state.state;
if state.skip_next {
continue;
}
let remaining = self.pattern_chars.len() - state.pos;
if remaining as u8 <= self.limits.max_edits - state.total_edits() {
let dels_needed = remaining as u8;
let total_dels = state.deletions + dels_needed;
if self
.limits
.max_deletions
.is_none_or(|max| total_dels <= max)
{
let final_state = State {
pos: self.pattern_chars.len(),
deletions: total_dels,
..state
};
let sim = self.calc_similarity(&final_state);
if sim >= threshold {
return Some(DamLevMatch {
start: active_state.start_byte,
end: text.len(),
insertions: final_state.insertions,
deletions: final_state.deletions,
substitutions: final_state.substitutions,
swaps: final_state.swaps,
similarity: sim,
});
}
}
}
}
None
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_exact_match() {
let nfa = DamLevNfa::new("hello", EditLimits::new(0), false);
let matches = nfa.find_all("hello world", 0.8);
assert_eq!(matches.len(), 1);
assert_eq!(matches[0].start, 0);
assert_eq!(matches[0].end, 5);
assert_eq!(matches[0].total_edits(), 0);
}
#[test]
fn test_one_substitution() {
let nfa = DamLevNfa::new("hello", EditLimits::new(1), false);
let matches = nfa.find_all("hallo world", 0.5);
assert!(
matches
.iter()
.any(|m| m.start == 0 && m.end == 5 && m.substitutions == 1)
);
}
#[test]
fn test_one_insertion() {
let nfa = DamLevNfa::new("hello", EditLimits::new(1), false);
let matches = nfa.find_all("heello world", 0.5);
assert!(matches.iter().any(|m| m.start == 0 && m.insertions == 1));
}
#[test]
fn test_one_deletion() {
let nfa = DamLevNfa::new("hello", EditLimits::new(1), false);
let matches = nfa.find_all("helo world", 0.5);
assert!(matches.iter().any(|m| m.start == 0 && m.deletions == 1));
}
#[test]
fn test_case_insensitive() {
let nfa = DamLevNfa::new("hello", EditLimits::new(0), true);
let matches = nfa.find_all("HELLO world", 0.8);
assert_eq!(matches.len(), 1);
assert_eq!(matches[0].start, 0);
assert_eq!(matches[0].end, 5);
}
#[test]
fn test_multiple_matches() {
let nfa = DamLevNfa::new("ab", EditLimits::new(0), false);
let matches = nfa.find_all("ab ab ab", 0.8);
assert_eq!(matches.len(), 3);
}
#[test]
fn test_no_match() {
let nfa = DamLevNfa::new("xyz", EditLimits::new(1), false);
let matches = nfa.find_all("hello world", 0.8);
assert!(matches.is_empty());
}
// --- Transposition tests ---
#[test]
fn test_transposition_simple() {
// Pattern "ab" should match "ba" with 1 swap
let nfa = DamLevNfa::new("ab", EditLimits::new(1), false);
let matches = nfa.find_all("ba", 0.0);
assert!(!matches.is_empty(), "Should find match for transposition");
// Find the swap match (there might be other matches like deletion-only)
let swap_match = matches.iter().find(|m| m.swaps == 1);
assert!(
swap_match.is_some(),
"Should find match with 1 swap, got: {matches:?}"
);
let m = swap_match.unwrap();
assert_eq!(m.substitutions, 0, "Should have 0 substitutions");
assert_eq!(m.total_edits(), 1, "Total edits should be 1");
}
#[test]
fn test_transposition_in_word() {
// Pattern "teh" should match "the" with 1 swap (common typo)
let nfa = DamLevNfa::new("the", EditLimits::new(1), false);
let matches = nfa.find_all("teh", 0.0);
assert!(!matches.is_empty(), "Should find match for 'teh' -> 'the'");
let m = matches.iter().find(|m| m.swaps == 1);
assert!(m.is_some(), "Should find match with 1 swap");
}
#[test]
fn test_transposition_longer_word() {
// Pattern "receive" should match "recieve" with 1 swap (common typo)
let nfa = DamLevNfa::new("receive", EditLimits::new(1), false);
let matches = nfa.find_all("recieve", 0.0);
assert!(
!matches.is_empty(),
"Should find match for 'recieve' -> 'receive'"
);
// Check that we found a match with 1 swap (i and e swapped)
let swap_match = matches.iter().find(|m| m.swaps == 1);
assert!(
swap_match.is_some(),
"Should find match with 1 swap, got: {matches:?}"
);
}
#[test]
fn test_transposition_with_other_edits() {
// "abcd" matching "badc" - two transpositions
let nfa = DamLevNfa::new("abcd", EditLimits::new(2), false);
let matches = nfa.find_all("badc", 0.0);
assert!(!matches.is_empty(), "Should find match with 2 swaps");
// Should find a match with 2 swaps
let m = matches.iter().find(|m| m.total_edits() == 2);
assert!(m.is_some(), "Should find match with 2 total edits");
}
#[test]
fn test_transposition_not_same_char() {
// Transposition should only work for different adjacent characters
// "aa" cannot be swapped to "aa" (same characters)
let nfa = DamLevNfa::new("aa", EditLimits::new(1), false);
let matches = nfa.find_all("aa", 0.0);
// Should find exact match with 0 edits
assert!(!matches.is_empty());
let exact = matches.iter().find(|m| m.total_edits() == 0);
assert!(exact.is_some(), "Should find exact match");
}
#[test]
fn test_transposition_vs_substitution() {
// With swaps enabled, "ab" -> "ba" should find a swap match with 1 edit
let nfa = DamLevNfa::new("ab", EditLimits::new(2), false);
let matches = nfa.find_all("ba", 0.0);
// Should find a swap match (1 edit) among the results
let swap_match = matches
.iter()
.find(|m| m.swaps == 1 && m.total_edits() == 1);
assert!(
swap_match.is_some(),
"Should find match with 1 swap, got: {matches:?}"
);
// The swap match should have better similarity than substitution matches
let best_sim = matches
.iter()
.map(|m| m.similarity)
.max_by(|a, b| a.partial_cmp(b).unwrap());
let swap_sim = swap_match.unwrap().similarity;
assert!(
swap_sim >= best_sim.unwrap() - 0.01,
"Swap match should have high similarity"
);
}
#[test]
fn test_transposition_case_insensitive() {
// Case insensitive transposition
let nfa = DamLevNfa::new("AB", EditLimits::new(1), true);
let matches = nfa.find_all("ba", 0.0);
assert!(
!matches.is_empty(),
"Should find case-insensitive transposition"
);
let m = matches.iter().find(|m| m.swaps == 1);
assert!(m.is_some(), "Should find match with 1 swap");
}
}
#[test]
fn test_find_with_candidates() {
let nfa = DamLevNfa::new("quik", EditLimits::new(1), false);
// Test 1: All positions as candidates (like find_all)
let text = "The quick brown fox";
let all_positions: FxHashSet<usize> = text.char_indices().map(|(i, _)| i).collect();
let matches = nfa.find_all_with_candidates(text, 0.8, &all_positions);
println!("All positions candidates: {matches:?}");
// Test 2: Only positions 3, 4 as candidates
let limited: FxHashSet<usize> = vec![3, 4].into_iter().collect();
let matches2 = nfa.find_all_with_candidates(text, 0.8, &limited);
println!("Limited candidates (3,4): {matches2:?}");
// Test 3: Compare with find_all
let matches3 = nfa.find_all(text, 0.8);
println!("find_all: {matches3:?}");
assert!(!matches.is_empty(), "Should find match with all positions");
assert!(
!matches2.is_empty(),
"Should find match with limited positions"
);
}