naive_opt
The optimized naive string-search algorithm.
Features
- The naive string-searching algorithm
- Enhanced with 1-byte search like the libc++ and the libstd++ string::find
- Specializing in UTF-8 strings, which is a feature of rust
- The ASCII Stochastics search
- Support the zero overhead trait.
- minimum support: rustc 1.41.1 (f3e1a954d 2020-02-24)
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:
use ;
use ;
let haystack = "111 a 111b";
let needle = "a";
let r = string_search;
assert_eq!;
assert_eq!;
assert_eq!;
let v: = string_search_indices.collect;
assert_eq!;
let v: = string_rsearch_indices.collect;
assert_eq!;
Example trait: Search
use Search;
let haystack = "111 a 111b";
let needle = "a";
let r = haystack.search;
assert_eq!;
assert_eq!;
assert_eq!;
let v: = "abc345abc901abc".search_indices.collect;
assert_eq!;
let v: = "abc345abc901abc".rsearch_indices.collect;
assert_eq!;
Example trait: SearchIn
use SearchIn;
let haystack = "111 a 111b";
let needle = "a";
let r = needle.search_in;
assert_eq!;
assert_eq!;
assert_eq!;
Benchmark
- compile by rustc 1.53.0 (53cb7b09b 2021-06-17)
name |
bench:en |
bench:ja |
musl:en |
musl:ja |
|---|---|---|---|---|
| std_str_str | 597.500 uc | 486.830 uc | 612.780 uc | 494.020 uc |
| std_string_string | 596.120 uc | 484.470 uc | 621.090 uc | 521.630 uc |
| func_str_str | 92.700 uc | 111.850 uc | 91.712 uc | 113.740 uc |
| func_string_string | 92.046 uc | 111.630 uc | 91.629 uc | 114.720 uc |
| trait_str_str | 86.913 uc | 107.620 uc | 86.574 uc | 108.830 uc |
| trait_string_string | 86.268 uc | 107.420 uc | 87.603 uc | 107.440 uc |
| std_indices | 537.580 uc | 403.150 uc | 530.250 uc | 405.990 uc |
| func_indices | 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 |
- std is std::str::find()
usis micro seconds:enis english haystack.:jais japanese haystack.muslis x86_64-unknown-linux-musl- bench on intel Q6600 @ 2.40GHz
Changelogs
References
- my research: string searching algorithm
- my research: string find
- wikipedia: string searching algprithm
memx- rust crate for the fast mem lib