Query Range
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.
Usage
Add this to your Cargo.toml:
[]
= "0.1.0"
Iteration
To use as an iterator:
use QueryRangeItr;
let query = "needle";
let content = "haystackneedlehaystackneedlehaystack";
let mut occurrences = new;
while let Some = occurrences.next
Collecting to an Array
This collects all ranges to an array of string indices.
use QueryRangeItr;
let query = "needle";
let content = "haystackneedlehaystackneedlehaystack";
let mut occurrences = new;
let needles: = occurrences.map.collect;
Collecting to Strings
This collects all ranges and extracts the string from the original content at those indices.
use QueryRangeItr;
let query = "needle";
let content = "haystackneedlehaystackneedlehaystack";
let mut occurrences = new;
let needles: = occurrences.collect_strings;
Transforming the query
If the end goal is performing a transform on all the found query, this is a convenience static method to do so.
use QueryRangeItr;
let query = "needle";
let content = "haystackneedlehaystackneedlehaystack";
let result = transform_query;
Reassembling the content
If you also need to transform the non-query content, you can with this static method:
use QueryRangeItr;
use to_title_case;
let query = "needle";
let content = "haystackneedlehaystackneedlehaystack";
let result = transform_all;