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::default::Default;
22 // FIXME: ICE's abound if you import the `Slice` type while importing `Slice` trait
23 use core::raw::Slice as RawSlice;
25 use {Mutable, MutableSeq};
27 use slice::CloneableVector;
29 use str::{CharRange, StrAllocating, MaybeOwned, Owned};
30 use str::Slice as MaybeOwnedSlice; // So many `Slice`s...
33 /// A growable string stored as a UTF-8 encoded buffer.
34 #[deriving(Clone, PartialEq, 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 /// Deprecated. Replaced by `string::raw::from_parts`
89 #[deprecated = "Replaced by string::raw::from_parts"]
90 pub unsafe fn from_raw_parts(length: uint, capacity: uint, ptr: *mut u8) -> String {
91 raw::from_parts(ptr, length, capacity)
95 #[deprecated = "obsoleted by the removal of ~str"]
97 pub fn from_owned_str(string: String) -> String {
101 /// Returns the vector as a string buffer, if possible, taking care not to
104 /// Returns `Err` with the original vector if the vector contains invalid
110 /// let hello_vec = vec![104, 101, 108, 108, 111];
111 /// let s = String::from_utf8(hello_vec);
112 /// assert_eq!(s, Ok("hello".to_string()));
114 /// let invalid_vec = vec![240, 144, 128];
115 /// let s = String::from_utf8(invalid_vec);
116 /// assert_eq!(s, Err(vec![240, 144, 128]));
119 #[unstable = "error type may change"]
120 pub fn from_utf8(vec: Vec<u8>) -> Result<String, Vec<u8>> {
121 if str::is_utf8(vec.as_slice()) {
122 Ok(String { vec: vec })
128 /// Converts a vector of bytes to a new UTF-8 string.
129 /// Any invalid UTF-8 sequences are replaced with U+FFFD REPLACEMENT CHARACTER.
134 /// let input = b"Hello \xF0\x90\x80World";
135 /// let output = String::from_utf8_lossy(input);
136 /// assert_eq!(output.as_slice(), "Hello \uFFFDWorld");
138 #[unstable = "return type may change"]
139 pub fn from_utf8_lossy<'a>(v: &'a [u8]) -> MaybeOwned<'a> {
141 return MaybeOwnedSlice(unsafe { mem::transmute(v) })
144 static TAG_CONT_U8: u8 = 128u8;
145 static REPLACEMENT: &'static [u8] = b"\xEF\xBF\xBD"; // U+FFFD in UTF-8
148 fn unsafe_get(xs: &[u8], i: uint) -> u8 {
149 unsafe { *xs.unsafe_get(i) }
151 fn safe_get(xs: &[u8], i: uint, total: uint) -> u8 {
159 let mut res = String::with_capacity(total);
163 res.as_mut_vec().push_all(v.slice_to(i))
167 // subseqidx is the index of the first byte of the subsequence we're looking at.
168 // It's used to copy a bunch of contiguous good codepoints at once instead of copying
170 let mut subseqidx = 0;
174 let byte = unsafe_get(v, i);
177 macro_rules! error(() => ({
180 res.as_mut_vec().push_all(v.slice(subseqidx, i_));
183 res.as_mut_vec().push_all(REPLACEMENT);
188 // subseqidx handles this
190 let w = str::utf8_char_width(byte);
194 if safe_get(v, i, total) & 192u8 != TAG_CONT_U8 {
201 match (byte, safe_get(v, i, total)) {
202 (0xE0 , 0xA0 .. 0xBF) => (),
203 (0xE1 .. 0xEC, 0x80 .. 0xBF) => (),
204 (0xED , 0x80 .. 0x9F) => (),
205 (0xEE .. 0xEF, 0x80 .. 0xBF) => (),
212 if safe_get(v, i, total) & 192u8 != TAG_CONT_U8 {
219 match (byte, safe_get(v, i, total)) {
220 (0xF0 , 0x90 .. 0xBF) => (),
221 (0xF1 .. 0xF3, 0x80 .. 0xBF) => (),
222 (0xF4 , 0x80 .. 0x8F) => (),
229 if safe_get(v, i, total) & 192u8 != TAG_CONT_U8 {
234 if safe_get(v, i, total) & 192u8 != TAG_CONT_U8 {
247 if subseqidx < total {
249 res.as_mut_vec().push_all(v.slice(subseqidx, total))
252 Owned(res.into_string())
255 /// Decode a UTF-16 encoded vector `v` into a `String`, returning `None`
256 /// if `v` contains any invalid data.
262 /// let mut v = [0xD834, 0xDD1E, 0x006d, 0x0075,
263 /// 0x0073, 0x0069, 0x0063];
264 /// assert_eq!(String::from_utf16(v), Some("𝄞music".to_string()));
266 /// // 𝄞mu<invalid>ic
268 /// assert_eq!(String::from_utf16(v), None);
270 #[unstable = "error value in return may change"]
271 pub fn from_utf16(v: &[u16]) -> Option<String> {
272 let mut s = String::with_capacity(v.len() / 2);
273 for c in str::utf16_items(v) {
275 str::ScalarValue(c) => s.push(c),
276 str::LoneSurrogate(_) => return None
282 /// Decode a UTF-16 encoded vector `v` into a string, replacing
283 /// invalid data with the replacement character (U+FFFD).
287 /// // 𝄞mus<invalid>ic<invalid>
288 /// let v = [0xD834, 0xDD1E, 0x006d, 0x0075,
289 /// 0x0073, 0xDD1E, 0x0069, 0x0063,
292 /// assert_eq!(String::from_utf16_lossy(v),
293 /// "𝄞mus\uFFFDic\uFFFD".to_string());
296 pub fn from_utf16_lossy(v: &[u16]) -> String {
297 str::utf16_items(v).map(|c| c.to_char_lossy()).collect()
300 /// Convert a vector of `char`s to a `String`.
305 /// let chars = ['h', 'e', 'l', 'l', 'o'];
306 /// let s = String::from_chars(chars);
307 /// assert_eq!(s.as_slice(), "hello");
310 #[unstable = "may be removed in favor of .collect()"]
311 pub fn from_chars(chs: &[char]) -> String {
312 chs.iter().map(|c| *c).collect()
315 /// Return the underlying byte buffer, encoded as UTF-8.
320 /// let s = String::from_str("hello");
321 /// let bytes = s.into_bytes();
322 /// assert_eq!(bytes, vec![104, 101, 108, 108, 111]);
326 pub fn into_bytes(self) -> Vec<u8> {
330 /// Pushes the given `String` onto this buffer then returns `self` so that it can be
336 /// # #![allow(deprecated)]
337 /// let s = String::from_str("hello");
338 /// let big = s.append(" ").append("world").append("!");
339 /// // s has now been moved and cannot be used
341 /// assert_eq!(big.as_slice(), "hello world!");
344 #[deprecated = "use .push_str() instead"]
345 pub fn append(mut self, second: &str) -> String {
346 self.push_str(second);
350 /// Creates a string buffer by repeating a character `length` times.
355 /// let s = String::from_char(5, 'a');
356 /// assert_eq!(s.as_slice(), "aaaaa");
359 #[unstable = "may be replaced with iterators, questionable usability, and \
360 the name may change"]
361 pub fn from_char(length: uint, ch: char) -> String {
366 let mut buf = String::new();
368 let size = buf.len() * length;
370 for _ in range(1, length) {
376 /// Converts a byte to a UTF-8 string.
380 /// Fails with invalid UTF-8 (i.e., the byte is greater than 127).
385 /// # #![allow(deprecated)]
386 /// let s = String::from_byte(104);
387 /// assert_eq!(s.as_slice(), "h");
389 #[deprecated = "use str::from_utf8 with a slice of one byte instead"]
390 pub fn from_byte(b: u8) -> String {
392 String::from_char(1, b as char)
395 /// Pushes the given string onto this string buffer.
400 /// let mut s = String::from_str("foo");
401 /// s.push_str("bar");
402 /// assert_eq!(s.as_slice(), "foobar");
405 #[unstable = "extra variants of `push`, could possibly be based on iterators"]
406 pub fn push_str(&mut self, string: &str) {
407 self.vec.push_all(string.as_bytes())
410 /// Pushes `ch` onto the given string `count` times.
415 /// let mut s = String::from_str("foo");
417 /// assert_eq!(s.as_slice(), "fooZZZZZ");
420 #[unstable = "duplicate of iterator-based functionality"]
421 pub fn grow(&mut self, count: uint, ch: char) {
422 for _ in range(0, count) {
427 /// Returns the number of bytes that this string buffer can hold without reallocating.
432 /// # #![allow(deprecated)]
433 /// let s = String::with_capacity(10);
434 /// assert!(s.byte_capacity() >= 10);
437 #[deprecated = "renamed to .capacity()"]
438 pub fn byte_capacity(&self) -> uint {
442 /// Returns the number of bytes that this string buffer can hold without reallocating.
447 /// let s = String::with_capacity(10);
448 /// assert!(s.capacity() >= 10);
451 #[unstable = "just implemented, needs to prove itself"]
452 pub fn capacity(&self) -> uint {
456 /// Reserves capacity for at least `extra` additional bytes in this string buffer.
461 /// let mut s = String::with_capacity(10);
462 /// let before = s.capacity();
463 /// s.reserve_additional(100);
464 /// assert!(s.capacity() - before >= 100);
467 pub fn reserve_additional(&mut self, extra: uint) {
468 self.vec.reserve_additional(extra)
471 /// Reserves capacity for at least `capacity` bytes in this string buffer.
476 /// let mut s = String::new();
478 /// assert!(s.capacity() >= 10);
481 pub fn reserve(&mut self, capacity: uint) {
482 self.vec.reserve(capacity)
485 /// Reserves capacity for exactly `capacity` bytes in this string buffer.
490 /// let mut s = String::new();
491 /// s.reserve_exact(10);
492 /// assert_eq!(s.capacity(), 10);
495 pub fn reserve_exact(&mut self, capacity: uint) {
496 self.vec.reserve_exact(capacity)
499 /// Shrinks the capacity of this string buffer to match its length.
504 /// let mut s = String::from_str("foo");
506 /// assert!(s.capacity() >= 100);
507 /// s.shrink_to_fit();
508 /// assert_eq!(s.capacity(), 3);
511 pub fn shrink_to_fit(&mut self) {
512 self.vec.shrink_to_fit()
515 /// Deprecated, use .push() instead.
517 #[deprecated = "renamed to .push()"]
518 pub fn push_char(&mut self, ch: char) {
522 /// Adds the given character to the end of the string.
527 /// let mut s = String::from_str("abc");
531 /// assert_eq!(s.as_slice(), "abc123");
534 #[stable = "function just renamed from push_char"]
535 pub fn push(&mut self, ch: char) {
536 let cur_len = self.len();
537 // This may use up to 4 bytes.
538 self.vec.reserve_additional(4);
541 // Attempt to not use an intermediate buffer by just pushing bytes
542 // directly onto this string.
543 let slice = RawSlice {
544 data: self.vec.as_ptr().offset(cur_len as int),
547 let used = ch.encode_utf8(mem::transmute(slice)).unwrap_or(0);
548 self.vec.set_len(cur_len + used);
552 /// Pushes the given bytes onto this string buffer.
553 /// This is unsafe because it does not check
554 /// to ensure that the resulting string will be valid UTF-8.
559 /// # #![allow(deprecated)]
560 /// let mut s = String::new();
562 /// s.push_bytes([104, 101, 108, 108, 111]);
564 /// assert_eq!(s.as_slice(), "hello");
567 #[deprecated = "call .as_mut_vec() and push onto that"]
568 pub unsafe fn push_bytes(&mut self, bytes: &[u8]) {
569 self.vec.push_all(bytes)
572 /// Works with the underlying buffer as a byte slice.
577 /// let s = String::from_str("hello");
578 /// let b: &[_] = &[104, 101, 108, 108, 111];
579 /// assert_eq!(s.as_bytes(), b);
583 pub fn as_bytes<'a>(&'a self) -> &'a [u8] {
587 /// Works with the underlying buffer as a mutable byte slice.
589 /// This is unsafe because it does not check
590 /// to ensure that the resulting string will be valid UTF-8.
595 /// # #![allow(deprecated)]
596 /// let mut s = String::from_str("hello");
598 /// let bytes = s.as_mut_bytes();
602 /// let b: &[_] = &[104, 51, 108, 108, 48];
603 /// assert_eq!(s.as_bytes(), b);
604 /// assert_eq!(s.as_slice(), "h3ll0")
607 #[deprecated = "call .as_mut_vec().as_mut_slice() instead"]
608 pub unsafe fn as_mut_bytes<'a>(&'a mut self) -> &'a mut [u8] {
609 self.vec.as_mut_slice()
612 /// Shortens a string to the specified length.
616 /// Fails if `len` > current length.
621 /// let mut s = String::from_str("hello");
623 /// assert_eq!(s.as_slice(), "he");
626 #[unstable = "the failure conventions for strings are under development"]
627 pub fn truncate(&mut self, len: uint) {
628 assert!(self.as_slice().is_char_boundary(len));
629 self.vec.truncate(len)
632 /// Appends a byte to this string buffer.
634 /// This is unsafe because it does not check
635 /// to ensure that the resulting string will be valid UTF-8.
640 /// # #![allow(deprecated)]
641 /// let mut s = String::from_str("hell");
643 /// s.push_byte(111);
645 /// assert_eq!(s.as_slice(), "hello");
648 #[deprecated = "call .as_mut_vec().push() instead"]
649 pub unsafe fn push_byte(&mut self, byte: u8) {
653 /// Removes the last byte from the string buffer and returns it.
654 /// Returns `None` if this string buffer is empty.
656 /// This is unsafe because it does not check
657 /// to ensure that the resulting string will be valid UTF-8.
662 /// # #![allow(deprecated)]
663 /// let mut s = String::from_str("foo");
665 /// assert_eq!(s.pop_byte(), Some(111));
666 /// assert_eq!(s.pop_byte(), Some(111));
667 /// assert_eq!(s.pop_byte(), Some(102));
668 /// assert_eq!(s.pop_byte(), None);
672 #[deprecated = "call .as_mut_vec().pop() instead"]
673 pub unsafe fn pop_byte(&mut self) -> Option<u8> {
674 let len = self.len();
679 let byte = self.as_bytes()[len - 1];
680 self.vec.set_len(len - 1);
684 /// Deprecated. Renamed to `pop`.
686 #[deprecated = "renamed to .pop()"]
687 pub fn pop_char(&mut self) -> Option<char> { self.pop() }
689 /// Removes the last character from the string buffer and returns it.
690 /// Returns `None` if this string buffer is empty.
695 /// let mut s = String::from_str("foo");
696 /// assert_eq!(s.pop(), Some('o'));
697 /// assert_eq!(s.pop(), Some('o'));
698 /// assert_eq!(s.pop(), Some('f'));
699 /// assert_eq!(s.pop(), None);
702 #[unstable = "this function was just renamed from pop_char"]
703 pub fn pop(&mut self) -> Option<char> {
704 let len = self.len();
709 let CharRange {ch, next} = self.as_slice().char_range_at_reverse(len);
711 self.vec.set_len(next);
716 /// Removes the first byte from the string buffer and returns it.
717 /// Returns `None` if this string buffer is empty.
719 /// This is unsafe because it does not check
720 /// to ensure that the resulting string will be valid UTF-8.
725 /// # #![allow(deprecated)]
726 /// let mut s = String::from_str("foo");
728 /// assert_eq!(s.shift_byte(), Some(102));
729 /// assert_eq!(s.shift_byte(), Some(111));
730 /// assert_eq!(s.shift_byte(), Some(111));
731 /// assert_eq!(s.shift_byte(), None);
734 #[deprecated = "call .as_mut_vec().remove(0)"]
735 pub unsafe fn shift_byte(&mut self) -> Option<u8> {
739 /// Deprecated, call `remove(0)` instead
740 #[deprecated = "call .remove(0) instead"]
741 pub fn shift_char(&mut self) -> Option<char> {
745 /// Removes the character from the string buffer at byte position `idx` and
746 /// returns it. Returns `None` if `idx` is out of bounds.
750 /// This is a O(n) operation as it requires copying every element in the
755 /// If `idx` does not lie on a character boundary, then this function will
761 /// let mut s = String::from_str("foo");
762 /// assert_eq!(s.remove(0), Some('f'));
763 /// assert_eq!(s.remove(1), Some('o'));
764 /// assert_eq!(s.remove(0), Some('o'));
765 /// assert_eq!(s.remove(0), None);
767 #[unstable = "the failure semantics of this function and return type \
769 pub fn remove(&mut self, idx: uint) -> Option<char> {
770 let len = self.len();
771 if idx >= len { return None }
773 let CharRange { ch, next } = self.as_slice().char_range_at(idx);
775 ptr::copy_memory(self.vec.as_mut_ptr().offset(idx as int),
776 self.vec.as_ptr().offset(next as int),
778 self.vec.set_len(len - (next - idx));
783 /// Insert a character into the string buffer at byte position `idx`.
787 /// This is a O(n) operation as it requires copying every element in the
792 /// If `idx` does not lie on a character boundary or is out of bounds, then
793 /// this function will fail.
794 #[unstable = "the failure semantics of this function are uncertain"]
795 pub fn insert(&mut self, idx: uint, ch: char) {
796 let len = self.len();
798 assert!(self.as_slice().is_char_boundary(idx));
799 self.vec.reserve_additional(4);
800 let mut bits = [0, ..4];
801 let amt = ch.encode_utf8(bits).unwrap();
804 ptr::copy_memory(self.vec.as_mut_ptr().offset((idx + amt) as int),
805 self.vec.as_ptr().offset(idx as int),
807 ptr::copy_memory(self.vec.as_mut_ptr().offset(idx as int),
810 self.vec.set_len(len + amt);
814 /// Views the string buffer as a mutable sequence of bytes.
816 /// This is unsafe because it does not check
817 /// to ensure that the resulting string will be valid UTF-8.
822 /// let mut s = String::from_str("hello");
824 /// let vec = s.as_mut_vec();
825 /// assert!(vec == &mut vec![104, 101, 108, 108, 111]);
828 /// assert_eq!(s.as_slice(), "olleh");
830 #[unstable = "the name of this method may be changed"]
831 pub unsafe fn as_mut_vec<'a>(&'a mut self) -> &'a mut Vec<u8> {
836 #[experimental = "collection traits will probably be removed"]
837 impl Collection for String {
840 fn len(&self) -> uint {
845 #[experimental = "collection traits will probably be removed"]
846 impl Mutable for String {
849 fn clear(&mut self) {
854 #[experimental = "waiting on FromIterator stabilization"]
855 impl FromIterator<char> for String {
856 fn from_iter<I:Iterator<char>>(iterator: I) -> String {
857 let mut buf = String::new();
858 buf.extend(iterator);
863 #[experimental = "waiting on Extendable stabilization"]
864 impl Extendable<char> for String {
865 fn extend<I:Iterator<char>>(&mut self, mut iterator: I) {
872 #[experimental = "waiting on Str stabilization"]
873 impl Str for String {
876 fn as_slice<'a>(&'a self) -> &'a str {
878 mem::transmute(self.vec.as_slice())
883 #[experimental = "waiting on StrAllocating stabilization"]
884 impl StrAllocating for String {
886 fn into_string(self) -> String {
892 impl Default for String {
893 fn default() -> String {
898 #[experimental = "waiting on Show stabilization"]
899 impl fmt::Show for String {
900 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
901 self.as_slice().fmt(f)
905 #[experimental = "waiting on Hash stabilization"]
906 impl<H: hash::Writer> hash::Hash<H> for String {
908 fn hash(&self, hasher: &mut H) {
909 self.as_slice().hash(hasher)
913 #[experimental = "waiting on Equiv stabilization"]
914 impl<'a, S: Str> Equiv<S> for String {
916 fn equiv(&self, other: &S) -> bool {
917 self.as_slice() == other.as_slice()
921 #[experimental = "waiting on Add stabilization"]
922 impl<S: Str> Add<S, String> for String {
923 fn add(&self, other: &S) -> String {
924 let mut s = String::from_str(self.as_slice());
925 s.push_str(other.as_slice());
930 impl ops::Slice<uint, str> for String {
932 fn as_slice_<'a>(&'a self) -> &'a str {
937 fn slice_from_<'a>(&'a self, from: &uint) -> &'a str {
942 fn slice_to_<'a>(&'a self, to: &uint) -> &'a str {
947 fn slice_<'a>(&'a self, from: &uint, to: &uint) -> &'a str {
952 /// Unsafe operations
953 #[unstable = "waiting on raw module conventions"]
956 use core::ptr::RawPtr;
957 use core::raw::Slice;
962 /// Creates a new `String` from a length, capacity, and pointer.
964 /// This is unsafe because:
965 /// * We call `Vec::from_raw_parts` to get a `Vec<u8>`;
966 /// * We assume that the `Vec` contains valid UTF-8.
968 pub unsafe fn from_parts(buf: *mut u8, length: uint, capacity: uint) -> String {
970 vec: Vec::from_raw_parts(length, capacity, buf),
974 /// Creates a `String` from a `*const u8` buffer of the given length.
976 /// This function is unsafe because of two reasons:
977 /// * A raw pointer is dereferenced and transmuted to `&[u8]`;
978 /// * The slice is not checked to see whether it contains valid UTF-8.
979 pub unsafe fn from_buf_len(buf: *const u8, len: uint) -> String {
980 use slice::CloneableVector;
981 let slice: &[u8] = mem::transmute(Slice {
985 self::from_utf8(slice.to_vec())
988 /// Creates a `String` from a null-terminated `*const u8` buffer.
990 /// This function is unsafe because we dereference memory until we find the NUL character,
991 /// which is not guaranteed to be present. Additionally, the slice is not checked to see
992 /// whether it contains valid UTF-8
993 pub unsafe fn from_buf(buf: *const u8) -> String {
995 while *buf.offset(len) != 0 {
998 self::from_buf_len(buf, len as uint)
1001 /// Converts a vector of bytes to a new `String` without checking if
1002 /// it contains valid UTF-8. This is unsafe because it assumes that
1003 /// the UTF-8-ness of the vector has already been validated.
1005 pub unsafe fn from_utf8(bytes: Vec<u8>) -> String {
1006 String { vec: bytes }
1012 use std::prelude::*;
1015 use {Mutable, MutableSeq};
1017 use str::{Str, StrSlice, Owned};
1022 fn test_from_str() {
1023 let owned: Option<::std::string::String> = from_str("string");
1024 assert_eq!(owned.as_ref().map(|s| s.as_slice()), Some("string"));
1028 fn test_from_utf8() {
1029 let xs = Vec::from_slice(b"hello");
1030 assert_eq!(String::from_utf8(xs), Ok(String::from_str("hello")));
1032 let xs = Vec::from_slice("ศไทย中华Việt Nam".as_bytes());
1033 assert_eq!(String::from_utf8(xs), Ok(String::from_str("ศไทย中华Việt Nam")));
1035 let xs = Vec::from_slice(b"hello\xFF");
1036 assert_eq!(String::from_utf8(xs),
1037 Err(Vec::from_slice(b"hello\xFF")));
1041 fn test_from_utf8_lossy() {
1043 assert_eq!(String::from_utf8_lossy(xs), str::Slice("hello"));
1045 let xs = "ศไทย中华Việt Nam".as_bytes();
1046 assert_eq!(String::from_utf8_lossy(xs), str::Slice("ศไทย中华Việt Nam"));
1048 let xs = b"Hello\xC2 There\xFF Goodbye";
1049 assert_eq!(String::from_utf8_lossy(xs),
1050 Owned(String::from_str("Hello\uFFFD There\uFFFD Goodbye")));
1052 let xs = b"Hello\xC0\x80 There\xE6\x83 Goodbye";
1053 assert_eq!(String::from_utf8_lossy(xs),
1054 Owned(String::from_str("Hello\uFFFD\uFFFD There\uFFFD Goodbye")));
1056 let xs = b"\xF5foo\xF5\x80bar";
1057 assert_eq!(String::from_utf8_lossy(xs),
1058 Owned(String::from_str("\uFFFDfoo\uFFFD\uFFFDbar")));
1060 let xs = b"\xF1foo\xF1\x80bar\xF1\x80\x80baz";
1061 assert_eq!(String::from_utf8_lossy(xs),
1062 Owned(String::from_str("\uFFFDfoo\uFFFDbar\uFFFDbaz")));
1064 let xs = b"\xF4foo\xF4\x80bar\xF4\xBFbaz";
1065 assert_eq!(String::from_utf8_lossy(xs),
1066 Owned(String::from_str("\uFFFDfoo\uFFFDbar\uFFFD\uFFFDbaz")));
1068 let xs = b"\xF0\x80\x80\x80foo\xF0\x90\x80\x80bar";
1069 assert_eq!(String::from_utf8_lossy(xs), Owned(String::from_str("\uFFFD\uFFFD\uFFFD\uFFFD\
1070 foo\U00010000bar")));
1073 let xs = b"\xED\xA0\x80foo\xED\xBF\xBFbar";
1074 assert_eq!(String::from_utf8_lossy(xs), Owned(String::from_str("\uFFFD\uFFFD\uFFFDfoo\
1075 \uFFFD\uFFFD\uFFFDbar")));
1079 fn test_from_utf16() {
1081 [(String::from_str("𐍅𐌿𐌻𐍆𐌹𐌻𐌰\n"),
1082 vec![0xd800_u16, 0xdf45_u16, 0xd800_u16, 0xdf3f_u16,
1083 0xd800_u16, 0xdf3b_u16, 0xd800_u16, 0xdf46_u16,
1084 0xd800_u16, 0xdf39_u16, 0xd800_u16, 0xdf3b_u16,
1085 0xd800_u16, 0xdf30_u16, 0x000a_u16]),
1087 (String::from_str("𐐒𐑉𐐮𐑀𐐲𐑋 𐐏𐐲𐑍\n"),
1088 vec![0xd801_u16, 0xdc12_u16, 0xd801_u16,
1089 0xdc49_u16, 0xd801_u16, 0xdc2e_u16, 0xd801_u16,
1090 0xdc40_u16, 0xd801_u16, 0xdc32_u16, 0xd801_u16,
1091 0xdc4b_u16, 0x0020_u16, 0xd801_u16, 0xdc0f_u16,
1092 0xd801_u16, 0xdc32_u16, 0xd801_u16, 0xdc4d_u16,
1095 (String::from_str("𐌀𐌖𐌋𐌄𐌑𐌉·𐌌𐌄𐌕𐌄𐌋𐌉𐌑\n"),
1096 vec![0xd800_u16, 0xdf00_u16, 0xd800_u16, 0xdf16_u16,
1097 0xd800_u16, 0xdf0b_u16, 0xd800_u16, 0xdf04_u16,
1098 0xd800_u16, 0xdf11_u16, 0xd800_u16, 0xdf09_u16,
1099 0x00b7_u16, 0xd800_u16, 0xdf0c_u16, 0xd800_u16,
1100 0xdf04_u16, 0xd800_u16, 0xdf15_u16, 0xd800_u16,
1101 0xdf04_u16, 0xd800_u16, 0xdf0b_u16, 0xd800_u16,
1102 0xdf09_u16, 0xd800_u16, 0xdf11_u16, 0x000a_u16 ]),
1104 (String::from_str("𐒋𐒘𐒈𐒑𐒛𐒒 𐒕𐒓 𐒈𐒚𐒍 𐒏𐒜𐒒𐒖𐒆 𐒕𐒆\n"),
1105 vec![0xd801_u16, 0xdc8b_u16, 0xd801_u16, 0xdc98_u16,
1106 0xd801_u16, 0xdc88_u16, 0xd801_u16, 0xdc91_u16,
1107 0xd801_u16, 0xdc9b_u16, 0xd801_u16, 0xdc92_u16,
1108 0x0020_u16, 0xd801_u16, 0xdc95_u16, 0xd801_u16,
1109 0xdc93_u16, 0x0020_u16, 0xd801_u16, 0xdc88_u16,
1110 0xd801_u16, 0xdc9a_u16, 0xd801_u16, 0xdc8d_u16,
1111 0x0020_u16, 0xd801_u16, 0xdc8f_u16, 0xd801_u16,
1112 0xdc9c_u16, 0xd801_u16, 0xdc92_u16, 0xd801_u16,
1113 0xdc96_u16, 0xd801_u16, 0xdc86_u16, 0x0020_u16,
1114 0xd801_u16, 0xdc95_u16, 0xd801_u16, 0xdc86_u16,
1116 // Issue #12318, even-numbered non-BMP planes
1117 (String::from_str("\U00020000"),
1118 vec![0xD840, 0xDC00])];
1120 for p in pairs.iter() {
1121 let (s, u) = (*p).clone();
1122 let s_as_utf16 = s.as_slice().utf16_units().collect::<Vec<u16>>();
1123 let u_as_string = String::from_utf16(u.as_slice()).unwrap();
1125 assert!(str::is_utf16(u.as_slice()));
1126 assert_eq!(s_as_utf16, u);
1128 assert_eq!(u_as_string, s);
1129 assert_eq!(String::from_utf16_lossy(u.as_slice()), s);
1131 assert_eq!(String::from_utf16(s_as_utf16.as_slice()).unwrap(), s);
1132 assert_eq!(u_as_string.as_slice().utf16_units().collect::<Vec<u16>>(), u);
1137 fn test_utf16_invalid() {
1138 // completely positive cases tested above.
1140 assert_eq!(String::from_utf16([0xD800]), None);
1142 assert_eq!(String::from_utf16([0xD800, 0xD800]), None);
1145 assert_eq!(String::from_utf16([0x0061, 0xDC00]), None);
1148 assert_eq!(String::from_utf16([0xD800, 0xd801, 0xdc8b, 0xD800]), None);
1152 fn test_from_utf16_lossy() {
1153 // completely positive cases tested above.
1155 assert_eq!(String::from_utf16_lossy([0xD800]), String::from_str("\uFFFD"));
1157 assert_eq!(String::from_utf16_lossy([0xD800, 0xD800]), String::from_str("\uFFFD\uFFFD"));
1160 assert_eq!(String::from_utf16_lossy([0x0061, 0xDC00]), String::from_str("a\uFFFD"));
1163 assert_eq!(String::from_utf16_lossy([0xD800, 0xd801, 0xdc8b, 0xD800]),
1164 String::from_str("\uFFFD𐒋\uFFFD"));
1168 fn test_from_buf_len() {
1170 let a = vec![65u8, 65, 65, 65, 65, 65, 65, 0];
1171 assert_eq!(super::raw::from_buf_len(a.as_ptr(), 3), String::from_str("AAA"));
1176 fn test_from_buf() {
1178 let a = vec![65, 65, 65, 65, 65, 65, 65, 0];
1180 let c = super::raw::from_buf(b);
1181 assert_eq!(c, String::from_str("AAAAAAA"));
1186 fn test_push_bytes() {
1187 let mut s = String::from_str("ABC");
1189 s.push_bytes([b'D']);
1191 assert_eq!(s.as_slice(), "ABCD");
1195 fn test_push_str() {
1196 let mut s = String::new();
1198 assert_eq!(s.as_slice().slice_from(0), "");
1200 assert_eq!(s.as_slice().slice_from(0), "abc");
1201 s.push_str("ประเทศไทย中华Việt Nam");
1202 assert_eq!(s.as_slice().slice_from(0), "abcประเทศไทย中华Việt Nam");
1207 let mut data = String::from_str("ประเทศไทย中");
1209 data.push('b'); // 1 byte
1210 data.push('¢'); // 2 byte
1211 data.push('€'); // 3 byte
1212 data.push('𤭢'); // 4 byte
1213 assert_eq!(data.as_slice(), "ประเทศไทย中华b¢€𤭢");
1217 fn test_pop_char() {
1218 let mut data = String::from_str("ประเทศไทย中华b¢€𤭢");
1219 assert_eq!(data.pop_char().unwrap(), '𤭢'); // 4 bytes
1220 assert_eq!(data.pop_char().unwrap(), '€'); // 3 bytes
1221 assert_eq!(data.pop_char().unwrap(), '¢'); // 2 bytes
1222 assert_eq!(data.pop_char().unwrap(), 'b'); // 1 bytes
1223 assert_eq!(data.pop_char().unwrap(), '华');
1224 assert_eq!(data.as_slice(), "ประเทศไทย中");
1228 fn test_shift_char() {
1229 let mut data = String::from_str("𤭢€¢b华ประเทศไทย中");
1230 assert_eq!(data.shift_char().unwrap(), '𤭢'); // 4 bytes
1231 assert_eq!(data.shift_char().unwrap(), '€'); // 3 bytes
1232 assert_eq!(data.shift_char().unwrap(), '¢'); // 2 bytes
1233 assert_eq!(data.shift_char().unwrap(), 'b'); // 1 bytes
1234 assert_eq!(data.shift_char().unwrap(), '华');
1235 assert_eq!(data.as_slice(), "ประเทศไทย中");
1239 fn test_str_truncate() {
1240 let mut s = String::from_str("12345");
1242 assert_eq!(s.as_slice(), "12345");
1244 assert_eq!(s.as_slice(), "123");
1246 assert_eq!(s.as_slice(), "");
1248 let mut s = String::from_str("12345");
1249 let p = s.as_slice().as_ptr();
1252 let p_ = s.as_slice().as_ptr();
1258 fn test_str_truncate_invalid_len() {
1259 let mut s = String::from_str("12345");
1265 fn test_str_truncate_split_codepoint() {
1266 let mut s = String::from_str("\u00FC"); // ü
1271 fn test_str_clear() {
1272 let mut s = String::from_str("12345");
1274 assert_eq!(s.len(), 0);
1275 assert_eq!(s.as_slice(), "");
1280 let a = String::from_str("12345");
1282 let b = b + String::from_str("2");
1283 assert_eq!(b.len(), 7);
1284 assert_eq!(b.as_slice(), "1234522");
1289 let mut s = "ศไทย中华Việt Nam; foobar".to_string();;
1290 assert_eq!(s.remove(0), Some('ศ'));
1291 assert_eq!(s.len(), 33);
1292 assert_eq!(s.as_slice(), "ไทย中华Việt Nam; foobar");
1293 assert_eq!(s.remove(33), None);
1294 assert_eq!(s.remove(300), None);
1295 assert_eq!(s.remove(17), Some('ệ'));
1296 assert_eq!(s.as_slice(), "ไทย中华Vit Nam; foobar");
1299 #[test] #[should_fail]
1301 "ศ".to_string().remove(1);
1306 let mut s = "foobar".to_string();
1308 assert_eq!(s.as_slice(), "ệfoobar");
1310 assert_eq!(s.as_slice(), "ệfooยbar");
1313 #[test] #[should_fail] fn insert_bad1() { "".to_string().insert(1, 't'); }
1314 #[test] #[should_fail] fn insert_bad2() { "ệ".to_string().insert(1, 't'); }
1318 let s = "foobar".to_string();
1319 assert_eq!("foobar", s[]);
1320 assert_eq!("foo", s[..3]);
1321 assert_eq!("bar", s[3..]);
1322 assert_eq!("oob", s[1..4]);
1326 fn bench_with_capacity(b: &mut Bencher) {
1328 String::with_capacity(100)
1333 fn bench_push_str(b: &mut Bencher) {
1334 let s = "ศไทย中华Việt Nam; Mary had a little lamb, Little lamb";
1336 let mut r = String::new();
1342 fn from_utf8_lossy_100_ascii(b: &mut Bencher) {
1343 let s = b"Hello there, the quick brown fox jumped over the lazy dog! \
1344 Lorem ipsum dolor sit amet, consectetur. ";
1346 assert_eq!(100, s.len());
1348 let _ = String::from_utf8_lossy(s);
1353 fn from_utf8_lossy_100_multibyte(b: &mut Bencher) {
1354 let s = "𐌀𐌖𐌋𐌄𐌑𐌉ปรدولة الكويتทศไทย中华𐍅𐌿𐌻𐍆𐌹𐌻𐌰".as_bytes();
1355 assert_eq!(100, s.len());
1357 let _ = String::from_utf8_lossy(s);
1362 fn from_utf8_lossy_invalid(b: &mut Bencher) {
1363 let s = b"Hello\xC0\x80 There\xE6\x83 Goodbye";
1365 let _ = String::from_utf8_lossy(s);
1370 fn from_utf8_lossy_100_invalid(b: &mut Bencher) {
1371 let s = Vec::from_elem(100, 0xF5u8);
1373 let _ = String::from_utf8_lossy(s.as_slice());