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 | 539.660 uc | 478.930 uc | 524.480 uc | 479.960 uc | | stdstringstring | 534.660 uc | 481.690 uc | 525.550 uc | 470.390 uc | | funcstrstr | 104.010 uc | 103.600 uc | 106.680 uc | 104.950 uc | | funcstringstring | 103.910 uc | 104.140 uc | 106.180 uc | 105.840 uc | | traitstrstr | 97.082 uc | 96.958 uc | 98.712 uc | 97.831 uc | | traitstringstring | 96.175 uc | 96.488 uc | 99.185 uc | 99.046 uc | | stdindices | 457.130 uc | 409.460 uc | 459.220 uc | 405.300 uc | | funcindices | 104.190 uc | 120.680 uc | 103.010 uc | 122.950 uc | | trait_indices | 103.770 uc | 120.560 uc | 103.290 uc | 122.650 uc |

Changelogs

This crate's changelog here.

References