Crates.io | kmpsearch |
lib.rs | kmpsearch |
version | 1.0.0 |
source | src |
created_at | 2019-02-17 20:52:39.572945 |
updated_at | 2019-02-23 15:38:21.160474 |
description | String/Byte pattern searching within byte slices or strings, using the Knuth Morris Pratt algorithm. |
homepage | |
repository | https://github.com/RemieRichards/kmpsearch-rs |
max_upload_size | |
id | 115470 |
size | 11,926 |
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
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]);