Merge pull request #1317 from boggle/fix1315

fix to #1315 + small additions to std::either and result
This commit is contained in:
Graydon Hoare
2011-12-16 13:01:47 -08:00
14 changed files with 58 additions and 25 deletions

View File

@@ -123,7 +123,7 @@ Returns:
An u8 whose first bit is set if `if_true(v)` holds An u8 whose first bit is set if `if_true(v)` holds
*/ */
fn to_bit(v: t) -> u8 { if v { 1u8 } else { 0u8 } } pure fn to_bit(v: t) -> u8 { if v { 1u8 } else { 0u8 } }
// Local Variables: // Local Variables:
// mode: rust; // mode: rust;

View File

@@ -10,7 +10,7 @@ Function: ptr_eq
Determine if two shared boxes point to the same object Determine if two shared boxes point to the same object
*/ */
fn ptr_eq<T>(a: @T, b: @T) -> bool { pure fn ptr_eq<T>(a: @T, b: @T) -> bool {
// FIXME: ptr::addr_of // FIXME: ptr::addr_of
unsafe { unsafe {
let a_ptr: uint = unsafe::reinterpret_cast(a); let a_ptr: uint = unsafe::reinterpret_cast(a);

View File

@@ -122,7 +122,7 @@ pure fn to_digit(c: char) -> u8 unsafe {
Convert a char to the corresponding digit. Returns none when the Convert a char to the corresponding digit. Returns none when the
character is not a valid hexadecimal digit. character is not a valid hexadecimal digit.
*/ */
fn maybe_digit(c: char) -> option::t<u8> { pure fn maybe_digit(c: char) -> option::t<u8> {
alt c { alt c {
'0' to '9' { option::some(c as u8 - ('0' as u8)) } '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)) }
@@ -143,7 +143,7 @@ fn maybe_digit(c: char) -> option::t<u8> {
Returns: Returns:
-1 if a<b, 0 if a==b, +1 if a>b -1 if a<b, 0 if a==b, +1 if a>b
*/ */
fn cmp(a: char, b: char) -> int { pure fn cmp(a: char, b: char) -> int {
ret if b > a { -1 } ret if b > a { -1 }
else if b < a { 1 } else if b < a { 1 }
else { 0 } else { 0 }

View File

@@ -78,6 +78,31 @@ fn partition<copy T, copy U>(eithers: [t<T, U>])
ret {lefts: lefts, rights: rights}; ret {lefts: lefts, rights: rights};
} }
/*
Function: flip
Flips between left and right of a given either
*/
pure fn flip<copy T, copy U>(eith: t<T, U>) -> t<U, T> {
alt eith {
right(r) { left(r) }
left(l) { right(l) }
}
}
/*
Function: to_result
Converts either::t to a result::t, making the "right" choice
an ok result, and the "left" choice a fail
*/
pure fn to_result<copy T, copy U>(eith: t<T, U>) -> result::t<U, T> {
alt eith {
right(r) { result::ok(r) }
left(l) { result::err(l) }
}
}
// //
// Local Variables: // Local Variables:
// mode: rust // mode: rust

View File

@@ -30,7 +30,7 @@ Failure:
Fails if the value equals `none`. Fails if the value equals `none`.
*/ */
fn get<copy T>(opt: t<T>) -> T { pure fn get<copy T>(opt: t<T>) -> T {
alt opt { some(x) { ret x; } none. { fail "option none"; } } alt opt { some(x) { ret x; } none. { fail "option none"; } }
} }
@@ -61,7 +61,7 @@ Function: from_maybe
Returns the contained value or a default Returns the contained value or a default
*/ */
fn from_maybe<T>(def: T, opt: t<T>) -> T { pure fn from_maybe<T>(def: T, opt: t<T>) -> T {
alt opt { some(x) { x } none. { def } } alt opt { some(x) { x } none. { def } }
} }

View File

@@ -42,7 +42,7 @@ fn get<T, U>(res: t<T, U>) -> T {
ok(t) { t } ok(t) { t }
err(_) { err(_) {
// FIXME: Serialize the error value // FIXME: Serialize the error value
// and include it in the fail message // and include it in the fail message (maybe just note it)
fail "get called on error result"; fail "get called on error result";
} }
} }
@@ -71,7 +71,7 @@ Function: success
Returns true if the result is <ok> Returns true if the result is <ok>
*/ */
fn success<T, U>(res: t<T, U>) -> bool { pure fn success<T, U>(res: t<T, U>) -> bool {
alt res { alt res {
ok(_) { true } ok(_) { true }
err(_) { false } err(_) { false }
@@ -83,10 +83,17 @@ Function: failure
Returns true if the result is <error> Returns true if the result is <error>
*/ */
fn failure<T, U>(res: t<T, U>) -> bool { pure fn failure<T, U>(res: t<T, U>) -> bool {
!success(res) !success(res)
} }
pure fn to_either<copy T, copy U>(res: t<U, T>) -> either::t<T, U> {
alt res {
ok(res) { either::right(res) }
err(fail_) { either::left(fail_) }
}
}
/* /*
Function: chain Function: chain

View File

@@ -26,14 +26,14 @@ Function: eq
Bytewise string equality Bytewise string equality
*/ */
fn eq(&&a: str, &&b: str) -> bool { a == b } pure fn eq(&&a: str, &&b: str) -> bool { a == b }
/* /*
Function: lteq Function: lteq
Bytewise less than or equal Bytewise less than or equal
*/ */
fn lteq(&&a: str, &&b: str) -> bool { a <= b } pure fn lteq(&&a: str, &&b: str) -> bool { a <= b }
/* /*
Function: hash Function: hash
@@ -131,7 +131,7 @@ Function: byte_len
Returns the length in bytes of a string Returns the length in bytes of a string
*/ */
fn byte_len(s: str) -> uint unsafe { pure fn byte_len(s: str) -> uint unsafe {
let v: [u8] = unsafe::reinterpret_cast(s); let v: [u8] = unsafe::reinterpret_cast(s);
let vlen = vec::len(v); let vlen = vec::len(v);
unsafe::leak(v); unsafe::leak(v);
@@ -261,7 +261,7 @@ Function: utf8_char_width
FIXME: What does this function do? FIXME: What does this function do?
*/ */
fn utf8_char_width(b: u8) -> uint { pure fn utf8_char_width(b: u8) -> uint {
let byte: uint = b as uint; let byte: uint = b as uint;
if byte < 128u { ret 1u; } if byte < 128u { ret 1u; }
if byte < 192u { if byte < 192u {

View File

@@ -42,7 +42,7 @@ Function: last_os_error
Get a string representing the platform-dependent last error Get a string representing the platform-dependent last error
*/ */
fn last_os_error() -> str { fn last_os_error() -> str {
ret rustrt::last_os_error(); rustrt::last_os_error()
} }
/* /*

View File

@@ -181,7 +181,7 @@ Returns the first element of a vector
Predicates: Predicates:
<is_not_empty> (v) <is_not_empty> (v)
*/ */
fn head<copy T>(v: [const T]) : is_not_empty(v) -> T { ret v[0]; } pure fn head<copy T>(v: [const T]) : is_not_empty(v) -> T { ret v[0]; }
/* /*
Function: tail Function: tail
@@ -221,7 +221,7 @@ Returns:
An option containing the last element of `v` if `v` is not empty, or An option containing the last element of `v` if `v` is not empty, or
none if `v` is empty. none if `v` is empty.
*/ */
fn last<copy T>(v: [const T]) -> option::t<T> { pure fn last<copy T>(v: [const T]) -> option::t<T> {
if len(v) == 0u { ret none; } if len(v) == 0u { ret none; }
ret some(v[len(v) - 1u]); ret some(v[len(v) - 1u]);
} }
@@ -234,7 +234,7 @@ Returns the last element of a non-empty vector `v`
Predicates: Predicates:
<is_not_empty> (v) <is_not_empty> (v)
*/ */
fn last_total<copy T>(v: [const T]) : is_not_empty(v) -> T { pure fn last_total<copy T>(v: [const T]) : is_not_empty(v) -> T {
ret v[len(v) - 1u]; ret v[len(v) - 1u];
} }

View File

@@ -128,7 +128,7 @@ Function: get
Retreive the value at index `i` Retreive the value at index `i`
*/ */
fn get(v: t, i: uint) -> bool { pure fn get(v: t, i: uint) -> bool {
assert (i < v.nbits); assert (i < v.nbits);
let bits = uint_bits; let bits = uint_bits;
let w = i / bits; let w = i / bits;

View File

@@ -112,7 +112,7 @@ Function: tail
Returns all but the first element of a list Returns all but the first element of a list
*/ */
fn tail<copy T>(ls: list<T>) -> list<T> { pure fn tail<copy T>(ls: list<T>) -> list<T> {
alt ls { cons(_, tl) { ret *tl; } nil. { fail "list empty" } } alt ls { cons(_, tl) { ret *tl; } nil. { fail "list empty" } }
} }
@@ -121,7 +121,7 @@ Function: head
Returns the first element of a list Returns the first element of a list
*/ */
fn head<copy T>(ls: list<T>) -> T { pure fn head<copy T>(ls: list<T>) -> T {
alt ls { cons(hd, _) { ret hd; } nil. { fail "list empty" } } alt ls { cons(hd, _) { ret hd; } nil. { fail "list empty" } }
} }
@@ -130,7 +130,7 @@ Function: append
Appends one list to another Appends one list to another
*/ */
fn append<copy T>(l: list<T>, m: list<T>) -> list<T> { pure fn append<copy T>(l: list<T>, m: list<T>) -> list<T> {
alt l { alt l {
nil. { ret m; } nil. { ret m; }
cons(x, xs) { let rest = append(*xs, m); ret cons(x, @rest); } cons(x, xs) { let rest = append(*xs, m); ret cons(x, @rest); }

View File

@@ -30,6 +30,7 @@ fn list_dir(path: str) -> [str] {
} }
// FIXME make pure when str::char_at is
fn path_is_absolute(p: str) -> bool { ret str::char_at(p, 0u) == '/'; } fn path_is_absolute(p: str) -> bool { ret str::char_at(p, 0u) == '/'; }
const path_sep: char = '/'; const path_sep: char = '/';

View File

@@ -310,7 +310,7 @@ Returns:
A negative value if `left < right`, 0 if eq(left, right) or a positive A negative value if `left < right`, 0 if eq(left, right) or a positive
value if `left > right` value if `left > right`
*/ */
fn cmp(left: rope, right: rope) -> int { fn cmp(left: rope, right: rope) -> int {
alt((left, right)) { alt((left, right)) {
(node::empty., node::empty.) { ret 0; } (node::empty., node::empty.) { ret 0; }

View File

@@ -151,16 +151,16 @@ mod icu {
#[link_name = "icuuc"] #[link_name = "icuuc"]
#[abi = "cdecl"] #[abi = "cdecl"]
native mod libicu { native mod libicu {
fn u_hasBinaryProperty(c: UChar32, which: UProperty) -> UBool; pure fn u_hasBinaryProperty(c: UChar32, which: UProperty) -> UBool;
} }
} }
fn is_XID_start(c: char) -> bool { pure fn is_XID_start(c: char) -> bool {
ret icu::libicu::u_hasBinaryProperty(c, icu::UCHAR_XID_START) ret icu::libicu::u_hasBinaryProperty(c, icu::UCHAR_XID_START)
== icu::TRUE; == icu::TRUE;
} }
fn is_XID_continue(c: char) -> bool { pure fn is_XID_continue(c: char) -> bool {
ret icu::libicu::u_hasBinaryProperty(c, icu::UCHAR_XID_START) ret icu::libicu::u_hasBinaryProperty(c, icu::UCHAR_XID_START)
== icu::TRUE; == icu::TRUE;
} }