Skip to content

Commit

Permalink
auto merge of #5607 : brson/rust/shapshot3, r=brson
Browse files Browse the repository at this point in the history
  • Loading branch information
bors committed Mar 28, 2013
2 parents 86024e3 + 145f66f commit e16f94f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 189 deletions.
189 changes: 0 additions & 189 deletions src/libcore/unstable/extfmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,9 +470,6 @@ pub mod ct {
// decisions made a runtime. If it proves worthwhile then some of these
// conditions can be evaluated at compile-time. For now though it's cleaner to
// implement it this way, I think.
#[cfg(stage1)]
#[cfg(stage2)]
#[cfg(stage3)]
#[doc(hidden)]
pub mod rt {
use float;
Expand Down Expand Up @@ -676,192 +673,6 @@ pub mod rt {
}
}

// XXX: remove after a snapshot of the above changes have gone in
#[cfg(stage0)]
#[doc(hidden)]
pub mod rt {
use float;
use str;
use sys;
use uint;
use vec;

pub static flag_none : u32 = 0u32;
pub static flag_left_justify : u32 = 0b00000000000001u32;
pub static flag_left_zero_pad : u32 = 0b00000000000010u32;
pub static flag_space_for_sign : u32 = 0b00000000000100u32;
pub static flag_sign_always : u32 = 0b00000000001000u32;
pub static flag_alternate : u32 = 0b00000000010000u32;

pub enum Count { CountIs(uint), CountImplied, }

pub enum Ty { TyDefault, TyBits, TyHexUpper, TyHexLower, TyOctal, }

pub struct Conv {
flags: u32,
width: Count,
precision: Count,
ty: Ty,
}

pub fn conv_int(cv: Conv, i: int) -> ~str {
let radix = 10;
let prec = get_int_precision(cv);
let mut s : ~str = int_to_str_prec(i, radix, prec);
if 0 <= i {
if have_flag(cv.flags, flag_sign_always) {
unsafe { str::unshift_char(&mut s, '+') };
} else if have_flag(cv.flags, flag_space_for_sign) {
unsafe { str::unshift_char(&mut s, ' ') };
}
}
return unsafe { pad(cv, s, PadSigned) };
}
pub fn conv_uint(cv: Conv, u: uint) -> ~str {
let prec = get_int_precision(cv);
let mut rs =
match cv.ty {
TyDefault => uint_to_str_prec(u, 10, prec),
TyHexLower => uint_to_str_prec(u, 16, prec),
TyHexUpper => str::to_upper(uint_to_str_prec(u, 16, prec)),
TyBits => uint_to_str_prec(u, 2, prec),
TyOctal => uint_to_str_prec(u, 8, prec)
};
return unsafe { pad(cv, rs, PadUnsigned) };
}
pub fn conv_bool(cv: Conv, b: bool) -> ~str {
let s = if b { ~"true" } else { ~"false" };
// run the boolean conversion through the string conversion logic,
// giving it the same rules for precision, etc.
return conv_str(cv, s);
}
pub fn conv_char(cv: Conv, c: char) -> ~str {
let mut s = str::from_char(c);
return unsafe { pad(cv, s, PadNozero) };
}
pub fn conv_str(cv: Conv, s: &str) -> ~str {
// For strings, precision is the maximum characters
// displayed
let mut unpadded = match cv.precision {
CountImplied => s.to_owned(),
CountIs(max) => if (max as uint) < str::char_len(s) {
str::substr(s, 0, max as uint).to_owned()
} else {
s.to_owned()
}
};
return unsafe { pad(cv, unpadded, PadNozero) };
}
pub fn conv_float(cv: Conv, f: float) -> ~str {
let (to_str, digits) = match cv.precision {
CountIs(c) => (float::to_str_exact, c as uint),
CountImplied => (float::to_str_digits, 6u)
};
let mut s = unsafe { to_str(f, digits) };
if 0.0 <= f {
if have_flag(cv.flags, flag_sign_always) {
s = ~"+" + s;
} else if have_flag(cv.flags, flag_space_for_sign) {
s = ~" " + s;
}
}
return unsafe { pad(cv, s, PadFloat) };
}
pub fn conv_poly<T>(cv: Conv, v: &T) -> ~str {
let s = sys::log_str(v);
return conv_str(cv, s);
}
// Convert an int to string with minimum number of digits. If precision is
// 0 and num is 0 then the result is the empty string.
pub fn int_to_str_prec(num: int, radix: uint, prec: uint) -> ~str {
return if num < 0 {
~"-" + uint_to_str_prec(-num as uint, radix, prec)
} else { uint_to_str_prec(num as uint, radix, prec) };
}
// Convert a uint to string with a minimum number of digits. If precision
// is 0 and num is 0 then the result is the empty string. Could move this
// to uint: but it doesn't seem all that useful.
pub fn uint_to_str_prec(num: uint, radix: uint,
prec: uint) -> ~str {
return if prec == 0u && num == 0u {
~""
} else {
let s = uint::to_str_radix(num, radix);
let len = str::char_len(s);
if len < prec {
let diff = prec - len;
let pad = str::from_chars(vec::from_elem(diff, '0'));
pad + s
} else { s }
};
}
pub fn get_int_precision(cv: Conv) -> uint {
return match cv.precision {
CountIs(c) => c as uint,
CountImplied => 1u
};
}

#[deriving(Eq)]
pub enum PadMode { PadSigned, PadUnsigned, PadNozero, PadFloat }

pub fn pad(cv: Conv, s: ~str, mode: PadMode) -> ~str {
let mut s = s; // sadtimes
let uwidth : uint = match cv.width {
CountImplied => return (s),
CountIs(width) => { width as uint }
};
let strlen = str::char_len(s);
if uwidth <= strlen { return (s); }
let mut padchar = ' ';
let diff = uwidth - strlen;
if have_flag(cv.flags, flag_left_justify) {
let padstr = str::from_chars(vec::from_elem(diff, padchar));
return s + padstr;
}
let (might_zero_pad, signed) = match mode {
PadNozero => (false, true),
PadSigned => (true, true),
PadFloat => (true, true),
PadUnsigned => (true, false)
};
fn have_precision(cv: Conv) -> bool {
return match cv.precision { CountImplied => false, _ => true };
}
let zero_padding = {
if might_zero_pad && have_flag(cv.flags, flag_left_zero_pad) &&
(!have_precision(cv) || mode == PadFloat) {
padchar = '0';
true
} else {
false
}
};
let padstr = str::from_chars(vec::from_elem(diff, padchar));
// This is completely heinous. If we have a signed value then
// potentially rip apart the intermediate result and insert some
// zeros. It may make sense to convert zero padding to a precision
// instead.

if signed && zero_padding && s.len() > 0 {
let head = str::shift_char(&mut s);
if head == '+' || head == '-' || head == ' ' {
let headstr = str::from_chars(vec::from_elem(1u, head));
return headstr + padstr + s;
}
else {
str::unshift_char(&mut s, head);
}
}
return padstr + s;
}
pub fn have_flag(flags: u32, f: u32) -> bool {
flags & f != 0
}
}

// Bulk of the tests are in src/test/run-pass/syntax-extension-fmt.rs
#[cfg(test)]
mod test {
Expand Down
8 changes: 8 additions & 0 deletions src/snapshots.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
S 2013-03-27 8c15409
macos-x86_64 05eb3801b60056d95715c891d00c5d372e34d00c
macos-i386 4119e3fa614fa86adf60ed0183d00db3ce6d0dbc
linux-x86_64 e9308bade0e068bca4abe59ef4afe8c8bb7c134d
linux-i386 bcb30ed1817df1a07588cde2fb0ccaf9ffad7efb
winnt-i386 a16d409465e125bc6f779b45821ae1ea276c6bd4
freebsd-x86_64 348192f348f03541549f4e2c97af78901d59ca6e

S 2013-03-21 ed25a67
freebsd-x86_64 5f0b08839ae3d1207808f0d57cbfdb00eff9c883
linux-i386 54765a17c6b6d04a7013cada2a51d190462979b8
Expand Down

0 comments on commit e16f94f

Please sign in to comment.