Crates.io | every-range |
lib.rs | every-range |
version | 0.1.0 |
source | src |
created_at | 2020-03-03 03:09:16.357209 |
updated_at | 2020-03-03 03:09:16.357209 |
description | Iterator for interleaving included and excluded ranges |
homepage | |
repository | https://github.com/vallentin/every-range |
max_upload_size | |
id | 214731 |
size | 19,325 |
This crate implements an extension to Iterator
,
which features an every_range
method
on any Iterator
with an Item
of Range<usize>
.
EveryRangeIter
iterates over Range
s and "fill in"
missing ranges, i.e. the gap between two consecutive ranges.
The original ranges and the generated ones,
can be distinguished by the Included
and
Excluded
enum variants.
EveryRangeIter
is useful when the ranges being iterated
are related to substrings that later are used to replaced
parts in a string.
Add this to your Cargo.toml
:
[dependencies]
every-range = "0.1"
Release notes are available in the repo at CHANGELOG.md.
use every_range::EveryRange;
// Lets use text as an example, but it could be anything
let text = "Foo rust-lang.org Bar
Baz crates.io Qux";
// Get some ranges from somewhere
let ranges = vec![
4..17, // "rust-lang.org"
26..35, // "crates.io"
];
// `text.len()` tells `EveryRange` the end, so it knows
// whether to produce an extra range after or not
let iter = ranges.into_iter().every_range(text.len());
// The input `ranges` result in `Included` every other range is `Excluded`
for (kind, range) in iter {
println!("{:?} {:>2?} - {:?}", kind, range.clone(), &text[range]);
}
This will output the following:
Excluded 0.. 4 - "Foo "
Included 4..17 - "rust-lang.org"
Excluded 17..26 - " Bar\nBaz "
Included 26..35 - "crates.io"
Excluded 35..39 - " Qux"
Using every_range
it is easy to collect ranges or
substring into a String
.
use std::borrow::Cow;
use every_range::{EveryRange, EveryRangeKind};
let text = "Foo rust-lang.org Bar
Baz crates.io Qux";
// For URLs input ranges could be produced by linkify
let ranges = vec![
4..17, // "rust-lang.org"
26..35, // "crates.io"
];
let output = ranges
.into_iter()
.every_range(text.len())
.map(|(kind, range)| {
if kind == EveryRangeKind::Included {
let url = &text[range];
format!("<a href=\"{0}\">{0}</a>", url).into()
} else {
Cow::Borrowed(&text[range])
}
})
.collect::<Vec<_>>()
.concat();
println!("{}", output);
This will output the following:
Foo <a href="rust-lang.org">rust-lang.org</a> Bar
Baz <a href="crates.io">crates.io</a> Qux