Skip to content

Instantly share code, notes, and snippets.

@jmcph4
Created December 14, 2025 04:53
Show Gist options
  • Select an option

  • Save jmcph4/d18c544e840274ea70f71def175077aa to your computer and use it in GitHub Desktop.

Select an option

Save jmcph4/d18c544e840274ea70f71def175077aa to your computer and use it in GitHub Desktop.
use rayon::prelude::*;
pub fn search(haystack: &[(Url, String)], needle: &str) -> Vec<Finding> {
if needle.is_empty() {
return Vec::new();
}
haystack
.par_iter()
.flat_map_iter(|(url, text)| {
text.lines()
.enumerate()
.flat_map(move |(line_idx, line)| {
let mut out = Vec::new();
let mut search_start = 0;
while let Some(pos) = line[search_start..].find(needle) {
let column = search_start + pos;
out.push(Finding {
url: url.clone(),
position: TextPosition {
line: line_idx + 1,
column: column + 1,
},
});
search_start = column + needle.len();
if search_start > line.len() {
break;
}
}
out
})
})
.collect()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment