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
356 downloads per month
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