naive_opt

The optimized naive string-search algorithm.

Features

Compatibility

This crate is implemented to replace the rust std library. However, the method names are different, so please rewrite your code. It shouldn't be too difficult.

compatibility: |:-----------------------------|:---------------------------------------| | rust std::str | this crate | |:-----------------------------|:---------------------------------------| | std::str::find() | naive_opt::Search::search() | | std::str::rfind() | naive_opt::Search::rsearch() | | std::str::contains() | naive_opt::Search::includes() | | std::str::match_indices() | naive_opt::Search::search_indices() | | std::str::rmatch_indices() | naive_opt::Search::rsearch_indices() |

Examples

Example function:

```rust use naiveopt::{stringsearch, stringrsearch}; use naiveopt::{stringsearchindices, stringrsearchindices};

let haystack = "111 a 111b"; let needle = "a"; let r = stringsearch(haystack, needle); asserteq!(r, Some(4));

asserteq!(stringsearch(haystack, "1"), Some(0)); asserteq!(stringrsearch(haystack, "1"), Some(8));

let v: Vec<_> = stringsearchindices("abc345abc901abc", "abc").collect(); asserteq!(v, [(0, "abc"), (6, "abc"), (12, "abc")]); let v: Vec<_> = stringrsearchindices("abc345abc901abc", "abc").collect(); asserteq!(v, [(12, "abc"), (6, "abc"), (0, "abc")]); ```

Example trait: Search

```rust use naive_opt::Search;

let haystack = "111 a 111b"; let needle = "a"; let r = haystack.search(needle); assert_eq!(r, Some(4));

asserteq!(haystack.search("1"), Some(0)); asserteq!(haystack.rsearch("1"), Some(8));

let v: Vec<_> = "abc345abc901abc".searchindices("abc").collect(); asserteq!(v, [(0, "abc"), (6, "abc"), (12, "abc")]); let v: Vec<_> = "abc345abc901abc".rsearchindices("abc").collect(); asserteq!(v, [(12, "abc"), (6, "abc"), (0, "abc")]); ```

Example trait: SearchIn

```rust use naive_opt::SearchIn;

let haystack = "111 a 111b"; let needle = "a"; let r = needle.searchin(haystack); asserteq!(r, Some(4));

asserteq!("1".searchin(haystack), Some(0)); asserteq!("1".rsearchin(haystack), Some(8)); ```

Benchmark

| name | bench:en | bench:ja | musl:en | musl:ja | |:------------------------|------------:|------------:|------------:|------------:| | stdstrstr | 445.560 uc | 355.440 uc | 458.540 uc | 359.400 uc | | stdstringstring | 450.900 uc | 355.110 uc | 453.940 uc | 354.490 uc | | funcstrstr | 57.928 uc | 56.888 uc | 59.602 uc | 59.365 uc | | funcstringstring | 56.754 uc | 56.721 uc | 59.276 uc | 59.016 uc | | traitstrstr | 50.825 uc | 49.719 uc | 52.659 uc | 52.919 uc | | traitstringstring | 49.898 uc | 49.951 uc | 52.263 uc | 52.721 uc | | stdindices | 349.200 uc | 269.560 uc | 373.310 uc | 267.570 uc | | funcindices | 51.955 uc | 52.269 uc | 55.074 uc | 54.534 uc | | trait_indices | 52.235 uc | 52.201 uc | 54.549 uc | 54.589 uc |

Changelogs

This crate's changelog here.

References

my research: string searching algorithm my research: string find wikipedia: string searching algprithm