diff options
| author | Huon Wilson <dbau.pp+github@gmail.com> | 2013-06-11 13:10:37 +1000 |
|---|---|---|
| committer | Huon Wilson <dbau.pp+github@gmail.com> | 2013-06-12 12:21:04 +1000 |
| commit | efc71a8bdb28fba88d0cc8916b33838bf43b3a8d (patch) | |
| tree | ad0086d4319facd8da21583e19a952a01250bbbd /src/libstd/num | |
| parent | ba4a4778cc17c64c33a891a0d2565a1fb04ddffc (diff) | |
| download | rust-efc71a8bdb28fba88d0cc8916b33838bf43b3a8d.tar.gz rust-efc71a8bdb28fba88d0cc8916b33838bf43b3a8d.zip | |
std: unify the str -> [u8] functions as 3 methods: .as_bytes() and .as_bytes_with_null[_consume]().
The first acts on &str and is not nul-terminated, the last two act on strings that are always null terminated (&'static str, ~str and @str).
Diffstat (limited to 'src/libstd/num')
| -rw-r--r-- | src/libstd/num/int_macros.rs | 42 | ||||
| -rw-r--r-- | src/libstd/num/strconv.rs | 13 | ||||
| -rw-r--r-- | src/libstd/num/uint_macros.rs | 20 |
3 files changed, 38 insertions, 37 deletions
diff --git a/src/libstd/num/int_macros.rs b/src/libstd/num/int_macros.rs index 3583e2f366f..74f74d11b73 100644 --- a/src/libstd/num/int_macros.rs +++ b/src/libstd/num/int_macros.rs @@ -793,27 +793,27 @@ mod tests { #[test] fn test_parse_bytes() { - use str::to_bytes; - assert_eq!(parse_bytes(to_bytes("123"), 10u), Some(123 as $T)); - assert_eq!(parse_bytes(to_bytes("1001"), 2u), Some(9 as $T)); - assert_eq!(parse_bytes(to_bytes("123"), 8u), Some(83 as $T)); - assert_eq!(i32::parse_bytes(to_bytes("123"), 16u), Some(291 as i32)); - assert_eq!(i32::parse_bytes(to_bytes("ffff"), 16u), Some(65535 as i32)); - assert_eq!(i32::parse_bytes(to_bytes("FFFF"), 16u), Some(65535 as i32)); - assert_eq!(parse_bytes(to_bytes("z"), 36u), Some(35 as $T)); - assert_eq!(parse_bytes(to_bytes("Z"), 36u), Some(35 as $T)); - - assert_eq!(parse_bytes(to_bytes("-123"), 10u), Some(-123 as $T)); - assert_eq!(parse_bytes(to_bytes("-1001"), 2u), Some(-9 as $T)); - assert_eq!(parse_bytes(to_bytes("-123"), 8u), Some(-83 as $T)); - assert_eq!(i32::parse_bytes(to_bytes("-123"), 16u), Some(-291 as i32)); - assert_eq!(i32::parse_bytes(to_bytes("-ffff"), 16u), Some(-65535 as i32)); - assert_eq!(i32::parse_bytes(to_bytes("-FFFF"), 16u), Some(-65535 as i32)); - assert_eq!(parse_bytes(to_bytes("-z"), 36u), Some(-35 as $T)); - assert_eq!(parse_bytes(to_bytes("-Z"), 36u), Some(-35 as $T)); - - assert!(parse_bytes(to_bytes("Z"), 35u).is_none()); - assert!(parse_bytes(to_bytes("-9"), 2u).is_none()); + use str::StrSlice; + assert_eq!(parse_bytes("123".as_bytes(), 10u), Some(123 as $T)); + assert_eq!(parse_bytes("1001".as_bytes(), 2u), Some(9 as $T)); + assert_eq!(parse_bytes("123".as_bytes(), 8u), Some(83 as $T)); + assert_eq!(i32::parse_bytes("123".as_bytes(), 16u), Some(291 as i32)); + assert_eq!(i32::parse_bytes("ffff".as_bytes(), 16u), Some(65535 as i32)); + assert_eq!(i32::parse_bytes("FFFF".as_bytes(), 16u), Some(65535 as i32)); + assert_eq!(parse_bytes("z".as_bytes(), 36u), Some(35 as $T)); + assert_eq!(parse_bytes("Z".as_bytes(), 36u), Some(35 as $T)); + + assert_eq!(parse_bytes("-123".as_bytes(), 10u), Some(-123 as $T)); + assert_eq!(parse_bytes("-1001".as_bytes(), 2u), Some(-9 as $T)); + assert_eq!(parse_bytes("-123".as_bytes(), 8u), Some(-83 as $T)); + assert_eq!(i32::parse_bytes("-123".as_bytes(), 16u), Some(-291 as i32)); + assert_eq!(i32::parse_bytes("-ffff".as_bytes(), 16u), Some(-65535 as i32)); + assert_eq!(i32::parse_bytes("-FFFF".as_bytes(), 16u), Some(-65535 as i32)); + assert_eq!(parse_bytes("-z".as_bytes(), 36u), Some(-35 as $T)); + assert_eq!(parse_bytes("-Z".as_bytes(), 36u), Some(-35 as $T)); + + assert!(parse_bytes("Z".as_bytes(), 35u).is_none()); + assert!(parse_bytes("-9".as_bytes(), 2u).is_none()); } #[test] diff --git a/src/libstd/num/strconv.rs b/src/libstd/num/strconv.rs index 30efe9a3922..3905d82cd0f 100644 --- a/src/libstd/num/strconv.rs +++ b/src/libstd/num/strconv.rs @@ -16,6 +16,7 @@ use ops::{Add, Sub, Mul, Div, Rem, Neg}; use option::{None, Option, Some}; use char; use str; +use str::{StrSlice}; use kinds::Copy; use vec; use vec::{CopyableVector, ImmutableVector}; @@ -189,18 +190,18 @@ pub fn to_str_bytes_common<T:NumCast+Zero+One+Eq+Ord+NumStrConv+Copy+ let _1: T = One::one(); if is_NaN(num) { - return (str::to_bytes("NaN"), true); + return ("NaN".as_bytes().to_owned(), true); } else if is_inf(num){ return match sign { - SignAll => (str::to_bytes("+inf"), true), - _ => (str::to_bytes("inf"), true) + SignAll => ("+inf".as_bytes().to_owned(), true), + _ => ("inf".as_bytes().to_owned(), true) } } else if is_neg_inf(num) { return match sign { - SignNone => (str::to_bytes("inf"), true), - _ => (str::to_bytes("-inf"), true), + SignNone => ("inf".as_bytes().to_owned(), true), + _ => ("-inf".as_bytes().to_owned(), true), } } @@ -638,7 +639,7 @@ pub fn from_str_common<T:NumCast+Zero+One+Eq+Ord+Copy+Div<T,T>+Mul<T,T>+ special: bool, exponent: ExponentFormat, empty_zero: bool, ignore_underscores: bool ) -> Option<T> { - from_str_bytes_common(str::to_bytes(buf), radix, negative, + from_str_bytes_common(buf.as_bytes(), radix, negative, fractional, special, exponent, empty_zero, ignore_underscores) } diff --git a/src/libstd/num/uint_macros.rs b/src/libstd/num/uint_macros.rs index a7aebf1f176..2bc1ca9c673 100644 --- a/src/libstd/num/uint_macros.rs +++ b/src/libstd/num/uint_macros.rs @@ -538,16 +538,16 @@ mod tests { #[test] pub fn test_parse_bytes() { - use str::to_bytes; - assert_eq!(parse_bytes(to_bytes("123"), 10u), Some(123u as $T)); - assert_eq!(parse_bytes(to_bytes("1001"), 2u), Some(9u as $T)); - assert_eq!(parse_bytes(to_bytes("123"), 8u), Some(83u as $T)); - assert_eq!(u16::parse_bytes(to_bytes("123"), 16u), Some(291u as u16)); - assert_eq!(u16::parse_bytes(to_bytes("ffff"), 16u), Some(65535u as u16)); - assert_eq!(parse_bytes(to_bytes("z"), 36u), Some(35u as $T)); - - assert!(parse_bytes(to_bytes("Z"), 10u).is_none()); - assert!(parse_bytes(to_bytes("_"), 2u).is_none()); + use str::StrSlice; + assert_eq!(parse_bytes("123".as_bytes(), 10u), Some(123u as $T)); + assert_eq!(parse_bytes("1001".as_bytes(), 2u), Some(9u as $T)); + assert_eq!(parse_bytes("123".as_bytes(), 8u), Some(83u as $T)); + assert_eq!(u16::parse_bytes("123".as_bytes(), 16u), Some(291u as u16)); + assert_eq!(u16::parse_bytes("ffff".as_bytes(), 16u), Some(65535u as u16)); + assert_eq!(parse_bytes("z".as_bytes(), 36u), Some(35u as $T)); + + assert!(parse_bytes("Z".as_bytes(), 10u).is_none()); + assert!(parse_bytes("_".as_bytes(), 2u).is_none()); } #[test] |
