2 releases (1 stable)
1.0.0 | Feb 23, 2019 |
---|---|
0.1.0 | Feb 17, 2019 |
#2470 in Algorithms
11KB
181 lines
kmpsearch
Allows searching for bytes or strings within byte slices or strings, using the Knuth Morris Pratt algorithm. KMP runs in O(n+h) time broken down as O(n) preparation, O(h) matching, where n is the length of the needle and h the length of the haystack.
Special thanks to PJB3005 for the help with AsRef which makes this library nicer to use. This is my first rust crate, so any feedback is welcome. Pull requests are accepted on github.
Usage:
Matching strings in strings
if "Hello World!".contains_needle("World") {
println!("Matches!");
} else {
println!("Doesn't match!");
}
Matching bytes in bytes
if b"DEADBEEF".contains_needle(b"BEEF") {
println!("Matches!");
} else {
println!("Doesn't match!");
}
Getting all the indexes of a string in another string
let res = "That fox is a cool fox, he knows magic".indexesof_needle("fox");
assert_eq!(res.unwrap(), vec![5, 19]);