Skip to content

Commit

Permalink
Fix errors
Browse files Browse the repository at this point in the history
  • Loading branch information
aochagavia committed Jul 15, 2014
1 parent c6b82c7 commit 584fbde
Show file tree
Hide file tree
Showing 22 changed files with 51 additions and 110 deletions.
58 changes: 7 additions & 51 deletions src/libcollections/str.rs
Expand Up @@ -94,66 +94,26 @@ pub use unicode::{Words, UnicodeStrSlice};
Section: Creating a string
*/

/// Consumes a vector of bytes to create a new utf-8 string.
///
/// Returns `Err` with the original vector if the vector contains invalid
/// UTF-8.
///
/// # Example
///
/// ```rust
/// use std::str;
/// let hello_vec = vec![104, 101, 108, 108, 111];
/// let string = str::from_utf8_owned(hello_vec);
/// assert_eq!(string, Ok("hello".to_string()));
/// ```
/// Deprecated. Replaced by `String::from_utf8`
#[deprecated = "Replaced by `String::from_utf8`"]
pub fn from_utf8_owned(vv: Vec<u8>) -> Result<String, Vec<u8>> {
String::from_utf8(vv)
}

/// Convert a byte to a UTF-8 string
///
/// # Failure
///
/// Fails if invalid UTF-8
///
/// # Example
///
/// ```rust
/// use std::str;
/// let string = str::from_byte(104);
/// assert_eq!(string.as_slice(), "h");
/// ```
/// Deprecated. Replaced by `String::from_byte`
#[deprecated = "Replaced by String::from_byte"]
pub fn from_byte(b: u8) -> String {
assert!(b < 128u8);
String::from_char(1, b as char)
}

/// Convert a char to a string
///
/// # Example
///
/// ```rust
/// use std::str;
/// let string = str::from_char('b');
/// assert_eq!(string.as_slice(), "b");
/// ```
/// Deprecated. Use `String::from_char` or `char::to_string()` instead
#[deprecated = "use String::from_char or char.to_string()"]
pub fn from_char(ch: char) -> String {
String::from_char(1, ch)
}

/// Convert a vector of chars to a string
///
/// # Example
///
/// ```rust
/// let chars = ['h', 'e', 'l', 'l', 'o'];
/// let string = String::from_chars(chars);
/// assert_eq!(string.as_slice(), "hello");
/// ```
/// Deprecated. Replaced by `String::from_chars`
#[deprecated = "use String::from_chars instead"]
pub fn from_chars(chs: &[char]) -> String {
chs.iter().map(|c| *c).collect()
Expand Down Expand Up @@ -649,7 +609,6 @@ pub mod raw {
#[test]
fn test_from_buf_len() {
use slice::ImmutableVector;
use str::StrAllocating;

unsafe {
let a = vec![65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 0u8];
Expand Down Expand Up @@ -854,8 +813,7 @@ mod tests {
use std::default::Default;
use std::char::Char;
use std::clone::Clone;
use std::cmp::{Equal, Greater, Less, Ord, Eq, PartialOrd, PartialEq, Equiv};
use std::result::{Ok, Err};
use std::cmp::{Equal, Greater, Less, Ord, PartialOrd, Equiv};
use std::option::{Some, None};
use std::ptr::RawPtr;
use std::iter::{Iterator, DoubleEndedIterator};
Expand Down Expand Up @@ -1546,7 +1504,7 @@ mod tests {
let mut pos = 0;
for ch in v.iter() {
assert!(s.char_at(pos) == *ch);
pos += from_char(*ch).len();
pos += String::from_char(1, *ch).len();
}
}

Expand All @@ -1557,7 +1515,7 @@ mod tests {
let mut pos = s.len();
for ch in v.iter().rev() {
assert!(s.char_at_reverse(pos) == *ch);
pos -= from_char(*ch).len();
pos -= String::from_char(1, *ch).len();
}
}

Expand Down Expand Up @@ -1996,10 +1954,8 @@ String::from_str("\u1111\u1171\u11b6"));
mod bench {
use test::Bencher;
use super::*;
use vec::Vec;
use std::iter::{Iterator, DoubleEndedIterator};
use std::collections::Collection;
use std::slice::Vector;

#[bench]
fn char_iterator(b: &mut Bencher) {
Expand Down
49 changes: 27 additions & 22 deletions src/libcollections/string.rs
Expand Up @@ -99,7 +99,7 @@ impl String {
///
/// ```rust
/// let input = b"Hello \xF0\x90\x80World";
/// let output = std::str::from_utf8_lossy(input);
/// let output = String::from_utf8_lossy(input);
/// assert_eq!(output.as_slice(), "Hello \uFFFDWorld");
/// ```
pub fn from_utf8_lossy<'a>(v: &'a [u8]) -> MaybeOwned<'a> {
Expand Down Expand Up @@ -218,18 +218,18 @@ impl String {
Owned(res.into_string())
}

/// Decode a UTF-16 encoded vector `v` into a string, returning `None`
/// Decode a UTF-16 encoded vector `v` into a `String`, returning `None`
/// if `v` contains any invalid data.
///
/// # Example
///
/// ```rust
/// // 𝄞music
/// // 𝄞music
/// let mut v = [0xD834, 0xDD1E, 0x006d, 0x0075,
/// 0x0073, 0x0069, 0x0063];
/// assert_eq!(String::from_utf16(v), Some("𝄞music".to_string()));
/// assert_eq!(String::from_utf16(v), Some("𝄞music".to_string()));
///
/// // 𝄞mu<invalid>ic
/// // 𝄞mu<invalid>ic
/// v[4] = 0xD800;
/// assert_eq!(String::from_utf16(v), None);
/// ```
Expand All @@ -249,13 +249,13 @@ impl String {
///
/// # Example
/// ```rust
/// // 𝄞mus<invalid>ic<invalid>
/// // 𝄞mus<invalid>ic<invalid>
/// let v = [0xD834, 0xDD1E, 0x006d, 0x0075,
/// 0x0073, 0xDD1E, 0x0069, 0x0063,
/// 0xD834];
///
/// assert_eq!(String::from_utf16_lossy(v),
/// "𝄞mus\uFFFDic\uFFFD".to_string());
/// "𝄞mus\uFFFDic\uFFFD".to_string());
/// ```
pub fn from_utf16_lossy(v: &[u16]) -> String {
str::utf16_items(v).map(|c| c.to_char_lossy()).collect()
Expand Down Expand Up @@ -575,8 +575,9 @@ mod tests {

use Mutable;
use str;
use str::{Str, StrSlice, MaybeOwned, Owned, Slice};
use str::{Str, StrSlice, Owned, Slice};
use super::String;
use vec::Vec;

#[test]
fn test_from_str() {
Expand All @@ -587,10 +588,10 @@ mod tests {
#[test]
fn test_from_utf8() {
let xs = Vec::from_slice(b"hello");
assert_eq!(String::from_utf8(xs), Ok("hello".to_string()));
assert_eq!(String::from_utf8(xs), Ok(String::from_str("hello")));

let xs = Vec::from_slice("ศไทย中华Việt Nam".as_bytes());
assert_eq!(String::from_utf8(xs), Ok("ศไทย中华Việt Nam".to_string()));
let xs = Vec::from_slice("ศไทย中华Việt Nam".as_bytes());
assert_eq!(String::from_utf8(xs), Ok(String::from_str("ศไทย中华Việt Nam")));

let xs = Vec::from_slice(b"hello\xFF");
assert_eq!(String::from_utf8(xs),
Expand All @@ -602,21 +603,24 @@ mod tests {
let xs = b"hello";
assert_eq!(String::from_utf8_lossy(xs), Slice("hello"));

let xs = "ศไทย中华Việt Nam".as_bytes();
assert_eq!(String::from_utf8_lossy(xs), Slice("ศไทย中华Việt Nam"));
let xs = "ศไทย中华Việt Nam".as_bytes();
assert_eq!(String::from_utf8_lossy(xs), Slice("ศไทย中华Việt Nam"));

let xs = b"Hello\xC2 There\xFF Goodbye";
assert_eq!(String::from_utf8_lossy(xs), Owned(String::from_str("Hello\uFFFD There\uFFFD Goodbye")));
assert_eq!(String::from_utf8_lossy(xs),
Owned(String::from_str("Hello\uFFFD There\uFFFD Goodbye")));

let xs = b"Hello\xC0\x80 There\xE6\x83 Goodbye";
assert_eq!(String::from_utf8_lossy(xs),
Owned(String::from_str("Hello\uFFFD\uFFFD There\uFFFD Goodbye")));

let xs = b"\xF5foo\xF5\x80bar";
assert_eq!(String::from_utf8_lossy(xs), Owned(String::from_str("\uFFFDfoo\uFFFD\uFFFDbar")));
assert_eq!(String::from_utf8_lossy(xs),
Owned(String::from_str("\uFFFDfoo\uFFFD\uFFFDbar")));

let xs = b"\xF1foo\xF1\x80bar\xF1\x80\x80baz";
assert_eq!(String::from_utf8_lossy(xs), Owned(String::from_str("\uFFFDfoo\uFFFDbar\uFFFDbaz")));
assert_eq!(String::from_utf8_lossy(xs),
Owned(String::from_str("\uFFFDfoo\uFFFDbar\uFFFDbaz")));

let xs = b"\xF4foo\xF4\x80bar\xF4\xBFbaz";
assert_eq!(String::from_utf8_lossy(xs),
Expand All @@ -635,21 +639,21 @@ mod tests {
#[test]
fn test_from_utf16() {
let pairs =
[(String::from_str("𐍅𐌿𐌻𐍆𐌹𐌻𐌰\n"),
[(String::from_str("𐍅𐌿𐌻𐍆𐌹𐌻𐌰\n"),
vec![0xd800_u16, 0xdf45_u16, 0xd800_u16, 0xdf3f_u16,
0xd800_u16, 0xdf3b_u16, 0xd800_u16, 0xdf46_u16,
0xd800_u16, 0xdf39_u16, 0xd800_u16, 0xdf3b_u16,
0xd800_u16, 0xdf30_u16, 0x000a_u16]),

(String::from_str("𐐒𐑉𐐮𐑀𐐲𐑋 𐐏𐐲𐑍\n"),
(String::from_str("𐐒𐑉𐐮𐑀𐐲𐑋 𐐏𐐲𐑍\n"),
vec![0xd801_u16, 0xdc12_u16, 0xd801_u16,
0xdc49_u16, 0xd801_u16, 0xdc2e_u16, 0xd801_u16,
0xdc40_u16, 0xd801_u16, 0xdc32_u16, 0xd801_u16,
0xdc4b_u16, 0x0020_u16, 0xd801_u16, 0xdc0f_u16,
0xd801_u16, 0xdc32_u16, 0xd801_u16, 0xdc4d_u16,
0x000a_u16]),

(String::from_str("𐌀𐌖𐌋𐌄𐌑𐌉·𐌌𐌄𐌕𐌄𐌋𐌉𐌑\n"),
(String::from_str("𐌀𐌖𐌋𐌄𐌑𐌉·𐌌𐌄𐌕𐌄𐌋𐌉𐌑\n"),
vec![0xd800_u16, 0xdf00_u16, 0xd800_u16, 0xdf16_u16,
0xd800_u16, 0xdf0b_u16, 0xd800_u16, 0xdf04_u16,
0xd800_u16, 0xdf11_u16, 0xd800_u16, 0xdf09_u16,
Expand All @@ -658,7 +662,7 @@ mod tests {
0xdf04_u16, 0xd800_u16, 0xdf0b_u16, 0xd800_u16,
0xdf09_u16, 0xd800_u16, 0xdf11_u16, 0x000a_u16 ]),

(String::from_str("𐒋𐒘𐒈𐒑𐒛𐒒 𐒕𐒓 𐒈𐒚𐒍 𐒏𐒜𐒒𐒖𐒆 𐒕𐒆\n"),
(String::from_str("𐒋𐒘𐒈𐒑𐒛𐒒 𐒕𐒓 𐒈𐒚𐒍 𐒏𐒜𐒒𐒖𐒆 𐒕𐒆\n"),
vec![0xd801_u16, 0xdc8b_u16, 0xd801_u16, 0xdc98_u16,
0xd801_u16, 0xdc88_u16, 0xd801_u16, 0xdc91_u16,
0xd801_u16, 0xdc9b_u16, 0xd801_u16, 0xdc92_u16,
Expand Down Expand Up @@ -718,7 +722,7 @@ mod tests {

// general
assert_eq!(String::from_utf16_lossy([0xD800, 0xd801, 0xdc8b, 0xD800]),
String::from_str("\uFFFD𐒋\uFFFD"));
String::from_str("\uFFFD𐒋\uFFFD"));
}

#[test]
Expand Down Expand Up @@ -852,7 +856,8 @@ mod tests {

#[bench]
fn from_utf8_lossy_100_multibyte(b: &mut Bencher) {
let s = "𐌀𐌖𐌋𐌄𐌑𐌉ปรدولة الكويتทศไทย中华𐍅𐌿𐌻𐍆𐌹𐌻𐌰".as_bytes();
let s = "𐌀𐌖𐌋𐌄𐌑𐌉ปรدولة\
الكويتทศไทย中华𐍅𐌿𐌻𐍆𐌹𐌻𐌰".as_bytes();
assert_eq!(100, s.len());
b.iter(|| {
let _ = String::from_utf8_lossy(s);
Expand Down
1 change: 0 additions & 1 deletion src/libregex/parse.rs
Expand Up @@ -13,7 +13,6 @@ use std::cmp;
use std::fmt;
use std::iter;
use std::num;
use std::str;

/// Static data containing Unicode ranges for general categories and scripts.
use unicode::regex::{UNICODE_CLASSES, PERLD, PERLS, PERLW};
Expand Down
1 change: 0 additions & 1 deletion src/libregex/test/bench.rs
Expand Up @@ -10,7 +10,6 @@
#![allow(non_snake_case_functions)]

use std::rand::{Rng, task_rng};
use std::str;
use stdtest::Bencher;

use regex::{Regex, NoExpand};
Expand Down
1 change: 0 additions & 1 deletion src/librustc/driver/mod.rs
Expand Up @@ -20,7 +20,6 @@ use metadata;
use std::any::AnyRefExt;
use std::io;
use std::os;
use std::str;
use std::task::TaskBuilder;

use syntax::ast;
Expand Down
5 changes: 2 additions & 3 deletions src/librustc/metadata/encoder.rs
Expand Up @@ -35,7 +35,6 @@ use std::hash::Hash;
use std::hash;
use std::io::MemWriter;
use std::mem;
use std::str;
use std::collections::HashMap;
use syntax::abi;
use syntax::ast::*;
Expand Down Expand Up @@ -619,7 +618,7 @@ fn encode_visibility(ebml_w: &mut Encoder, visibility: Visibility) {
Public => 'y',
Inherited => 'i',
};
ebml_w.wr_str(ch.to_str().as_slice());
ebml_w.wr_str(ch.to_string().as_slice());
ebml_w.end_tag();
}

Expand Down Expand Up @@ -1922,5 +1921,5 @@ pub fn encoded_ty(tcx: &ty::ctxt, t: ty::t) -> String {
tcx: tcx,
abbrevs: &RefCell::new(HashMap::new())
}, t);
str::from_utf8(wr.get_ref()).unwrap().to_string()
String::from_utf8(wr.unwrap()).unwrap()
}
1 change: 0 additions & 1 deletion src/librustdoc/html/highlight.rs
Expand Up @@ -13,7 +13,6 @@
//! This module uses libsyntax's lexer to provide token-based highlighting for
//! the HTML documentation generated by rustdoc.

use std::str;
use std::io;

use syntax::parse;
Expand Down
4 changes: 2 additions & 2 deletions src/librustdoc/html/render.rs
Expand Up @@ -723,9 +723,9 @@ impl<'a> SourceCollector<'a> {

// Remove the utf-8 BOM if any
let contents = if contents.starts_with("\ufeff") {
contents.as_slice().slice_from(3)
contents.slice_from(3)
} else {
contents.as_slice()
contents
};

// Create the intermediate directories
Expand Down
1 change: 0 additions & 1 deletion src/librustdoc/lib.rs
Expand Up @@ -29,7 +29,6 @@ extern crate time;

use std::io;
use std::io::{File, MemWriter};
use std::str;
use std::gc::Gc;
use serialize::{json, Decodable, Encodable};
use externalfiles::ExternalHtml;
Expand Down

5 comments on commit 584fbde

@bors
Copy link
Contributor

@bors bors commented on 584fbde Jul 15, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 584fbde Jul 15, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging aochagavia/rust/str = 584fbde into auto

@bors
Copy link
Contributor

@bors bors commented on 584fbde Jul 15, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aochagavia/rust/str = 584fbde merged ok, testing candidate = b422373

@bors
Copy link
Contributor

@bors bors commented on 584fbde Jul 15, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = b422373

Please sign in to comment.