int/uint parse_buf => parse_bytes (#3444)

This commit is contained in:
Erick Tryzelaar
2012-09-14 09:55:33 -07:00
parent 651e63cc5c
commit b73f801cc1
7 changed files with 43 additions and 43 deletions

View File

@@ -16,7 +16,7 @@ export is_nonpositive, is_nonnegative;
export range; export range;
export compl; export compl;
export abs; export abs;
export parse_buf, from_str, to_str, to_str_bytes, str; export parse_bytes, from_str, to_str, to_str_bytes, str;
export num, ord, eq, times, timesi; export num, ord, eq, times, timesi;
export bits, bytes; export bits, bytes;
@@ -137,7 +137,7 @@ impl T: iter::TimesIx {
* * buf - A byte buffer * * buf - A byte buffer
* * radix - The base of the number * * radix - The base of the number
*/ */
fn parse_buf(buf: &[u8], radix: uint) -> Option<T> { fn parse_bytes(buf: &[u8], radix: uint) -> Option<T> {
if vec::len(buf) == 0u { return None; } if vec::len(buf) == 0u { return None; }
let mut i = vec::len(buf) - 1u; let mut i = vec::len(buf) - 1u;
let mut start = 0u; let mut start = 0u;
@@ -160,7 +160,7 @@ fn parse_buf(buf: &[u8], radix: uint) -> Option<T> {
} }
/// Parse a string to an int /// Parse a string to an int
fn from_str(s: &str) -> Option<T> { parse_buf(str::to_bytes(s), 10u) } fn from_str(s: &str) -> Option<T> { parse_bytes(str::to_bytes(s), 10u) }
impl T : FromStr { impl T : FromStr {
static fn from_str(s: &str) -> Option<T> { from_str(s) } static fn from_str(s: &str) -> Option<T> { from_str(s) }
@@ -209,28 +209,28 @@ fn test_from_str() {
// FIXME: Has alignment issues on windows and 32-bit linux (#2609) // FIXME: Has alignment issues on windows and 32-bit linux (#2609)
#[test] #[test]
#[ignore] #[ignore]
fn test_parse_buf() { fn test_parse_bytes() {
use str::to_bytes; use str::to_bytes;
assert parse_buf(to_bytes(~"123"), 10u) == Some(123 as T); assert parse_bytes(to_bytes(~"123"), 10u) == Some(123 as T);
assert parse_buf(to_bytes(~"1001"), 2u) == Some(9 as T); assert parse_bytes(to_bytes(~"1001"), 2u) == Some(9 as T);
assert parse_buf(to_bytes(~"123"), 8u) == Some(83 as T); assert parse_bytes(to_bytes(~"123"), 8u) == Some(83 as T);
assert parse_buf(to_bytes(~"123"), 16u) == Some(291 as T); assert parse_bytes(to_bytes(~"123"), 16u) == Some(291 as T);
assert parse_buf(to_bytes(~"ffff"), 16u) == Some(65535 as T); assert parse_bytes(to_bytes(~"ffff"), 16u) == Some(65535 as T);
assert parse_buf(to_bytes(~"FFFF"), 16u) == Some(65535 as T); assert parse_bytes(to_bytes(~"FFFF"), 16u) == Some(65535 as T);
assert parse_buf(to_bytes(~"z"), 36u) == Some(35 as T); assert parse_bytes(to_bytes(~"z"), 36u) == Some(35 as T);
assert parse_buf(to_bytes(~"Z"), 36u) == Some(35 as T); assert parse_bytes(to_bytes(~"Z"), 36u) == Some(35 as T);
assert parse_buf(to_bytes(~"-123"), 10u) == Some(-123 as T); assert parse_bytes(to_bytes(~"-123"), 10u) == Some(-123 as T);
assert parse_buf(to_bytes(~"-1001"), 2u) == Some(-9 as T); assert parse_bytes(to_bytes(~"-1001"), 2u) == Some(-9 as T);
assert parse_buf(to_bytes(~"-123"), 8u) == Some(-83 as T); assert parse_bytes(to_bytes(~"-123"), 8u) == Some(-83 as T);
assert parse_buf(to_bytes(~"-123"), 16u) == Some(-291 as T); assert parse_bytes(to_bytes(~"-123"), 16u) == Some(-291 as T);
assert parse_buf(to_bytes(~"-ffff"), 16u) == Some(-65535 as T); assert parse_bytes(to_bytes(~"-ffff"), 16u) == Some(-65535 as T);
assert parse_buf(to_bytes(~"-FFFF"), 16u) == Some(-65535 as T); assert parse_bytes(to_bytes(~"-FFFF"), 16u) == Some(-65535 as T);
assert parse_buf(to_bytes(~"-z"), 36u) == Some(-35 as T); assert parse_bytes(to_bytes(~"-z"), 36u) == Some(-35 as T);
assert parse_buf(to_bytes(~"-Z"), 36u) == Some(-35 as T); assert parse_bytes(to_bytes(~"-Z"), 36u) == Some(-35 as T);
assert parse_buf(to_bytes(~"Z"), 35u).is_none(); assert parse_bytes(to_bytes(~"Z"), 35u).is_none();
assert parse_buf(to_bytes(~"-9"), 2u).is_none(); assert parse_bytes(to_bytes(~"-9"), 2u).is_none();
} }
#[test] #[test]

View File

@@ -94,7 +94,7 @@ pure fn to_either<T: Copy, U: Copy>(res: Result<U, T>) -> Either<T, U> {
* Example: * Example:
* *
* let res = chain(read_file(file)) { |buf| * let res = chain(read_file(file)) { |buf|
* ok(parse_buf(buf)) * ok(parse_bytes(buf))
* } * }
*/ */
fn chain<T, U: Copy, V: Copy>(res: Result<T, V>, op: fn(T) -> Result<U, V>) fn chain<T, U: Copy, V: Copy>(res: Result<T, V>, op: fn(T) -> Result<U, V>)
@@ -170,7 +170,7 @@ fn iter_err<T, E>(res: Result<T, E>, f: fn(E)) {
* Example: * Example:
* *
* let res = map(read_file(file)) { |buf| * let res = map(read_file(file)) { |buf|
* parse_buf(buf) * parse_bytes(buf)
* } * }
*/ */
fn map<T, E: Copy, U: Copy>(res: Result<T, E>, op: fn(T) -> U) fn map<T, E: Copy, U: Copy>(res: Result<T, E>, op: fn(T) -> U)

View File

@@ -15,7 +15,7 @@ export is_nonpositive, is_nonnegative;
export range; export range;
export compl; export compl;
export to_str, to_str_bytes; export to_str, to_str_bytes;
export from_str, from_str_radix, str, parse_buf; export from_str, from_str_radix, str, parse_bytes;
export num, ord, eq, times, timesi; export num, ord, eq, times, timesi;
export bits, bytes; export bits, bytes;
@@ -126,7 +126,7 @@ impl T: iter::TimesIx {
* *
* `buf` must not be empty * `buf` must not be empty
*/ */
fn parse_buf(buf: &[const u8], radix: uint) -> Option<T> { fn parse_bytes(buf: &[const u8], radix: uint) -> Option<T> {
if vec::len(buf) == 0u { return None; } if vec::len(buf) == 0u { return None; }
let mut i = vec::len(buf) - 1u; let mut i = vec::len(buf) - 1u;
let mut power = 1u as T; let mut power = 1u as T;
@@ -143,7 +143,7 @@ fn parse_buf(buf: &[const u8], radix: uint) -> Option<T> {
} }
/// Parse a string to an int /// Parse a string to an int
fn from_str(s: &str) -> Option<T> { parse_buf(str::to_bytes(s), 10u) } fn from_str(s: &str) -> Option<T> { parse_bytes(str::to_bytes(s), 10u) }
impl T : FromStr { impl T : FromStr {
static fn from_str(s: &str) -> Option<T> { from_str(s) } static fn from_str(s: &str) -> Option<T> { from_str(s) }
@@ -275,17 +275,17 @@ fn test_from_str() {
#[test] #[test]
#[ignore] #[ignore]
fn test_parse_buf() { fn test_parse_bytes() {
use str::to_bytes; use str::to_bytes;
assert parse_buf(to_bytes(~"123"), 10u) == Some(123u as T); assert parse_bytes(to_bytes(~"123"), 10u) == Some(123u as T);
assert parse_buf(to_bytes(~"1001"), 2u) == Some(9u as T); assert parse_bytes(to_bytes(~"1001"), 2u) == Some(9u as T);
assert parse_buf(to_bytes(~"123"), 8u) == Some(83u as T); assert parse_bytes(to_bytes(~"123"), 8u) == Some(83u as T);
assert parse_buf(to_bytes(~"123"), 16u) == Some(291u as T); assert parse_bytes(to_bytes(~"123"), 16u) == Some(291u as T);
assert parse_buf(to_bytes(~"ffff"), 16u) == Some(65535u as T); assert parse_bytes(to_bytes(~"ffff"), 16u) == Some(65535u as T);
assert parse_buf(to_bytes(~"z"), 36u) == Some(35u as T); assert parse_bytes(to_bytes(~"z"), 36u) == Some(35u as T);
assert parse_buf(to_bytes(~"Z"), 10u).is_none(); assert parse_bytes(to_bytes(~"Z"), 10u).is_none();
assert parse_buf(to_bytes(~"_"), 2u).is_none(); assert parse_bytes(to_bytes(~"_"), 2u).is_none();
} }
#[test] #[test]

View File

@@ -116,7 +116,7 @@ fn decode_inner(s: &str, full_url: bool) -> ~str {
match rdr.read_char() { match rdr.read_char() {
'%' => { '%' => {
let bytes = rdr.read_bytes(2u); let bytes = rdr.read_bytes(2u);
let ch = uint::parse_buf(bytes, 16u).get() as char; let ch = uint::parse_bytes(bytes, 16u).get() as char;
if full_url { if full_url {
// Only decode some characters: // Only decode some characters:
@@ -241,7 +241,7 @@ fn decode_form_urlencoded(s: ~[u8]) ->
ch => { ch => {
let ch = match ch { let ch = match ch {
'%' => { '%' => {
uint::parse_buf(rdr.read_bytes(2u), 16u).get() as char uint::parse_bytes(rdr.read_bytes(2u), 16u).get() as char
} }
'+' => ' ', '+' => ' ',
ch => ch ch => ch

View File

@@ -205,7 +205,7 @@ fn field_mutability(d: ebml::Doc) -> ast::class_mutability {
fn variant_disr_val(d: ebml::Doc) -> Option<int> { fn variant_disr_val(d: ebml::Doc) -> Option<int> {
do option::chain(ebml::maybe_get_doc(d, tag_disr_val)) |val_doc| { do option::chain(ebml::maybe_get_doc(d, tag_disr_val)) |val_doc| {
int::parse_buf(ebml::doc_data(val_doc), 10u) int::parse_bytes(ebml::doc_data(val_doc), 10u)
} }
} }

View File

@@ -440,12 +440,12 @@ fn parse_def_id(buf: &[u8]) -> ast::def_id {
let crate_part = vec::view(buf, 0u, colon_idx); let crate_part = vec::view(buf, 0u, colon_idx);
let def_part = vec::view(buf, colon_idx + 1u, len); let def_part = vec::view(buf, colon_idx + 1u, len);
let crate_num = match uint::parse_buf(crate_part, 10u) { let crate_num = match uint::parse_bytes(crate_part, 10u) {
Some(cn) => cn as int, Some(cn) => cn as int,
None => fail (fmt!("internal error: parse_def_id: crate number \ None => fail (fmt!("internal error: parse_def_id: crate number \
expected, but found %?", crate_part)) expected, but found %?", crate_part))
}; };
let def_num = match uint::parse_buf(def_part, 10u) { let def_num = match uint::parse_bytes(def_part, 10u) {
Some(dn) => dn as int, Some(dn) => dn as int,
None => fail (fmt!("internal error: parse_def_id: id expected, but \ None => fail (fmt!("internal error: parse_def_id: id expected, but \
found %?", def_part)) found %?", def_part))

View File

@@ -93,8 +93,8 @@ fn main(args: ~[~str]) {
if opts.stress { if opts.stress {
stress(2); stress(2);
} else { } else {
let max = option::get(uint::parse_buf(str::to_bytes(args[1]), let max = option::get(uint::parse_bytes(str::to_bytes(args[1]),
10u)) as int; 10u)) as int;
let num_trials = 10; let num_trials = 10;