Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct handling of non-numeric chars in parse_buf #1106

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/lib/char.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,12 @@ pure fn is_whitespace(c: char) -> bool {
true
} else if c == ch_no_break_space { true } else { false }
}

pure fn to_digit(c: char) -> u8 {
alt c {
'0' to '9' { c as u8 - ('0' as u8) }
'a' to 'z' { c as u8 + 10u8 - ('a' as u8) }
'A' to 'Z' { c as u8 + 10u8 - ('A' as u8) }
_ { fail; }
}
}
6 changes: 5 additions & 1 deletion src/lib/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,11 @@ fn parse_buf(buf: [u8], radix: uint) -> int {
}
let n = 0;
while true {
n += (buf[i] - ('0' as u8) as int) * power;
let digit = char::to_digit(buf[i] as char);
if (digit as uint) >= radix {
fail;
}
n += (digit as int) * power;
power *= radix as int;
if i <= start { ret n; }
i -= 1u;
Expand Down
6 changes: 5 additions & 1 deletion src/lib/uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,11 @@ fn parse_buf(buf: [u8], radix: uint) -> uint {
let power = 1u;
let n = 0u;
while true {
n += (buf[i] - ('0' as u8) as uint) * power;
let digit = char::to_digit(buf[i] as char);
if (digit as uint) >= radix {
fail;
}
n += (digit as uint) * power;
power *= radix;
if i == 0u { ret n; }
i -= 1u;
Expand Down
28 changes: 28 additions & 0 deletions src/test/stdtest/char.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use std;
import std::char;

#[test]
fn test_is_whitespace() {
assert char::is_whitespace(' ');
assert char::is_whitespace('\u2007');
assert char::is_whitespace('\t');
assert char::is_whitespace('\n');

assert !char::is_whitespace('a');
assert !char::is_whitespace('_');
assert !char::is_whitespace('\u0000');
}

#[test]
fn test_to_digit() {
assert (char::to_digit('0') == 0u8);
assert (char::to_digit('1') == 1u8);
assert (char::to_digit('2') == 2u8);
assert (char::to_digit('9') == 9u8);
assert (char::to_digit('a') == 10u8);
assert (char::to_digit('A') == 10u8);
assert (char::to_digit('b') == 11u8);
assert (char::to_digit('B') == 11u8);
assert (char::to_digit('z') == 35u8);
assert (char::to_digit('Z') == 35u8);
}
22 changes: 22 additions & 0 deletions src/test/stdtest/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use std;
import std::int;
import std::str::eq;
import std::str::bytes;

#[test]
fn test_from_str() {
Expand All @@ -18,6 +19,27 @@ fn test_from_str() {
assert(int::from_str("-00100") == -100);
}

#[test]
fn test_parse_buf() {
assert (int::parse_buf(bytes("123"), 10u) == 123);
assert (int::parse_buf(bytes("1001"), 2u) == 9);
assert (int::parse_buf(bytes("123"), 8u) == 83);
assert (int::parse_buf(bytes("123"), 16u) == 291);
assert (int::parse_buf(bytes("ffff"), 16u) == 65535);
assert (int::parse_buf(bytes("FFFF"), 16u) == 65535);
assert (int::parse_buf(bytes("z"), 36u) == 35);
assert (int::parse_buf(bytes("Z"), 36u) == 35);

assert (int::parse_buf(bytes("-123"), 10u) == -123);
assert (int::parse_buf(bytes("-1001"), 2u) == -9);
assert (int::parse_buf(bytes("-123"), 8u) == -83);
assert (int::parse_buf(bytes("-123"), 16u) == -291);
assert (int::parse_buf(bytes("-ffff"), 16u) == -65535);
assert (int::parse_buf(bytes("-FFFF"), 16u) == -65535);
assert (int::parse_buf(bytes("-z"), 36u) == -35);
assert (int::parse_buf(bytes("-Z"), 36u) == -35);
}

#[test]
fn test_to_str() {
assert (eq(int::to_str(0, 10u), "0"));
Expand Down
1 change: 1 addition & 0 deletions src/test/stdtest/stdtest.rc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std;

mod bitv;
mod box;
mod char;
mod comm;
mod deque;
mod either;
Expand Down
22 changes: 21 additions & 1 deletion src/test/stdtest/uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,26 @@
// -*- rust -*-
use std;
import std::uint;
import std::str::bytes;

#[test]
fn test_from_str() {
assert (uint::from_str("0") == 0u);
assert (uint::from_str("3") == 3u);
assert (uint::from_str("10") == 10u);
assert (uint::from_str("123456789") == 123456789u);
assert (uint::from_str("00100") == 100u);
}

#[test]
fn test_parse_buf() {
assert (uint::parse_buf(bytes("123"), 10u) == 123u);
assert (uint::parse_buf(bytes("1001"), 2u) == 9u);
assert (uint::parse_buf(bytes("123"), 8u) == 83u);
assert (uint::parse_buf(bytes("123"), 16u) == 291u);
assert (uint::parse_buf(bytes("ffff"), 16u) == 65535u);
assert (uint::parse_buf(bytes("z"), 36u) == 35u);
}

#[test]
fn test_next_power_of_two() {
Expand Down Expand Up @@ -53,4 +73,4 @@ fn test_overflows() {
assert (uint::max_value() > 0u);
assert (uint::min_value() <= 0u);
assert (uint::min_value() + uint::max_value() + 1u == 0u);
}
}