Skip to content

Commit 42cde76

Browse files
committed
printf: Change get_char and write_padded to handle bytes instead of chars
1 parent 5d74a6e commit 42cde76

File tree

3 files changed

+23
-28
lines changed

3 files changed

+23
-28
lines changed

src/uucore/src/lib/features/format/argument.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,22 @@ pub enum FormatArgument {
3131
}
3232

3333
pub trait ArgumentIter<'a>: Iterator<Item = &'a FormatArgument> {
34-
fn get_char(&mut self) -> char;
34+
fn get_char(&mut self) -> u8;
3535
fn get_i64(&mut self) -> i64;
3636
fn get_u64(&mut self) -> u64;
3737
fn get_f64(&mut self) -> f64;
3838
fn get_str(&mut self) -> &'a str;
3939
}
4040

4141
impl<'a, T: Iterator<Item = &'a FormatArgument>> ArgumentIter<'a> for T {
42-
fn get_char(&mut self) -> char {
42+
fn get_char(&mut self) -> u8 {
4343
let Some(next) = self.next() else {
44-
return '\0';
44+
return b'\0';
4545
};
4646
match next {
47-
FormatArgument::Char(c) => *c,
48-
FormatArgument::Unparsed(s) => s.bytes().next().map_or('\0', char::from),
49-
_ => '\0',
47+
FormatArgument::Char(c) => *c as u8,
48+
FormatArgument::Unparsed(s) => s.bytes().next().unwrap_or(b'\0'),
49+
_ => b'\0',
5050
}
5151
}
5252

src/uucore/src/lib/features/format/spec.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use super::{
1414
},
1515
parse_escape_only, ArgumentIter, FormatChar, FormatError,
1616
};
17-
use std::{fmt::Display, io::Write, ops::ControlFlow};
17+
use std::{io::Write, ops::ControlFlow};
1818

1919
/// A parsed specification for formatting a value
2020
///
@@ -312,7 +312,7 @@ impl Spec {
312312
match self {
313313
Self::Char { width, align_left } => {
314314
let width = resolve_asterisk(*width, &mut args)?.unwrap_or(0);
315-
write_padded(writer, args.get_char(), width, false, *align_left)
315+
write_padded(writer, &[args.get_char()], width, *align_left)
316316
}
317317
Self::String {
318318
width,
@@ -333,7 +333,7 @@ impl Spec {
333333
Some(p) if p < s.len() => &s[..p],
334334
_ => s,
335335
};
336-
write_padded(writer, truncated, width, false, *align_left)
336+
write_padded(writer, truncated.as_bytes(), width, *align_left)
337337
}
338338
Self::EscapedString => {
339339
let s = args.get_str();
@@ -445,16 +445,17 @@ fn resolve_asterisk<'a>(
445445

446446
fn write_padded(
447447
mut writer: impl Write,
448-
text: impl Display,
448+
text: &[u8],
449449
width: usize,
450-
pad_zero: bool,
451450
left: bool,
452451
) -> Result<(), FormatError> {
453-
match (left, pad_zero) {
454-
(false, false) => write!(writer, "{text: >width$}"),
455-
(false, true) => write!(writer, "{text:0>width$}"),
456-
// 0 is ignored if we pad left.
457-
(true, _) => write!(writer, "{text: <width$}"),
452+
let padlen = width.saturating_sub(text.len());
453+
if left {
454+
writer.write_all(text)?;
455+
write!(writer, "{: <padlen$}", "")
456+
} else {
457+
write!(writer, "{: >padlen$}", "")?;
458+
writer.write_all(text)
458459
}
459460
.map_err(FormatError::IoError)
460461
}

tests/by-util/test_printf.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -743,31 +743,25 @@ fn pad_unsigned_three() {
743743

744744
#[test]
745745
fn pad_char() {
746-
for (format, expected) in [
747-
("%3c", " X"),
748-
("%1c", "X"),
749-
("%-1c", "X"),
750-
("%-3c", "X "),
751-
] {
746+
for (format, expected) in [("%3c", " X"), ("%1c", "X"), ("%-1c", "X"), ("%-3c", "X ")] {
752747
new_ucmd!()
753748
.args(&[format, "X"])
754749
.succeeds()
755750
.stdout_only(expected);
756751
}
757752
}
758753

759-
760754
#[test]
761755
fn pad_string() {
762756
for (format, expected) in [
763-
("%8s", " bottle"),
764-
("%-8s", "bottle "),
765-
("%6s", "bottle"),
766-
("%-6s", "bottle"),
757+
("%8s", " bottle"),
758+
("%-8s", "bottle "),
759+
("%6s", "bottle"),
760+
("%-6s", "bottle"),
767761
] {
768762
new_ucmd!()
769763
.args(&[format, "bottle"])
770764
.succeeds()
771765
.stdout_only(expected);
772766
}
773-
}
767+
}

0 commit comments

Comments
 (0)