Skip to content
Closed
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
89 changes: 21 additions & 68 deletions src/libcollections/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@

#![stable(feature = "rust1", since = "1.0.0")]

use core::borrow::Borrow;
use core::fmt;
use core::hash;
use core::iter::{FromIterator, FusedIterator};
Expand Down Expand Up @@ -476,9 +477,9 @@ impl String {
Ok(..) => Ok(String { vec: vec }),
Err(e) => {
Err(FromUtf8Error {
bytes: vec,
error: e,
})
bytes: vec,
error: e,
})
}
}
}
Expand Down Expand Up @@ -663,7 +664,9 @@ impl String {
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn from_utf16(v: &[u16]) -> Result<String, FromUtf16Error> {
decode_utf16(v.iter().cloned()).collect::<Result<_, _>>().map_err(|_| FromUtf16Error(()))
decode_utf16(v.iter().cloned())
.collect::<Result<_, _>>()
.map_err(|_| FromUtf16Error(()))
}

/// Decode a UTF-16 encoded vector `v` into a string, replacing
Expand All @@ -685,7 +688,9 @@ impl String {
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn from_utf16_lossy(v: &[u16]) -> String {
decode_utf16(v.iter().cloned()).map(|r| r.unwrap_or(REPLACEMENT_CHARACTER)).collect()
decode_utf16(v.iter().cloned())
.map(|r| r.unwrap_or(REPLACEMENT_CHARACTER))
.collect()
}

/// Creates a new `String` from a length, capacity, and pointer.
Expand Down Expand Up @@ -977,7 +982,10 @@ impl String {
pub fn push(&mut self, ch: char) {
match ch.len_utf8() {
1 => self.vec.push(ch as u8),
_ => self.vec.extend_from_slice(ch.encode_utf8(&mut [0; 4]).as_bytes()),
_ => {
self.vec
.extend_from_slice(ch.encode_utf8(&mut [0; 4]).as_bytes())
}
}
}

Expand Down Expand Up @@ -1502,73 +1510,18 @@ impl Clone for String {
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl FromIterator<char> for String {
fn from_iter<I: IntoIterator<Item = char>>(iter: I) -> String {
let mut buf = String::new();
buf.extend(iter);
buf
}
}

#[stable(feature = "string_from_iter_by_ref", since = "1.17.0")]
impl<'a> FromIterator<&'a char> for String {
fn from_iter<I: IntoIterator<Item = &'a char>>(iter: I) -> String {
let mut buf = String::new();
buf.extend(iter);
buf
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<'a> FromIterator<&'a str> for String {
fn from_iter<I: IntoIterator<Item = &'a str>>(iter: I) -> String {
#[stable(feature = "herd_cows", since = "1.9.0")]
impl<T: Borrow<str>> FromIterator<T> for String {
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> String {
let mut buf = String::new();
buf.extend(iter);
buf
}
}

#[stable(feature = "extend_string", since = "1.4.0")]
impl FromIterator<String> for String {
fn from_iter<I: IntoIterator<Item = String>>(iter: I) -> String {
let mut buf = String::new();
buf.extend(iter);
buf
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl Extend<char> for String {
fn extend<I: IntoIterator<Item = char>>(&mut self, iter: I) {
let iterator = iter.into_iter();
let (lower_bound, _) = iterator.size_hint();
self.reserve(lower_bound);
for ch in iterator {
self.push(ch)
}
}
}

#[stable(feature = "extend_ref", since = "1.2.0")]
impl<'a> Extend<&'a char> for String {
fn extend<I: IntoIterator<Item = &'a char>>(&mut self, iter: I) {
self.extend(iter.into_iter().cloned());
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<'a> Extend<&'a str> for String {
fn extend<I: IntoIterator<Item = &'a str>>(&mut self, iter: I) {
for s in iter {
self.push_str(s)
}
}
}

#[stable(feature = "extend_string", since = "1.4.0")]
impl Extend<String> for String {
fn extend<I: IntoIterator<Item = String>>(&mut self, iter: I) {
#[stable(feature = "herd_cows", since = "1.9.0")]
impl<T: Borrow<str>> Extend<T> for String {
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
for s in iter {
self.push_str(&s)
}
Expand Down Expand Up @@ -1941,7 +1894,7 @@ impl<T: fmt::Display + ?Sized> ToString for T {
use core::fmt::Write;
let mut buf = String::new();
buf.write_fmt(format_args!("{}", self))
.expect("a Display implementation return an error unexpectedly");
.expect("a Display implementation return an error unexpectedly");
buf.shrink_to_fit();
buf
}
Expand Down