1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-30 12:37:49 +00:00

uniq: avoid copying Strings all the time

This commit is contained in:
Simon Vandel Sillesen 2018-07-12 11:03:11 +02:00
parent d624dbcfa0
commit f233d8e5d7

View file

@ -44,6 +44,10 @@ struct Uniq {
zero_terminated: bool, zero_terminated: bool,
} }
fn iter_ne<'a>(lhs: Box<Iterator<Item = char> + 'a>, rhs: Box<Iterator<Item = char> + 'a>) -> bool {
lhs.ne(rhs)
}
impl Uniq { impl Uniq {
pub fn print_uniq<R: Read, W: Write>( pub fn print_uniq<R: Read, W: Write>(
&self, &self,
@ -57,7 +61,7 @@ impl Uniq {
for io_line in reader.split(line_terminator) { for io_line in reader.split(line_terminator) {
let line = String::from_utf8(crash_if_err!(1, io_line)).unwrap(); let line = String::from_utf8(crash_if_err!(1, io_line)).unwrap();
if !lines.is_empty() && self.cmp_key(&lines[0]) != self.cmp_key(&line) { if !lines.is_empty() && self.cmp_keys(&lines[0], &line) {
let print_delimiter = delimiters == &Delimiters::Prepend let print_delimiter = delimiters == &Delimiters::Prepend
|| (delimiters == &Delimiters::Separate && first_line_printed); || (delimiters == &Delimiters::Separate && first_line_printed);
first_line_printed |= self.print_lines(writer, &lines, print_delimiter); first_line_printed |= self.print_lines(writer, &lines, print_delimiter);
@ -103,21 +107,22 @@ impl Uniq {
} }
} }
fn cmp_key(&self, line: &str) -> String { fn cmp_key<'a>(&'a self, line: &'a str) -> Box<Iterator<Item = char> + 'a> {
let fields_to_check = self.skip_fields(line); let fields_to_check = self.skip_fields(line);
let len = fields_to_check.len(); let len = fields_to_check.len();
if len > 0 { if len > 0 {
fields_to_check Box::new(
.chars() fields_to_check
.skip(self.slice_start.unwrap_or(0)) .chars()
.take(self.slice_stop.unwrap_or(len)) .skip(self.slice_start.unwrap_or(0))
.map(|c| match c { .take(self.slice_stop.unwrap_or(len))
'a'...'z' if self.ignore_case => ((c as u8) - 32) as char, .map(move |c| match c {
_ => c, 'a'...'z' if self.ignore_case => ((c as u8) - 32) as char,
}) _ => c,
.collect() }),
)
} else { } else {
fields_to_check.to_owned() Box::new(fields_to_check.chars())
} }
} }