Skip to content

Commit

Permalink
Reduce reliance on to_str_radix
Browse files Browse the repository at this point in the history
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}`
  • Loading branch information
brendanzab committed Feb 21, 2014
1 parent e37327b commit 6943acd
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 97 deletions.
9 changes: 5 additions & 4 deletions src/doc/complement-cheatsheet.md
Expand Up @@ -22,13 +22,14 @@ let y: int = x.unwrap();

**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**
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/ty.rs
Expand Up @@ -2024,7 +2024,7 @@ impl ops::Sub<TypeContents,TypeContents> for TypeContents {

impl ToStr for TypeContents {
fn to_str(&self) -> ~str {
format!("TypeContents({})", self.bits.to_str_radix(2))
format!("TypeContents({:t})", self.bits)
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/librustc/middle/typeck/infer/to_str.rs
Expand Up @@ -70,8 +70,7 @@ impl<V:Vid + ToStr,T:InferStr> InferStr for VarValue<V, T> {
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)
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/libstd/hash.rs
Expand Up @@ -29,7 +29,6 @@
use container::Container;
use io::{Writer, IoResult};
use iter::Iterator;
use num::ToStrRadix;
use option::{Some, None};
use result::Ok;
use str::OwnedStr;
Expand Down Expand Up @@ -281,7 +280,7 @@ impl Streaming for SipState {
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
}
Expand Down Expand Up @@ -391,7 +390,7 @@ mod tests {
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
}
Expand Down
4 changes: 2 additions & 2 deletions src/libstd/io/mod.rs
Expand Up @@ -857,12 +857,12 @@ pub trait Writer {

/// 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).
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/num/int_macros.rs
Expand Up @@ -401,7 +401,7 @@ impl ToStr for $T {
/// Convert to a string in base 10.
#[inline]
fn to_str(&self) -> ~str {
self.to_str_radix(10)
format!("{:d}", *self)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/libstd/num/uint_macros.rs
Expand Up @@ -245,7 +245,7 @@ impl ToStr for $T {
/// Convert to a string in base 10.
#[inline]
fn to_str(&self) -> ~str {
self.to_str_radix(10u)
format!("{:u}", *self)
}
}

Expand Down
10 changes: 2 additions & 8 deletions src/libstd/repr.rs
Expand Up @@ -60,19 +60,13 @@ impl Repr for bool {

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)
}
}))

Expand Down
21 changes: 3 additions & 18 deletions src/libsyntax/print/pprust.rs
Expand Up @@ -2267,29 +2267,14 @@ pub fn print_literal(s: &mut State, lit: &ast::Lit) -> io::IoResult<()> {
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))
}
Expand Down
94 changes: 37 additions & 57 deletions src/libterm/terminfo/parm.rs
Expand Up @@ -12,7 +12,6 @@

use std::{char, vec};
use std::mem::replace;
use std::num::strconv::{SignNone,SignNeg,SignAll,int_to_str_bytes_common};

#[deriving(Eq)]
enum States {
Expand Down Expand Up @@ -480,68 +479,49 @@ impl FormatOp {
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 {
Expand Down

5 comments on commit 6943acd

@bors
Copy link
Contributor

@bors bors commented on 6943acd Feb 22, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from alexcrichton
at brendanzab@6943acd

@bors
Copy link
Contributor

@bors bors commented on 6943acd Feb 22, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging bjz/rust/fmt-int = 6943acd into auto

@bors
Copy link
Contributor

@bors bors commented on 6943acd Feb 22, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bjz/rust/fmt-int = 6943acd merged ok, testing candidate = d2f73ab

@bors
Copy link
Contributor

@bors bors commented on 6943acd Feb 22, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 6943acd Feb 22, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = d2f73ab

Please sign in to comment.