pub fn naive(pattern: &[u8], text: &[u8], i0: usize) -> Option<usize>Expand description
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 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.