Permalink
Browse files

Make the various from_str functions return options

So that they can be used with user input without causing task
failures.

Closes #1335
  • Loading branch information...
1 parent 7237343 commit ffd50b9cdfa38bc80f2444d917eef5a02c38c32f @marijnh marijnh committed Feb 22, 2012
@@ -95,13 +95,8 @@ fn variant_enum_id(d: ebml::doc) -> ast::def_id {
}
fn variant_disr_val(d: ebml::doc) -> option<int> {
- alt ebml::maybe_get_doc(d, tag_disr_val) {
- some(val_doc) {
- let val_buf = ebml::doc_data(val_doc);
- let val = int::parse_buf(val_buf, 10u);
- ret some(val);
- }
- _ { ret none;}
+ option::chain(ebml::maybe_get_doc(d, tag_disr_val)) {|val_doc|
+ int::parse_buf(ebml::doc_data(val_doc), 10u)
}
}
@@ -390,8 +390,8 @@ fn parse_def_id(buf: [u8]) -> ast::def_id {
for b: u8 in crate_part { crate_part_vec += [b]; }
for b: u8 in def_part { def_part_vec += [b]; }
- let crate_num = uint::parse_buf(crate_part_vec, 10u) as int;
- let def_num = uint::parse_buf(def_part_vec, 10u) as int;
+ let crate_num = option::get(uint::parse_buf(crate_part_vec, 10u)) as int;
+ let def_num = option::get(uint::parse_buf(def_part_vec, 10u)) as int;
ret {crate: crate_num, node: def_num};
}
@@ -301,7 +301,7 @@ fn lit_to_const(lit: @lit) -> const_val {
lit_str(s) { const_str(s) }
lit_int(n, _) { const_int(n) }
lit_uint(n, _) { const_uint(n) }
- lit_float(n, _) { const_float(float::from_str(n)) }
+ lit_float(n, _) { const_float(option::get(float::from_str(n))) }
lit_nil { const_int(0i64) }
lit_bool(b) { const_int(b as i64) }
}
@@ -174,8 +174,8 @@ fn scan_digits(rdr: reader, radix: uint) -> str {
while true {
let c = rdr.curr;
if c == '_' { rdr.bump(); cont; }
- alt char::maybe_digit(c) {
- some(d) if (d as uint) < radix {
+ alt char::to_digit(c, radix) {
+ some(d) {
str::push_char(rslt, c);
rdr.bump();
}
@@ -229,7 +229,7 @@ fn scan_number(c: char, rdr: reader) -> token::token {
if str::len_bytes(num_str) == 0u {
rdr.fatal("no valid digits found for number");
}
- let parsed = u64::from_str(num_str, base as u64);
+ let parsed = option::get(u64::from_str(num_str, base as u64));
alt tp {
either::left(t) { ret token::LIT_INT(parsed as i64, t); }
either::right(t) { ret token::LIT_UINT(parsed, t); }
@@ -276,7 +276,7 @@ fn scan_number(c: char, rdr: reader) -> token::token {
if str::len_bytes(num_str) == 0u {
rdr.fatal("no valid digits found for number");
}
- let parsed = u64::from_str(num_str, base as u64);
+ let parsed = option::get(u64::from_str(num_str, base as u64));
ret token::LIT_INT(parsed as i64, ast::ty_i);
}
}
View
@@ -59,11 +59,11 @@ pure fn is_false(v: t) -> bool { !v }
#[doc(
brief = "Parse logic value from `s`"
)]
-pure fn from_str(s: str) -> t {
+pure fn from_str(s: str) -> option<t> {
alt check s {
- "true" { true }
- "false" { false }
- _ { fail "'" + s + "' is not a valid boolean string"; }
+ "true" { some(true) }
+ "false" { some(false) }
+ _ { none }
}
}
@@ -89,7 +89,7 @@ pure fn to_bit(v: t) -> u8 { if v { 1u8 } else { 0u8 } }
#[test]
fn test_bool_from_str() {
all_values { |v|
- assert v == from_str(bool::to_str(v))
+ assert some(v) == from_str(bool::to_str(v))
}
}
View
@@ -38,7 +38,7 @@ export is_alphabetic,
is_lowercase, is_uppercase,
is_whitespace, is_alphanumeric,
is_ascii, is_digit,
- to_digit, to_lower, to_upper, maybe_digit, cmp;
+ to_digit, to_lower, to_upper, cmp;
import is_alphabetic = unicode::derived_property::Alphabetic;
import is_XID_start = unicode::derived_property::XID_Start;
@@ -102,26 +102,18 @@ pure fn is_digit(c: char) -> bool {
Safety note: This function fails if `c` is not a valid char",
return = "If `c` is between '0' and '9', the corresponding value \
between 0 and 9. If `c` is 'a' or 'A', 10. If `c` is \
- 'b' or 'B', 11, etc."
+ 'b' or 'B', 11, etc. Returns none if the char does not \
+ refer to a digit in the given radix."
)]
-pure fn to_digit(c: char) -> u8 unsafe {
- alt maybe_digit(c) {
- option::some(x) { x }
- option::none { fail; }
- }
-}
-
-#[doc(
- brief = "Convert a char to the corresponding digit. Returns none when \
- character is not a valid hexadecimal digit."
-)]
-pure fn maybe_digit(c: char) -> option<u8> {
- alt c {
- '0' to '9' { option::some(c as u8 - ('0' as u8)) }
- 'a' to 'z' { option::some(c as u8 + 10u8 - ('a' as u8)) }
- 'A' to 'Z' { option::some(c as u8 + 10u8 - ('A' as u8)) }
- _ { option::none }
- }
+pure fn to_digit(c: char, radix: uint) -> option<uint> {
+ let val = alt c {
+ '0' to '9' { c as uint - ('0' as uint) }
+ 'a' to 'z' { c as uint + 10u - ('a' as uint) }
+ 'A' to 'Z' { c as uint + 10u - ('A' as uint) }
+ _ { ret none; }
+ };
+ if val < radix { some(val) }
+ else { none }
}
/*
@@ -192,30 +184,19 @@ fn test_is_whitespace() {
#[test]
fn test_to_digit() {
- assert (to_digit('0') == 0u8);
- assert (to_digit('1') == 1u8);
- assert (to_digit('2') == 2u8);
- assert (to_digit('9') == 9u8);
- assert (to_digit('a') == 10u8);
- assert (to_digit('A') == 10u8);
- assert (to_digit('b') == 11u8);
- assert (to_digit('B') == 11u8);
- assert (to_digit('z') == 35u8);
- assert (to_digit('Z') == 35u8);
-}
-
-#[test]
-#[should_fail]
-#[ignore(cfg(target_os = "win32"))]
-fn test_to_digit_fail_1() {
- to_digit(' ');
-}
-
-#[test]
-#[should_fail]
-#[ignore(cfg(target_os = "win32"))]
-fn test_to_digit_fail_2() {
- to_digit('$');
+ assert to_digit('0', 10u) == some(0u);
+ assert to_digit('1', 2u) == some(1u);
+ assert to_digit('2', 3u) == some(2u);
+ assert to_digit('9', 10u) == some(9u);
+ assert to_digit('a', 16u) == some(10u);
+ assert to_digit('A', 16u) == some(10u);
+ assert to_digit('b', 16u) == some(11u);
+ assert to_digit('B', 16u) == some(11u);
+ assert to_digit('z', 36u) == some(35u);
+ assert to_digit('Z', 36u) == some(35u);
+
+ assert to_digit(' ', 10u) == none;
+ assert to_digit('$', 36u) == none;
}
#[test]
View
@@ -122,28 +122,27 @@ Leading and trailing whitespace are ignored.
Parameters:
-num - A string, possibly empty.
+num - A string
Returns:
-<NaN> If the string did not represent a valid number.
-Otherwise, the floating-point number represented [num].
+none if the string did not represent a valid number.
+Otherwise, some(n) where n is the floating-point
+number represented by [num].
*/
-fn from_str(num: str) -> float {
- let num = str::trim(num);
-
+fn from_str(num: str) -> option<float> {
let pos = 0u; //Current byte position in the string.
//Used to walk the string in O(n).
let len = str::len_bytes(num); //Length of the string, in bytes.
- if len == 0u { ret 0.; }
+ if len == 0u { ret none; }
let total = 0f; //Accumulated result
let c = 'z'; //Latest char.
//The string must start with one of the following characters.
alt str::char_at(num, 0u) {
'-' | '+' | '0' to '9' | '.' {}
- _ { ret NaN; }
+ _ { ret none; }
}
//Determine if first char is '-'/'+'. Set [pos] and [neg] accordingly.
@@ -173,7 +172,7 @@ fn from_str(num: str) -> float {
break;
}
_ {
- ret NaN;
+ ret none;
}
}
}
@@ -193,7 +192,7 @@ fn from_str(num: str) -> float {
break;
}
_ {
- ret NaN;
+ ret none;
}
}
}
@@ -238,17 +237,17 @@ fn from_str(num: str) -> float {
total = total * multiplier;
}
} else {
- ret NaN;
+ ret none;
}
}
if(pos < len) {
- ret NaN;
+ ret none;
} else {
if(neg) {
total *= -1f;
}
- ret total;
+ ret some(total);
}
}
@@ -291,39 +290,36 @@ fn pow_uint_to_uint_as_float(x: uint, pow: uint) -> float {
#[test]
fn test_from_str() {
- assert ( from_str("3") == 3. );
- assert ( from_str(" 3 ") == 3. );
- assert ( from_str("3.14") == 3.14 );
- assert ( from_str("+3.14") == 3.14 );
- assert ( from_str("-3.14") == -3.14 );
- assert ( from_str("2.5E10") == 25000000000. );
- assert ( from_str("2.5e10") == 25000000000. );
- assert ( from_str("25000000000.E-10") == 2.5 );
- assert ( from_str("") == 0. );
- assert ( from_str(".") == 0. );
- assert ( from_str(".e1") == 0. );
- assert ( from_str(".e-1") == 0. );
- assert ( from_str("5.") == 5. );
- assert ( from_str(".5") == 0.5 );
- assert ( from_str("0.5") == 0.5 );
- assert ( from_str("0.5 ") == 0.5 );
- assert ( from_str(" 0.5 ") == 0.5 );
- assert ( from_str(" -.5 ") == -0.5 );
- assert ( from_str(" -.5 ") == -0.5 );
- assert ( from_str(" -5 ") == -5. );
-
- assert ( is_NaN(from_str("x")) );
- assert ( from_str(" ") == 0. );
- assert ( from_str(" ") == 0. );
- assert ( from_str(" 0.5") == 0.5 );
- assert ( from_str(" 0.5 ") == 0.5 );
- assert ( from_str(" .1 ") == 0.1 );
- assert ( is_NaN(from_str("e")) );
- assert ( is_NaN(from_str("E")) );
- assert ( is_NaN(from_str("E1")) );
- assert ( is_NaN(from_str("1e1e1")) );
- assert ( is_NaN(from_str("1e1.1")) );
- assert ( is_NaN(from_str("1e1-1")) );
+ assert from_str("3") == some(3.);
+ assert from_str("3") == some(3.);
+ assert from_str("3.14") == some(3.14);
+ assert from_str("+3.14") == some(3.14);
+ assert from_str("-3.14") == some(-3.14);
+ assert from_str("2.5E10") == some(25000000000.);
+ assert from_str("2.5e10") == some(25000000000.);
+ assert from_str("25000000000.E-10") == some(2.5);
+ assert from_str(".") == some(0.);
+ assert from_str(".e1") == some(0.);
+ assert from_str(".e-1") == some(0.);
+ assert from_str("5.") == some(5.);
+ assert from_str(".5") == some(0.5);
+ assert from_str("0.5") == some(0.5);
+ assert from_str("0.5") == some(0.5);
+ assert from_str("0.5") == some(0.5);
+ assert from_str("-.5") == some(-0.5);
+ assert from_str("-.5") == some(-0.5);
+ assert from_str("-5") == some(-5.);
+
+ assert from_str("") == none;
+ assert from_str("x") == none;
+ assert from_str(" ") == none;
+ assert from_str(" ") == none;
+ assert from_str("e") == none;
+ assert from_str("E") == none;
+ assert from_str("E1") == none;
+ assert from_str("1e1e1") == none;
+ assert from_str("1e1.1") == none;
+ assert from_str("1e1-1") == none;
}
#[test]
Oops, something went wrong.

0 comments on commit ffd50b9

Please sign in to comment.