Specialize to_str_common for floats/integers in strconv

This allows the integral paths to avoid allocations on the heap

Closes #4424, #4423
This commit is contained in:
Alex Crichton 2013-06-28 14:05:10 -07:00
parent 8fe6fc11de
commit d3155faede
10 changed files with 250 additions and 197 deletions

View file

@ -11,7 +11,7 @@
//! Parameterized string expansion
use std::{char, vec, util};
use std::num::strconv::{SignNone,SignNeg,SignAll,DigAll,to_str_bytes_common};
use std::num::strconv::{SignNone,SignNeg,SignAll,int_to_str_bytes_common};
use std::iterator::IteratorUtil;
#[deriving(Eq)]
@ -469,14 +469,20 @@ priv fn format(val: Param, op: FormatOp, flags: Flags) -> Result<~[u8],~str> {
FormatHex|FormatHEX => 16,
FormatString => util::unreachable()
};
let (s,_) = match op {
let mut s = ~[];
match op {
FormatDigit => {
let sign = if flags.sign { SignAll } else { SignNeg };
to_str_bytes_common(&d, radix, false, sign, DigAll)
do int_to_str_bytes_common(d, radix, sign) |c| {
s.push(c);
}
}
_ => {
do int_to_str_bytes_common(d as uint, radix, SignNone) |c| {
s.push(c);
}
}
_ => to_str_bytes_common(&(d as uint), radix, false, SignNone, DigAll)
};
let mut s = s;
if flags.precision > s.len() {
let mut s_ = vec::with_capacity(flags.precision);
let n = flags.precision - s.len();