From f8b87f64995107943bb12f8a6757f6b62177f0a9 Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Wed, 19 Jun 2024 16:55:16 +0900 Subject: [PATCH] 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. --- lib/src/diff.rs | 35 +++++++++++------------------------ 1 file changed, 11 insertions(+), 24 deletions(-) diff --git a/lib/src/diff.rs b/lib/src/diff.rs index 076f0557d2..6015b27892 100644 --- a/lib/src/diff.rs +++ b/lib/src/diff.rs @@ -23,23 +23,13 @@ use std::slice; use itertools::Itertools; pub fn find_line_ranges(text: &[u8]) -> Vec> { - 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> { } pub fn find_nonword_ranges(text: &[u8]) -> Vec> { - 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> {