From aefc884cf6e1b2efbb6b629689b1daa3ebbd1e38 Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Mon, 3 Sep 2012 13:09:24 -0700 Subject: [PATCH] libcore: Add a from_str trait --- src/libcore/core.rc | 2 ++ src/libcore/int-template.rs | 5 +++++ src/libcore/uint-template.rs | 5 +++++ 3 files changed, 12 insertions(+) diff --git a/src/libcore/core.rc b/src/libcore/core.rc index 5b2378a21cdb..6cf9c82a5111 100644 --- a/src/libcore/core.rc +++ b/src/libcore/core.rc @@ -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 diff --git a/src/libcore/int-template.rs b/src/libcore/int-template.rs index 757a64d0d797..6c972da7353b 100644 --- a/src/libcore/int-template.rs +++ b/src/libcore/int-template.rs @@ -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 { /// Parse a string to an int fn from_str(s: &str) -> Option { parse_buf(str::to_bytes(s), 10u) } +impl T : FromStr { + static fn from_str(s: &str) -> Option { 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| { diff --git a/src/libcore/uint-template.rs b/src/libcore/uint-template.rs index dfab2e36096d..cf4abe4e16f1 100644 --- a/src/libcore/uint-template.rs +++ b/src/libcore/uint-template.rs @@ -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 { /// Parse a string to an int fn from_str(s: &str) -> Option { parse_buf(str::to_bytes(s), 10u) } +impl T : FromStr { + static fn from_str(s: &str) -> Option { from_str(s) } +} + /// Parse a string as an unsigned integer. fn from_str_radix(buf: &str, radix: u64) -> Option { if str::len(buf) == 0u { return None; }