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.
This commit is contained in:
committed by
Brian Anderson
parent
26e72bf92b
commit
eb19462104
@@ -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) });
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -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 as a number in base 10.
|
||||
#[inline(always)]
|
||||
pub pure fn from_str(s: &str) -> Option<T> {
|
||||
num::from_str_common(s, 10u, false, false, false,
|
||||
num::ExpNone, false)
|
||||
}
|
||||
|
||||
/// Parse a string to an int
|
||||
/// Parse a string as a number in the given base.
|
||||
#[inline(always)]
|
||||
pub pure fn from_str(s: &str) -> Option<T>
|
||||
{
|
||||
parse_bytes(str::to_bytes(s), 10u)
|
||||
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 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)
|
||||
}
|
||||
|
||||
impl T : FromStr {
|
||||
#[inline(always)]
|
||||
static pure fn from_str(s: &str) -> Option<T> { from_str(s) }
|
||||
}
|
||||
|
||||
/// 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;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 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) }
|
||||
}
|
||||
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; }
|
||||
}
|
||||
|
||||
assert 0u < i && i < len;
|
||||
|
||||
if neg {
|
||||
i -= 1u;
|
||||
buf[i] = '-' as u8;
|
||||
}
|
||||
|
||||
f(vec::view(buf, i, len))
|
||||
}
|
||||
|
||||
/// Convert to a string
|
||||
// String conversion functions and impl num -> str
|
||||
|
||||
/// Convert to a string as a byte slice in a given base.
|
||||
#[inline(always)]
|
||||
pub pure fn str(i: T) -> ~str { return to_str(i, 10u); }
|
||||
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)
|
||||
}
|
||||
|
||||
/// 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
|
||||
}
|
||||
|
||||
/// 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.
|
||||
/// *Deprecated*, use to_str() instead.
|
||||
#[inline(always)]
|
||||
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]
|
||||
#[should_fail]
|
||||
#[ignore(cfg(windows))]
|
||||
pub fn to_str_radix1() {
|
||||
uint::to_str(100u, 1u);
|
||||
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_radix17() {
|
||||
uint::to_str(100u, 17u);
|
||||
pub fn to_str_radix1() {
|
||||
uint::to_str_radix(100u, 1u);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_fail]
|
||||
#[ignore(cfg(windows))]
|
||||
pub fn to_str_radix37() {
|
||||
uint::to_str_radix(100u, 37u);
|
||||
}
|
||||
|
||||
use io;
|
||||
|
||||
@@ -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) }
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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'])
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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) => {
|
||||
|
||||
Reference in New Issue
Block a user