#string #no-alloc #iterator #characters #substr #numbers #iterate

no-std substr-iterator

Substring extractor based on characters without allocation

4 releases

0.1.3 Jan 12, 2025
0.1.2 Jan 12, 2025
0.1.1 Jan 11, 2025
0.1.0 Jan 11, 2025

#861 in Encoding

Download history 345/week @ 2025-01-08 11/week @ 2025-01-15

356 downloads per month

MIT license

13KB
183 lines

Substr iterator

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

Usage

cargo add substr-iterator
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\",\"今天我\"]",
);

Dependencies

~100–325KB