]> git.lizzy.rs Git - rust.git/commitdiff
Reduce reliance on `to_str_radix`
authorBrendan Zabarauskas <bjzaba@yahoo.com.au>
Fri, 21 Feb 2014 16:53:18 +0000 (03:53 +1100)
committerBrendan Zabarauskas <bjzaba@yahoo.com.au>
Fri, 21 Feb 2014 16:56:16 +0000 (03:56 +1100)
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}`

src/doc/complement-cheatsheet.md
src/librustc/middle/ty.rs
src/librustc/middle/typeck/infer/to_str.rs
src/libstd/hash.rs
src/libstd/io/mod.rs
src/libstd/num/int_macros.rs
src/libstd/num/uint_macros.rs
src/libstd/repr.rs
src/libsyntax/print/pprust.rs
src/libterm/terminfo/parm.rs

index a2d75fc95d1a289a5d43b217d22baec9de4a07e3..79f33a88562737653dfcecbe990013d9869b97a6 100644 (file)
@@ -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**
index f3016ff68a80213b8dfe1e71eeb2db503058265c..09c21d54c87a0ce3cc9d1b1652d5ca32a47e49d0 100644 (file)
@@ -2024,7 +2024,7 @@ fn sub(&self, other: &TypeContents) -> TypeContents {
 
 impl ToStr for TypeContents {
     fn to_str(&self) -> ~str {
-        format!("TypeContents({})", self.bits.to_str_radix(2))
+        format!("TypeContents({:t})", self.bits)
     }
 }
 
index d604d68dd54d7f6af4950d80ba372e23e3d4798a..aa3e3a23182e0ea0d1daa44fdd45cb91d9760e46 100644 (file)
@@ -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)
         }
     }
 }
index 4163d1e0c96e8d94cffbdba914bd0c731e0b6482..432e27257f355fe9f3e9ce5f250e9447059dab01 100644 (file)
@@ -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;
@@ -281,7 +280,7 @@ fn result_str(&mut self) -> ~str {
         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
     }
@@ -391,7 +390,7 @@ fn test_siphash() {
         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
         }
index db5ee09fa14c93cbf77b41df7f53a16318a7eb60..f09dc3da9bfe9d31c0edca2f133cb4de72c52fec 100644 (file)
@@ -857,12 +857,12 @@ fn write_char(&mut self, c: char) -> IoResult<()> {
 
     /// 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).
index 3ecc6f32017ea77b727c1c847ce62b2054394828..16af826eba80e35dc86e513de2060a73aa89fbc5 100644 (file)
@@ -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)
     }
 }
 
index 4fc30b43895e41f570ac81a2c5c46f840d28abf0..e4cac0960c09d8e5bf816dcc22f1400d95fe928b 100644 (file)
@@ -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)
     }
 }
 
index d367aaa6dc49e305e0fd5c6a16331fccb3d4db58..680ac11230021b8a377d54e20959f42d22a1fa27 100644 (file)
@@ -60,19 +60,13 @@ fn write_repr(&self, writer: &mut io::Writer) -> io::IoResult<()> {
 
 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)
     }
 }))
 
index 21e1998208c96b99e0a1b9cad35923354d3fcd3f..3b1386021d23209801f95dc779cdb7076b22bdf7 100644 (file)
@@ -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))
       }
index 0491a567f15020845235cc73876777ea03a70d48..948e79b4481052e583383dd465f631aee58349e8 100644 (file)
@@ -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 {
@@ -480,68 +479,49 @@ fn to_char(self) -> char {
 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 {