Skip to content

Simplify num formatting helpers #143540

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 8, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 3 additions & 22 deletions library/core/src/num/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,7 @@ impl<'a> Part<'a> {
pub fn len(&self) -> usize {
match *self {
Part::Zero(nzeroes) => nzeroes,
Part::Num(v) => {
if v < 1_000 {
if v < 10 {
1
} else if v < 100 {
2
} else {
3
}
} else {
if v < 10_000 { 4 } else { 5 }
}
}
Part::Num(v) => v.checked_ilog10().unwrap_or_default() as usize + 1,
Part::Copy(buf) => buf.len(),
}
}
Expand Down Expand Up @@ -82,21 +70,14 @@ pub struct Formatted<'a> {
impl<'a> Formatted<'a> {
/// Returns the exact byte length of combined formatted result.
pub fn len(&self) -> usize {
let mut len = self.sign.len();
for part in self.parts {
len += part.len();
}
len
self.sign.len() + self.parts.iter().map(|part| part.len()).sum::<usize>()
}

/// Writes all formatted parts into the supplied buffer.
/// Returns the number of written bytes, or `None` if the buffer is not enough.
/// (It may still leave partially written bytes in the buffer; do not rely on that.)
pub fn write(&self, out: &mut [u8]) -> Option<usize> {
if out.len() < self.sign.len() {
return None;
}
out[..self.sign.len()].copy_from_slice(self.sign.as_bytes());
out.get_mut(..self.sign.len())?.copy_from_slice(self.sign.as_bytes());

let mut written = self.sign.len();
for part in self.parts {
Expand Down
Loading