Fix int::parse_buf for negative numbers (#1102)
This commit is contained in:
parent
8c51d4b002
commit
7080ac15fb
@ -103,16 +103,18 @@ fn parse_buf(buf: [u8], radix: uint) -> int {
|
||||
fail;
|
||||
}
|
||||
let i = vec::len::<u8>(buf) - 1u;
|
||||
let start = 0u;
|
||||
let power = 1;
|
||||
|
||||
if buf[0] == ('-' as u8) {
|
||||
power = -1;
|
||||
i -= 1u;
|
||||
start = 1u;
|
||||
}
|
||||
let n = 0;
|
||||
while true {
|
||||
n += (buf[i] - ('0' as u8) as int) * power;
|
||||
power *= radix as int;
|
||||
if i == 0u { ret n; }
|
||||
if i <= start { ret n; }
|
||||
i -= 1u;
|
||||
}
|
||||
fail;
|
||||
|
@ -3,6 +3,21 @@
|
||||
import std::int;
|
||||
import std::str::eq;
|
||||
|
||||
#[test]
|
||||
fn test_from_str() {
|
||||
assert(int::from_str("0") == 0);
|
||||
assert(int::from_str("3") == 3);
|
||||
assert(int::from_str("10") == 10);
|
||||
assert(int::from_str("123456789") == 123456789);
|
||||
assert(int::from_str("00100") == 100);
|
||||
|
||||
assert(int::from_str("-1") == -1);
|
||||
assert(int::from_str("-3") == -3);
|
||||
assert(int::from_str("-10") == -10);
|
||||
assert(int::from_str("-123456789") == -123456789);
|
||||
assert(int::from_str("-00100") == -100);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_to_str() {
|
||||
assert (eq(int::to_str(0, 10u), "0"));
|
||||
@ -29,4 +44,4 @@ fn test_overflows() {
|
||||
assert (int::max_value() > 0);
|
||||
assert (int::min_value() <= 0);
|
||||
assert (int::min_value() + int::max_value() + 1 == 0);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user