Crate substr_iterator

Source
Expand description

This library is made to iterate over a &str by a number of characters without allocating.

use substr_iterator::{Trigram, TrigramIter};

let mut iter = TrigramIter::from("whatever");
assert_eq!(iter.next(), Some(['w', 'h', 'a']));
let mut iter = TrigramIter::from("今天我吃饭");
assert_eq!(iter.next(), Some(['今', '天', '我']));

It’s also possible to handle bigger windows.

use substr_iterator::{Substr, SubstrIter};

let mut iter = SubstrIter::<2>::from("whatever");
assert_eq!(iter.next(), Some(['w', 'h']));
let mut iter = SubstrIter::<2>::from("今天我吃饭");
assert_eq!(iter.next(), Some(['今', '天']));

When the std feature is enabled, the SubstrWrapper allows to display Substr as a String.

use substr_iterator::{SubstrWrapper, Trigram, TrigramIter};

let mut iter = TrigramIter::from("whatever");
let item = SubstrWrapper(iter.next().unwrap());
assert_eq!(item.to_string(), "wha");

When the serde feature is enabled, the SubstrWrapper allows to serialize and deserialize.

use substr_iterator::{SubstrWrapper, Trigram, TrigramIter};

let data: Vec<SubstrWrapper<3>> = vec![
    SubstrWrapper(['a', 'b', 'c']),
    SubstrWrapper(['今', '天', '我']),
];
assert_eq!(
    serde_json::to_string(&data).unwrap(),
    "[\"abc\",\"今天我\"]",
);

Structs§

SubstrIter
This is an iterator that allows to take a number of characters out of a string and iterate like a window.
SubstrParserError
String parsing error for SubstrWrapper.
SubstrWrapper
Wrapper around Substr in order to bring extra capabilities.

Type Aliases§

Substr
A set of N characters stored as an array.
Trigram
A set of 3 characters stored as an array.
TrigramIter
An iterator for only 3 characters. This is just an alias to SubstrIter.