Skip to content

Commit db77543

Browse files
committed
refactor(uu/fmt): switch string outputs to byte slices for efficiency
- Changed `indent_str` field in `BreakArgs` to `indent: &[u8]` to avoid repeated UTF-8 conversions. - Updated `write_all` calls to pass `&s` instead of `s.as_bytes()` in fmt.rs and similar string/byteslicing in linebreak.rs. - Modified method signatures in parasplit.rs to accept `&[u8]` instead of `&str` for prefix matching, ensuring consistent byte-level operations without assuming valid UTF-8.
1 parent 2c617d4 commit db77543

File tree

3 files changed

+164
-98
lines changed

3 files changed

+164
-98
lines changed

src/uu/fmt/src/fmt.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ fn process_file(
234234
match para_result {
235235
Err(s) => {
236236
ostream
237-
.write_all(s.as_bytes())
237+
.write_all(&s)
238238
.map_err_context(|| translate!("fmt-error-failed-to-write-output"))?;
239239
ostream
240240
.write_all(b"\n")

src/uu/fmt/src/linebreak.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::parasplit::{ParaWords, Paragraph, WordInfo};
1414
struct BreakArgs<'a> {
1515
opts: &'a FmtOptions,
1616
init_len: usize,
17-
indent_str: &'a str,
17+
indent: &'a [u8],
1818
indent_len: usize,
1919
uniform: bool,
2020
ostream: &'a mut BufWriter<Stdout>,
@@ -59,27 +59,27 @@ pub fn break_lines(
5959
let p_init_len = winfo.word_nchars
6060
+ if opts.crown || opts.tagged {
6161
// handle "init" portion
62-
ostream.write_all(para.init_str.as_bytes())?;
62+
ostream.write_all(&para.init_str)?;
6363
para.init_len
6464
} else if !para.mail_header {
6565
// for non-(crown, tagged) that's the same as a normal indent
66-
ostream.write_all(p_indent.as_bytes())?;
66+
ostream.write_all(p_indent)?;
6767
p_indent_len
6868
} else {
6969
// except that mail headers get no indent at all
7070
0
7171
};
7272

7373
// write first word after writing init
74-
ostream.write_all(winfo.word.as_bytes())?;
74+
ostream.write_all(winfo.word)?;
7575

7676
// does this paragraph require uniform spacing?
7777
let uniform = para.mail_header || opts.uniform;
7878

7979
let mut break_args = BreakArgs {
8080
opts,
8181
init_len: p_init_len,
82-
indent_str: p_indent,
82+
indent: p_indent,
8383
indent_len: p_indent_len,
8484
uniform,
8585
ostream,
@@ -121,7 +121,7 @@ fn accum_words_simple<'a>(
121121
);
122122

123123
if l + wlen + slen > args.opts.width {
124-
write_newline(args.indent_str, args.ostream)?;
124+
write_newline(args.indent, args.ostream)?;
125125
write_with_spaces(&winfo.word[winfo.word_start..], 0, args.ostream)?;
126126
Ok((args.indent_len + winfo.word_nchars, winfo.ends_punct))
127127
} else {
@@ -146,7 +146,7 @@ fn break_knuth_plass<'a, T: Clone + Iterator<Item = &'a WordInfo<'a>>>(
146146
(false, false),
147147
|(mut prev_punct, mut fresh), &(next_break, break_before)| {
148148
if fresh {
149-
write_newline(args.indent_str, args.ostream)?;
149+
write_newline(args.indent, args.ostream)?;
150150
}
151151
// at each breakpoint, keep emitting words until we find the word matching this breakpoint
152152
for winfo in &mut iter {
@@ -167,7 +167,7 @@ fn break_knuth_plass<'a, T: Clone + Iterator<Item = &'a WordInfo<'a>>>(
167167
if std::ptr::eq(winfo, next_break) {
168168
// OK, we found the matching word
169169
if break_before {
170-
write_newline(args.indent_str, args.ostream)?;
170+
write_newline(args.indent, args.ostream)?;
171171
write_with_spaces(&winfo.word[winfo.word_start..], 0, args.ostream)?;
172172
} else {
173173
// breaking after this word, so that means "fresh" is true for the next iteration
@@ -186,7 +186,7 @@ fn break_knuth_plass<'a, T: Clone + Iterator<Item = &'a WordInfo<'a>>>(
186186
// after the last linebreak, write out the rest of the final line.
187187
for winfo in iter {
188188
if fresh {
189-
write_newline(args.indent_str, args.ostream)?;
189+
write_newline(args.indent, args.ostream)?;
190190
}
191191
let (slen, word) = slice_if_fresh(
192192
fresh,
@@ -474,13 +474,13 @@ fn compute_slen(uniform: bool, newline: bool, start: bool, punct: bool) -> usize
474474
/// Otherwise, compute `slen` and leave whitespace alone.
475475
fn slice_if_fresh(
476476
fresh: bool,
477-
word: &str,
477+
word: &[u8],
478478
start: usize,
479479
uniform: bool,
480480
newline: bool,
481481
sstart: bool,
482482
punct: bool,
483-
) -> (usize, &str) {
483+
) -> (usize, &[u8]) {
484484
if fresh {
485485
(0, &word[start..])
486486
} else {
@@ -489,14 +489,14 @@ fn slice_if_fresh(
489489
}
490490

491491
/// Write a newline and add the indent.
492-
fn write_newline(indent: &str, ostream: &mut BufWriter<Stdout>) -> std::io::Result<()> {
492+
fn write_newline(indent: &[u8], ostream: &mut BufWriter<Stdout>) -> std::io::Result<()> {
493493
ostream.write_all(b"\n")?;
494-
ostream.write_all(indent.as_bytes())
494+
ostream.write_all(indent)
495495
}
496496

497497
/// Write the word, along with slen spaces.
498498
fn write_with_spaces(
499-
word: &str,
499+
word: &[u8],
500500
slen: usize,
501501
ostream: &mut BufWriter<Stdout>,
502502
) -> std::io::Result<()> {
@@ -505,5 +505,5 @@ fn write_with_spaces(
505505
} else if slen == 1 {
506506
ostream.write_all(b" ")?;
507507
}
508-
ostream.write_all(word.as_bytes())
508+
ostream.write_all(word)
509509
}

0 commit comments

Comments
 (0)