liblevenshtein 0.9.1

Levenshtein/Universal Automata for approximate string matching using various dictionary backends
Documentation
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
//! Tests for the phonetic NFA implementations (character-level and byte-level).
//!
//! These tests exercise both [`super::NFAChar`] and [`super::NFA`] using the
//! types re-exported via [`super`]. The tests are kept in a sibling test file
//! because they mix both character- and byte-level constructions in a single
//! test surface.

#[cfg(test)]
use super::{NFAChar, TransitionLabelChar, NFA};

// --- NFAChar basic tests ---

#[test]
fn test_nfa_char_new() {
    let nfa = NFAChar::new();
    assert_eq!(nfa.num_states(), 1);
    assert_eq!(nfa.num_transitions(), 0);
    assert_eq!(nfa.start(), 0);
    assert!(!nfa.is_final(0));
}

#[test]
fn test_nfa_char_add_state() {
    let mut nfa = NFAChar::new();
    let q1 = nfa.add_state(false);
    let q2 = nfa.add_state(true);

    assert_eq!(q1, 1);
    assert_eq!(q2, 2);
    assert_eq!(nfa.num_states(), 3);
    assert!(!nfa.is_final(q1));
    assert!(nfa.is_final(q2));
}

#[test]
fn test_nfa_char_simple_accept() {
    let mut nfa = NFAChar::new();
    let q0 = nfa.start();
    let q1 = nfa.add_state(true);

    nfa.add_transition_char(q0, 'a', q1);

    assert!(nfa.accepts("a"));
    assert!(!nfa.accepts("b"));
    assert!(!nfa.accepts(""));
    assert!(!nfa.accepts("aa"));
}

#[test]
fn test_nfa_char_epsilon_closure() {
    let mut nfa = NFAChar::new();
    let q0 = nfa.start();
    let q1 = nfa.add_state(false);
    let q2 = nfa.add_state(true);

    nfa.add_epsilon(q0, q1);
    nfa.add_epsilon(q1, q2);

    let closure = nfa.epsilon_closure_single(q0);
    assert!(closure.contains(&q0));
    assert!(closure.contains(&q1));
    assert!(closure.contains(&q2));
    assert_eq!(closure.len(), 3);
}

#[test]
fn test_nfa_char_accepts_with_epsilon() {
    let mut nfa = NFAChar::new();
    let q0 = nfa.start();
    let q1 = nfa.add_state(false);
    let q2 = nfa.add_state(true);

    nfa.add_epsilon(q0, q1);
    nfa.add_transition_char(q1, 'x', q2);

    assert!(nfa.accepts("x"));
    assert!(!nfa.accepts("y"));
}

// --- NFA combination tests ---

#[test]
fn test_nfa_char_union() {
    // NFA for "a"
    let mut nfa_a = NFAChar::new();
    let q0 = nfa_a.start();
    let q1 = nfa_a.add_state(true);
    nfa_a.add_transition_char(q0, 'a', q1);

    // NFA for "b"
    let mut nfa_b = NFAChar::new();
    let q0 = nfa_b.start();
    let q1 = nfa_b.add_state(true);
    nfa_b.add_transition_char(q0, 'b', q1);

    // Union: a | b
    let union = nfa_a.union(nfa_b);

    assert!(union.accepts("a"));
    assert!(union.accepts("b"));
    assert!(!union.accepts("c"));
    assert!(!union.accepts("ab"));
    assert!(!union.accepts(""));
}

#[test]
fn test_nfa_char_concatenate() {
    // NFA for "a"
    let mut nfa_a = NFAChar::new();
    let q0 = nfa_a.start();
    let q1 = nfa_a.add_state(true);
    nfa_a.add_transition_char(q0, 'a', q1);

    // NFA for "b"
    let mut nfa_b = NFAChar::new();
    let q0 = nfa_b.start();
    let q1 = nfa_b.add_state(true);
    nfa_b.add_transition_char(q0, 'b', q1);

    // Concatenation: ab
    let concat = nfa_a.concatenate(nfa_b);

    assert!(concat.accepts("ab"));
    assert!(!concat.accepts("a"));
    assert!(!concat.accepts("b"));
    assert!(!concat.accepts("ba"));
    assert!(!concat.accepts(""));
}

#[test]
fn test_nfa_char_kleene_star() {
    // NFA for "a"
    let mut nfa_a = NFAChar::new();
    let q0 = nfa_a.start();
    let q1 = nfa_a.add_state(true);
    nfa_a.add_transition_char(q0, 'a', q1);

    // Kleene star: a*
    let star = nfa_a.kleene_star();

    assert!(star.accepts(""));
    assert!(star.accepts("a"));
    assert!(star.accepts("aa"));
    assert!(star.accepts("aaa"));
    assert!(!star.accepts("b"));
    assert!(!star.accepts("ab"));
}

#[test]
fn test_nfa_char_kleene_plus() {
    // NFA for "a"
    let mut nfa_a = NFAChar::new();
    let q0 = nfa_a.start();
    let q1 = nfa_a.add_state(true);
    nfa_a.add_transition_char(q0, 'a', q1);

    // Kleene plus: a+
    let plus = nfa_a.kleene_plus();

    assert!(!plus.accepts(""));
    assert!(plus.accepts("a"));
    assert!(plus.accepts("aa"));
    assert!(plus.accepts("aaa"));
    assert!(!plus.accepts("b"));
}

#[test]
fn test_nfa_char_optional() {
    // NFA for "a"
    let mut nfa_a = NFAChar::new();
    let q0 = nfa_a.start();
    let q1 = nfa_a.add_state(true);
    nfa_a.add_transition_char(q0, 'a', q1);

    // Optional: a?
    let opt = nfa_a.optional();

    assert!(opt.accepts(""));
    assert!(opt.accepts("a"));
    assert!(!opt.accepts("aa"));
    assert!(!opt.accepts("b"));
}

// --- Byte-level NFA tests ---

#[test]
fn test_nfa_byte_simple() {
    let mut nfa = NFA::new();
    let q0 = nfa.start();
    let q1 = nfa.add_state(true);

    nfa.add_transition_byte(q0, b'a', q1);

    assert!(nfa.accepts(b"a"));
    assert!(nfa.accepts_str("a"));
    assert!(!nfa.accepts(b"b"));
    assert!(!nfa.accepts(b""));
}

#[test]
fn test_nfa_byte_union() {
    let mut nfa_a = NFA::new();
    let q0 = nfa_a.start();
    let q1 = nfa_a.add_state(true);
    nfa_a.add_transition_byte(q0, b'a', q1);

    let mut nfa_b = NFA::new();
    let q0 = nfa_b.start();
    let q1 = nfa_b.add_state(true);
    nfa_b.add_transition_byte(q0, b'b', q1);

    let union = nfa_a.union(nfa_b);

    assert!(union.accepts_str("a"));
    assert!(union.accepts_str("b"));
    assert!(!union.accepts_str("c"));
}

#[test]
fn test_nfa_byte_concatenate() {
    let mut nfa_a = NFA::new();
    let q0 = nfa_a.start();
    let q1 = nfa_a.add_state(true);
    nfa_a.add_transition_byte(q0, b'a', q1);

    let mut nfa_b = NFA::new();
    let q0 = nfa_b.start();
    let q1 = nfa_b.add_state(true);
    nfa_b.add_transition_byte(q0, b'b', q1);

    let concat = nfa_a.concatenate(nfa_b);

    assert!(concat.accepts_str("ab"));
    assert!(!concat.accepts_str("a"));
    assert!(!concat.accepts_str("b"));
}

// --- Complex pattern tests ---

#[test]
fn test_nfa_char_complex_pattern() {
    // Pattern: (a|b)*c
    // Build NFA for 'a'
    let mut nfa_a = NFAChar::new();
    let q0 = nfa_a.start();
    let q1 = nfa_a.add_state(true);
    nfa_a.add_transition_char(q0, 'a', q1);

    // Build NFA for 'b'
    let mut nfa_b = NFAChar::new();
    let q0 = nfa_b.start();
    let q1 = nfa_b.add_state(true);
    nfa_b.add_transition_char(q0, 'b', q1);

    // Build NFA for 'c'
    let mut nfa_c = NFAChar::new();
    let q0 = nfa_c.start();
    let q1 = nfa_c.add_state(true);
    nfa_c.add_transition_char(q0, 'c', q1);

    // (a|b)*
    let union = nfa_a.union(nfa_b);
    let star = union.kleene_star();

    // (a|b)*c
    let pattern = star.concatenate(nfa_c);

    assert!(pattern.accepts("c"));
    assert!(pattern.accepts("ac"));
    assert!(pattern.accepts("bc"));
    assert!(pattern.accepts("aac"));
    assert!(pattern.accepts("abc"));
    assert!(pattern.accepts("bac"));
    assert!(pattern.accepts("aabc"));
    assert!(!pattern.accepts(""));
    assert!(!pattern.accepts("a"));
    assert!(!pattern.accepts("b"));
    assert!(!pattern.accepts("ca"));
}

// --- Anchor tests ---

#[test]
fn test_nfa_char_start_of_line_anchor() {
    // Build NFA for ^hello
    let mut nfa = NFAChar::new();
    let q0 = nfa.start();
    let q1 = nfa.add_state(false);
    let q2 = nfa.add_state(false);
    let q3 = nfa.add_state(false);
    let q4 = nfa.add_state(false);
    let q5 = nfa.add_state(false);
    let q6 = nfa.add_state(true);

    // ^
    nfa.add_transition(q0, TransitionLabelChar::StartOfLine, q1);
    // hello
    nfa.add_transition_char(q1, 'h', q2);
    nfa.add_transition_char(q2, 'e', q3);
    nfa.add_transition_char(q3, 'l', q4);
    nfa.add_transition_char(q4, 'l', q5);
    nfa.add_transition_char(q5, 'o', q6);

    // Should match "hello" at start of input
    assert!(nfa.accepts("hello"));
    // Should not match with prefix
    assert!(!nfa.accepts("xhello"));
}

#[test]
fn test_nfa_char_end_of_line_anchor() {
    // Build NFA for hello$
    let mut nfa = NFAChar::new();
    let q0 = nfa.start();
    let q1 = nfa.add_state(false);
    let q2 = nfa.add_state(false);
    let q3 = nfa.add_state(false);
    let q4 = nfa.add_state(false);
    let q5 = nfa.add_state(false);
    let q6 = nfa.add_state(true);

    // hello
    nfa.add_transition_char(q0, 'h', q1);
    nfa.add_transition_char(q1, 'e', q2);
    nfa.add_transition_char(q2, 'l', q3);
    nfa.add_transition_char(q3, 'l', q4);
    nfa.add_transition_char(q4, 'o', q5);
    // $
    nfa.add_transition(q5, TransitionLabelChar::EndOfLine, q6);

    // Should match "hello" at end of input
    assert!(nfa.accepts("hello"));
}

#[test]
fn test_nfa_char_anchored_pattern() {
    // Build NFA for ^hello$
    let mut nfa = NFAChar::new();
    let q0 = nfa.start();
    let q1 = nfa.add_state(false);
    let q2 = nfa.add_state(false);
    let q3 = nfa.add_state(false);
    let q4 = nfa.add_state(false);
    let q5 = nfa.add_state(false);
    let q6 = nfa.add_state(false);
    let q7 = nfa.add_state(true);

    // ^
    nfa.add_transition(q0, TransitionLabelChar::StartOfLine, q1);
    // hello
    nfa.add_transition_char(q1, 'h', q2);
    nfa.add_transition_char(q2, 'e', q3);
    nfa.add_transition_char(q3, 'l', q4);
    nfa.add_transition_char(q4, 'l', q5);
    nfa.add_transition_char(q5, 'o', q6);
    // $
    nfa.add_transition(q6, TransitionLabelChar::EndOfLine, q7);

    // Should match exact "hello"
    assert!(nfa.accepts("hello"));
    // Should not match with extra chars
    assert!(!nfa.accepts("xhello"));
}

#[test]
fn test_nfa_char_multiline_start_of_line() {
    // Build NFA for ^line
    let mut nfa = NFAChar::new();
    let q0 = nfa.start();
    let q1 = nfa.add_state(false);
    let q2 = nfa.add_state(false);
    let q3 = nfa.add_state(false);
    let q4 = nfa.add_state(false);
    let q5 = nfa.add_state(true);

    // ^
    nfa.add_transition(q0, TransitionLabelChar::StartOfLine, q1);
    // line
    nfa.add_transition_char(q1, 'l', q2);
    nfa.add_transition_char(q2, 'i', q3);
    nfa.add_transition_char(q3, 'n', q4);
    nfa.add_transition_char(q4, 'e', q5);

    // Without multiline, should only match at start
    assert!(nfa.accepts_with_flags("line", false, false));

    // In multiline mode, ^ should match after newline
    // But the NFA expects the entire input to match, not a substring
    // So "first\nline" won't match ^line in our whole-string matching
}

#[test]
fn test_nfa_char_start_of_input_anchor() {
    // Build NFA for \Ahello
    let mut nfa = NFAChar::new();
    let q0 = nfa.start();
    let q1 = nfa.add_state(false);
    let q2 = nfa.add_state(false);
    let q3 = nfa.add_state(false);
    let q4 = nfa.add_state(false);
    let q5 = nfa.add_state(false);
    let q6 = nfa.add_state(true);

    // \A
    nfa.add_transition(q0, TransitionLabelChar::StartOfInput, q1);
    // hello
    nfa.add_transition_char(q1, 'h', q2);
    nfa.add_transition_char(q2, 'e', q3);
    nfa.add_transition_char(q3, 'l', q4);
    nfa.add_transition_char(q4, 'l', q5);
    nfa.add_transition_char(q5, 'o', q6);

    // \A only matches at absolute start, regardless of multiline mode
    assert!(nfa.accepts_with_flags("hello", true, false));
}

#[test]
fn test_nfa_char_end_of_input_anchor() {
    // Build NFA for hello\Z
    let mut nfa = NFAChar::new();
    let q0 = nfa.start();
    let q1 = nfa.add_state(false);
    let q2 = nfa.add_state(false);
    let q3 = nfa.add_state(false);
    let q4 = nfa.add_state(false);
    let q5 = nfa.add_state(false);
    let q6 = nfa.add_state(true);

    // hello
    nfa.add_transition_char(q0, 'h', q1);
    nfa.add_transition_char(q1, 'e', q2);
    nfa.add_transition_char(q2, 'l', q3);
    nfa.add_transition_char(q3, 'l', q4);
    nfa.add_transition_char(q4, 'o', q5);
    // \Z
    nfa.add_transition(q5, TransitionLabelChar::EndOfInput, q6);

    // \Z matches at end, optionally with trailing newline
    assert!(nfa.accepts("hello"));
    assert!(nfa.accepts("hello\n"));
}

#[test]
fn test_nfa_char_strict_end_of_input_anchor() {
    // Build NFA for hello\z
    let mut nfa = NFAChar::new();
    let q0 = nfa.start();
    let q1 = nfa.add_state(false);
    let q2 = nfa.add_state(false);
    let q3 = nfa.add_state(false);
    let q4 = nfa.add_state(false);
    let q5 = nfa.add_state(false);
    let q6 = nfa.add_state(true);

    // hello
    nfa.add_transition_char(q0, 'h', q1);
    nfa.add_transition_char(q1, 'e', q2);
    nfa.add_transition_char(q2, 'l', q3);
    nfa.add_transition_char(q3, 'l', q4);
    nfa.add_transition_char(q4, 'o', q5);
    // \z
    nfa.add_transition(q5, TransitionLabelChar::EndOfInputStrict, q6);

    // \z matches only at absolute end, no trailing newline
    assert!(nfa.accepts("hello"));
    assert!(!nfa.accepts("hello\n")); // \z doesn't allow trailing newline
}