1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-08-01 05:27:45 +00:00

uniq: do not allocate a new string in skip_fields

Instead, reuse the existing line and just view into that.
This commit is contained in:
Simon Vandel Sillesen 2018-07-12 00:00:22 +02:00
parent 32ec6646a3
commit d624dbcfa0

View file

@ -72,7 +72,7 @@ impl Uniq {
} }
} }
fn skip_fields(&self, line: &str) -> String { fn skip_fields<'a>(&self, line: &'a str) -> &'a str {
if let Some(skip_fields) = self.skip_fields { if let Some(skip_fields) = self.skip_fields {
if line.split_whitespace().count() > skip_fields { if line.split_whitespace().count() > skip_fields {
let mut field = 0; let mut field = 0;
@ -86,12 +86,12 @@ impl Uniq {
} }
field = field + 1; field = field + 1;
} }
line[i..].to_owned() &line[i..]
} else { } else {
"".to_owned() ""
} }
} else { } else {
line[..].to_owned() line
} }
} }
@ -104,7 +104,7 @@ impl Uniq {
} }
fn cmp_key(&self, line: &str) -> String { fn cmp_key(&self, line: &str) -> String {
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 fields_to_check