1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 // ignore-lexer-test FIXME #15679
13 //! An owned, growable string that enforces that its contents are valid UTF-8.
17 use core::borrow::{Cow, IntoCow};
18 use core::default::Default;
24 // FIXME: ICE's abound if you import the `Slice` type while importing `Slice` trait
25 use core::raw::Slice as RawSlice;
27 use slice::CloneSliceExt;
29 use str::{CharRange, CowString, FromStr, StrAllocating};
30 use str::MaybeOwned::Owned;
31 use vec::{DerefVec, Vec, as_vec};
33 /// A growable string stored as a UTF-8 encoded buffer.
34 #[deriving(Clone, PartialOrd, Eq, Ord)]
41 /// Creates a new string buffer initialized with the empty string.
46 /// let mut s = String::new();
50 pub fn new() -> String {
56 /// Creates a new string buffer with the given capacity.
57 /// The string will be able to hold exactly `capacity` bytes without
58 /// reallocating. If `capacity` is 0, the string will not allocate.
63 /// let mut s = String::with_capacity(10);
67 pub fn with_capacity(capacity: uint) -> String {
69 vec: Vec::with_capacity(capacity),
73 /// Creates a new string buffer from the given string.
78 /// let s = String::from_str("hello");
79 /// assert_eq!(s.as_slice(), "hello");
82 #[experimental = "needs investigation to see if to_string() can match perf"]
83 pub fn from_str(string: &str) -> String {
84 String { vec: string.as_bytes().to_vec() }
87 /// Returns the vector as a string buffer, if possible, taking care not to
90 /// Returns `Err` with the original vector if the vector contains invalid
96 /// let hello_vec = vec![104, 101, 108, 108, 111];
97 /// let s = String::from_utf8(hello_vec);
98 /// assert_eq!(s, Ok("hello".to_string()));
100 /// let invalid_vec = vec![240, 144, 128];
101 /// let s = String::from_utf8(invalid_vec);
102 /// assert_eq!(s, Err(vec![240, 144, 128]));
105 #[unstable = "error type may change"]
106 pub fn from_utf8(vec: Vec<u8>) -> Result<String, Vec<u8>> {
107 if str::is_utf8(vec.as_slice()) {
108 Ok(String { vec: vec })
114 /// Converts a vector of bytes to a new UTF-8 string.
115 /// Any invalid UTF-8 sequences are replaced with U+FFFD REPLACEMENT CHARACTER.
120 /// let input = b"Hello \xF0\x90\x80World";
121 /// let output = String::from_utf8_lossy(input);
122 /// assert_eq!(output.as_slice(), "Hello \u{FFFD}World");
124 #[unstable = "return type may change"]
125 pub fn from_utf8_lossy<'a>(v: &'a [u8]) -> CowString<'a> {
127 return Cow::Borrowed(unsafe { mem::transmute(v) })
130 static TAG_CONT_U8: u8 = 128u8;
131 static REPLACEMENT: &'static [u8] = b"\xEF\xBF\xBD"; // U+FFFD in UTF-8
134 fn unsafe_get(xs: &[u8], i: uint) -> u8 {
135 unsafe { *xs.unsafe_get(i) }
137 fn safe_get(xs: &[u8], i: uint, total: uint) -> u8 {
145 let mut res = String::with_capacity(total);
149 res.as_mut_vec().push_all(v[..i])
153 // subseqidx is the index of the first byte of the subsequence we're looking at.
154 // It's used to copy a bunch of contiguous good codepoints at once instead of copying
156 let mut subseqidx = 0;
160 let byte = unsafe_get(v, i);
163 macro_rules! error(() => ({
166 res.as_mut_vec().push_all(v[subseqidx..i_]);
169 res.as_mut_vec().push_all(REPLACEMENT);
174 // subseqidx handles this
176 let w = str::utf8_char_width(byte);
180 if safe_get(v, i, total) & 192u8 != TAG_CONT_U8 {
187 match (byte, safe_get(v, i, total)) {
188 (0xE0 , 0xA0 ... 0xBF) => (),
189 (0xE1 ... 0xEC, 0x80 ... 0xBF) => (),
190 (0xED , 0x80 ... 0x9F) => (),
191 (0xEE ... 0xEF, 0x80 ... 0xBF) => (),
198 if safe_get(v, i, total) & 192u8 != TAG_CONT_U8 {
205 match (byte, safe_get(v, i, total)) {
206 (0xF0 , 0x90 ... 0xBF) => (),
207 (0xF1 ... 0xF3, 0x80 ... 0xBF) => (),
208 (0xF4 , 0x80 ... 0x8F) => (),
215 if safe_get(v, i, total) & 192u8 != TAG_CONT_U8 {
220 if safe_get(v, i, total) & 192u8 != TAG_CONT_U8 {
233 if subseqidx < total {
235 res.as_mut_vec().push_all(v[subseqidx..total])
238 Cow::Owned(res.into_string())
241 /// Decode a UTF-16 encoded vector `v` into a `String`, returning `None`
242 /// if `v` contains any invalid data.
248 /// let mut v = &mut [0xD834, 0xDD1E, 0x006d, 0x0075,
249 /// 0x0073, 0x0069, 0x0063];
250 /// assert_eq!(String::from_utf16(v), Some("𝄞music".to_string()));
252 /// // 𝄞mu<invalid>ic
254 /// assert_eq!(String::from_utf16(v), None);
256 #[unstable = "error value in return may change"]
257 pub fn from_utf16(v: &[u16]) -> Option<String> {
258 let mut s = String::with_capacity(v.len());
259 for c in str::utf16_items(v) {
261 str::ScalarValue(c) => s.push(c),
262 str::LoneSurrogate(_) => return None
268 /// Decode a UTF-16 encoded vector `v` into a string, replacing
269 /// invalid data with the replacement character (U+FFFD).
274 /// // 𝄞mus<invalid>ic<invalid>
275 /// let v = &[0xD834, 0xDD1E, 0x006d, 0x0075,
276 /// 0x0073, 0xDD1E, 0x0069, 0x0063,
279 /// assert_eq!(String::from_utf16_lossy(v),
280 /// "𝄞mus\u{FFFD}ic\u{FFFD}".to_string());
283 pub fn from_utf16_lossy(v: &[u16]) -> String {
284 str::utf16_items(v).map(|c| c.to_char_lossy()).collect()
287 /// Convert a vector of `char`s to a `String`.
292 /// let chars = &['h', 'e', 'l', 'l', 'o'];
293 /// let s = String::from_chars(chars);
294 /// assert_eq!(s.as_slice(), "hello");
297 #[unstable = "may be removed in favor of .collect()"]
298 pub fn from_chars(chs: &[char]) -> String {
299 chs.iter().map(|c| *c).collect()
302 /// Creates a new `String` from a length, capacity, and pointer.
304 /// This is unsafe because:
305 /// * We call `Vec::from_raw_parts` to get a `Vec<u8>`;
306 /// * We assume that the `Vec` contains valid UTF-8.
308 #[unstable = "function just moved from string::raw"]
309 pub unsafe fn from_raw_parts(buf: *mut u8, length: uint, capacity: uint) -> String {
311 vec: Vec::from_raw_parts(buf, length, capacity),
315 /// Creates a `String` from a null-terminated `*const u8` buffer.
317 /// This function is unsafe because we dereference memory until we find the
318 /// NUL character, which is not guaranteed to be present. Additionally, the
319 /// slice is not checked to see whether it contains valid UTF-8
320 #[unstable = "just renamed from `mod raw`"]
321 pub unsafe fn from_raw_buf(buf: *const u8) -> String {
322 String::from_str(str::from_c_str(buf as *const i8))
325 /// Creates a `String` from a `*const u8` buffer of the given length.
327 /// This function is unsafe because it blindly assumes the validity of the
328 /// pointer `buf` for `len` bytes of memory. This function will copy the
329 /// memory from `buf` into a new allocation (owned by the returned
332 /// This function is also unsafe because it does not validate that the
333 /// buffer is valid UTF-8 encoded data.
334 #[unstable = "just renamed from `mod raw`"]
335 pub unsafe fn from_raw_buf_len(buf: *const u8, len: uint) -> String {
336 String::from_utf8_unchecked(Vec::from_raw_buf(buf, len))
339 /// Converts a vector of bytes to a new `String` without checking if
340 /// it contains valid UTF-8. This is unsafe because it assumes that
341 /// the UTF-8-ness of the vector has already been validated.
343 #[unstable = "awaiting stabilization"]
344 pub unsafe fn from_utf8_unchecked(bytes: Vec<u8>) -> String {
345 String { vec: bytes }
348 /// Return the underlying byte buffer, encoded as UTF-8.
353 /// let s = String::from_str("hello");
354 /// let bytes = s.into_bytes();
355 /// assert_eq!(bytes, vec![104, 101, 108, 108, 111]);
359 pub fn into_bytes(self) -> Vec<u8> {
363 /// Creates a string buffer by repeating a character `length` times.
368 /// let s = String::from_char(5, 'a');
369 /// assert_eq!(s.as_slice(), "aaaaa");
372 #[unstable = "may be replaced with iterators, questionable usability, and \
373 the name may change"]
374 pub fn from_char(length: uint, ch: char) -> String {
379 let mut buf = String::new();
381 let size = buf.len() * (length - 1);
382 buf.reserve_exact(size);
383 for _ in range(1, length) {
389 /// Pushes the given string onto this string buffer.
394 /// let mut s = String::from_str("foo");
395 /// s.push_str("bar");
396 /// assert_eq!(s.as_slice(), "foobar");
399 #[unstable = "extra variants of `push`, could possibly be based on iterators"]
400 pub fn push_str(&mut self, string: &str) {
401 self.vec.push_all(string.as_bytes())
404 /// Pushes `ch` onto the given string `count` times.
409 /// let mut s = String::from_str("foo");
411 /// assert_eq!(s.as_slice(), "fooZZZZZ");
414 #[unstable = "duplicate of iterator-based functionality"]
415 pub fn grow(&mut self, count: uint, ch: char) {
416 for _ in range(0, count) {
421 /// Returns the number of bytes that this string buffer can hold without reallocating.
426 /// let s = String::with_capacity(10);
427 /// assert!(s.capacity() >= 10);
430 #[unstable = "matches collection reform specification, waiting for dust to settle"]
431 pub fn capacity(&self) -> uint {
435 /// Deprecated: Renamed to `reserve`.
436 #[deprecated = "Renamed to `reserve`"]
437 pub fn reserve_additional(&mut self, extra: uint) {
438 self.vec.reserve(extra)
441 /// Reserves capacity for at least `additional` more bytes to be inserted in the given
442 /// `String`. The collection may reserve more space to avoid frequent reallocations.
446 /// Panics if the new capacity overflows `uint`.
451 /// let mut s = String::new();
453 /// assert!(s.capacity() >= 10);
456 #[unstable = "matches collection reform specification, waiting for dust to settle"]
457 pub fn reserve(&mut self, additional: uint) {
458 self.vec.reserve(additional)
461 /// Reserves the minimum capacity for exactly `additional` more bytes to be inserted in the
462 /// given `String`. Does nothing if the capacity is already sufficient.
464 /// Note that the allocator may give the collection more space than it requests. Therefore
465 /// capacity can not be relied upon to be precisely minimal. Prefer `reserve` if future
466 /// insertions are expected.
470 /// Panics if the new capacity overflows `uint`.
475 /// let mut s = String::new();
477 /// assert!(s.capacity() >= 10);
480 #[unstable = "matches collection reform specification, waiting for dust to settle"]
481 pub fn reserve_exact(&mut self, additional: uint) {
482 self.vec.reserve_exact(additional)
485 /// Shrinks the capacity of this string buffer to match its length.
490 /// let mut s = String::from_str("foo");
492 /// assert!(s.capacity() >= 100);
493 /// s.shrink_to_fit();
494 /// assert_eq!(s.capacity(), 3);
497 #[unstable = "matches collection reform specification, waiting for dust to settle"]
498 pub fn shrink_to_fit(&mut self) {
499 self.vec.shrink_to_fit()
502 /// Adds the given character to the end of the string.
507 /// let mut s = String::from_str("abc");
511 /// assert_eq!(s.as_slice(), "abc123");
514 #[stable = "function just renamed from push_char"]
515 pub fn push(&mut self, ch: char) {
516 if (ch as u32) < 0x80 {
517 self.vec.push(ch as u8);
521 let cur_len = self.len();
522 // This may use up to 4 bytes.
526 // Attempt to not use an intermediate buffer by just pushing bytes
527 // directly onto this string.
528 let slice = RawSlice {
529 data: self.vec.as_ptr().offset(cur_len as int),
532 let used = ch.encode_utf8(mem::transmute(slice)).unwrap_or(0);
533 self.vec.set_len(cur_len + used);
537 /// Works with the underlying buffer as a byte slice.
542 /// let s = String::from_str("hello");
543 /// let b: &[_] = &[104, 101, 108, 108, 111];
544 /// assert_eq!(s.as_bytes(), b);
548 pub fn as_bytes<'a>(&'a self) -> &'a [u8] {
552 /// Shortens a string to the specified length.
556 /// Panics if `new_len` > current length,
557 /// or if `new_len` is not a character boundary.
562 /// let mut s = String::from_str("hello");
564 /// assert_eq!(s.as_slice(), "he");
567 #[unstable = "the panic conventions for strings are under development"]
568 pub fn truncate(&mut self, new_len: uint) {
569 assert!(self.is_char_boundary(new_len));
570 self.vec.truncate(new_len)
573 /// Removes the last character from the string buffer and returns it.
574 /// Returns `None` if this string buffer is empty.
579 /// let mut s = String::from_str("foo");
580 /// assert_eq!(s.pop(), Some('o'));
581 /// assert_eq!(s.pop(), Some('o'));
582 /// assert_eq!(s.pop(), Some('f'));
583 /// assert_eq!(s.pop(), None);
586 #[unstable = "this function was just renamed from pop_char"]
587 pub fn pop(&mut self) -> Option<char> {
588 let len = self.len();
593 let CharRange {ch, next} = self.char_range_at_reverse(len);
595 self.vec.set_len(next);
600 /// Removes the character from the string buffer at byte position `idx` and
601 /// returns it. Returns `None` if `idx` is out of bounds.
605 /// This is an O(n) operation as it requires copying every element in the
610 /// If `idx` does not lie on a character boundary, then this function will
616 /// let mut s = String::from_str("foo");
617 /// assert_eq!(s.remove(0), Some('f'));
618 /// assert_eq!(s.remove(1), Some('o'));
619 /// assert_eq!(s.remove(0), Some('o'));
620 /// assert_eq!(s.remove(0), None);
622 #[unstable = "the panic semantics of this function and return type \
624 pub fn remove(&mut self, idx: uint) -> Option<char> {
625 let len = self.len();
626 if idx >= len { return None }
628 let CharRange { ch, next } = self.char_range_at(idx);
630 ptr::copy_memory(self.vec.as_mut_ptr().offset(idx as int),
631 self.vec.as_ptr().offset(next as int),
633 self.vec.set_len(len - (next - idx));
638 /// Insert a character into the string buffer at byte position `idx`.
642 /// This is an O(n) operation as it requires copying every element in the
647 /// If `idx` does not lie on a character boundary or is out of bounds, then
648 /// this function will panic.
649 #[unstable = "the panic semantics of this function are uncertain"]
650 pub fn insert(&mut self, idx: uint, ch: char) {
651 let len = self.len();
653 assert!(self.is_char_boundary(idx));
655 let mut bits = [0, ..4];
656 let amt = ch.encode_utf8(&mut bits).unwrap();
659 ptr::copy_memory(self.vec.as_mut_ptr().offset((idx + amt) as int),
660 self.vec.as_ptr().offset(idx as int),
662 ptr::copy_memory(self.vec.as_mut_ptr().offset(idx as int),
665 self.vec.set_len(len + amt);
669 /// Views the string buffer as a mutable sequence of bytes.
671 /// This is unsafe because it does not check
672 /// to ensure that the resulting string will be valid UTF-8.
677 /// let mut s = String::from_str("hello");
679 /// let vec = s.as_mut_vec();
680 /// assert!(vec == &mut vec![104, 101, 108, 108, 111]);
683 /// assert_eq!(s.as_slice(), "olleh");
685 #[unstable = "the name of this method may be changed"]
686 pub unsafe fn as_mut_vec<'a>(&'a mut self) -> &'a mut Vec<u8> {
690 /// Return the number of bytes in this string.
695 /// let a = "foo".to_string();
696 /// assert_eq!(a.len(), 3);
700 pub fn len(&self) -> uint { self.vec.len() }
702 /// Returns true if the string contains no bytes
707 /// let mut v = String::new();
708 /// assert!(v.is_empty());
710 /// assert!(!v.is_empty());
712 pub fn is_empty(&self) -> bool { self.len() == 0 }
714 /// Truncates the string, returning it to 0 length.
719 /// let mut s = "foo".to_string();
721 /// assert!(s.is_empty());
725 pub fn clear(&mut self) {
730 #[experimental = "waiting on FromIterator stabilization"]
731 impl FromIterator<char> for String {
732 fn from_iter<I:Iterator<char>>(iterator: I) -> String {
733 let mut buf = String::new();
734 buf.extend(iterator);
739 #[experimental = "waiting on FromIterator stabilization"]
740 impl<'a> FromIterator<&'a str> for String {
741 fn from_iter<I:Iterator<&'a str>>(iterator: I) -> String {
742 let mut buf = String::new();
743 buf.extend(iterator);
748 #[experimental = "waiting on Extend stabilization"]
749 impl Extend<char> for String {
750 fn extend<I:Iterator<char>>(&mut self, mut iterator: I) {
751 let (lower_bound, _) = iterator.size_hint();
752 self.reserve(lower_bound);
759 #[experimental = "waiting on Extend stabilization"]
760 impl<'a> Extend<&'a str> for String {
761 fn extend<I: Iterator<&'a str>>(&mut self, mut iterator: I) {
762 // A guess that at least one byte per iterator element will be needed.
763 let (lower_bound, _) = iterator.size_hint();
764 self.reserve(lower_bound);
771 impl PartialEq for String {
773 fn eq(&self, other: &String) -> bool { PartialEq::eq(&**self, &**other) }
775 fn ne(&self, other: &String) -> bool { PartialEq::ne(&**self, &**other) }
778 macro_rules! impl_eq {
779 ($lhs:ty, $rhs: ty) => {
780 impl<'a> PartialEq<$rhs> for $lhs {
782 fn eq(&self, other: &$rhs) -> bool { PartialEq::eq(&**self, &**other) }
784 fn ne(&self, other: &$rhs) -> bool { PartialEq::ne(&**self, &**other) }
787 impl<'a> PartialEq<$lhs> for $rhs {
789 fn eq(&self, other: &$lhs) -> bool { PartialEq::eq(&**self, &**other) }
791 fn ne(&self, other: &$lhs) -> bool { PartialEq::ne(&**self, &**other) }
797 impl_eq! { String, &'a str }
798 impl_eq! { CowString<'a>, String }
800 impl<'a, 'b> PartialEq<&'b str> for CowString<'a> {
802 fn eq(&self, other: &&'b str) -> bool { PartialEq::eq(&**self, &**other) }
804 fn ne(&self, other: &&'b str) -> bool { PartialEq::ne(&**self, &**other) }
807 impl<'a, 'b> PartialEq<CowString<'a>> for &'b str {
809 fn eq(&self, other: &CowString<'a>) -> bool { PartialEq::eq(&**self, &**other) }
811 fn ne(&self, other: &CowString<'a>) -> bool { PartialEq::ne(&**self, &**other) }
814 #[experimental = "waiting on Str stabilization"]
815 impl Str for String {
818 fn as_slice<'a>(&'a self) -> &'a str {
820 mem::transmute(self.vec.as_slice())
825 #[experimental = "waiting on StrAllocating stabilization"]
826 impl StrAllocating for String {
828 fn into_string(self) -> String {
834 impl Default for String {
836 fn default() -> String {
841 #[experimental = "waiting on Show stabilization"]
842 impl fmt::Show for String {
843 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
844 self.as_slice().fmt(f)
848 #[experimental = "waiting on Hash stabilization"]
849 impl<H: hash::Writer> hash::Hash<H> for String {
851 fn hash(&self, hasher: &mut H) {
852 self.as_slice().hash(hasher)
857 #[deprecated = "Use overloaded `core::cmp::PartialEq`"]
858 impl<'a, S: Str> Equiv<S> for String {
860 fn equiv(&self, other: &S) -> bool {
861 self.as_slice() == other.as_slice()
865 #[experimental = "waiting on Add stabilization"]
866 impl<'a> Add<&'a str, String> for String {
867 fn add(mut self, other: &str) -> String {
868 self.push_str(other);
873 impl ops::Slice<uint, str> for String {
875 fn as_slice_<'a>(&'a self) -> &'a str {
880 fn slice_from_or_fail<'a>(&'a self, from: &uint) -> &'a str {
885 fn slice_to_or_fail<'a>(&'a self, to: &uint) -> &'a str {
890 fn slice_or_fail<'a>(&'a self, from: &uint, to: &uint) -> &'a str {
895 #[experimental = "waiting on Deref stabilization"]
896 impl ops::Deref<str> for String {
897 fn deref<'a>(&'a self) -> &'a str { self.as_slice() }
900 /// Wrapper type providing a `&String` reference via `Deref`.
902 pub struct DerefString<'a> {
906 impl<'a> Deref<String> for DerefString<'a> {
907 fn deref<'b>(&'b self) -> &'b String {
908 unsafe { mem::transmute(&*self.x) }
912 /// Convert a string slice to a wrapper type providing a `&String` reference.
917 /// use std::string::as_string;
919 /// fn string_consumer(s: String) {
920 /// assert_eq!(s, "foo".to_string());
923 /// let string = as_string("foo").clone();
924 /// string_consumer(string);
927 pub fn as_string<'a>(x: &'a str) -> DerefString<'a> {
928 DerefString { x: as_vec(x.as_bytes()) }
931 impl FromStr for String {
933 fn from_str(s: &str) -> Option<String> {
934 Some(String::from_str(s))
938 /// Trait for converting a type to a string, consuming it in the process.
939 pub trait IntoString {
940 /// Consume and convert to a string.
941 fn into_string(self) -> String;
944 /// A generic trait for converting a value to a string
946 /// Converts the value of `self` to an owned string
947 fn to_string(&self) -> String;
950 impl<T: fmt::Show> ToString for T {
951 fn to_string(&self) -> String {
952 let mut buf = Vec::<u8>::new();
953 let _ = format_args!(|args| fmt::write(&mut buf, args), "{}", self);
954 String::from_utf8(buf).unwrap()
958 impl IntoCow<'static, String, str> for String {
959 fn into_cow(self) -> CowString<'static> {
964 impl<'a> IntoCow<'a, String, str> for &'a str {
965 fn into_cow(self) -> CowString<'a> {
970 /// Unsafe operations
976 /// Creates a new `String` from a length, capacity, and pointer.
978 /// This is unsafe because:
979 /// * We call `Vec::from_raw_parts` to get a `Vec<u8>`;
980 /// * We assume that the `Vec` contains valid UTF-8.
982 #[deprecated = "renamed to String::from_raw_parts"]
983 pub unsafe fn from_parts(buf: *mut u8, length: uint, capacity: uint) -> String {
984 String::from_raw_parts(buf, length, capacity)
987 /// Creates a `String` from a `*const u8` buffer of the given length.
989 /// This function is unsafe because of two reasons:
991 /// * A raw pointer is dereferenced and transmuted to `&[u8]`;
992 /// * The slice is not checked to see whether it contains valid UTF-8.
993 #[deprecated = "renamed to String::from_raw_buf_len"]
994 pub unsafe fn from_buf_len(buf: *const u8, len: uint) -> String {
995 String::from_raw_buf_len(buf, len)
998 /// Creates a `String` from a null-terminated `*const u8` buffer.
1000 /// This function is unsafe because we dereference memory until we find the NUL character,
1001 /// which is not guaranteed to be present. Additionally, the slice is not checked to see
1002 /// whether it contains valid UTF-8
1003 #[deprecated = "renamed to String::from_raw_buf"]
1004 pub unsafe fn from_buf(buf: *const u8) -> String {
1005 String::from_raw_buf(buf)
1008 /// Converts a vector of bytes to a new `String` without checking if
1009 /// it contains valid UTF-8. This is unsafe because it assumes that
1010 /// the UTF-8-ness of the vector has already been validated.
1012 #[deprecated = "renamed to String::from_utf8_unchecked"]
1013 pub unsafe fn from_utf8(bytes: Vec<u8>) -> String {
1014 String::from_utf8_unchecked(bytes)
1024 use super::as_string;
1027 fn test_as_string() {
1029 assert_eq!(x, as_string(x).as_slice());
1033 fn test_from_str() {
1034 let owned: Option<::std::string::String> = from_str("string");
1035 assert_eq!(owned.as_ref().map(|s| s.as_slice()), Some("string"));
1039 fn test_from_utf8() {
1040 let xs = b"hello".to_vec();
1041 assert_eq!(String::from_utf8(xs), Ok(String::from_str("hello")));
1043 let xs = "ศไทย中华Việt Nam".as_bytes().to_vec();
1044 assert_eq!(String::from_utf8(xs), Ok(String::from_str("ศไทย中华Việt Nam")));
1046 let xs = b"hello\xFF".to_vec();
1047 assert_eq!(String::from_utf8(xs),
1048 Err(b"hello\xFF".to_vec()));
1052 fn test_from_utf8_lossy() {
1054 let ys: str::CowString = "hello".into_cow();
1055 assert_eq!(String::from_utf8_lossy(xs), ys);
1057 let xs = "ศไทย中华Việt Nam".as_bytes();
1058 let ys: str::CowString = "ศไทย中华Việt Nam".into_cow();
1059 assert_eq!(String::from_utf8_lossy(xs), ys);
1061 let xs = b"Hello\xC2 There\xFF Goodbye";
1062 assert_eq!(String::from_utf8_lossy(xs),
1063 String::from_str("Hello\u{FFFD} There\u{FFFD} Goodbye").into_cow());
1065 let xs = b"Hello\xC0\x80 There\xE6\x83 Goodbye";
1066 assert_eq!(String::from_utf8_lossy(xs),
1067 String::from_str("Hello\u{FFFD}\u{FFFD} There\u{FFFD} Goodbye").into_cow());
1069 let xs = b"\xF5foo\xF5\x80bar";
1070 assert_eq!(String::from_utf8_lossy(xs),
1071 String::from_str("\u{FFFD}foo\u{FFFD}\u{FFFD}bar").into_cow());
1073 let xs = b"\xF1foo\xF1\x80bar\xF1\x80\x80baz";
1074 assert_eq!(String::from_utf8_lossy(xs),
1075 String::from_str("\u{FFFD}foo\u{FFFD}bar\u{FFFD}baz").into_cow());
1077 let xs = b"\xF4foo\xF4\x80bar\xF4\xBFbaz";
1078 assert_eq!(String::from_utf8_lossy(xs),
1079 String::from_str("\u{FFFD}foo\u{FFFD}bar\u{FFFD}\u{FFFD}baz").into_cow());
1081 let xs = b"\xF0\x80\x80\x80foo\xF0\x90\x80\x80bar";
1082 assert_eq!(String::from_utf8_lossy(xs), String::from_str("\u{FFFD}\u{FFFD}\u{FFFD}\u{FFFD}\
1083 foo\u{10000}bar").into_cow());
1086 let xs = b"\xED\xA0\x80foo\xED\xBF\xBFbar";
1087 assert_eq!(String::from_utf8_lossy(xs), String::from_str("\u{FFFD}\u{FFFD}\u{FFFD}foo\
1088 \u{FFFD}\u{FFFD}\u{FFFD}bar").into_cow());
1092 fn test_from_utf16() {
1094 [(String::from_str("𐍅𐌿𐌻𐍆𐌹𐌻𐌰\n"),
1095 vec![0xd800_u16, 0xdf45_u16, 0xd800_u16, 0xdf3f_u16,
1096 0xd800_u16, 0xdf3b_u16, 0xd800_u16, 0xdf46_u16,
1097 0xd800_u16, 0xdf39_u16, 0xd800_u16, 0xdf3b_u16,
1098 0xd800_u16, 0xdf30_u16, 0x000a_u16]),
1100 (String::from_str("𐐒𐑉𐐮𐑀𐐲𐑋 𐐏𐐲𐑍\n"),
1101 vec![0xd801_u16, 0xdc12_u16, 0xd801_u16,
1102 0xdc49_u16, 0xd801_u16, 0xdc2e_u16, 0xd801_u16,
1103 0xdc40_u16, 0xd801_u16, 0xdc32_u16, 0xd801_u16,
1104 0xdc4b_u16, 0x0020_u16, 0xd801_u16, 0xdc0f_u16,
1105 0xd801_u16, 0xdc32_u16, 0xd801_u16, 0xdc4d_u16,
1108 (String::from_str("𐌀𐌖𐌋𐌄𐌑𐌉·𐌌𐌄𐌕𐌄𐌋𐌉𐌑\n"),
1109 vec![0xd800_u16, 0xdf00_u16, 0xd800_u16, 0xdf16_u16,
1110 0xd800_u16, 0xdf0b_u16, 0xd800_u16, 0xdf04_u16,
1111 0xd800_u16, 0xdf11_u16, 0xd800_u16, 0xdf09_u16,
1112 0x00b7_u16, 0xd800_u16, 0xdf0c_u16, 0xd800_u16,
1113 0xdf04_u16, 0xd800_u16, 0xdf15_u16, 0xd800_u16,
1114 0xdf04_u16, 0xd800_u16, 0xdf0b_u16, 0xd800_u16,
1115 0xdf09_u16, 0xd800_u16, 0xdf11_u16, 0x000a_u16 ]),
1117 (String::from_str("𐒋𐒘𐒈𐒑𐒛𐒒 𐒕𐒓 𐒈𐒚𐒍 𐒏𐒜𐒒𐒖𐒆 𐒕𐒆\n"),
1118 vec![0xd801_u16, 0xdc8b_u16, 0xd801_u16, 0xdc98_u16,
1119 0xd801_u16, 0xdc88_u16, 0xd801_u16, 0xdc91_u16,
1120 0xd801_u16, 0xdc9b_u16, 0xd801_u16, 0xdc92_u16,
1121 0x0020_u16, 0xd801_u16, 0xdc95_u16, 0xd801_u16,
1122 0xdc93_u16, 0x0020_u16, 0xd801_u16, 0xdc88_u16,
1123 0xd801_u16, 0xdc9a_u16, 0xd801_u16, 0xdc8d_u16,
1124 0x0020_u16, 0xd801_u16, 0xdc8f_u16, 0xd801_u16,
1125 0xdc9c_u16, 0xd801_u16, 0xdc92_u16, 0xd801_u16,
1126 0xdc96_u16, 0xd801_u16, 0xdc86_u16, 0x0020_u16,
1127 0xd801_u16, 0xdc95_u16, 0xd801_u16, 0xdc86_u16,
1129 // Issue #12318, even-numbered non-BMP planes
1130 (String::from_str("\u{20000}"),
1131 vec![0xD840, 0xDC00])];
1133 for p in pairs.iter() {
1134 let (s, u) = (*p).clone();
1135 let s_as_utf16 = s.utf16_units().collect::<Vec<u16>>();
1136 let u_as_string = String::from_utf16(u.as_slice()).unwrap();
1138 assert!(str::is_utf16(u.as_slice()));
1139 assert_eq!(s_as_utf16, u);
1141 assert_eq!(u_as_string, s);
1142 assert_eq!(String::from_utf16_lossy(u.as_slice()), s);
1144 assert_eq!(String::from_utf16(s_as_utf16.as_slice()).unwrap(), s);
1145 assert_eq!(u_as_string.utf16_units().collect::<Vec<u16>>(), u);
1150 fn test_utf16_invalid() {
1151 // completely positive cases tested above.
1153 assert_eq!(String::from_utf16(&[0xD800]), None);
1155 assert_eq!(String::from_utf16(&[0xD800, 0xD800]), None);
1158 assert_eq!(String::from_utf16(&[0x0061, 0xDC00]), None);
1161 assert_eq!(String::from_utf16(&[0xD800, 0xd801, 0xdc8b, 0xD800]), None);
1165 fn test_from_utf16_lossy() {
1166 // completely positive cases tested above.
1168 assert_eq!(String::from_utf16_lossy(&[0xD800]), String::from_str("\u{FFFD}"));
1170 assert_eq!(String::from_utf16_lossy(&[0xD800, 0xD800]),
1171 String::from_str("\u{FFFD}\u{FFFD}"));
1174 assert_eq!(String::from_utf16_lossy(&[0x0061, 0xDC00]), String::from_str("a\u{FFFD}"));
1177 assert_eq!(String::from_utf16_lossy(&[0xD800, 0xd801, 0xdc8b, 0xD800]),
1178 String::from_str("\u{FFFD}𐒋\u{FFFD}"));
1182 fn test_from_buf_len() {
1184 let a = vec![65u8, 65, 65, 65, 65, 65, 65, 0];
1185 assert_eq!(super::raw::from_buf_len(a.as_ptr(), 3), String::from_str("AAA"));
1190 fn test_from_buf() {
1192 let a = vec![65, 65, 65, 65, 65, 65, 65, 0];
1194 let c = super::raw::from_buf(b);
1195 assert_eq!(c, String::from_str("AAAAAAA"));
1200 fn test_push_bytes() {
1201 let mut s = String::from_str("ABC");
1203 let mv = s.as_mut_vec();
1204 mv.push_all(&[b'D']);
1206 assert_eq!(s, "ABCD");
1210 fn test_push_str() {
1211 let mut s = String::new();
1213 assert_eq!(s.slice_from(0), "");
1215 assert_eq!(s.slice_from(0), "abc");
1216 s.push_str("ประเทศไทย中华Việt Nam");
1217 assert_eq!(s.slice_from(0), "abcประเทศไทย中华Việt Nam");
1222 let mut data = String::from_str("ประเทศไทย中");
1224 data.push('b'); // 1 byte
1225 data.push('¢'); // 2 byte
1226 data.push('€'); // 3 byte
1227 data.push('𤭢'); // 4 byte
1228 assert_eq!(data, "ประเทศไทย中华b¢€𤭢");
1233 let mut data = String::from_str("ประเทศไทย中华b¢€𤭢");
1234 assert_eq!(data.pop().unwrap(), '𤭢'); // 4 bytes
1235 assert_eq!(data.pop().unwrap(), '€'); // 3 bytes
1236 assert_eq!(data.pop().unwrap(), '¢'); // 2 bytes
1237 assert_eq!(data.pop().unwrap(), 'b'); // 1 bytes
1238 assert_eq!(data.pop().unwrap(), '华');
1239 assert_eq!(data, "ประเทศไทย中");
1243 fn test_str_truncate() {
1244 let mut s = String::from_str("12345");
1246 assert_eq!(s, "12345");
1248 assert_eq!(s, "123");
1252 let mut s = String::from_str("12345");
1256 let p_ = s.as_ptr();
1262 fn test_str_truncate_invalid_len() {
1263 let mut s = String::from_str("12345");
1269 fn test_str_truncate_split_codepoint() {
1270 let mut s = String::from_str("\u{FC}"); // ü
1275 fn test_str_clear() {
1276 let mut s = String::from_str("12345");
1278 assert_eq!(s.len(), 0);
1284 let a = String::from_str("12345");
1287 assert_eq!(b.len(), 7);
1288 assert_eq!(b, "1234522");
1293 let mut s = "ศไทย中华Việt Nam; foobar".to_string();;
1294 assert_eq!(s.remove(0), Some('ศ'));
1295 assert_eq!(s.len(), 33);
1296 assert_eq!(s, "ไทย中华Việt Nam; foobar");
1297 assert_eq!(s.remove(33), None);
1298 assert_eq!(s.remove(300), None);
1299 assert_eq!(s.remove(17), Some('ệ'));
1300 assert_eq!(s, "ไทย中华Vit Nam; foobar");
1303 #[test] #[should_fail]
1305 "ศ".to_string().remove(1);
1310 let mut s = "foobar".to_string();
1312 assert_eq!(s, "ệfoobar");
1314 assert_eq!(s, "ệfooยbar");
1317 #[test] #[should_fail] fn insert_bad1() { "".to_string().insert(1, 't'); }
1318 #[test] #[should_fail] fn insert_bad2() { "ệ".to_string().insert(1, 't'); }
1322 let s = "foobar".to_string();
1323 assert_eq!("foobar", s[]);
1324 assert_eq!("foo", s[..3]);
1325 assert_eq!("bar", s[3..]);
1326 assert_eq!("oob", s[1..4]);
1330 fn test_simple_types() {
1331 assert_eq!(1i.to_string(), "1");
1332 assert_eq!((-1i).to_string(), "-1");
1333 assert_eq!(200u.to_string(), "200");
1334 assert_eq!(2u8.to_string(), "2");
1335 assert_eq!(true.to_string(), "true");
1336 assert_eq!(false.to_string(), "false");
1337 assert_eq!(().to_string(), "()");
1338 assert_eq!(("hi".to_string()).to_string(), "hi");
1343 let x: Vec<int> = vec![];
1344 assert_eq!(x.to_string(), "[]");
1345 assert_eq!((vec![1i]).to_string(), "[1]");
1346 assert_eq!((vec![1i, 2, 3]).to_string(), "[1, 2, 3]");
1347 assert!((vec![vec![], vec![1i], vec![1i, 1]]).to_string() ==
1348 "[[], [1], [1, 1]]");
1352 fn test_from_iterator() {
1353 let s = "ศไทย中华Việt Nam".to_string();
1357 let a: String = s.chars().collect();
1360 let mut b = t.to_string();
1361 b.extend(u.chars());
1364 let c: String = vec![t, u].into_iter().collect();
1367 let mut d = t.to_string();
1368 d.extend(vec![u].into_iter());
1373 fn bench_with_capacity(b: &mut Bencher) {
1375 String::with_capacity(100)
1380 fn bench_push_str(b: &mut Bencher) {
1381 let s = "ศไทย中华Việt Nam; Mary had a little lamb, Little lamb";
1383 let mut r = String::new();
1388 const REPETITIONS: u64 = 10_000;
1391 fn bench_push_str_one_byte(b: &mut Bencher) {
1392 b.bytes = REPETITIONS;
1394 let mut r = String::new();
1395 for _ in range(0, REPETITIONS) {
1402 fn bench_push_char_one_byte(b: &mut Bencher) {
1403 b.bytes = REPETITIONS;
1405 let mut r = String::new();
1406 for _ in range(0, REPETITIONS) {
1413 fn bench_push_char_two_bytes(b: &mut Bencher) {
1414 b.bytes = REPETITIONS * 2;
1416 let mut r = String::new();
1417 for _ in range(0, REPETITIONS) {
1424 fn from_utf8_lossy_100_ascii(b: &mut Bencher) {
1425 let s = b"Hello there, the quick brown fox jumped over the lazy dog! \
1426 Lorem ipsum dolor sit amet, consectetur. ";
1428 assert_eq!(100, s.len());
1430 let _ = String::from_utf8_lossy(s);
1435 fn from_utf8_lossy_100_multibyte(b: &mut Bencher) {
1436 let s = "𐌀𐌖𐌋𐌄𐌑𐌉ปรدولة الكويتทศไทย中华𐍅𐌿𐌻𐍆𐌹𐌻𐌰".as_bytes();
1437 assert_eq!(100, s.len());
1439 let _ = String::from_utf8_lossy(s);
1444 fn from_utf8_lossy_invalid(b: &mut Bencher) {
1445 let s = b"Hello\xC0\x80 There\xE6\x83 Goodbye";
1447 let _ = String::from_utf8_lossy(s);
1452 fn from_utf8_lossy_100_invalid(b: &mut Bencher) {
1453 let s = Vec::from_elem(100, 0xF5u8);
1455 let _ = String::from_utf8_lossy(s.as_slice());