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
// Copyright 2020 Alexander Korn
//
// Licensed under the MIT license
/// A simple algorithm for matching a single pattern on a text returning the first occurrence.
///
/// Takes a `pattern`, a text `text` and a value `i0` specifying at which index
/// of the text it should start to search for the next occurence.
///
/// If the given text contains the given pattern, the algorithm returns the
/// index `i` of the first letter of the first occurrence with `i >= i0`.
/// If the pattern could not be found in the text, `None` is returned.
///
/// This algorithm terminates after finding one occurrence of the given pattern
/// in the given text. If you want to find all occurrences, consider using
/// [`naive_all`](naive_all) instead.
///
/// # Runtime
///
/// The worst case runtime is `O(n * m)`, with `n` being the length of the text
/// (perhaps starting at `i0`) and `m` being the length of the pattern.
///
/// # When to Use It
///
/// Probably never. This algorithm is the most simple approach to matching a
/// pattern on a text. Could be useful for comparing runtimes or correctness
/// with other algorithms.
///
/// # How It Works
///
/// The algorithm iterates over each index `i` of the text's characters starting
/// at `i0` and compares the following `m` characters starting at `i` with the
/// pattern, `m` being the length of the pattern.
///
/// After an occurrence has been found, the algorithm returns the index
/// marking the first character of the occurrence and therefore terminates.
/// A simple algorithm for matching a single pattern on a text returning all occurrences.
///
/// Takes a `pattern`, and text `text`.
///
/// If the given text contains the given pattern, the algorithm returns the
/// indexes of the first letters of all found occurrences.
/// If the pattern could not be found in the text, an empty vector is returned.
///
/// # When to Use It
///
/// Probably never. This algorithm is the most simple approach to matching a
/// pattern on a text. Could be useful for comparing runtimes or correctness
/// with other algorithms.
///
/// # How It Works
///
/// The algorithm calls the [`naive`](naive) algorithm starting with a the `i0`
/// parameter being `0`. After the `naive` algorithm returns, it calls that same
/// algorithm again with an `i0` parameter of the position in text right after
/// the found occurrence.