]> git.lizzy.rs Git - rust.git/blob - src/libcollections/string.rs
debuginfo: Make debuginfo source location assignment more stable (Pt. 1)
[rust.git] / src / libcollections / string.rs
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.
4 //
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.
10 //
11 // ignore-lexer-test FIXME #15679
12
13 //! An owned, growable string that enforces that its contents are valid UTF-8.
14
15 #![stable]
16
17 use core::prelude::*;
18
19 use core::borrow::{Cow, IntoCow};
20 use core::default::Default;
21 use core::fmt;
22 use core::hash;
23 use core::iter::FromIterator;
24 use core::mem;
25 use core::ops::{self, Deref, Add, Index};
26 use core::ptr;
27 use core::raw::Slice as RawSlice;
28 use unicode::str as unicode_str;
29 use unicode::str::Utf16Item;
30
31 use str::{self, CharRange, FromStr, Utf8Error};
32 use vec::{DerefVec, Vec, as_vec};
33
34 /// A growable string stored as a UTF-8 encoded buffer.
35 #[derive(Clone, PartialOrd, Eq, Ord)]
36 #[stable]
37 pub struct String {
38     vec: Vec<u8>,
39 }
40
41 /// A possible error value from the `String::from_utf8` function.
42 #[stable]
43 pub struct FromUtf8Error {
44     bytes: Vec<u8>,
45     error: Utf8Error,
46 }
47
48 /// A possible error value from the `String::from_utf16` function.
49 #[stable]
50 #[allow(missing_copy_implementations)]
51 pub struct FromUtf16Error(());
52
53 impl String {
54     /// Creates a new string buffer initialized with the empty string.
55     ///
56     /// # Examples
57     ///
58     /// ```
59     /// let mut s = String::new();
60     /// ```
61     #[inline]
62     #[stable]
63     pub fn new() -> String {
64         String {
65             vec: Vec::new(),
66         }
67     }
68
69     /// Creates a new string buffer with the given capacity.
70     /// The string will be able to hold exactly `capacity` bytes without
71     /// reallocating. If `capacity` is 0, the string will not allocate.
72     ///
73     /// # Examples
74     ///
75     /// ```
76     /// let mut s = String::with_capacity(10);
77     /// ```
78     #[inline]
79     #[stable]
80     pub fn with_capacity(capacity: uint) -> String {
81         String {
82             vec: Vec::with_capacity(capacity),
83         }
84     }
85
86     /// Creates a new string buffer from the given string.
87     ///
88     /// # Examples
89     ///
90     /// ```
91     /// let s = String::from_str("hello");
92     /// assert_eq!(s.as_slice(), "hello");
93     /// ```
94     #[inline]
95     #[unstable = "needs investigation to see if to_string() can match perf"]
96     pub fn from_str(string: &str) -> String {
97         String { vec: ::slice::SliceExt::to_vec(string.as_bytes()) }
98     }
99
100     /// Returns the vector as a string buffer, if possible, taking care not to
101     /// copy it.
102     ///
103     /// # Failure
104     ///
105     /// If the given vector is not valid UTF-8, then the original vector and the
106     /// corresponding error is returned.
107     ///
108     /// # Examples
109     ///
110     /// ```rust
111     /// use std::str::Utf8Error;
112     ///
113     /// let hello_vec = vec![104, 101, 108, 108, 111];
114     /// let s = String::from_utf8(hello_vec).unwrap();
115     /// assert_eq!(s, "hello");
116     ///
117     /// let invalid_vec = vec![240, 144, 128];
118     /// let s = String::from_utf8(invalid_vec).err().unwrap();
119     /// assert_eq!(s.utf8_error(), Utf8Error::TooShort);
120     /// assert_eq!(s.into_bytes(), vec![240, 144, 128]);
121     /// ```
122     #[inline]
123     #[stable]
124     pub fn from_utf8(vec: Vec<u8>) -> Result<String, FromUtf8Error> {
125         match str::from_utf8(vec.as_slice()) {
126             Ok(..) => Ok(String { vec: vec }),
127             Err(e) => Err(FromUtf8Error { bytes: vec, error: e })
128         }
129     }
130
131     /// Converts a vector of bytes to a new UTF-8 string.
132     /// Any invalid UTF-8 sequences are replaced with U+FFFD REPLACEMENT CHARACTER.
133     ///
134     /// # Examples
135     ///
136     /// ```rust
137     /// let input = b"Hello \xF0\x90\x80World";
138     /// let output = String::from_utf8_lossy(input);
139     /// assert_eq!(output.as_slice(), "Hello \u{FFFD}World");
140     /// ```
141     #[stable]
142     pub fn from_utf8_lossy<'a>(v: &'a [u8]) -> CowString<'a> {
143         let mut i = 0;
144         match str::from_utf8(v) {
145             Ok(s) => return Cow::Borrowed(s),
146             Err(e) => {
147                 if let Utf8Error::InvalidByte(firstbad) = e {
148                     i = firstbad;
149                 }
150             }
151         }
152
153         static TAG_CONT_U8: u8 = 128u8;
154         static REPLACEMENT: &'static [u8] = b"\xEF\xBF\xBD"; // U+FFFD in UTF-8
155         let total = v.len();
156         fn unsafe_get(xs: &[u8], i: uint) -> u8 {
157             unsafe { *xs.get_unchecked(i) }
158         }
159         fn safe_get(xs: &[u8], i: uint, total: uint) -> u8 {
160             if i >= total {
161                 0
162             } else {
163                 unsafe_get(xs, i)
164             }
165         }
166
167         let mut res = String::with_capacity(total);
168
169         if i > 0 {
170             unsafe {
171                 res.as_mut_vec().push_all(&v[..i])
172             };
173         }
174
175         // subseqidx is the index of the first byte of the subsequence we're looking at.
176         // It's used to copy a bunch of contiguous good codepoints at once instead of copying
177         // them one by one.
178         let mut subseqidx = i;
179
180         while i < total {
181             let i_ = i;
182             let byte = unsafe_get(v, i);
183             i += 1;
184
185             macro_rules! error { () => ({
186                 unsafe {
187                     if subseqidx != i_ {
188                         res.as_mut_vec().push_all(&v[subseqidx..i_]);
189                     }
190                     subseqidx = i;
191                     res.as_mut_vec().push_all(REPLACEMENT);
192                 }
193             })}
194
195             if byte < 128u8 {
196                 // subseqidx handles this
197             } else {
198                 let w = unicode_str::utf8_char_width(byte);
199
200                 match w {
201                     2 => {
202                         if safe_get(v, i, total) & 192u8 != TAG_CONT_U8 {
203                             error!();
204                             continue;
205                         }
206                         i += 1;
207                     }
208                     3 => {
209                         match (byte, safe_get(v, i, total)) {
210                             (0xE0         , 0xA0 ... 0xBF) => (),
211                             (0xE1 ... 0xEC, 0x80 ... 0xBF) => (),
212                             (0xED         , 0x80 ... 0x9F) => (),
213                             (0xEE ... 0xEF, 0x80 ... 0xBF) => (),
214                             _ => {
215                                 error!();
216                                 continue;
217                             }
218                         }
219                         i += 1;
220                         if safe_get(v, i, total) & 192u8 != TAG_CONT_U8 {
221                             error!();
222                             continue;
223                         }
224                         i += 1;
225                     }
226                     4 => {
227                         match (byte, safe_get(v, i, total)) {
228                             (0xF0         , 0x90 ... 0xBF) => (),
229                             (0xF1 ... 0xF3, 0x80 ... 0xBF) => (),
230                             (0xF4         , 0x80 ... 0x8F) => (),
231                             _ => {
232                                 error!();
233                                 continue;
234                             }
235                         }
236                         i += 1;
237                         if safe_get(v, i, total) & 192u8 != TAG_CONT_U8 {
238                             error!();
239                             continue;
240                         }
241                         i += 1;
242                         if safe_get(v, i, total) & 192u8 != TAG_CONT_U8 {
243                             error!();
244                             continue;
245                         }
246                         i += 1;
247                     }
248                     _ => {
249                         error!();
250                         continue;
251                     }
252                 }
253             }
254         }
255         if subseqidx < total {
256             unsafe {
257                 res.as_mut_vec().push_all(&v[subseqidx..total])
258             };
259         }
260         Cow::Owned(res)
261     }
262
263     /// Decode a UTF-16 encoded vector `v` into a `String`, returning `None`
264     /// if `v` contains any invalid data.
265     ///
266     /// # Examples
267     ///
268     /// ```rust
269     /// // 𝄞music
270     /// let mut v = &mut [0xD834, 0xDD1E, 0x006d, 0x0075,
271     ///                   0x0073, 0x0069, 0x0063];
272     /// assert_eq!(String::from_utf16(v).unwrap(),
273     ///            "𝄞music".to_string());
274     ///
275     /// // 𝄞mu<invalid>ic
276     /// v[4] = 0xD800;
277     /// assert!(String::from_utf16(v).is_err());
278     /// ```
279     #[stable]
280     pub fn from_utf16(v: &[u16]) -> Result<String, FromUtf16Error> {
281         let mut s = String::with_capacity(v.len());
282         for c in unicode_str::utf16_items(v) {
283             match c {
284                 Utf16Item::ScalarValue(c) => s.push(c),
285                 Utf16Item::LoneSurrogate(_) => return Err(FromUtf16Error(())),
286             }
287         }
288         Ok(s)
289     }
290
291     /// Decode a UTF-16 encoded vector `v` into a string, replacing
292     /// invalid data with the replacement character (U+FFFD).
293     ///
294     /// # Examples
295     ///
296     /// ```rust
297     /// // 𝄞mus<invalid>ic<invalid>
298     /// let v = &[0xD834, 0xDD1E, 0x006d, 0x0075,
299     ///           0x0073, 0xDD1E, 0x0069, 0x0063,
300     ///           0xD834];
301     ///
302     /// assert_eq!(String::from_utf16_lossy(v),
303     ///            "𝄞mus\u{FFFD}ic\u{FFFD}".to_string());
304     /// ```
305     #[inline]
306     #[stable]
307     pub fn from_utf16_lossy(v: &[u16]) -> String {
308         unicode_str::utf16_items(v).map(|c| c.to_char_lossy()).collect()
309     }
310
311     /// Creates a new `String` from a length, capacity, and pointer.
312     ///
313     /// This is unsafe because:
314     /// * We call `Vec::from_raw_parts` to get a `Vec<u8>`;
315     /// * We assume that the `Vec` contains valid UTF-8.
316     #[inline]
317     #[stable]
318     pub unsafe fn from_raw_parts(buf: *mut u8, length: uint, capacity: uint) -> String {
319         String {
320             vec: Vec::from_raw_parts(buf, length, capacity),
321         }
322     }
323
324     /// Converts a vector of bytes to a new `String` without checking if
325     /// it contains valid UTF-8. This is unsafe because it assumes that
326     /// the UTF-8-ness of the vector has already been validated.
327     #[inline]
328     #[stable]
329     pub unsafe fn from_utf8_unchecked(bytes: Vec<u8>) -> String {
330         String { vec: bytes }
331     }
332
333     /// Return the underlying byte buffer, encoded as UTF-8.
334     ///
335     /// # Examples
336     ///
337     /// ```
338     /// let s = String::from_str("hello");
339     /// let bytes = s.into_bytes();
340     /// assert_eq!(bytes, vec![104, 101, 108, 108, 111]);
341     /// ```
342     #[inline]
343     #[stable]
344     pub fn into_bytes(self) -> Vec<u8> {
345         self.vec
346     }
347
348     /// Pushes the given string onto this string buffer.
349     ///
350     /// # Examples
351     ///
352     /// ```
353     /// let mut s = String::from_str("foo");
354     /// s.push_str("bar");
355     /// assert_eq!(s.as_slice(), "foobar");
356     /// ```
357     #[inline]
358     #[stable]
359     pub fn push_str(&mut self, string: &str) {
360         self.vec.push_all(string.as_bytes())
361     }
362
363     /// Returns the number of bytes that this string buffer can hold without
364     /// reallocating.
365     ///
366     /// # Examples
367     ///
368     /// ```
369     /// let s = String::with_capacity(10);
370     /// assert!(s.capacity() >= 10);
371     /// ```
372     #[inline]
373     #[stable]
374     pub fn capacity(&self) -> uint {
375         self.vec.capacity()
376     }
377
378     /// Reserves capacity for at least `additional` more bytes to be inserted
379     /// in the given `String`. The collection may reserve more space to avoid
380     /// frequent reallocations.
381     ///
382     /// # Panics
383     ///
384     /// Panics if the new capacity overflows `uint`.
385     ///
386     /// # Examples
387     ///
388     /// ```
389     /// let mut s = String::new();
390     /// s.reserve(10);
391     /// assert!(s.capacity() >= 10);
392     /// ```
393     #[inline]
394     #[stable]
395     pub fn reserve(&mut self, additional: uint) {
396         self.vec.reserve(additional)
397     }
398
399     /// Reserves the minimum capacity for exactly `additional` more bytes to be
400     /// inserted in the given `String`. Does nothing if the capacity is already
401     /// sufficient.
402     ///
403     /// Note that the allocator may give the collection more space than it
404     /// requests. Therefore capacity can not be relied upon to be precisely
405     /// minimal. Prefer `reserve` if future insertions are expected.
406     ///
407     /// # Panics
408     ///
409     /// Panics if the new capacity overflows `uint`.
410     ///
411     /// # Examples
412     ///
413     /// ```
414     /// let mut s = String::new();
415     /// s.reserve(10);
416     /// assert!(s.capacity() >= 10);
417     /// ```
418     #[inline]
419     #[stable]
420     pub fn reserve_exact(&mut self, additional: uint) {
421         self.vec.reserve_exact(additional)
422     }
423
424     /// Shrinks the capacity of this string buffer to match its length.
425     ///
426     /// # Examples
427     ///
428     /// ```
429     /// let mut s = String::from_str("foo");
430     /// s.reserve(100);
431     /// assert!(s.capacity() >= 100);
432     /// s.shrink_to_fit();
433     /// assert_eq!(s.capacity(), 3);
434     /// ```
435     #[inline]
436     #[stable]
437     pub fn shrink_to_fit(&mut self) {
438         self.vec.shrink_to_fit()
439     }
440
441     /// Adds the given character to the end of the string.
442     ///
443     /// # Examples
444     ///
445     /// ```
446     /// let mut s = String::from_str("abc");
447     /// s.push('1');
448     /// s.push('2');
449     /// s.push('3');
450     /// assert_eq!(s.as_slice(), "abc123");
451     /// ```
452     #[inline]
453     #[stable]
454     pub fn push(&mut self, ch: char) {
455         if (ch as u32) < 0x80 {
456             self.vec.push(ch as u8);
457             return;
458         }
459
460         let cur_len = self.len();
461         // This may use up to 4 bytes.
462         self.vec.reserve(4);
463
464         unsafe {
465             // Attempt to not use an intermediate buffer by just pushing bytes
466             // directly onto this string.
467             let slice = RawSlice {
468                 data: self.vec.as_ptr().offset(cur_len as int),
469                 len: 4,
470             };
471             let used = ch.encode_utf8(mem::transmute(slice)).unwrap_or(0);
472             self.vec.set_len(cur_len + used);
473         }
474     }
475
476     /// Works with the underlying buffer as a byte slice.
477     ///
478     /// # Examples
479     ///
480     /// ```
481     /// let s = String::from_str("hello");
482     /// let b: &[_] = &[104, 101, 108, 108, 111];
483     /// assert_eq!(s.as_bytes(), b);
484     /// ```
485     #[inline]
486     #[stable]
487     pub fn as_bytes<'a>(&'a self) -> &'a [u8] {
488         self.vec.as_slice()
489     }
490
491     /// Shortens a string to the specified length.
492     ///
493     /// # Panics
494     ///
495     /// Panics if `new_len` > current length,
496     /// or if `new_len` is not a character boundary.
497     ///
498     /// # Examples
499     ///
500     /// ```
501     /// let mut s = String::from_str("hello");
502     /// s.truncate(2);
503     /// assert_eq!(s.as_slice(), "he");
504     /// ```
505     #[inline]
506     #[stable]
507     pub fn truncate(&mut self, new_len: uint) {
508         assert!(self.is_char_boundary(new_len));
509         self.vec.truncate(new_len)
510     }
511
512     /// Removes the last character from the string buffer and returns it.
513     /// Returns `None` if this string buffer is empty.
514     ///
515     /// # Examples
516     ///
517     /// ```
518     /// let mut s = String::from_str("foo");
519     /// assert_eq!(s.pop(), Some('o'));
520     /// assert_eq!(s.pop(), Some('o'));
521     /// assert_eq!(s.pop(), Some('f'));
522     /// assert_eq!(s.pop(), None);
523     /// ```
524     #[inline]
525     #[stable]
526     pub fn pop(&mut self) -> Option<char> {
527         let len = self.len();
528         if len == 0 {
529             return None
530         }
531
532         let CharRange {ch, next} = self.char_range_at_reverse(len);
533         unsafe {
534             self.vec.set_len(next);
535         }
536         Some(ch)
537     }
538
539     /// Removes the character from the string buffer at byte position `idx` and
540     /// returns it.
541     ///
542     /// # Warning
543     ///
544     /// This is an O(n) operation as it requires copying every element in the
545     /// buffer.
546     ///
547     /// # Panics
548     ///
549     /// If `idx` does not lie on a character boundary, or if it is out of
550     /// bounds, then this function will panic.
551     ///
552     /// # Examples
553     ///
554     /// ```
555     /// let mut s = String::from_str("foo");
556     /// assert_eq!(s.remove(0), 'f');
557     /// assert_eq!(s.remove(1), 'o');
558     /// assert_eq!(s.remove(0), 'o');
559     /// ```
560     #[inline]
561     #[stable]
562     pub fn remove(&mut self, idx: uint) -> char {
563         let len = self.len();
564         assert!(idx <= len);
565
566         let CharRange { ch, next } = self.char_range_at(idx);
567         unsafe {
568             ptr::copy_memory(self.vec.as_mut_ptr().offset(idx as int),
569                              self.vec.as_ptr().offset(next as int),
570                              len - next);
571             self.vec.set_len(len - (next - idx));
572         }
573         ch
574     }
575
576     /// Insert a character into the string buffer at byte position `idx`.
577     ///
578     /// # Warning
579     ///
580     /// This is an O(n) operation as it requires copying every element in the
581     /// buffer.
582     ///
583     /// # Panics
584     ///
585     /// If `idx` does not lie on a character boundary or is out of bounds, then
586     /// this function will panic.
587     #[inline]
588     #[stable]
589     pub fn insert(&mut self, idx: uint, ch: char) {
590         let len = self.len();
591         assert!(idx <= len);
592         assert!(self.is_char_boundary(idx));
593         self.vec.reserve(4);
594         let mut bits = [0; 4];
595         let amt = ch.encode_utf8(&mut bits).unwrap();
596
597         unsafe {
598             ptr::copy_memory(self.vec.as_mut_ptr().offset((idx + amt) as int),
599                              self.vec.as_ptr().offset(idx as int),
600                              len - idx);
601             ptr::copy_memory(self.vec.as_mut_ptr().offset(idx as int),
602                              bits.as_ptr(),
603                              amt);
604             self.vec.set_len(len + amt);
605         }
606     }
607
608     /// Views the string buffer as a mutable sequence of bytes.
609     ///
610     /// This is unsafe because it does not check
611     /// to ensure that the resulting string will be valid UTF-8.
612     ///
613     /// # Examples
614     ///
615     /// ```
616     /// let mut s = String::from_str("hello");
617     /// unsafe {
618     ///     let vec = s.as_mut_vec();
619     ///     assert!(vec == &mut vec![104, 101, 108, 108, 111]);
620     ///     vec.reverse();
621     /// }
622     /// assert_eq!(s.as_slice(), "olleh");
623     /// ```
624     #[inline]
625     #[stable]
626     pub unsafe fn as_mut_vec<'a>(&'a mut self) -> &'a mut Vec<u8> {
627         &mut self.vec
628     }
629
630     /// Return the number of bytes in this string.
631     ///
632     /// # Examples
633     ///
634     /// ```
635     /// let a = "foo".to_string();
636     /// assert_eq!(a.len(), 3);
637     /// ```
638     #[inline]
639     #[stable]
640     pub fn len(&self) -> uint { self.vec.len() }
641
642     /// Returns true if the string contains no bytes
643     ///
644     /// # Examples
645     ///
646     /// ```
647     /// let mut v = String::new();
648     /// assert!(v.is_empty());
649     /// v.push('a');
650     /// assert!(!v.is_empty());
651     /// ```
652     #[inline]
653     #[stable]
654     pub fn is_empty(&self) -> bool { self.len() == 0 }
655
656     /// Truncates the string, returning it to 0 length.
657     ///
658     /// # Examples
659     ///
660     /// ```
661     /// let mut s = "foo".to_string();
662     /// s.clear();
663     /// assert!(s.is_empty());
664     /// ```
665     #[inline]
666     #[stable]
667     pub fn clear(&mut self) {
668         self.vec.clear()
669     }
670 }
671
672 impl FromUtf8Error {
673     /// Consume this error, returning the bytes that were attempted to make a
674     /// `String` with.
675     #[stable]
676     pub fn into_bytes(self) -> Vec<u8> { self.bytes }
677
678     /// Access the underlying UTF8-error that was the cause of this error.
679     #[stable]
680     pub fn utf8_error(&self) -> Utf8Error { self.error }
681 }
682
683 impl fmt::Show for FromUtf8Error {
684     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
685         fmt::String::fmt(self, f)
686     }
687 }
688
689 #[stable]
690 impl fmt::String for FromUtf8Error {
691     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
692         fmt::String::fmt(&self.error, f)
693     }
694 }
695
696 impl fmt::Show for FromUtf16Error {
697     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
698         fmt::String::fmt(self, f)
699     }
700 }
701
702 #[stable]
703 impl fmt::String for FromUtf16Error {
704     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
705         fmt::String::fmt("invalid utf-16: lone surrogate found", f)
706     }
707 }
708
709 #[stable]
710 impl FromIterator<char> for String {
711     fn from_iter<I:Iterator<Item=char>>(iterator: I) -> String {
712         let mut buf = String::new();
713         buf.extend(iterator);
714         buf
715     }
716 }
717
718 #[stable]
719 impl<'a> FromIterator<&'a str> for String {
720     fn from_iter<I:Iterator<Item=&'a str>>(iterator: I) -> String {
721         let mut buf = String::new();
722         buf.extend(iterator);
723         buf
724     }
725 }
726
727 #[unstable = "waiting on Extend stabilization"]
728 impl Extend<char> for String {
729     fn extend<I:Iterator<Item=char>>(&mut self, mut iterator: I) {
730         let (lower_bound, _) = iterator.size_hint();
731         self.reserve(lower_bound);
732         for ch in iterator {
733             self.push(ch)
734         }
735     }
736 }
737
738 #[unstable = "waiting on Extend stabilization"]
739 impl<'a> Extend<&'a str> for String {
740     fn extend<I: Iterator<Item=&'a str>>(&mut self, mut iterator: I) {
741         // A guess that at least one byte per iterator element will be needed.
742         let (lower_bound, _) = iterator.size_hint();
743         self.reserve(lower_bound);
744         for s in iterator {
745             self.push_str(s)
746         }
747     }
748 }
749
750 #[stable]
751 impl PartialEq for String {
752     #[inline]
753     fn eq(&self, other: &String) -> bool { PartialEq::eq(&**self, &**other) }
754     #[inline]
755     fn ne(&self, other: &String) -> bool { PartialEq::ne(&**self, &**other) }
756 }
757
758 macro_rules! impl_eq {
759     ($lhs:ty, $rhs: ty) => {
760         #[stable]
761         impl<'a> PartialEq<$rhs> for $lhs {
762             #[inline]
763             fn eq(&self, other: &$rhs) -> bool { PartialEq::eq(&**self, &**other) }
764             #[inline]
765             fn ne(&self, other: &$rhs) -> bool { PartialEq::ne(&**self, &**other) }
766         }
767
768         #[stable]
769         impl<'a> PartialEq<$lhs> for $rhs {
770             #[inline]
771             fn eq(&self, other: &$lhs) -> bool { PartialEq::eq(&**self, &**other) }
772             #[inline]
773             fn ne(&self, other: &$lhs) -> bool { PartialEq::ne(&**self, &**other) }
774         }
775
776     }
777 }
778
779 impl_eq! { String, &'a str }
780 impl_eq! { CowString<'a>, String }
781
782 #[stable]
783 impl<'a, 'b> PartialEq<&'b str> for CowString<'a> {
784     #[inline]
785     fn eq(&self, other: &&'b str) -> bool { PartialEq::eq(&**self, &**other) }
786     #[inline]
787     fn ne(&self, other: &&'b str) -> bool { PartialEq::ne(&**self, &**other) }
788 }
789
790 #[stable]
791 impl<'a, 'b> PartialEq<CowString<'a>> for &'b str {
792     #[inline]
793     fn eq(&self, other: &CowString<'a>) -> bool { PartialEq::eq(&**self, &**other) }
794     #[inline]
795     fn ne(&self, other: &CowString<'a>) -> bool { PartialEq::ne(&**self, &**other) }
796 }
797
798 #[unstable = "waiting on Str stabilization"]
799 impl Str for String {
800     #[inline]
801     #[stable]
802     fn as_slice<'a>(&'a self) -> &'a str {
803         unsafe { mem::transmute(self.vec.as_slice()) }
804     }
805 }
806
807 #[stable]
808 impl Default for String {
809     #[inline]
810     #[stable]
811     fn default() -> String {
812         String::new()
813     }
814 }
815
816 #[stable]
817 impl fmt::String for String {
818     #[inline]
819     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
820         fmt::String::fmt(&**self, f)
821     }
822 }
823
824 #[unstable = "waiting on fmt stabilization"]
825 impl fmt::Show for String {
826     #[inline]
827     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
828         fmt::Show::fmt(&**self, f)
829     }
830 }
831
832 #[unstable = "waiting on Hash stabilization"]
833 impl<H: hash::Writer + hash::Hasher> hash::Hash<H> for String {
834     #[inline]
835     fn hash(&self, hasher: &mut H) {
836         (**self).hash(hasher)
837     }
838 }
839
840 #[unstable = "recent addition, needs more experience"]
841 impl<'a> Add<&'a str> for String {
842     type Output = String;
843
844     #[inline]
845     fn add(mut self, other: &str) -> String {
846         self.push_str(other);
847         self
848     }
849 }
850
851 impl ops::Index<ops::Range<uint>> for String {
852     type Output = str;
853     #[inline]
854     fn index(&self, index: &ops::Range<uint>) -> &str {
855         &self[][*index]
856     }
857 }
858 impl ops::Index<ops::RangeTo<uint>> for String {
859     type Output = str;
860     #[inline]
861     fn index(&self, index: &ops::RangeTo<uint>) -> &str {
862         &self[][*index]
863     }
864 }
865 impl ops::Index<ops::RangeFrom<uint>> for String {
866     type Output = str;
867     #[inline]
868     fn index(&self, index: &ops::RangeFrom<uint>) -> &str {
869         &self[][*index]
870     }
871 }
872 impl ops::Index<ops::FullRange> for String {
873     type Output = str;
874     #[inline]
875     fn index(&self, _index: &ops::FullRange) -> &str {
876         unsafe { mem::transmute(self.vec.as_slice()) }
877     }
878 }
879
880 #[stable]
881 impl ops::Deref for String {
882     type Target = str;
883
884     #[inline]
885     fn deref<'a>(&'a self) -> &'a str {
886         unsafe { mem::transmute(&self.vec[]) }
887     }
888 }
889
890 /// Wrapper type providing a `&String` reference via `Deref`.
891 #[unstable]
892 pub struct DerefString<'a> {
893     x: DerefVec<'a, u8>
894 }
895
896 impl<'a> Deref for DerefString<'a> {
897     type Target = String;
898
899     #[inline]
900     fn deref<'b>(&'b self) -> &'b String {
901         unsafe { mem::transmute(&*self.x) }
902     }
903 }
904
905 /// Convert a string slice to a wrapper type providing a `&String` reference.
906 ///
907 /// # Examples
908 ///
909 /// ```
910 /// use std::string::as_string;
911 ///
912 /// fn string_consumer(s: String) {
913 ///     assert_eq!(s, "foo".to_string());
914 /// }
915 ///
916 /// let string = as_string("foo").clone();
917 /// string_consumer(string);
918 /// ```
919 #[unstable]
920 pub fn as_string<'a>(x: &'a str) -> DerefString<'a> {
921     DerefString { x: as_vec(x.as_bytes()) }
922 }
923
924 impl FromStr for String {
925     #[inline]
926     fn from_str(s: &str) -> Option<String> {
927         Some(String::from_str(s))
928     }
929 }
930
931 /// A generic trait for converting a value to a string
932 pub trait ToString {
933     /// Converts the value of `self` to an owned string
934     fn to_string(&self) -> String;
935 }
936
937 impl<T: fmt::String + ?Sized> ToString for T {
938     #[inline]
939     fn to_string(&self) -> String {
940         use core::fmt::Writer;
941         let mut buf = String::new();
942         let _ = buf.write_fmt(format_args!("{}", self));
943         buf.shrink_to_fit();
944         buf
945     }
946 }
947
948 impl IntoCow<'static, String, str> for String {
949     #[inline]
950     fn into_cow(self) -> CowString<'static> {
951         Cow::Owned(self)
952     }
953 }
954
955 impl<'a> IntoCow<'a, String, str> for &'a str {
956     #[inline]
957     fn into_cow(self) -> CowString<'a> {
958         Cow::Borrowed(self)
959     }
960 }
961
962 /// A clone-on-write string
963 #[stable]
964 pub type CowString<'a> = Cow<'a, String, str>;
965
966 impl<'a> Str for CowString<'a> {
967     #[inline]
968     fn as_slice<'b>(&'b self) -> &'b str {
969         (**self).as_slice()
970     }
971 }
972
973 impl fmt::Writer for String {
974     #[inline]
975     fn write_str(&mut self, s: &str) -> fmt::Result {
976         self.push_str(s);
977         Ok(())
978     }
979 }
980
981 #[cfg(test)]
982 mod tests {
983     use prelude::*;
984     use test::Bencher;
985
986     use str::Utf8Error;
987     use core::iter::repeat;
988     use super::{as_string, CowString};
989     use core::ops::FullRange;
990
991     #[test]
992     fn test_as_string() {
993         let x = "foo";
994         assert_eq!(x, as_string(x).as_slice());
995     }
996
997     #[test]
998     fn test_from_str() {
999       let owned: Option<::std::string::String> = "string".parse();
1000       assert_eq!(owned.as_ref().map(|s| s.as_slice()), Some("string"));
1001     }
1002
1003     #[test]
1004     fn test_unsized_to_string() {
1005         let s: &str = "abc";
1006         let _: String = (*s).to_string();
1007     }
1008
1009     #[test]
1010     fn test_from_utf8() {
1011         let xs = b"hello".to_vec();
1012         assert_eq!(String::from_utf8(xs).unwrap(),
1013                    String::from_str("hello"));
1014
1015         let xs = "ศไทย中华Việt Nam".as_bytes().to_vec();
1016         assert_eq!(String::from_utf8(xs).unwrap(),
1017                    String::from_str("ศไทย中华Việt Nam"));
1018
1019         let xs = b"hello\xFF".to_vec();
1020         let err = String::from_utf8(xs).err().unwrap();
1021         assert_eq!(err.utf8_error(), Utf8Error::TooShort);
1022         assert_eq!(err.into_bytes(), b"hello\xff".to_vec());
1023     }
1024
1025     #[test]
1026     fn test_from_utf8_lossy() {
1027         let xs = b"hello";
1028         let ys: CowString = "hello".into_cow();
1029         assert_eq!(String::from_utf8_lossy(xs), ys);
1030
1031         let xs = "ศไทย中华Việt Nam".as_bytes();
1032         let ys: CowString = "ศไทย中华Việt Nam".into_cow();
1033         assert_eq!(String::from_utf8_lossy(xs), ys);
1034
1035         let xs = b"Hello\xC2 There\xFF Goodbye";
1036         assert_eq!(String::from_utf8_lossy(xs),
1037                    String::from_str("Hello\u{FFFD} There\u{FFFD} Goodbye").into_cow());
1038
1039         let xs = b"Hello\xC0\x80 There\xE6\x83 Goodbye";
1040         assert_eq!(String::from_utf8_lossy(xs),
1041                    String::from_str("Hello\u{FFFD}\u{FFFD} There\u{FFFD} Goodbye").into_cow());
1042
1043         let xs = b"\xF5foo\xF5\x80bar";
1044         assert_eq!(String::from_utf8_lossy(xs),
1045                    String::from_str("\u{FFFD}foo\u{FFFD}\u{FFFD}bar").into_cow());
1046
1047         let xs = b"\xF1foo\xF1\x80bar\xF1\x80\x80baz";
1048         assert_eq!(String::from_utf8_lossy(xs),
1049                    String::from_str("\u{FFFD}foo\u{FFFD}bar\u{FFFD}baz").into_cow());
1050
1051         let xs = b"\xF4foo\xF4\x80bar\xF4\xBFbaz";
1052         assert_eq!(String::from_utf8_lossy(xs),
1053                    String::from_str("\u{FFFD}foo\u{FFFD}bar\u{FFFD}\u{FFFD}baz").into_cow());
1054
1055         let xs = b"\xF0\x80\x80\x80foo\xF0\x90\x80\x80bar";
1056         assert_eq!(String::from_utf8_lossy(xs), String::from_str("\u{FFFD}\u{FFFD}\u{FFFD}\u{FFFD}\
1057                                                foo\u{10000}bar").into_cow());
1058
1059         // surrogates
1060         let xs = b"\xED\xA0\x80foo\xED\xBF\xBFbar";
1061         assert_eq!(String::from_utf8_lossy(xs), String::from_str("\u{FFFD}\u{FFFD}\u{FFFD}foo\
1062                                                \u{FFFD}\u{FFFD}\u{FFFD}bar").into_cow());
1063     }
1064
1065     #[test]
1066     fn test_from_utf16() {
1067         let pairs =
1068             [(String::from_str("𐍅𐌿𐌻𐍆𐌹𐌻𐌰\n"),
1069               vec![0xd800_u16, 0xdf45_u16, 0xd800_u16, 0xdf3f_u16,
1070                 0xd800_u16, 0xdf3b_u16, 0xd800_u16, 0xdf46_u16,
1071                 0xd800_u16, 0xdf39_u16, 0xd800_u16, 0xdf3b_u16,
1072                 0xd800_u16, 0xdf30_u16, 0x000a_u16]),
1073
1074              (String::from_str("𐐒𐑉𐐮𐑀𐐲𐑋 𐐏𐐲𐑍\n"),
1075               vec![0xd801_u16, 0xdc12_u16, 0xd801_u16,
1076                 0xdc49_u16, 0xd801_u16, 0xdc2e_u16, 0xd801_u16,
1077                 0xdc40_u16, 0xd801_u16, 0xdc32_u16, 0xd801_u16,
1078                 0xdc4b_u16, 0x0020_u16, 0xd801_u16, 0xdc0f_u16,
1079                 0xd801_u16, 0xdc32_u16, 0xd801_u16, 0xdc4d_u16,
1080                 0x000a_u16]),
1081
1082              (String::from_str("𐌀𐌖𐌋𐌄𐌑𐌉·𐌌𐌄𐌕𐌄𐌋𐌉𐌑\n"),
1083               vec![0xd800_u16, 0xdf00_u16, 0xd800_u16, 0xdf16_u16,
1084                 0xd800_u16, 0xdf0b_u16, 0xd800_u16, 0xdf04_u16,
1085                 0xd800_u16, 0xdf11_u16, 0xd800_u16, 0xdf09_u16,
1086                 0x00b7_u16, 0xd800_u16, 0xdf0c_u16, 0xd800_u16,
1087                 0xdf04_u16, 0xd800_u16, 0xdf15_u16, 0xd800_u16,
1088                 0xdf04_u16, 0xd800_u16, 0xdf0b_u16, 0xd800_u16,
1089                 0xdf09_u16, 0xd800_u16, 0xdf11_u16, 0x000a_u16 ]),
1090
1091              (String::from_str("𐒋𐒘𐒈𐒑𐒛𐒒 𐒕𐒓 𐒈𐒚𐒍 𐒏𐒜𐒒𐒖𐒆 𐒕𐒆\n"),
1092               vec![0xd801_u16, 0xdc8b_u16, 0xd801_u16, 0xdc98_u16,
1093                 0xd801_u16, 0xdc88_u16, 0xd801_u16, 0xdc91_u16,
1094                 0xd801_u16, 0xdc9b_u16, 0xd801_u16, 0xdc92_u16,
1095                 0x0020_u16, 0xd801_u16, 0xdc95_u16, 0xd801_u16,
1096                 0xdc93_u16, 0x0020_u16, 0xd801_u16, 0xdc88_u16,
1097                 0xd801_u16, 0xdc9a_u16, 0xd801_u16, 0xdc8d_u16,
1098                 0x0020_u16, 0xd801_u16, 0xdc8f_u16, 0xd801_u16,
1099                 0xdc9c_u16, 0xd801_u16, 0xdc92_u16, 0xd801_u16,
1100                 0xdc96_u16, 0xd801_u16, 0xdc86_u16, 0x0020_u16,
1101                 0xd801_u16, 0xdc95_u16, 0xd801_u16, 0xdc86_u16,
1102                 0x000a_u16 ]),
1103              // Issue #12318, even-numbered non-BMP planes
1104              (String::from_str("\u{20000}"),
1105               vec![0xD840, 0xDC00])];
1106
1107         for p in pairs.iter() {
1108             let (s, u) = (*p).clone();
1109             let s_as_utf16 = s.utf16_units().collect::<Vec<u16>>();
1110             let u_as_string = String::from_utf16(u.as_slice()).unwrap();
1111
1112             assert!(::unicode::str::is_utf16(u.as_slice()));
1113             assert_eq!(s_as_utf16, u);
1114
1115             assert_eq!(u_as_string, s);
1116             assert_eq!(String::from_utf16_lossy(u.as_slice()), s);
1117
1118             assert_eq!(String::from_utf16(s_as_utf16.as_slice()).unwrap(), s);
1119             assert_eq!(u_as_string.utf16_units().collect::<Vec<u16>>(), u);
1120         }
1121     }
1122
1123     #[test]
1124     fn test_utf16_invalid() {
1125         // completely positive cases tested above.
1126         // lead + eof
1127         assert!(String::from_utf16(&[0xD800]).is_err());
1128         // lead + lead
1129         assert!(String::from_utf16(&[0xD800, 0xD800]).is_err());
1130
1131         // isolated trail
1132         assert!(String::from_utf16(&[0x0061, 0xDC00]).is_err());
1133
1134         // general
1135         assert!(String::from_utf16(&[0xD800, 0xd801, 0xdc8b, 0xD800]).is_err());
1136     }
1137
1138     #[test]
1139     fn test_from_utf16_lossy() {
1140         // completely positive cases tested above.
1141         // lead + eof
1142         assert_eq!(String::from_utf16_lossy(&[0xD800]), String::from_str("\u{FFFD}"));
1143         // lead + lead
1144         assert_eq!(String::from_utf16_lossy(&[0xD800, 0xD800]),
1145                    String::from_str("\u{FFFD}\u{FFFD}"));
1146
1147         // isolated trail
1148         assert_eq!(String::from_utf16_lossy(&[0x0061, 0xDC00]), String::from_str("a\u{FFFD}"));
1149
1150         // general
1151         assert_eq!(String::from_utf16_lossy(&[0xD800, 0xd801, 0xdc8b, 0xD800]),
1152                    String::from_str("\u{FFFD}𐒋\u{FFFD}"));
1153     }
1154
1155     #[test]
1156     fn test_push_bytes() {
1157         let mut s = String::from_str("ABC");
1158         unsafe {
1159             let mv = s.as_mut_vec();
1160             mv.push_all(&[b'D']);
1161         }
1162         assert_eq!(s, "ABCD");
1163     }
1164
1165     #[test]
1166     fn test_push_str() {
1167         let mut s = String::new();
1168         s.push_str("");
1169         assert_eq!(s.slice_from(0), "");
1170         s.push_str("abc");
1171         assert_eq!(s.slice_from(0), "abc");
1172         s.push_str("ประเทศไทย中华Việt Nam");
1173         assert_eq!(s.slice_from(0), "abcประเทศไทย中华Việt Nam");
1174     }
1175
1176     #[test]
1177     fn test_push() {
1178         let mut data = String::from_str("ประเทศไทย中");
1179         data.push('华');
1180         data.push('b'); // 1 byte
1181         data.push('¢'); // 2 byte
1182         data.push('€'); // 3 byte
1183         data.push('𤭢'); // 4 byte
1184         assert_eq!(data, "ประเทศไทย中华b¢€𤭢");
1185     }
1186
1187     #[test]
1188     fn test_pop() {
1189         let mut data = String::from_str("ประเทศไทย中华b¢€𤭢");
1190         assert_eq!(data.pop().unwrap(), '𤭢'); // 4 bytes
1191         assert_eq!(data.pop().unwrap(), '€'); // 3 bytes
1192         assert_eq!(data.pop().unwrap(), '¢'); // 2 bytes
1193         assert_eq!(data.pop().unwrap(), 'b'); // 1 bytes
1194         assert_eq!(data.pop().unwrap(), '华');
1195         assert_eq!(data, "ประเทศไทย中");
1196     }
1197
1198     #[test]
1199     fn test_str_truncate() {
1200         let mut s = String::from_str("12345");
1201         s.truncate(5);
1202         assert_eq!(s, "12345");
1203         s.truncate(3);
1204         assert_eq!(s, "123");
1205         s.truncate(0);
1206         assert_eq!(s, "");
1207
1208         let mut s = String::from_str("12345");
1209         let p = s.as_ptr();
1210         s.truncate(3);
1211         s.push_str("6");
1212         let p_ = s.as_ptr();
1213         assert_eq!(p_, p);
1214     }
1215
1216     #[test]
1217     #[should_fail]
1218     fn test_str_truncate_invalid_len() {
1219         let mut s = String::from_str("12345");
1220         s.truncate(6);
1221     }
1222
1223     #[test]
1224     #[should_fail]
1225     fn test_str_truncate_split_codepoint() {
1226         let mut s = String::from_str("\u{FC}"); // ü
1227         s.truncate(1);
1228     }
1229
1230     #[test]
1231     fn test_str_clear() {
1232         let mut s = String::from_str("12345");
1233         s.clear();
1234         assert_eq!(s.len(), 0);
1235         assert_eq!(s, "");
1236     }
1237
1238     #[test]
1239     fn test_str_add() {
1240         let a = String::from_str("12345");
1241         let b = a + "2";
1242         let b = b + "2";
1243         assert_eq!(b.len(), 7);
1244         assert_eq!(b, "1234522");
1245     }
1246
1247     #[test]
1248     fn remove() {
1249         let mut s = "ศไทย中华Việt Nam; foobar".to_string();;
1250         assert_eq!(s.remove(0), 'ศ');
1251         assert_eq!(s.len(), 33);
1252         assert_eq!(s, "ไทย中华Việt Nam; foobar");
1253         assert_eq!(s.remove(17), 'ệ');
1254         assert_eq!(s, "ไทย中华Vit Nam; foobar");
1255     }
1256
1257     #[test] #[should_fail]
1258     fn remove_bad() {
1259         "ศ".to_string().remove(1);
1260     }
1261
1262     #[test]
1263     fn insert() {
1264         let mut s = "foobar".to_string();
1265         s.insert(0, 'ệ');
1266         assert_eq!(s, "ệfoobar");
1267         s.insert(6, 'ย');
1268         assert_eq!(s, "ệfooยbar");
1269     }
1270
1271     #[test] #[should_fail] fn insert_bad1() { "".to_string().insert(1, 't'); }
1272     #[test] #[should_fail] fn insert_bad2() { "ệ".to_string().insert(1, 't'); }
1273
1274     #[test]
1275     fn test_slicing() {
1276         let s = "foobar".to_string();
1277         assert_eq!("foobar", &s[]);
1278         assert_eq!("foo", &s[..3]);
1279         assert_eq!("bar", &s[3..]);
1280         assert_eq!("oob", &s[1..4]);
1281     }
1282
1283     #[test]
1284     fn test_simple_types() {
1285         assert_eq!(1i.to_string(), "1");
1286         assert_eq!((-1i).to_string(), "-1");
1287         assert_eq!(200u.to_string(), "200");
1288         assert_eq!(2u8.to_string(), "2");
1289         assert_eq!(true.to_string(), "true");
1290         assert_eq!(false.to_string(), "false");
1291         assert_eq!(("hi".to_string()).to_string(), "hi");
1292     }
1293
1294     #[test]
1295     fn test_vectors() {
1296         let x: Vec<int> = vec![];
1297         assert_eq!(format!("{:?}", x), "[]");
1298         assert_eq!(format!("{:?}", vec![1i]), "[1i]");
1299         assert_eq!(format!("{:?}", vec![1i, 2, 3]), "[1i, 2i, 3i]");
1300         assert!(format!("{:?}", vec![vec![], vec![1i], vec![1i, 1]]) ==
1301                "[[], [1i], [1i, 1i]]");
1302     }
1303
1304     #[test]
1305     fn test_from_iterator() {
1306         let s = "ศไทย中华Việt Nam".to_string();
1307         let t = "ศไทย中华";
1308         let u = "Việt Nam";
1309
1310         let a: String = s.chars().collect();
1311         assert_eq!(s, a);
1312
1313         let mut b = t.to_string();
1314         b.extend(u.chars());
1315         assert_eq!(s, b);
1316
1317         let c: String = vec![t, u].into_iter().collect();
1318         assert_eq!(s, c);
1319
1320         let mut d = t.to_string();
1321         d.extend(vec![u].into_iter());
1322         assert_eq!(s, d);
1323     }
1324
1325     #[bench]
1326     fn bench_with_capacity(b: &mut Bencher) {
1327         b.iter(|| {
1328             String::with_capacity(100)
1329         });
1330     }
1331
1332     #[bench]
1333     fn bench_push_str(b: &mut Bencher) {
1334         let s = "ศไทย中华Việt Nam; Mary had a little lamb, Little lamb";
1335         b.iter(|| {
1336             let mut r = String::new();
1337             r.push_str(s);
1338         });
1339     }
1340
1341     const REPETITIONS: u64 = 10_000;
1342
1343     #[bench]
1344     fn bench_push_str_one_byte(b: &mut Bencher) {
1345         b.bytes = REPETITIONS;
1346         b.iter(|| {
1347             let mut r = String::new();
1348             for _ in range(0, REPETITIONS) {
1349                 r.push_str("a")
1350             }
1351         });
1352     }
1353
1354     #[bench]
1355     fn bench_push_char_one_byte(b: &mut Bencher) {
1356         b.bytes = REPETITIONS;
1357         b.iter(|| {
1358             let mut r = String::new();
1359             for _ in range(0, REPETITIONS) {
1360                 r.push('a')
1361             }
1362         });
1363     }
1364
1365     #[bench]
1366     fn bench_push_char_two_bytes(b: &mut Bencher) {
1367         b.bytes = REPETITIONS * 2;
1368         b.iter(|| {
1369             let mut r = String::new();
1370             for _ in range(0, REPETITIONS) {
1371                 r.push('â')
1372             }
1373         });
1374     }
1375
1376     #[bench]
1377     fn from_utf8_lossy_100_ascii(b: &mut Bencher) {
1378         let s = b"Hello there, the quick brown fox jumped over the lazy dog! \
1379                   Lorem ipsum dolor sit amet, consectetur. ";
1380
1381         assert_eq!(100, s.len());
1382         b.iter(|| {
1383             let _ = String::from_utf8_lossy(s);
1384         });
1385     }
1386
1387     #[bench]
1388     fn from_utf8_lossy_100_multibyte(b: &mut Bencher) {
1389         let s = "𐌀𐌖𐌋𐌄𐌑𐌉ปรدولة الكويتทศไทย中华𐍅𐌿𐌻𐍆𐌹𐌻𐌰".as_bytes();
1390         assert_eq!(100, s.len());
1391         b.iter(|| {
1392             let _ = String::from_utf8_lossy(s);
1393         });
1394     }
1395
1396     #[bench]
1397     fn from_utf8_lossy_invalid(b: &mut Bencher) {
1398         let s = b"Hello\xC0\x80 There\xE6\x83 Goodbye";
1399         b.iter(|| {
1400             let _ = String::from_utf8_lossy(s);
1401         });
1402     }
1403
1404     #[bench]
1405     fn from_utf8_lossy_100_invalid(b: &mut Bencher) {
1406         let s = repeat(0xf5u8).take(100).collect::<Vec<_>>();
1407         b.iter(|| {
1408             let _ = String::from_utf8_lossy(s.as_slice());
1409         });
1410     }
1411 }