Skip to content

Commit

Permalink
files: replace Vec + index access with iterator in DiffLineIterator
Browse files Browse the repository at this point in the history
  • Loading branch information
yuja committed Aug 15, 2024
1 parent 54f5c01 commit f857922
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions lib/src/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

use std::collections::VecDeque;
use std::fmt::{Debug, Error, Formatter};
use std::{iter, vec};

use crate::diff;
use crate::diff::{Diff, DiffHunk};
Expand Down Expand Up @@ -50,8 +51,7 @@ pub fn diff<'a>(left: &'a [u8], right: &'a [u8]) -> DiffLineIterator<'a> {
}

pub struct DiffLineIterator<'a> {
diff_hunks: Vec<DiffHunk<'a>>,
current_pos: usize,
diff_hunks: iter::Fuse<vec::IntoIter<DiffHunk<'a>>>,
current_line: DiffLine<'a>,
queued_lines: VecDeque<DiffLine<'a>>,
}
Expand All @@ -66,8 +66,7 @@ impl<'a> DiffLineIterator<'a> {
hunks: vec![],
};
DiffLineIterator {
diff_hunks,
current_pos: 0,
diff_hunks: diff_hunks.into_iter().fuse(),
current_line,
queued_lines: VecDeque::new(),
}
Expand All @@ -80,10 +79,11 @@ impl<'a> Iterator for DiffLineIterator<'a> {
fn next(&mut self) -> Option<Self::Item> {
// TODO: Should we attempt to interpret as utf-8 and otherwise break only at
// newlines?
while self.current_pos < self.diff_hunks.len() && self.queued_lines.is_empty() {
let hunk = &self.diff_hunks[self.current_pos];
self.current_pos += 1;
match hunk {
while self.queued_lines.is_empty() {
let Some(hunk) = self.diff_hunks.next() else {
break;
};
match &hunk {
DiffHunk::Matching(text) => {
let lines = text.split_inclusive(|b| *b == b'\n');
for line in lines {
Expand Down

0 comments on commit f857922

Please sign in to comment.