mirror of
https://github.com/martinvonz/jj.git
synced 2024-12-27 14:57:14 +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;
|
use itertools::Itertools;
|
||||||
|
|
||||||
pub fn find_line_ranges(text: &[u8]) -> Vec<Range<usize>> {
|
pub fn find_line_ranges(text: &[u8]) -> Vec<Range<usize>> {
|
||||||
let mut ranges = vec![];
|
text.split_inclusive(|b| *b == b'\n')
|
||||||
let mut start = 0;
|
.scan(0, |total, line| {
|
||||||
loop {
|
let start = *total;
|
||||||
match text[start..].iter().position(|b| *b == b'\n') {
|
*total += line.len();
|
||||||
None => {
|
Some(start..*total)
|
||||||
break;
|
})
|
||||||
}
|
.collect()
|
||||||
Some(i) => {
|
|
||||||
ranges.push(start..start + i + 1);
|
|
||||||
start += i + 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if start < text.len() {
|
|
||||||
ranges.push(start..text.len());
|
|
||||||
}
|
|
||||||
ranges
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_word_byte(b: u8) -> bool {
|
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>> {
|
pub fn find_nonword_ranges(text: &[u8]) -> Vec<Range<usize>> {
|
||||||
let mut ranges = vec![];
|
text.iter()
|
||||||
for (i, b) in text.iter().enumerate() {
|
.positions(|b| !is_word_byte(*b))
|
||||||
if !is_word_byte(*b) {
|
.map(|i| i..i + 1)
|
||||||
ranges.push(i..i + 1);
|
.collect()
|
||||||
}
|
|
||||||
}
|
|
||||||
ranges
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Histogram<'a> {
|
struct Histogram<'a> {
|
||||||
|
|
Loading…
Reference in a new issue