mirror of
https://github.com/martinvonz/jj.git
synced 2024-12-27 06:27:43 +00:00
diff: rewrite find_line/nonword_ranges() by using iterator adapters
I don't think .scan() is descriptive name, but it looks slightly better than placing accumulator out of the closure.
This commit is contained in:
parent
5f2f13a876
commit
f8b87f6499
1 changed files with 11 additions and 24 deletions
|
@ -23,23 +23,13 @@ use std::slice;
|
|||
use itertools::Itertools;
|
||||
|
||||
pub fn find_line_ranges(text: &[u8]) -> Vec<Range<usize>> {
|
||||
let mut ranges = vec![];
|
||||
let mut start = 0;
|
||||
loop {
|
||||
match text[start..].iter().position(|b| *b == b'\n') {
|
||||
None => {
|
||||
break;
|
||||
}
|
||||
Some(i) => {
|
||||
ranges.push(start..start + i + 1);
|
||||
start += i + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
if start < text.len() {
|
||||
ranges.push(start..text.len());
|
||||
}
|
||||
ranges
|
||||
text.split_inclusive(|b| *b == b'\n')
|
||||
.scan(0, |total, line| {
|
||||
let start = *total;
|
||||
*total += line.len();
|
||||
Some(start..*total)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn is_word_byte(b: u8) -> bool {
|
||||
|
@ -73,13 +63,10 @@ pub fn find_word_ranges(text: &[u8]) -> Vec<Range<usize>> {
|
|||
}
|
||||
|
||||
pub fn find_nonword_ranges(text: &[u8]) -> Vec<Range<usize>> {
|
||||
let mut ranges = vec![];
|
||||
for (i, b) in text.iter().enumerate() {
|
||||
if !is_word_byte(*b) {
|
||||
ranges.push(i..i + 1);
|
||||
}
|
||||
}
|
||||
ranges
|
||||
text.iter()
|
||||
.positions(|b| !is_word_byte(*b))
|
||||
.map(|i| i..i + 1)
|
||||
.collect()
|
||||
}
|
||||
|
||||
struct Histogram<'a> {
|
||||
|
|
Loading…
Reference in a new issue