#search

naive_opt

The optimized naive string-search algorithm

14 releases

0.1.15 Sep 10, 2021
0.1.14 Sep 10, 2021
0.1.13 Jul 6, 2021
0.1.12 Jun 20, 2021
0.1.5 Mar 20, 2021

#98 in Algorithms

Download history 21/week @ 2021-05-29 35/week @ 2021-06-05 8/week @ 2021-06-12 24/week @ 2021-06-19 6/week @ 2021-06-26 26/week @ 2021-07-03 10/week @ 2021-07-10 23/week @ 2021-07-17 27/week @ 2021-07-24 38/week @ 2021-07-31 22/week @ 2021-08-07 34/week @ 2021-08-14 17/week @ 2021-08-21 2/week @ 2021-08-28 16/week @ 2021-09-04 34/week @ 2021-09-11

83 downloads per month
Used in 4 crates (2 directly)

MIT/Apache

34KB
627 lines

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 naive_opt::{string_search, string_rsearch};
use naive_opt::{string_search_indices, string_rsearch_indices};

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

assert_eq!(string_search(haystack, "1"), Some(0));
assert_eq!(string_rsearch(haystack, "1"), Some(8));

let v: Vec<_> = string_search_indices("abc345abc901abc", "abc").collect();
assert_eq!(v, [(0, "abc"), (6, "abc"), (12, "abc")]);
let v: Vec<_> = string_rsearch_indices("abc345abc901abc", "abc").collect();
assert_eq!(v, [(12, "abc"), (6, "abc"), (0, "abc")]);

Example trait: Search

use naive_opt::Search;

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

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

let v: Vec<_> = "abc345abc901abc".search_indices("abc").collect();
assert_eq!(v, [(0, "abc"), (6, "abc"), (12, "abc")]);
let v: Vec<_> = "abc345abc901abc".rsearch_indices("abc").collect();
assert_eq!(v, [(12, "abc"), (6, "abc"), (0, "abc")]);

Example trait: SearchIn

use naive_opt::SearchIn;

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

assert_eq!("1".search_in(haystack), Some(0));
assert_eq!("1".rsearch_in(haystack), Some(8));

Benchmark results

  • 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()
  • us is micro seconds
  • :en is english haystack.
  • :ja is japanese haystack.
  • musl is x86_64-unknown-linux-musl
  • bench on intel Q6600 @ 2.40GHz

Changelogs

This crate's changelog here.

References

License

This project is licensed under either of

at your option.

Dependencies

~250KB