Crates.io | query-range |
lib.rs | query-range |
version | 0.1.0 |
source | src |
created_at | 2020-10-26 05:42:21.254347 |
updated_at | 2020-10-26 05:42:21.254347 |
description | This package provides an iterator which finds all ranges of a query within the searched content. |
homepage | https://github.com/reuschj/query-range |
repository | https://github.com/reuschj/query-range |
max_upload_size | |
id | 305498 |
size | 23,437 |
This package provides an iterator (conforming to Iterator
) which finds all ranges of a query within the searched content. The iterator can be collected to an Vec<Range<usize>>
, mapped or used manually (by calling the next()
method until no further result is returned).
This also exports several range utilities for use with strings.
Add this to your Cargo.toml:
[dependencies]
query-range = "0.1.0"
To use as an iterator:
use query_range::QueryRangeItr;
let query = "needle";
let content = "haystackneedlehaystackneedlehaystack";
let mut occurrences = QueryRangeItr::new(query, content);
while let Some(next) = occurrences.next() {
println!("{}", &content[next]);
}
This collects all ranges to an array of string indices.
use query_range::QueryRangeItr;
let query = "needle";
let content = "haystackneedlehaystackneedlehaystack";
let mut occurrences = QueryRangeItr::new(query, content);
let needles: Vec<String> = occurrences.map(|range| String::from(&content[range])).collect();
This collects all ranges and extracts the string from the original content at those indices.
use query_range::QueryRangeItr;
let query = "needle";
let content = "haystackneedlehaystackneedlehaystack";
let mut occurrences = QueryRangeItr::new(query, content);
let needles: Vec<String> = occurrences.collect_strings();
If the end goal is performing a transform on all the found query, this is a convenience static method to do so.
use query_range::QueryRangeItr;
let query = "needle";
let content = "haystackneedlehaystackneedlehaystack";
let result = QueryRangeItr::transform_query(query, content, |it| it.to_uppercase());
If you also need to transform the non-query content, you can with this static method:
use query_range::QueryRangeItr;
use query_range::utility::to_title_case;
let query = "needle";
let content = "haystackneedlehaystackneedlehaystack";
let result = QueryRangeItr::transform_all(
query,
content,
|it| it.to_uppercase(), // query transform
|it| to_title_case(it), // non-query transform
);