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() |

Ignore ascii case match

This crate supports an ASCII case-insensitive match with each function.

| this crate | |:---------------------------------------------------------| | naive_opt::Search::search_ignore_ascii_case() | | naive_opt::Search::rsearch_ignore_ascii_case() | | naive_opt::Search::includes_ignore_ascii_case() | | naive_opt::Search::search_indices_ignore_ascii_case() | | naive_opt::Search::rsearch_indices_ignore_ascii_case() |

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)); ```

Example Ignore ascii case match

```rust use naive_opt::Search;

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

let v: Vec<_> = "abc345aBc901abc".searchindicesignoreasciicase("abc").collect(); asserteq!(v, [(0, "abc"), (6, "aBc"), (12, "abc")]); let v: Vec<_> = "abc345aBc901abc".rsearchindicesignoreasciicase("abc").collect(); asserteq!(v, [(12, "abc"), (6, "aBc"), (0, "abc")]);

asserteq!("ignoreasciicase("href"), true); ```

Benchmark results

| name | bench:en | bench:ja | musl:en | musl:ja | |:------------------------|------------:|------------:|------------:|------------:| | stdstrstr | 748.440 μc | 520.660 μc | 753.480 μc | 508.910 μc | | stdstringstring | 756.080 μc | 520.690 μc | 757.250 μc | 508.680 μc | | funcstrstr | 92.168 μc | 111.290 μc | 110.980 μc | 110.710 μc | | funcstringstring | 92.432 μc | 112.260 μc | 93.275 μc | 110.790 μc | | traitstrstr | 87.074 μc | 107.730 μc | 107.540 μc | 106.330 μc | | traitstringstring | 87.373 μc | 107.170 μc | 88.310 μc | 106.250 μc | | stdindices | 470.500 μc | 371.010 μc | 468.910 μc | 374.780 μc | | funcindices | 92.939 μc | 108.660 μc | 92.794 μc | 124.560 μc | | trait_indices | 93.142 μc | 108.450 μc | 92.940 μc | 122.630 μc |

| name | bench:en | bench:ja | musl:en | musl:ja | |:------------------------|------------:|------------:|------------:|------------:| | stdstrstr | 597.500 uc | 486.830 uc | 612.780 uc | 494.020 uc | | stdstringstring | 596.120 uc | 484.470 uc | 621.090 uc | 521.630 uc | | funcstrstr | 92.700 uc | 111.850 uc | 91.712 uc | 113.740 uc | | funcstringstring | 92.046 uc | 111.630 uc | 91.629 uc | 114.720 uc | | traitstrstr | 86.913 uc | 107.620 uc | 86.574 uc | 108.830 uc | | traitstringstring | 86.268 uc | 107.420 uc | 87.603 uc | 107.440 uc | | stdindices | 537.580 uc | 403.150 uc | 530.250 uc | 405.990 uc | | funcindices | 87.310 uc | 108.470 uc | 87.587 uc | 109.770 uc | | trait_indices | 87.383 uc | 107.750 uc | 87.895 uc | 109.070 uc |

Changelogs

This crate's changelog here.

References

License

This project is licensed under either of

at your option.