Skip to content

Commit 6943acd

Browse files
committed
Reduce reliance on to_str_radix
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}`
1 parent e37327b commit 6943acd

File tree

10 files changed

+55
-97
lines changed

10 files changed

+55
-97
lines changed

src/doc/complement-cheatsheet.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@ let y: int = x.unwrap();
2222

2323
**Int to string, in non-base-10**
2424

25-
Use [`ToStrRadix`](http://static.rust-lang.org/doc/master/std/num/trait.ToStrRadix.html).
25+
Use the `format!` syntax extension.
2626

2727
~~~
28-
use std::num::ToStrRadix;
29-
3028
let x: int = 42;
31-
let y: ~str = x.to_str_radix(16);
29+
let y: ~str = format!("{:t}", x); // binary
30+
let y: ~str = format!("{:o}", x); // octal
31+
let y: ~str = format!("{:x}", x); // lowercase hexadecimal
32+
let y: ~str = format!("{:X}", x); // uppercase hexidecimal
3233
~~~
3334

3435
**String to int, in non-base-10**

src/librustc/middle/ty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2024,7 +2024,7 @@ impl ops::Sub<TypeContents,TypeContents> for TypeContents {
20242024

20252025
impl ToStr for TypeContents {
20262026
fn to_str(&self) -> ~str {
2027-
format!("TypeContents({})", self.bits.to_str_radix(2))
2027+
format!("TypeContents({:t})", self.bits)
20282028
}
20292029
}
20302030

src/librustc/middle/typeck/infer/to_str.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@ impl<V:Vid + ToStr,T:InferStr> InferStr for VarValue<V, T> {
7070
fn inf_str(&self, cx: &InferCtxt) -> ~str {
7171
match *self {
7272
Redirect(ref vid) => format!("Redirect({})", vid.to_str()),
73-
Root(ref pt, rk) => format!("Root({}, {})", pt.inf_str(cx),
74-
rk.to_str_radix(10u))
73+
Root(ref pt, rk) => format!("Root({}, {})", pt.inf_str(cx), rk)
7574
}
7675
}
7776
}

src/libstd/hash.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
use container::Container;
3030
use io::{Writer, IoResult};
3131
use iter::Iterator;
32-
use num::ToStrRadix;
3332
use option::{Some, None};
3433
use result::Ok;
3534
use str::OwnedStr;
@@ -281,7 +280,7 @@ impl Streaming for SipState {
281280
let r = self.result_bytes();
282281
let mut s = ~"";
283282
for b in r.iter() {
284-
s.push_str((*b as uint).to_str_radix(16u));
283+
s.push_str(format!("{:x}", *b));
285284
}
286285
s
287286
}
@@ -391,7 +390,7 @@ mod tests {
391390
fn to_hex_str(r: &[u8, ..8]) -> ~str {
392391
let mut s = ~"";
393392
for b in r.iter() {
394-
s.push_str((*b as uint).to_str_radix(16u));
393+
s.push_str(format!("{:x}", *b));
395394
}
396395
s
397396
}

src/libstd/io/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -857,12 +857,12 @@ pub trait Writer {
857857

858858
/// Write the result of passing n through `int::to_str_bytes`.
859859
fn write_int(&mut self, n: int) -> IoResult<()> {
860-
int::to_str_bytes(n, 10u, |bytes| self.write(bytes))
860+
write!(self, "{:d}", n)
861861
}
862862

863863
/// Write the result of passing n through `uint::to_str_bytes`.
864864
fn write_uint(&mut self, n: uint) -> IoResult<()> {
865-
uint::to_str_bytes(n, 10u, |bytes| self.write(bytes))
865+
write!(self, "{:u}", n)
866866
}
867867

868868
/// Write a little-endian uint (number of bytes depends on system).

src/libstd/num/int_macros.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ impl ToStr for $T {
401401
/// Convert to a string in base 10.
402402
#[inline]
403403
fn to_str(&self) -> ~str {
404-
self.to_str_radix(10)
404+
format!("{:d}", *self)
405405
}
406406
}
407407

src/libstd/num/uint_macros.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ impl ToStr for $T {
245245
/// Convert to a string in base 10.
246246
#[inline]
247247
fn to_str(&self) -> ~str {
248-
self.to_str_radix(10u)
248+
format!("{:u}", *self)
249249
}
250250
}
251251

src/libstd/repr.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,13 @@ impl Repr for bool {
6060

6161
impl Repr for int {
6262
fn write_repr(&self, writer: &mut io::Writer) -> io::IoResult<()> {
63-
::int::to_str_bytes(*self, 10u, |bits| {
64-
writer.write(bits)
65-
})
63+
write!(writer, "{}", *self)
6664
}
6765
}
6866

6967
macro_rules! int_repr(($ty:ident, $suffix:expr) => (impl Repr for $ty {
7068
fn write_repr(&self, writer: &mut io::Writer) -> io::IoResult<()> {
71-
::$ty::to_str_bytes(*self, 10u, |bits| {
72-
writer.write(bits).and_then(|()| {
73-
writer.write(bytes!($suffix))
74-
})
75-
})
69+
write!(writer, "{}{}", *self, $suffix)
7670
}
7771
}))
7872

src/libsyntax/print/pprust.rs

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2267,29 +2267,14 @@ pub fn print_literal(s: &mut State, lit: &ast::Lit) -> io::IoResult<()> {
22672267
word(&mut s.s, res)
22682268
}
22692269
ast::LitInt(i, t) => {
2270-
if i < 0_i64 {
2271-
word(&mut s.s,
2272-
~"-" + (-i as u64).to_str_radix(10u)
2273-
+ ast_util::int_ty_to_str(t))
2274-
} else {
2275-
word(&mut s.s,
2276-
(i as u64).to_str_radix(10u)
2277-
+ ast_util::int_ty_to_str(t))
2278-
}
2270+
word(&mut s.s, format!("{}{}", i, ast_util::int_ty_to_str(t)))
22792271
}
22802272
ast::LitUint(u, t) => {
2281-
word(&mut s.s,
2282-
u.to_str_radix(10u)
2283-
+ ast_util::uint_ty_to_str(t))
2273+
word(&mut s.s, format!("{}{}", u, ast_util::uint_ty_to_str(t)))
22842274
}
22852275
ast::LitIntUnsuffixed(i) => {
2286-
if i < 0_i64 {
2287-
word(&mut s.s, ~"-" + (-i as u64).to_str_radix(10u))
2288-
} else {
2289-
word(&mut s.s, (i as u64).to_str_radix(10u))
2290-
}
2276+
word(&mut s.s, format!("{}", i))
22912277
}
2292-
22932278
ast::LitFloat(ref f, t) => {
22942279
word(&mut s.s, f.get() + ast_util::float_ty_to_str(t))
22952280
}

src/libterm/terminfo/parm.rs

Lines changed: 37 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
1313
use std::{char, vec};
1414
use std::mem::replace;
15-
use std::num::strconv::{SignNone,SignNeg,SignAll,int_to_str_bytes_common};
1615

1716
#[deriving(Eq)]
1817
enum States {
@@ -480,68 +479,49 @@ impl FormatOp {
480479
fn format(val: Param, op: FormatOp, flags: Flags) -> Result<~[u8],~str> {
481480
let mut s = match val {
482481
Number(d) => {
482+
let mut s = match (op, flags.sign) {
483+
(FormatDigit, true) => format!("{:+d}", d).into_bytes(),
484+
(FormatDigit, false) => format!("{:d}", d).into_bytes(),
485+
(FormatOctal, _) => format!("{:o}", d).into_bytes(),
486+
(FormatHex, _) => format!("{:x}", d).into_bytes(),
487+
(FormatHEX, _) => format!("{:X}", d).into_bytes(),
488+
(FormatString, _) => return Err(~"non-number on stack with %s"),
489+
};
490+
if flags.precision > s.len() {
491+
let mut s_ = vec::with_capacity(flags.precision);
492+
let n = flags.precision - s.len();
493+
s_.grow(n, &('0' as u8));
494+
s_.push_all_move(s);
495+
s = s_;
496+
}
497+
assert!(!s.is_empty(), "string conversion produced empty result");
483498
match op {
484-
FormatString => {
485-
return Err(~"non-number on stack with %s")
499+
FormatDigit => {
500+
if flags.space && !(s[0] == '-' as u8 || s[0] == '+' as u8) {
501+
s.unshift(' ' as u8);
502+
}
486503
}
487-
_ => {
488-
let radix = match op {
489-
FormatDigit => 10,
490-
FormatOctal => 8,
491-
FormatHex|FormatHEX => 16,
492-
FormatString => unreachable!()
493-
};
494-
let mut s = ~[];
495-
match op {
496-
FormatDigit => {
497-
let sign = if flags.sign { SignAll } else { SignNeg };
498-
int_to_str_bytes_common(d, radix, sign, |c| {
499-
s.push(c);
500-
})
501-
}
502-
_ => {
503-
int_to_str_bytes_common(d as uint, radix, SignNone, |c| {
504-
s.push(c);
505-
})
506-
}
507-
};
508-
if flags.precision > s.len() {
509-
let mut s_ = vec::with_capacity(flags.precision);
510-
let n = flags.precision - s.len();
511-
s_.grow(n, &('0' as u8));
512-
s_.push_all_move(s);
513-
s = s_;
504+
FormatOctal => {
505+
if flags.alternate && s[0] != '0' as u8 {
506+
s.unshift('0' as u8);
514507
}
515-
assert!(!s.is_empty(), "string conversion produced empty result");
516-
match op {
517-
FormatDigit => {
518-
if flags.space && !(s[0] == '-' as u8 || s[0] == '+' as u8) {
519-
s.unshift(' ' as u8);
520-
}
521-
}
522-
FormatOctal => {
523-
if flags.alternate && s[0] != '0' as u8 {
524-
s.unshift('0' as u8);
525-
}
526-
}
527-
FormatHex => {
528-
if flags.alternate {
529-
let s_ = replace(&mut s, ~['0' as u8, 'x' as u8]);
530-
s.push_all_move(s_);
531-
}
532-
}
533-
FormatHEX => {
534-
s = s.into_ascii().to_upper().into_bytes();
535-
if flags.alternate {
536-
let s_ = replace(&mut s, ~['0' as u8, 'X' as u8]);
537-
s.push_all_move(s_);
538-
}
539-
}
540-
FormatString => unreachable!()
508+
}
509+
FormatHex => {
510+
if flags.alternate {
511+
let s_ = replace(&mut s, ~['0' as u8, 'x' as u8]);
512+
s.push_all_move(s_);
513+
}
514+
}
515+
FormatHEX => {
516+
s = s.into_ascii().to_upper().into_bytes();
517+
if flags.alternate {
518+
let s_ = replace(&mut s, ~['0' as u8, 'X' as u8]);
519+
s.push_all_move(s_);
541520
}
542-
s
543521
}
522+
FormatString => unreachable!()
544523
}
524+
s
545525
}
546526
String(s) => {
547527
match op {

0 commit comments

Comments
 (0)