Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String conversion updates #18976

Merged
merged 6 commits into from
Nov 16, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/compiletest/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::from_str::FromStr;
use std::fmt;
use std::str::FromStr;
use regex::Regex;

#[deriving(Clone, PartialEq)]
Expand Down
2 changes: 1 addition & 1 deletion src/compiletest/compiletest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ extern crate regex;
use std::os;
use std::io;
use std::io::fs;
use std::from_str::FromStr;
use std::str::FromStr;
use getopts::{optopt, optflag, reqopt};
use common::Config;
use common::{Pretty, DebugInfoGdb, DebugInfoLldb, Codegen};
Expand Down
8 changes: 3 additions & 5 deletions src/compiletest/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ use common::Config;
use common;
use util;

use std::from_str::FromStr;

pub struct TestProps {
// Lines that should be expected, in order, on standard out
pub error_patterns: Vec<String> ,
Expand Down Expand Up @@ -353,8 +351,8 @@ pub fn gdb_version_to_int(version_string: &str) -> int {
panic!("{}", error_string);
}

let major: int = FromStr::from_str(components[0]).expect(error_string);
let minor: int = FromStr::from_str(components[1]).expect(error_string);
let major: int = from_str(components[0]).expect(error_string);
let minor: int = from_str(components[1]).expect(error_string);

return major * 1000 + minor;
}
Expand All @@ -364,6 +362,6 @@ pub fn lldb_version_to_int(version_string: &str) -> int {
"Encountered LLDB version string with unexpected format: {}",
version_string);
let error_string = error_string.as_slice();
let major: int = FromStr::from_str(version_string).expect(error_string);
let major: int = from_str(version_string).expect(error_string);
return major;
}
3 changes: 1 addition & 2 deletions src/doc/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -3108,11 +3108,10 @@ then the expression completes.
Some examples of call expressions:

```
# use std::from_str::FromStr;
# fn add(x: int, y: int) -> int { 0 }

let x: int = add(1, 2);
let pi: Option<f32> = FromStr::from_str("3.14");
let pi: Option<f32> = from_str("3.14");
```

### Lambda expressions
Expand Down
2 changes: 1 addition & 1 deletion src/etc/vim/syntax/rust.vim
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ syn keyword rustTrait RawPtr
syn keyword rustTrait Buffer Writer Reader Seek
syn keyword rustTrait Str StrVector StrSlice
syn keyword rustTrait IntoMaybeOwned StrAllocating UnicodeStrSlice
syn keyword rustTrait ToString IntoStr
syn keyword rustTrait ToString IntoString
syn keyword rustTrait Tuple1 Tuple2 Tuple3 Tuple4
syn keyword rustTrait Tuple5 Tuple6 Tuple7 Tuple8
syn keyword rustTrait Tuple9 Tuple10 Tuple11 Tuple12
Expand Down
1 change: 1 addition & 0 deletions src/libcollections/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ pub use core::str::{CharSplitsN, AnyLines, MatchIndices, StrSplits};
pub use core::str::{Utf16CodeUnits, eq_slice, is_utf8, is_utf16, Utf16Items};
pub use core::str::{Utf16Item, ScalarValue, LoneSurrogate, utf16_items};
pub use core::str::{truncate_utf16_at_nul, utf8_char_width, CharRange};
pub use core::str::{FromStr, from_str};
pub use core::str::{Str, StrPrelude};
pub use unicode::str::{UnicodeStrPrelude, Words, Graphemes, GraphemeIndices};

Expand Down
53 changes: 51 additions & 2 deletions src/libcollections/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use core::raw::Slice as RawSlice;
use hash;
use slice::CloneSliceAllocPrelude;
use str;
use str::{CharRange, StrAllocating, MaybeOwned, Owned};
use str::{CharRange, FromStr, StrAllocating, MaybeOwned, Owned};
use str::Slice as MaybeOwnedSlice; // So many `Slice`s...
use vec::{DerefVec, Vec, as_vec};

Expand Down Expand Up @@ -795,6 +795,33 @@ pub fn as_string<'a>(x: &'a str) -> DerefString<'a> {
DerefString { x: as_vec(x.as_bytes()) }
}

impl FromStr for String {
#[inline]
fn from_str(s: &str) -> Option<String> {
Some(String::from_str(s))
}
}

/// Trait for converting a type to a string, consuming it in the process.
pub trait IntoString {
/// Consume and convert to a string.
fn into_string(self) -> String;
}

/// A generic trait for converting a value to a string
pub trait ToString {
/// Converts the value of `self` to an owned string
fn to_string(&self) -> String;
}

impl<T: fmt::Show> ToString for T {
fn to_string(&self) -> String {
let mut buf = Vec::<u8>::new();
let _ = format_args!(|args| fmt::write(&mut buf, args), "{}", self);
String::from_utf8(buf).unwrap()
}
}

/// Unsafe operations
#[unstable = "waiting on raw module conventions"]
pub mod raw {
Expand Down Expand Up @@ -860,7 +887,7 @@ mod tests {

use str;
use str::{Str, StrPrelude, Owned};
use super::{as_string, String};
use super::{as_string, String, ToString};
use vec::Vec;
use slice::CloneSliceAllocPrelude;

Expand Down Expand Up @@ -1164,6 +1191,28 @@ mod tests {
assert_eq!("oob", s[1..4]);
}

#[test]
fn test_simple_types() {
assert_eq!(1i.to_string(), "1".to_string());
assert_eq!((-1i).to_string(), "-1".to_string());
assert_eq!(200u.to_string(), "200".to_string());
assert_eq!(2u8.to_string(), "2".to_string());
assert_eq!(true.to_string(), "true".to_string());
assert_eq!(false.to_string(), "false".to_string());
assert_eq!(().to_string(), "()".to_string());
assert_eq!(("hi".to_string()).to_string(), "hi".to_string());
}

#[test]
fn test_vectors() {
let x: Vec<int> = vec![];
assert_eq!(x.to_string(), "[]".to_string());
assert_eq!((vec![1i]).to_string(), "[1]".to_string());
assert_eq!((vec![1i, 2, 3]).to_string(), "[1, 2, 3]".to_string());
assert!((vec![vec![], vec![1i], vec![1i, 1]]).to_string() ==
"[[], [1], [1, 1]]".to_string());
}

#[bench]
fn bench_with_capacity(b: &mut Bencher) {
b.iter(|| {
Expand Down
7 changes: 7 additions & 0 deletions src/libcollections/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1653,6 +1653,13 @@ impl<T> Vec<T> {
}
}

impl<'a> fmt::FormatWriter for Vec<u8> {
fn write(&mut self, buf: &[u8]) -> fmt::Result {
self.push_all(buf);
Ok(())
}
}

#[cfg(test)]
mod tests {
extern crate test;
Expand Down
11 changes: 9 additions & 2 deletions src/libcore/num/f32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

use intrinsics;
use mem;
use num::{FPNormal, FPCategory, FPZero, FPSubnormal, FPInfinite, FPNaN};
use num::Float;
use num::{Float, FPNormal, FPCategory, FPZero, FPSubnormal, FPInfinite, FPNaN};
use num::from_str_radix;
use option::Option;

pub const RADIX: uint = 2u;
Expand Down Expand Up @@ -424,3 +424,10 @@ impl Float for f32 {
self * (value / 180.0f32)
}
}

#[inline]
#[allow(missing_docs)]
#[deprecated="Use `FromStrRadix::from_str_radix(src, 16)`"]
pub fn from_str_hex(src: &str) -> Option<f32> {
from_str_radix(src, 16)
}
11 changes: 9 additions & 2 deletions src/libcore/num/f64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

use intrinsics;
use mem;
use num::{FPNormal, FPCategory, FPZero, FPSubnormal, FPInfinite, FPNaN};
use num::Float;
use num::{Float, FPNormal, FPCategory, FPZero, FPSubnormal, FPInfinite, FPNaN};
use num::from_str_radix;
use option::Option;

// FIXME(#5527): These constants should be deprecated once associated
Expand Down Expand Up @@ -430,3 +430,10 @@ impl Float for f64 {
self * (value / 180.0)
}
}

#[inline]
#[allow(missing_docs)]
#[deprecated="Use `FromStrRadix::from_str_radix(src, 16)`"]
pub fn from_str_hex(src: &str) -> Option<f64> {
from_str_radix(src, 16)
}
Loading