Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions src/uu/csplit/src/csplit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#![allow(rustdoc::private_intra_doc_links)]

use std::cmp::Ordering;
use std::collections::VecDeque;
use std::ffi::OsString;
use std::io::{self, BufReader, ErrorKind};
use std::{
Expand Down Expand Up @@ -530,7 +531,7 @@ where
I: Iterator<Item = (usize, UResult<String>)>,
{
iter: I,
buffer: Vec<<I as Iterator>::Item>,
buffer: VecDeque<<I as Iterator>::Item>,
/// the number of elements the buffer may hold
size: usize,
/// flag to indicate content off the buffer should be returned instead of off the wrapped
Expand All @@ -545,7 +546,7 @@ where
fn new(iter: I) -> Self {
Self {
iter,
buffer: Vec::new(),
buffer: VecDeque::new(),
rewind: false,
size: 1,
}
Expand Down Expand Up @@ -584,14 +585,14 @@ where
/// option.
fn add_line_to_buffer(&mut self, ln: usize, line: String) -> Option<String> {
if self.rewind {
self.buffer.insert(0, (ln, Ok(line)));
self.buffer.push_front((ln, Ok(line)));
None
} else if self.buffer.len() >= self.size {
let (_, head_line) = self.buffer.remove(0);
self.buffer.push((ln, Ok(line)));
let (_, head_line) = self.buffer.pop_front().unwrap();
self.buffer.push_back((ln, Ok(line)));
Some(head_line.unwrap())
} else {
self.buffer.push((ln, Ok(line)));
self.buffer.push_back((ln, Ok(line)));
None
}
}
Expand All @@ -611,7 +612,7 @@ where
fn next(&mut self) -> Option<Self::Item> {
if self.rewind {
if !self.buffer.is_empty() {
return Some(self.buffer.remove(0));
return Some(self.buffer.pop_front().unwrap());
}
self.rewind = false;
}
Expand Down
Loading