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 | 585.580 uc | 578.320 uc | 615.830 uc | 479.560 uc | | stdstringstring | 585.960 uc | 535.340 uc | 617.810 uc | 489.860 uc | | funcstrstr | 85.994 uc | 106.520 uc | 87.228 uc | 111.430 uc | | funcstringstring | 85.223 uc | 104.990 uc | 86.863 uc | 111.340 uc | | traitstrstr | 81.330 uc | 100.390 uc | 82.727 uc | 103.500 uc | | traitstringstring | 80.777 uc | 100.920 uc | 81.788 uc | 102.520 uc | | stdindices | 527.490 uc | 402.170 uc | 514.510 uc | 394.700 uc | | funcindices | 81.626 uc | 101.900 uc | 82.144 uc | 104.220 uc | | trait_indices | 81.608 uc | 101.920 uc | 82.037 uc | 104.050 uc |

Changelogs

This crate's changelog here.

References