Skip to content

Commit 3016496

Browse files
oech3cakebaker
authored andcommitted
numfmt: stop a clone
1 parent 9e26f3c commit 3016496

File tree

3 files changed

+11
-11
lines changed

3 files changed

+11
-11
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/uu/numfmt/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ clap = { workspace = true }
2323
uucore = { workspace = true, features = ["parser", "ranges"] }
2424
thiserror = { workspace = true }
2525
fluent = { workspace = true }
26+
memchr = { workspace = true }
2627

2728
[dev-dependencies]
2829
divan = { workspace = true }

src/uu/numfmt/src/numfmt.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -87,21 +87,19 @@ fn write_line<W: std::io::Write>(
8787
eol: Option<u8>,
8888
) -> UResult<()> {
8989
// Read lines only up to null byte (as GNU does)
90-
let line = input_line
91-
.iter()
92-
.take_while(|&&b| b != b'\0')
93-
.copied()
94-
.collect::<Vec<u8>>();
95-
90+
let line = match memchr::memchr(b'\0', input_line) {
91+
Some(i) => &input_line[..i],
92+
None => input_line,
93+
};
9694
let handled_line = if options.delimiter.is_some() {
97-
write_formatted_with_delimiter(writer, &line, options, eol)
95+
write_formatted_with_delimiter(writer, line, options, eol)
9896
} else {
9997
// Whitespace mode requires valid UTF-8
100-
match std::str::from_utf8(&line) {
98+
match std::str::from_utf8(line) {
10199
Ok(s) => write_formatted_with_whitespace(writer, s, options, eol),
102-
Err(_) => Err(
103-
translate!("numfmt-error-invalid-number", "input" => escape_line(&line).quote()),
104-
),
100+
Err(_) => {
101+
Err(translate!("numfmt-error-invalid-number", "input" => escape_line(line).quote()))
102+
}
105103
}
106104
};
107105

0 commit comments

Comments
 (0)