libcore: Add a from_str trait

This commit is contained in:
Patrick Walton 2012-09-03 13:09:24 -07:00
parent 07ee2997e6
commit aefc884cf6
3 changed files with 12 additions and 0 deletions

View File

@ -49,6 +49,7 @@ export extfmt;
export rt;
export tuple;
export to_str, to_bytes;
export from_str;
export util;
export dvec, dvec_iter;
export dlist, dlist_iter;
@ -188,6 +189,7 @@ mod option_iter {
mod result;
mod to_str;
mod to_bytes;
mod from_str;
mod util;
// Data structure modules

View File

@ -4,6 +4,7 @@
import T = inst::T;
import cmp::{Eq, Ord};
import from_str::FromStr;
import num::from_int;
export min_value, max_value;
@ -162,6 +163,10 @@ fn parse_buf(buf: &[u8], radix: uint) -> Option<T> {
/// Parse a string to an int
fn from_str(s: &str) -> Option<T> { parse_buf(str::to_bytes(s), 10u) }
impl T : FromStr {
static fn from_str(s: &str) -> Option<T> { from_str(s) }
}
/// Convert to a string in a given base
fn to_str(n: T, radix: uint) -> ~str {
do to_str_bytes(n, radix) |slice| {

View File

@ -4,6 +4,7 @@
import T = inst::T;
import cmp::{Eq, Ord};
import from_str::FromStr;
export min_value, max_value;
export min, max;
@ -145,6 +146,10 @@ fn parse_buf(buf: &[const u8], radix: uint) -> Option<T> {
/// Parse a string to an int
fn from_str(s: &str) -> Option<T> { parse_buf(str::to_bytes(s), 10u) }
impl T : FromStr {
static fn from_str(s: &str) -> Option<T> { from_str(s) }
}
/// Parse a string as an unsigned integer.
fn from_str_radix(buf: &str, radix: u64) -> Option<u64> {
if str::len(buf) == 0u { return None; }