This is in preparation to remove the implementations of ToStrRadix in integers, and to remove the associated logic from `std::num::strconv`.
The parts that still need to be liberated are:
- `std::fmt::Formatter::runplural`
- `num::{bigint, complex, rational}`
**Int to string, in non-base-10**
-Use [`ToStrRadix`](http://static.rust-lang.org/doc/master/std/num/trait.ToStrRadix.html).
+Use the `format!` syntax extension.
~~~
-use std::num::ToStrRadix;
-
let x: int = 42;
-let y: ~str = x.to_str_radix(16);
+let y: ~str = format!("{:t}", x); // binary
+let y: ~str = format!("{:o}", x); // octal
+let y: ~str = format!("{:x}", x); // lowercase hexadecimal
+let y: ~str = format!("{:X}", x); // uppercase hexidecimal
~~~
**String to int, in non-base-10**
impl ToStr for TypeContents {
fn to_str(&self) -> ~str {
- format!("TypeContents({})", self.bits.to_str_radix(2))
+ format!("TypeContents({:t})", self.bits)
}
}
fn inf_str(&self, cx: &InferCtxt) -> ~str {
match *self {
Redirect(ref vid) => format!("Redirect({})", vid.to_str()),
- Root(ref pt, rk) => format!("Root({}, {})", pt.inf_str(cx),
- rk.to_str_radix(10u))
+ Root(ref pt, rk) => format!("Root({}, {})", pt.inf_str(cx), rk)
}
}
}
use container::Container;
use io::{Writer, IoResult};
use iter::Iterator;
-use num::ToStrRadix;
use option::{Some, None};
use result::Ok;
use str::OwnedStr;
let r = self.result_bytes();
let mut s = ~"";
for b in r.iter() {
- s.push_str((*b as uint).to_str_radix(16u));
+ s.push_str(format!("{:x}", *b));
}
s
}
fn to_hex_str(r: &[u8, ..8]) -> ~str {
let mut s = ~"";
for b in r.iter() {
- s.push_str((*b as uint).to_str_radix(16u));
+ s.push_str(format!("{:x}", *b));
}
s
}
/// Write the result of passing n through `int::to_str_bytes`.
fn write_int(&mut self, n: int) -> IoResult<()> {
- int::to_str_bytes(n, 10u, |bytes| self.write(bytes))
+ write!(self, "{:d}", n)
}
/// Write the result of passing n through `uint::to_str_bytes`.
fn write_uint(&mut self, n: uint) -> IoResult<()> {
- uint::to_str_bytes(n, 10u, |bytes| self.write(bytes))
+ write!(self, "{:u}", n)
}
/// Write a little-endian uint (number of bytes depends on system).
/// Convert to a string in base 10.
#[inline]
fn to_str(&self) -> ~str {
- self.to_str_radix(10)
+ format!("{:d}", *self)
}
}
/// Convert to a string in base 10.
#[inline]
fn to_str(&self) -> ~str {
- self.to_str_radix(10u)
+ format!("{:u}", *self)
}
}
impl Repr for int {
fn write_repr(&self, writer: &mut io::Writer) -> io::IoResult<()> {
- ::int::to_str_bytes(*self, 10u, |bits| {
- writer.write(bits)
- })
+ write!(writer, "{}", *self)
}
}
macro_rules! int_repr(($ty:ident, $suffix:expr) => (impl Repr for $ty {
fn write_repr(&self, writer: &mut io::Writer) -> io::IoResult<()> {
- ::$ty::to_str_bytes(*self, 10u, |bits| {
- writer.write(bits).and_then(|()| {
- writer.write(bytes!($suffix))
- })
- })
+ write!(writer, "{}{}", *self, $suffix)
}
}))
word(&mut s.s, res)
}
ast::LitInt(i, t) => {
- if i < 0_i64 {
- word(&mut s.s,
- ~"-" + (-i as u64).to_str_radix(10u)
- + ast_util::int_ty_to_str(t))
- } else {
- word(&mut s.s,
- (i as u64).to_str_radix(10u)
- + ast_util::int_ty_to_str(t))
- }
+ word(&mut s.s, format!("{}{}", i, ast_util::int_ty_to_str(t)))
}
ast::LitUint(u, t) => {
- word(&mut s.s,
- u.to_str_radix(10u)
- + ast_util::uint_ty_to_str(t))
+ word(&mut s.s, format!("{}{}", u, ast_util::uint_ty_to_str(t)))
}
ast::LitIntUnsuffixed(i) => {
- if i < 0_i64 {
- word(&mut s.s, ~"-" + (-i as u64).to_str_radix(10u))
- } else {
- word(&mut s.s, (i as u64).to_str_radix(10u))
- }
+ word(&mut s.s, format!("{}", i))
}
-
ast::LitFloat(ref f, t) => {
word(&mut s.s, f.get() + ast_util::float_ty_to_str(t))
}
use std::{char, vec};
use std::mem::replace;
-use std::num::strconv::{SignNone,SignNeg,SignAll,int_to_str_bytes_common};
#[deriving(Eq)]
enum States {
fn format(val: Param, op: FormatOp, flags: Flags) -> Result<~[u8],~str> {
let mut s = match val {
Number(d) => {
+ let mut s = match (op, flags.sign) {
+ (FormatDigit, true) => format!("{:+d}", d).into_bytes(),
+ (FormatDigit, false) => format!("{:d}", d).into_bytes(),
+ (FormatOctal, _) => format!("{:o}", d).into_bytes(),
+ (FormatHex, _) => format!("{:x}", d).into_bytes(),
+ (FormatHEX, _) => format!("{:X}", d).into_bytes(),
+ (FormatString, _) => return Err(~"non-number on stack with %s"),
+ };
+ if flags.precision > s.len() {
+ let mut s_ = vec::with_capacity(flags.precision);
+ let n = flags.precision - s.len();
+ s_.grow(n, &('0' as u8));
+ s_.push_all_move(s);
+ s = s_;
+ }
+ assert!(!s.is_empty(), "string conversion produced empty result");
match op {
- FormatString => {
- return Err(~"non-number on stack with %s")
+ FormatDigit => {
+ if flags.space && !(s[0] == '-' as u8 || s[0] == '+' as u8) {
+ s.unshift(' ' as u8);
+ }
}
- _ => {
- let radix = match op {
- FormatDigit => 10,
- FormatOctal => 8,
- FormatHex|FormatHEX => 16,
- FormatString => unreachable!()
- };
- let mut s = ~[];
- match op {
- FormatDigit => {
- let sign = if flags.sign { SignAll } else { SignNeg };
- int_to_str_bytes_common(d, radix, sign, |c| {
- s.push(c);
- })
- }
- _ => {
- int_to_str_bytes_common(d as uint, radix, SignNone, |c| {
- s.push(c);
- })
- }
- };
- if flags.precision > s.len() {
- let mut s_ = vec::with_capacity(flags.precision);
- let n = flags.precision - s.len();
- s_.grow(n, &('0' as u8));
- s_.push_all_move(s);
- s = s_;
+ FormatOctal => {
+ if flags.alternate && s[0] != '0' as u8 {
+ s.unshift('0' as u8);
}
- assert!(!s.is_empty(), "string conversion produced empty result");
- match op {
- FormatDigit => {
- if flags.space && !(s[0] == '-' as u8 || s[0] == '+' as u8) {
- s.unshift(' ' as u8);
- }
- }
- FormatOctal => {
- if flags.alternate && s[0] != '0' as u8 {
- s.unshift('0' as u8);
- }
- }
- FormatHex => {
- if flags.alternate {
- let s_ = replace(&mut s, ~['0' as u8, 'x' as u8]);
- s.push_all_move(s_);
- }
- }
- FormatHEX => {
- s = s.into_ascii().to_upper().into_bytes();
- if flags.alternate {
- let s_ = replace(&mut s, ~['0' as u8, 'X' as u8]);
- s.push_all_move(s_);
- }
- }
- FormatString => unreachable!()
+ }
+ FormatHex => {
+ if flags.alternate {
+ let s_ = replace(&mut s, ~['0' as u8, 'x' as u8]);
+ s.push_all_move(s_);
+ }
+ }
+ FormatHEX => {
+ s = s.into_ascii().to_upper().into_bytes();
+ if flags.alternate {
+ let s_ = replace(&mut s, ~['0' as u8, 'X' as u8]);
+ s.push_all_move(s_);
}
- s
}
+ FormatString => unreachable!()
}
+ s
}
String(s) => {
match op {