Skip to content

Commit

Permalink
Closer similarities.
Browse files Browse the repository at this point in the history
  • Loading branch information
gilescope committed Mar 8, 2021
1 parent 6a58b6a commit 05330aa
Showing 1 changed file with 16 additions and 26 deletions.
42 changes: 16 additions & 26 deletions library/alloc/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2228,50 +2228,40 @@ impl ToString for char {
impl ToString for u8 {
#[inline]
fn to_string(&self) -> String {
let mut result = String::with_capacity(3);
let mut buf = String::with_capacity(3);
let mut n = *self;
if n >= 100 {
result.push((b'0' + n / 100) as char);
n %= 100;
}
if !result.is_empty() || n >= 10 {
result.push((b'0' + n / 10) as char);
if n >= 10 {
if n >= 100 {
buf.push((b'0' + n / 100) as char);
n %= 100;
}
buf.push((b'0' + n / 10) as char);
n %= 10;
};
result.push((b'0' + n) as char);
result
}
buf.push((b'0' + n) as char);
buf
}
}

#[stable(feature = "i8_to_string_specialization", since = "1.999.0")]
impl ToString for i8 {
#[inline]
fn to_string(&self) -> String {
let mut vec = vec![0; 4];
let mut free = 0;
let mut buf = String::with_capacity(4);
if self.is_negative() {
vec[free] = b'-';
free += 1;
buf.push('-');
}
let mut n = self.unsigned_abs();
if n >= 10 {
if n >= 100 {
buf.push('1');
n -= 100;
vec[free] = b'1';
free += 1;
}
debug_assert!(n < 100);
vec[free] = b'0' + n / 10;
free += 1;
buf.push((b'0' + n / 10) as char);
n %= 10;
}
debug_assert!(n < 10);
vec[free] = b'0' + n;
free += 1;
vec.truncate(free);

// SAFETY: Vec only contains ascii so valid utf8
unsafe { String::from_utf8_unchecked(vec) }
buf.push((b'0' + n) as char);
buf
}
}

Expand Down

0 comments on commit 05330aa

Please sign in to comment.