about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMarvin Löbel <loebel.marvin@gmail.com>2013-01-24 21:47:57 +0100
committerBrian Anderson <banderson@mozilla.com>2013-02-03 15:37:24 -0800
commiteb194621044253fae32649511d76515a64009a53 (patch)
treea23632e8039ccea8f82d6e1ca2c09f8b82193494
parent26e72bf92bb0f9cf4d10a5edb07dbbd5c09f0e24 (diff)
downloadrust-eb194621044253fae32649511d76515a64009a53.tar.gz
rust-eb194621044253fae32649511d76515a64009a53.zip
Converted libcore/uint-template.rs to the new string functions.
- Moved ToStr implementation of unsigned integers to uint-template.rs.
- Marked the `str()` function as deprecated.
- Forwarded all conversion functions to `core::num::to_str_common()`
  and `core::num::from_str_common()`.
- Fixed most places in the codebase where `to_str()` is being used.
- Added uint-template to_str and from_str overflow tests.
-rw-r--r--src/libcore/char.rs2
-rw-r--r--src/libcore/extfmt.rs2
-rw-r--r--src/libcore/hash.rs4
-rw-r--r--src/libcore/io.rs2
-rw-r--r--src/libcore/num/int-template.rs16
-rw-r--r--src/libcore/num/uint-template.rs238
-rw-r--r--src/libcore/to_str.rs20
-rw-r--r--src/librustc/metadata/tyencode.rs4
-rw-r--r--src/librustc/middle/typeck/infer/to_str.rs2
-rw-r--r--src/libstd/bigint.rs2
-rw-r--r--src/libstd/md4.rs2
-rw-r--r--src/libstd/sha1.rs2
-rw-r--r--src/libsyntax/parse/lexer.rs4
-rw-r--r--src/libsyntax/print/pprust.rs10
14 files changed, 155 insertions, 155 deletions
diff --git a/src/libcore/char.rs b/src/libcore/char.rs
index 8f1f0b3666b..2fcd2332032 100644
--- a/src/libcore/char.rs
+++ b/src/libcore/char.rs
@@ -200,7 +200,7 @@ pub pure fn from_digit(num: uint, radix: uint) -> Option<char> {
  *   - chars above 0x10000 get 8-digit escapes: `\\UNNNNNNNN`
  */
 pub pure fn escape_unicode(c: char) -> ~str {
-    let s = u32::to_str(c as u32, 16u);
+    let s = u32::to_str_radix(c as u32, 16u);
     let (c, pad) = (if c <= '\xff' { ('x', 2u) }
                     else if c <= '\uffff' { ('u', 4u) }
                     else { ('U', 8u) });
diff --git a/src/libcore/extfmt.rs b/src/libcore/extfmt.rs
index e452735777e..4627c048015 100644
--- a/src/libcore/extfmt.rs
+++ b/src/libcore/extfmt.rs
@@ -596,7 +596,7 @@ pub mod rt {
         return if prec == 0u && num == 0u {
                 ~""
             } else {
-                let s = uint::to_str(num, radix);
+                let s = uint::to_str_radix(num, radix);
                 let len = str::char_len(s);
                 if len < prec {
                     let diff = prec - len;
diff --git a/src/libcore/hash.rs b/src/libcore/hash.rs
index d9e53811b65..d676570e1e6 100644
--- a/src/libcore/hash.rs
+++ b/src/libcore/hash.rs
@@ -354,7 +354,7 @@ impl &SipState : Streaming {
         let r = self.result_bytes();
         let mut s = ~"";
         for vec::each(r) |b| {
-            s += uint::to_str(*b as uint, 16u);
+            s += uint::to_str_radix(*b as uint, 16u);
         }
         move s
     }
@@ -449,7 +449,7 @@ pub fn test_siphash() {
     fn to_hex_str(r:  &[u8 * 8]) -> ~str {
         let mut s = ~"";
         for vec::each(*r) |b| {
-            s += uint::to_str(*b as uint, 16u);
+            s += uint::to_str_radix(*b as uint, 16u);
         }
         move s
     }
diff --git a/src/libcore/io.rs b/src/libcore/io.rs
index d2ddbc40885..b726cf62cfe 100644
--- a/src/libcore/io.rs
+++ b/src/libcore/io.rs
@@ -910,7 +910,7 @@ impl<T: Writer> T : WriterUtil {
         int::to_str_bytes(n, 10u, |bytes| self.write(bytes))
     }
     fn write_uint(&self, n: uint) {
-        uint::to_str_bytes(false, n, 10u, |bytes| self.write(bytes))
+        uint::to_str_bytes(n, 10u, |bytes| self.write(bytes))
     }
     fn write_le_uint(&self, n: uint) {
         u64_to_le_bytes(n as u64, uint::bytes, |v| self.write(v))
diff --git a/src/libcore/num/int-template.rs b/src/libcore/num/int-template.rs
index 27e3b0d04ea..6263a9d887b 100644
--- a/src/libcore/num/int-template.rs
+++ b/src/libcore/num/int-template.rs
@@ -353,25 +353,25 @@ fn test_to_str() {
 fn test_int_to_str_overflow() {
     let mut i8_val: i8 = 127_i8;
     assert (i8::to_str(i8_val) == ~"127");
-    
+
     i8_val += 1 as i8;
     assert (i8::to_str(i8_val) == ~"-128");
 
     let mut i16_val: i16 = 32_767_i16;
     assert (i16::to_str(i16_val) == ~"32767");
-    
+
     i16_val += 1 as i16;
     assert (i16::to_str(i16_val) == ~"-32768");
 
     let mut i32_val: i32 = 2_147_483_647_i32;
     assert (i32::to_str(i32_val) == ~"2147483647");
-    
+
     i32_val += 1 as i32;
     assert (i32::to_str(i32_val) == ~"-2147483648");
 
     let mut i64_val: i64 = 9_223_372_036_854_775_807_i64;
     assert (i64::to_str(i64_val) == ~"9223372036854775807");
-    
+
     i64_val += 1 as i64;
     assert (i64::to_str(i64_val) == ~"-9223372036854775808");
 }
@@ -380,25 +380,25 @@ fn test_int_to_str_overflow() {
 fn test_int_from_str_overflow() {
     let mut i8_val: i8 = 127_i8;
     assert (i8::from_str(~"127") == Some(i8_val));
-    
+
     i8_val += 1 as i8;
     assert (i8::from_str(~"-128") == Some(i8_val));
 
     let mut i16_val: i16 = 32_767_i16;
     assert (i16::from_str(~"32767") == Some(i16_val));
-    
+
     i16_val += 1 as i16;
     assert (i16::from_str(~"-32768") == Some(i16_val));
 
     let mut i32_val: i32 = 2_147_483_647_i32;
     assert (i32::from_str(~"2147483647") == Some(i32_val));
-    
+
     i32_val += 1 as i32;
     assert (i32::from_str(~"-2147483648") == Some(i32_val));
 
     let mut i64_val: i64 = 9_223_372_036_854_775_807_i64;
     assert (i64::from_str(~"9223372036854775807") == Some(i64_val));
-    
+
     i64_val += 1 as i64;
     assert (i64::from_str(~"-9223372036854775808") == Some(i64_val));
 }
diff --git a/src/libcore/num/uint-template.rs b/src/libcore/num/uint-template.rs
index a59813b2df4..0c39186824a 100644
--- a/src/libcore/num/uint-template.rs
+++ b/src/libcore/num/uint-template.rs
@@ -17,9 +17,13 @@ use T_SIGNED = self::inst::T_SIGNED;
 
 use char;
 use cmp::{Eq, Ord};
+use cmp;
+use to_str::ToStr;
 use from_str::FromStr;
+use num::{ToStrRadix, FromStrRadix};
 use num;
 use option::{None, Option, Some};
+use prelude::*;
 use str;
 use uint;
 use vec;
@@ -172,135 +176,97 @@ impl T: num::Round {
     pure fn fract(&self) -> T { 0 }
 }
 
-/**
- * Parse a buffer of bytes
- *
- * # Arguments
- *
- * * buf - A byte buffer
- * * radix - The base of the number
- *
- * # Failure
- *
- * `buf` must not be empty
- */
-pub pure fn parse_bytes(buf: &[const u8], radix: uint) -> Option<T> {
-    if vec::len(buf) == 0u { return None; }
-    let mut i = vec::len(buf) - 1u;
-    let mut power = 1u as T;
-    let mut n = 0u as T;
-    loop {
-        match char::to_digit(buf[i] as char, radix) {
-          Some(d) => n += d as T * power,
-          None => return None
-        }
-        power *= radix as T;
-        if i == 0u { return Some(n); }
-        i -= 1u;
-    };
-}
+// String conversion functions and impl str -> num
 
-/// Parse a string to an int
+/// Parse a string as a number in base 10.
 #[inline(always)]
-pub pure fn from_str(s: &str) -> Option<T>
-{
-    parse_bytes(str::to_bytes(s), 10u)
+pub pure fn from_str(s: &str) -> Option<T> {
+    num::from_str_common(s, 10u, false, false, false,
+                         num::ExpNone, false)
 }
 
-impl T : FromStr {
-    #[inline(always)]
-    static pure fn from_str(s: &str) -> Option<T> { from_str(s) }
+/// Parse a string as a number in the given base.
+#[inline(always)]
+pub pure fn from_str_radix(s: &str, radix: uint) -> Option<T> {
+    num::from_str_common(s, radix, false, false, false,
+                         num::ExpNone, false)
 }
 
-/// Parse a string as an unsigned integer.
-pub fn from_str_radix(buf: &str, radix: u64) -> Option<u64> {
-    if str::len(buf) == 0u { return None; }
-    let mut i = str::len(buf) - 1u;
-    let mut power = 1u64, n = 0u64;
-    loop {
-        match char::to_digit(buf[i] as char, radix as uint) {
-          Some(d) => n += d as u64 * power,
-          None => return None
-        }
-        power *= radix;
-        if i == 0u { return Some(n); }
-        i -= 1u;
-    };
+/// Parse a byte slice as a number in the given base.
+#[inline(always)]
+pub pure fn parse_bytes(buf: &[u8], radix: uint) -> Option<T> {
+    num::from_str_bytes_common(buf, radix, false, false, false,
+                               num::ExpNone, false)
 }
 
-/**
- * Convert to a string in a given base
- *
- * # Failure
- *
- * Fails if `radix` < 2 or `radix` > 16
- */
-#[inline(always)]
-pub pure fn to_str(num: T, radix: uint) -> ~str {
-    do to_str_bytes(false, num, radix) |slice| {
-        do vec::as_imm_buf(slice) |p, len| {
-            unsafe { str::raw::from_buf_len(p, len) }
-        }
+impl T : FromStr {
+    #[inline(always)]
+    static pure fn from_str(s: &str) -> Option<T> {
+        from_str(s)
     }
 }
 
-/// Low-level helper routine for string conversion.
-pub pure fn to_str_bytes<U>(neg: bool, num: T, radix: uint,
-                   f: fn(v: &[u8]) -> U) -> U {
-
+impl T : FromStrRadix {
     #[inline(always)]
-    pure fn digit(n: T) -> u8 {
-        if n <= 9u as T {
-            n as u8 + '0' as u8
-        } else if n <= 15u as T {
-            (n - 10 as T) as u8 + 'a' as u8
-        } else {
-            die!();
-        }
+    static pure fn from_str_radix(&self, s: &str, radix: uint) -> Option<T> {
+        from_str_radix(s, radix)
     }
+}
 
-    assert (1u < radix && radix <= 16u);
-
-    // Enough room to hold any number in any radix.
-    // Worst case: 64-bit number, binary-radix, with
-    // a leading negative sign = 65 bytes.
-    let buf : [mut u8 * 65] = [mut 0u8, ..65];
-    let len = buf.len();
-
-    let mut i = len;
-    let mut n = num;
-    let radix = radix as T;
-    loop {
-        i -= 1u;
-        assert 0u < i && i < len;
-        buf[i] = digit(n % radix);
-        n /= radix;
-        if n == 0 as T { break; }
-    }
+// String conversion functions and impl num -> str
 
-    assert 0u < i && i < len;
+/// Convert to a string as a byte slice in a given base.
+#[inline(always)]
+pub pure fn to_str_bytes<U>(n: T, radix: uint, f: fn(v: &[u8]) -> U) -> U {
+    let (buf, _) = num::to_str_bytes_common(&n, radix, false, false,
+                                            num::SignNeg, num::DigAll);
+    f(buf)
+}
 
-    if neg {
-        i -= 1u;
-        buf[i] = '-' as u8;
-    }
+/// Convert to a string in base 10.
+#[inline(always)]
+pub pure fn to_str(num: T) -> ~str {
+    let (buf, _) = num::to_str_common(&num, 10u, false, false,
+                                      num::SignNeg, num::DigAll);
+    buf
+}
 
-    f(vec::view(buf, i, len))
+/// Convert to a string in a given base.
+#[inline(always)]
+pub pure fn to_str_radix(num: T, radix: uint) -> ~str {
+    let (buf, _) = num::to_str_common(&num, radix, false, false,
+                                      num::SignNeg, num::DigAll);
+    buf
 }
 
-/// Convert to a string
+/// Convert to a string.
+/// *Deprecated*, use to_str() instead.
 #[inline(always)]
-pub pure fn str(i: T) -> ~str { return to_str(i, 10u); }
+pub pure fn str(i: T) -> ~str { to_str(i) }
+
+impl T : ToStr {
+    #[inline(always)]
+    pure fn to_str() -> ~str {
+        to_str(self)
+    }
+}
+
+impl T : ToStrRadix {
+    #[inline(always)]
+    pure fn to_str_radix(&self, radix: uint) -> ~str {
+        to_str_radix(*self, radix)
+    }
+}
 
 #[test]
 pub fn test_to_str() {
-    assert to_str(0 as T, 10u) == ~"0";
-    assert to_str(1 as T, 10u) == ~"1";
-    assert to_str(2 as T, 10u) == ~"2";
-    assert to_str(11 as T, 10u) == ~"11";
-    assert to_str(11 as T, 16u) == ~"b";
-    assert to_str(255 as T, 16u) == ~"ff";
-    assert to_str(0xff as T, 10u) == ~"255";
+    assert to_str_radix(0 as T, 10u) == ~"0";
+    assert to_str_radix(1 as T, 10u) == ~"1";
+    assert to_str_radix(2 as T, 10u) == ~"2";
+    assert to_str_radix(11 as T, 10u) == ~"11";
+    assert to_str_radix(11 as T, 16u) == ~"b";
+    assert to_str_radix(255 as T, 16u) == ~"ff";
+    assert to_str_radix(0xff as T, 10u) == ~"255";
 }
 
 #[test]
@@ -331,17 +297,71 @@ pub fn test_parse_bytes() {
 }
 
 #[test]
+fn test_uint_to_str_overflow() {
+    let mut u8_val: u8 = 255_u8;
+    assert (u8::to_str(u8_val) == ~"255");
+
+    u8_val += 1 as u8;
+    assert (u8::to_str(u8_val) == ~"0");
+
+    let mut u16_val: u16 = 65_535_u16;
+    assert (u16::to_str(u16_val) == ~"65535");
+
+    u16_val += 1 as u16;
+    assert (u16::to_str(u16_val) == ~"0");
+
+    let mut u32_val: u32 = 4_294_967_295_u32;
+    assert (u32::to_str(u32_val) == ~"4294967295");
+
+    u32_val += 1 as u32;
+    assert (u32::to_str(u32_val) == ~"0");
+
+    let mut u64_val: u64 = 18_446_744_073_709_551_615_u64;
+    assert (u64::to_str(u64_val) == ~"18446744073709551615");
+
+    u64_val += 1 as u64;
+    assert (u64::to_str(u64_val) == ~"0");
+}
+
+#[test]
+fn test_uint_from_str_overflow() {
+    let mut u8_val: u8 = 255_u8;
+    assert (u8::from_str(~"255") == Some(u8_val));
+
+    u8_val += 1 as u8;
+    assert (u8::from_str(~"0") == Some(u8_val));
+
+    let mut u16_val: u16 = 65_535_u16;
+    assert (u16::from_str(~"65535") == Some(u16_val));
+
+    u16_val += 1 as u16;
+    assert (u16::from_str(~"0") == Some(u16_val));
+
+    let mut u32_val: u32 = 4_294_967_295_u32;
+    assert (u32::from_str(~"4294967295") == Some(u32_val));
+
+    u32_val += 1 as u32;
+    assert (u32::from_str(~"0") == Some(u32_val));
+
+    let mut u64_val: u64 = 18_446_744_073_709_551_615_u64;
+    assert (u64::from_str(~"18446744073709551615") == Some(u64_val));
+
+    u64_val += 1 as u64;
+    assert (u64::from_str(~"0") == Some(u64_val));
+}
+
+#[test]
 #[should_fail]
 #[ignore(cfg(windows))]
 pub fn to_str_radix1() {
-    uint::to_str(100u, 1u);
+    uint::to_str_radix(100u, 1u);
 }
 
 #[test]
 #[should_fail]
 #[ignore(cfg(windows))]
-pub fn to_str_radix17() {
-    uint::to_str(100u, 17u);
+pub fn to_str_radix37() {
+    uint::to_str_radix(100u, 37u);
 }
 
 use io;
diff --git a/src/libcore/to_str.rs b/src/libcore/to_str.rs
index 054512811be..13b09590b44 100644
--- a/src/libcore/to_str.rs
+++ b/src/libcore/to_str.rs
@@ -24,26 +24,6 @@ use vec;
 
 pub trait ToStr { pub pure fn to_str() -> ~str; }
 
-impl uint: ToStr {
-    #[inline(always)]
-    pure fn to_str() -> ~str { ::uint::str(self) }
-}
-impl u8: ToStr {
-    #[inline(always)]
-    pure fn to_str() -> ~str { ::u8::str(self) }
-}
-impl u16: ToStr {
-    #[inline(always)]
-    pure fn to_str() -> ~str { ::u16::str(self) }
-}
-impl u32: ToStr {
-    #[inline(always)]
-    pure fn to_str() -> ~str { ::u32::str(self) }
-}
-impl u64: ToStr {
-    #[inline(always)]
-    pure fn to_str() -> ~str { ::u64::str(self) }
-}
 impl float: ToStr {
     #[inline(always)]
     pure fn to_str() -> ~str { ::float::to_str(self, 4u) }
diff --git a/src/librustc/metadata/tyencode.rs b/src/librustc/metadata/tyencode.rs
index 69d69537f2a..f02f187c629 100644
--- a/src/librustc/metadata/tyencode.rs
+++ b/src/librustc/metadata/tyencode.rs
@@ -98,8 +98,8 @@ pub fn enc_ty(w: io::Writer, cx: @ctxt, t: ty::t) {
             let abbrev_len = 3u + estimate_sz(pos) + estimate_sz(len);
             if abbrev_len < len {
                 // I.e. it's actually an abbreviation.
-                let s = ~"#" + uint::to_str(pos, 16u) + ~":" +
-                    uint::to_str(len, 16u) + ~"#";
+                let s = ~"#" + uint::to_str_radix(pos, 16u) + ~":" +
+                    uint::to_str_radix(len, 16u) + ~"#";
                 let a = {pos: pos, len: len, s: @s};
                 abbrevs.insert(t, a);
             }
diff --git a/src/librustc/middle/typeck/infer/to_str.rs b/src/librustc/middle/typeck/infer/to_str.rs
index e100392bba9..57e05926173 100644
--- a/src/librustc/middle/typeck/infer/to_str.rs
+++ b/src/librustc/middle/typeck/infer/to_str.rs
@@ -88,7 +88,7 @@ pub impl<V:Vid ToStr, T:InferStr> VarValue<V, T> : InferStr {
         match *self {
           Redirect(ref vid) => fmt!("Redirect(%s)", vid.to_str()),
           Root(ref pt, rk) => fmt!("Root(%s, %s)", pt.inf_str(cx),
-                               uint::to_str(rk, 10u))
+                               uint::to_str_radix(rk, 10u))
         }
     }
 }
diff --git a/src/libstd/bigint.rs b/src/libstd/bigint.rs
index 0104e72764f..64126ea918f 100644
--- a/src/libstd/bigint.rs
+++ b/src/libstd/bigint.rs
@@ -456,7 +456,7 @@ pub impl BigUint {
         pure fn fill_concat(v: &[BigDigit], radix: uint, l: uint) -> ~str {
             if v.is_empty() { return ~"0" }
             str::trim_left_chars(str::concat(vec::reversed(v).map(|n| {
-                let s = uint::to_str(*n as uint, radix);
+                let s = uint::to_str_radix(*n as uint, radix);
                 str::from_chars(vec::from_elem(l - s.len(), '0')) + s
             })), ['0'])
         }
diff --git a/src/libstd/md4.rs b/src/libstd/md4.rs
index 06b6aca6895..e1e2bb2697f 100644
--- a/src/libstd/md4.rs
+++ b/src/libstd/md4.rs
@@ -109,7 +109,7 @@ pub pure fn md4_str(msg: &[u8]) -> ~str {
         while i < 4u32 {
             let byte = (u >> (i * 8u32)) as u8;
             if byte <= 16u8 { result += ~"0"; }
-            result += uint::to_str(byte as uint, 16u);
+            result += uint::to_str_radix(byte as uint, 16u);
             i += 1u32;
         }
     }
diff --git a/src/libstd/sha1.rs b/src/libstd/sha1.rs
index 608d071d90e..a5c740c343c 100644
--- a/src/libstd/sha1.rs
+++ b/src/libstd/sha1.rs
@@ -253,7 +253,7 @@ pub fn sha1() -> Sha1 {
             let rr = mk_result(&self);
             let mut s = ~"";
             for vec::each(rr) |b| {
-                s += uint::to_str(*b as uint, 16u);
+                s += uint::to_str_radix(*b as uint, 16u);
             }
             return s;
         }
diff --git a/src/libsyntax/parse/lexer.rs b/src/libsyntax/parse/lexer.rs
index 71e96699c3d..381183e736c 100644
--- a/src/libsyntax/parse/lexer.rs
+++ b/src/libsyntax/parse/lexer.rs
@@ -417,7 +417,7 @@ fn scan_number(c: char, rdr: string_reader) -> token::Token {
         if str::len(num_str) == 0u {
             rdr.fatal(~"no valid digits found for number");
         }
-        let parsed = u64::from_str_radix(num_str, base as u64).get();
+        let parsed = u64::from_str_radix(num_str, base as uint).get();
         match tp {
           either::Left(t) => return token::LIT_INT(parsed as i64, t),
           either::Right(t) => return token::LIT_UINT(parsed, t)
@@ -471,7 +471,7 @@ fn scan_number(c: char, rdr: string_reader) -> token::Token {
         if str::len(num_str) == 0u {
             rdr.fatal(~"no valid digits found for number");
         }
-        let parsed = u64::from_str_radix(num_str, base as u64).get();
+        let parsed = u64::from_str_radix(num_str, base as uint).get();
 
         debug!("lexing %s as an unsuffixed integer literal",
                num_str);
diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs
index 527b036a46c..5079766239b 100644
--- a/src/libsyntax/print/pprust.rs
+++ b/src/libsyntax/print/pprust.rs
@@ -2006,24 +2006,24 @@ pub fn print_literal(s: ps, &&lit: @ast::lit) {
       ast::lit_int(i, t) => {
         if i < 0_i64 {
             word(s.s,
-                 ~"-" + u64::to_str(-i as u64, 10u)
+                 ~"-" + u64::to_str_radix(-i as u64, 10u)
                  + ast_util::int_ty_to_str(t));
         } else {
             word(s.s,
-                 u64::to_str(i as u64, 10u)
+                 u64::to_str_radix(i as u64, 10u)
                  + ast_util::int_ty_to_str(t));
         }
       }
       ast::lit_uint(u, t) => {
         word(s.s,
-             u64::to_str(u, 10u)
+             u64::to_str_radix(u, 10u)
              + ast_util::uint_ty_to_str(t));
       }
       ast::lit_int_unsuffixed(i) => {
         if i < 0_i64 {
-            word(s.s, ~"-" + u64::to_str(-i as u64, 10u));
+            word(s.s, ~"-" + u64::to_str_radix(-i as u64, 10u));
         } else {
-            word(s.s, u64::to_str(i as u64, 10u));
+            word(s.s, u64::to_str_radix(i as u64, 10u));
         }
       }
       ast::lit_float(f, t) => {