mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-12 15:04:03 +00:00
Add a char::to_digit function
This commit is contained in:
parent
4739942e74
commit
80c926c5e2
@ -93,3 +93,12 @@ pure fn is_whitespace(c: char) -> bool {
|
|||||||
true
|
true
|
||||||
} else if c == ch_no_break_space { true } else { false }
|
} 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; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -112,11 +112,7 @@ fn parse_buf(buf: [u8], radix: uint) -> int {
|
|||||||
}
|
}
|
||||||
let n = 0;
|
let n = 0;
|
||||||
while true {
|
while true {
|
||||||
let digit = alt buf[i] as char {
|
let digit = char::to_digit(buf[i] as char);
|
||||||
'0' to '9' { buf[i] - ('0' as u8) }
|
|
||||||
'a' to 'z' { 10u8 + buf[i] - ('a' as u8) }
|
|
||||||
'A' to 'Z' { 10u8 + buf[i] - ('A' as u8) }
|
|
||||||
};
|
|
||||||
if (digit as uint) >= radix {
|
if (digit as uint) >= radix {
|
||||||
fail;
|
fail;
|
||||||
}
|
}
|
||||||
|
@ -100,11 +100,7 @@ fn parse_buf(buf: [u8], radix: uint) -> uint {
|
|||||||
let power = 1u;
|
let power = 1u;
|
||||||
let n = 0u;
|
let n = 0u;
|
||||||
while true {
|
while true {
|
||||||
let digit = alt buf[i] as char {
|
let digit = char::to_digit(buf[i] as char);
|
||||||
'0' to '9' { buf[i] - ('0' as u8) }
|
|
||||||
'a' to 'z' { 10u8 + buf[i] - ('a' as u8) }
|
|
||||||
'A' to 'Z' { 10u8 + buf[i] - ('A' as u8) }
|
|
||||||
};
|
|
||||||
if (digit as uint) >= radix {
|
if (digit as uint) >= radix {
|
||||||
fail;
|
fail;
|
||||||
}
|
}
|
||||||
|
28
src/test/stdtest/char.rs
Normal file
28
src/test/stdtest/char.rs
Normal file
@ -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);
|
||||||
|
}
|
@ -2,6 +2,7 @@ use std;
|
|||||||
|
|
||||||
mod bitv;
|
mod bitv;
|
||||||
mod box;
|
mod box;
|
||||||
|
mod char;
|
||||||
mod comm;
|
mod comm;
|
||||||
mod deque;
|
mod deque;
|
||||||
mod either;
|
mod either;
|
||||||
|
Loading…
Reference in New Issue
Block a user