naive_opt 0.1.10

The optimized naive string-search algorithm.
Documentation

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
  • 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

  • compile by rustc 1.52.1 (9bc8c42bb 2021-05-09)
name bench:en bench:ja musl:en musl:ja
std_str_str 539.660 uc 478.930 uc 524.480 uc 479.960 uc
std_string_string 534.660 uc 481.690 uc 525.550 uc 470.390 uc
func_str_str 104.010 uc 103.600 uc 106.680 uc 104.950 uc
func_string_string 103.910 uc 104.140 uc 106.180 uc 105.840 uc
trait_str_str 97.082 uc 96.958 uc 98.712 uc 97.831 uc
trait_string_string 96.175 uc 96.488 uc 99.185 uc 99.046 uc
std_indices 457.130 uc 409.460 uc 459.220 uc 405.300 uc
func_indices 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
  • 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