Remove code that was waiting for a snapshot
extfmt is now demoded
This commit is contained in:
parent
c37c243e95
commit
777baeb298
@ -1,4 +1,7 @@
|
||||
#[doc(hidden)];
|
||||
// NB: transitionary, de-mode-ing.
|
||||
#[forbid(deprecated_mode)];
|
||||
#[forbid(deprecated_pattern)];
|
||||
|
||||
/*
|
||||
Syntax Extension: fmt
|
||||
@ -463,202 +466,6 @@ pub mod rt {
|
||||
}
|
||||
}
|
||||
|
||||
// Remove after snapshot
|
||||
|
||||
// Functions used by the fmt extension at runtime. For now there are a lot of
|
||||
// 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 0this way, I think.
|
||||
pub mod rt2 {
|
||||
|
||||
pub const flag_none : u32 = 0u32;
|
||||
pub const flag_left_justify : u32 = 0b00000000000001u32;
|
||||
pub const flag_left_zero_pad : u32 = 0b00000000000010u32;
|
||||
pub const flag_space_for_sign : u32 = 0b00000000000100u32;
|
||||
pub const flag_sign_always : u32 = 0b00000000001000u32;
|
||||
pub const flag_alternate : u32 = 0b00000000010000u32;
|
||||
|
||||
pub enum Count { CountIs(int), CountImplied, }
|
||||
pub enum Ty { TyDefault, TyBits, TyHexUpper, TyHexLower, TyOctal, }
|
||||
|
||||
pub type Conv = {flags: u32, width: Count, precision: Count, ty: Ty};
|
||||
|
||||
pub pure 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 pure 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, 10u, prec),
|
||||
TyHexLower => uint_to_str_prec(u, 16u, prec),
|
||||
TyHexUpper => str::to_upper(uint_to_str_prec(u, 16u, prec)),
|
||||
TyBits => uint_to_str_prec(u, 2u, prec),
|
||||
TyOctal => uint_to_str_prec(u, 8u, prec)
|
||||
};
|
||||
return unsafe { pad(cv, rs, PadUnsigned) };
|
||||
}
|
||||
pub pure 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 pure fn conv_char(cv: Conv, c: char) -> ~str {
|
||||
let mut s = str::from_char(c);
|
||||
return unsafe { pad(cv, s, PadNozero) };
|
||||
}
|
||||
pub pure 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_unique(),
|
||||
CountIs(max) => if max as uint < str::char_len(s) {
|
||||
str::substr(s, 0u, max as uint)
|
||||
} else {
|
||||
s.to_unique()
|
||||
}
|
||||
};
|
||||
return unsafe { pad(cv, unpadded, PadNozero) };
|
||||
}
|
||||
pub pure 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, 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 pure 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 pure 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 pure fn uint_to_str_prec(num: uint, radix: uint,
|
||||
prec: uint) -> ~str {
|
||||
return if prec == 0u && num == 0u {
|
||||
~""
|
||||
} else {
|
||||
let s = uint::to_str(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 { move s }
|
||||
};
|
||||
}
|
||||
pub pure fn get_int_precision(cv: Conv) -> uint {
|
||||
return match cv.precision {
|
||||
CountIs(c) => c as uint,
|
||||
CountImplied => 1u
|
||||
};
|
||||
}
|
||||
|
||||
pub enum PadMode { PadSigned, PadUnsigned, PadNozero, PadFloat }
|
||||
|
||||
pub impl PadMode : Eq {
|
||||
pure fn eq(other: &PadMode) -> bool {
|
||||
match (self, (*other)) {
|
||||
(PadSigned, PadSigned) => true,
|
||||
(PadUnsigned, PadUnsigned) => true,
|
||||
(PadNozero, PadNozero) => true,
|
||||
(PadFloat, PadFloat) => true,
|
||||
(PadSigned, _) => false,
|
||||
(PadUnsigned, _) => false,
|
||||
(PadNozero, _) => false,
|
||||
(PadFloat, _) => false
|
||||
}
|
||||
}
|
||||
pure fn ne(other: &PadMode) -> bool { !self.eq(other) }
|
||||
}
|
||||
|
||||
pub fn pad(cv: Conv, s: ~str, mode: PadMode) -> ~str {
|
||||
let mut s = move s; // sadtimes
|
||||
let uwidth : uint = match cv.width {
|
||||
CountImplied => return s,
|
||||
CountIs(width) => {
|
||||
// FIXME: width should probably be uint (see Issue #1996)
|
||||
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 => {might_zero_pad:false, signed:false},
|
||||
PadSigned => {might_zero_pad:true, signed:true },
|
||||
PadFloat => {might_zero_pad:true, signed:true},
|
||||
PadUnsigned => {might_zero_pad:true, signed:false}
|
||||
};
|
||||
pure 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 pure fn have_flag(flags: u32, f: u32) -> bool {
|
||||
flags & f != 0
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
#[test]
|
||||
|
Loading…
x
Reference in New Issue
Block a user