Crate naive_opt

source ·
Expand description

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.
  • Support ignore ascii case match.
  • minimum support rustc 1.56.1 (59eed8a2a 2021-11-01)

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::strthis 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:

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")]);
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));

Example Ignore ascii case match

use naive_opt::Search;

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

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

assert_eq!("<A HREF=http://".includes_ignore_ascii_case("href"), true);

Structs

Traits

search the needle
search in the haystack

Functions

reverse search the needle in the haystack
reverse search the needle in the haystack bytes
reverse search the needle in the haystack bytes, ignore ascii case.
reverse search the needle in the haystack, ignore ascii case.
An reverse search iterator over the matches of the needle in the haystack.
search the needle in the haystack
search the needle in the haystack bytes
search the needle in the haystack bytes, ignore ascii case.
search the needle in the haystack, ignore ascii case.
An iterator over the matches of the needle in the haystack.