]> git.proxmox.com Git - rustc.git/blob - src/libcollections/string.rs
Move away from hash to the same rust naming schema
[rustc.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 //! An owned, growable string that enforces that its contents are valid UTF-8.
12
13 #![stable(feature = "rust1", since = "1.0.0")]
14
15 use core::fmt;
16 use core::hash;
17 use core::iter::FromIterator;
18 use core::mem;
19 use core::ops::{self, Deref, Add, Index};
20 use core::ptr;
21 use core::slice;
22 use core::str::pattern::Pattern;
23 use rustc_unicode::char::{decode_utf16, REPLACEMENT_CHARACTER};
24 use rustc_unicode::str as unicode_str;
25
26 use borrow::{Cow, IntoCow};
27 use range::RangeArgument;
28 use str::{self, FromStr, Utf8Error, Chars};
29 use vec::Vec;
30 use boxed::Box;
31
32 /// A growable string stored as a UTF-8 encoded buffer.
33 #[derive(Clone, PartialOrd, Eq, Ord)]
34 #[stable(feature = "rust1", since = "1.0.0")]
35 pub struct String {
36 vec: Vec<u8>,
37 }
38
39 /// A possible error value from the `String::from_utf8` function.
40 #[stable(feature = "rust1", since = "1.0.0")]
41 #[derive(Debug)]
42 pub struct FromUtf8Error {
43 bytes: Vec<u8>,
44 error: Utf8Error,
45 }
46
47 /// A possible error value from the `String::from_utf16` function.
48 #[stable(feature = "rust1", since = "1.0.0")]
49 #[derive(Debug)]
50 pub struct FromUtf16Error(());
51
52 impl String {
53 /// Creates a new string buffer initialized with the empty string.
54 ///
55 /// # Examples
56 ///
57 /// ```
58 /// let mut s = String::new();
59 /// ```
60 #[inline]
61 #[stable(feature = "rust1", since = "1.0.0")]
62 pub fn new() -> String {
63 String {
64 vec: Vec::new(),
65 }
66 }
67
68 /// Creates a new string buffer with the given capacity.
69 /// The string will be able to hold exactly `capacity` bytes without
70 /// reallocating. If `capacity` is 0, the string will not allocate.
71 ///
72 /// # Examples
73 ///
74 /// ```
75 /// let mut s = String::with_capacity(10);
76 /// ```
77 #[inline]
78 #[stable(feature = "rust1", since = "1.0.0")]
79 pub fn with_capacity(capacity: usize) -> String {
80 String {
81 vec: Vec::with_capacity(capacity),
82 }
83 }
84
85 // HACK(japaric): with cfg(test) the inherent `[T]::to_vec` method, which is
86 // required for this method definition, is not available. Since we don't
87 // require this method for testing purposes, I'll just stub it
88 // NB see the slice::hack module in slice.rs for more information
89 #[inline]
90 #[cfg(test)]
91 pub fn from_str(_: &str) -> String {
92 panic!("not available with cfg(test)");
93 }
94
95 /// Returns the vector as a string buffer, if possible, taking care not to
96 /// copy it.
97 ///
98 /// # Failure
99 ///
100 /// If the given vector is not valid UTF-8, then the original vector and the
101 /// corresponding error is returned.
102 ///
103 /// # Examples
104 ///
105 /// ```
106 /// let hello_vec = vec![104, 101, 108, 108, 111];
107 /// let s = String::from_utf8(hello_vec).unwrap();
108 /// assert_eq!(s, "hello");
109 ///
110 /// let invalid_vec = vec![240, 144, 128];
111 /// let s = String::from_utf8(invalid_vec).err().unwrap();
112 /// let err = s.utf8_error();
113 /// assert_eq!(s.into_bytes(), [240, 144, 128]);
114 /// ```
115 #[inline]
116 #[stable(feature = "rust1", since = "1.0.0")]
117 pub fn from_utf8(vec: Vec<u8>) -> Result<String, FromUtf8Error> {
118 match str::from_utf8(&vec) {
119 Ok(..) => Ok(String { vec: vec }),
120 Err(e) => Err(FromUtf8Error { bytes: vec, error: e })
121 }
122 }
123
124 /// Converts a vector of bytes to a new UTF-8 string.
125 /// Any invalid UTF-8 sequences are replaced with U+FFFD REPLACEMENT CHARACTER.
126 ///
127 /// # Examples
128 ///
129 /// ```
130 /// let input = b"Hello \xF0\x90\x80World";
131 /// let output = String::from_utf8_lossy(input);
132 /// assert_eq!(output, "Hello \u{FFFD}World");
133 /// ```
134 #[stable(feature = "rust1", since = "1.0.0")]
135 pub fn from_utf8_lossy<'a>(v: &'a [u8]) -> Cow<'a, str> {
136 let mut i;
137 match str::from_utf8(v) {
138 Ok(s) => return Cow::Borrowed(s),
139 Err(e) => i = e.valid_up_to(),
140 }
141
142 const TAG_CONT_U8: u8 = 128;
143 const REPLACEMENT: &'static [u8] = b"\xEF\xBF\xBD"; // U+FFFD in UTF-8
144 let total = v.len();
145 fn unsafe_get(xs: &[u8], i: usize) -> u8 {
146 unsafe { *xs.get_unchecked(i) }
147 }
148 fn safe_get(xs: &[u8], i: usize, total: usize) -> u8 {
149 if i >= total {
150 0
151 } else {
152 unsafe_get(xs, i)
153 }
154 }
155
156 let mut res = String::with_capacity(total);
157
158 if i > 0 {
159 unsafe {
160 res.as_mut_vec().push_all(&v[..i])
161 };
162 }
163
164 // subseqidx is the index of the first byte of the subsequence we're
165 // looking at. It's used to copy a bunch of contiguous good codepoints
166 // at once instead of copying them one by one.
167 let mut subseqidx = i;
168
169 while i < total {
170 let i_ = i;
171 let byte = unsafe_get(v, i);
172 i += 1;
173
174 macro_rules! error { () => ({
175 unsafe {
176 if subseqidx != i_ {
177 res.as_mut_vec().push_all(&v[subseqidx..i_]);
178 }
179 subseqidx = i;
180 res.as_mut_vec().push_all(REPLACEMENT);
181 }
182 })}
183
184 if byte < 128 {
185 // subseqidx handles this
186 } else {
187 let w = unicode_str::utf8_char_width(byte);
188
189 match w {
190 2 => {
191 if safe_get(v, i, total) & 192 != TAG_CONT_U8 {
192 error!();
193 continue;
194 }
195 i += 1;
196 }
197 3 => {
198 match (byte, safe_get(v, i, total)) {
199 (0xE0 , 0xA0 ... 0xBF) => (),
200 (0xE1 ... 0xEC, 0x80 ... 0xBF) => (),
201 (0xED , 0x80 ... 0x9F) => (),
202 (0xEE ... 0xEF, 0x80 ... 0xBF) => (),
203 _ => {
204 error!();
205 continue;
206 }
207 }
208 i += 1;
209 if safe_get(v, i, total) & 192 != TAG_CONT_U8 {
210 error!();
211 continue;
212 }
213 i += 1;
214 }
215 4 => {
216 match (byte, safe_get(v, i, total)) {
217 (0xF0 , 0x90 ... 0xBF) => (),
218 (0xF1 ... 0xF3, 0x80 ... 0xBF) => (),
219 (0xF4 , 0x80 ... 0x8F) => (),
220 _ => {
221 error!();
222 continue;
223 }
224 }
225 i += 1;
226 if safe_get(v, i, total) & 192 != TAG_CONT_U8 {
227 error!();
228 continue;
229 }
230 i += 1;
231 if safe_get(v, i, total) & 192 != TAG_CONT_U8 {
232 error!();
233 continue;
234 }
235 i += 1;
236 }
237 _ => {
238 error!();
239 continue;
240 }
241 }
242 }
243 }
244 if subseqidx < total {
245 unsafe {
246 res.as_mut_vec().push_all(&v[subseqidx..total])
247 };
248 }
249 Cow::Owned(res)
250 }
251
252 /// Decode a UTF-16 encoded vector `v` into a `String`, returning `None`
253 /// if `v` contains any invalid data.
254 ///
255 /// # Examples
256 ///
257 /// ```
258 /// // 𝄞music
259 /// let mut v = &mut [0xD834, 0xDD1E, 0x006d, 0x0075,
260 /// 0x0073, 0x0069, 0x0063];
261 /// assert_eq!(String::from_utf16(v).unwrap(),
262 /// "𝄞music".to_string());
263 ///
264 /// // 𝄞mu<invalid>ic
265 /// v[4] = 0xD800;
266 /// assert!(String::from_utf16(v).is_err());
267 /// ```
268 #[stable(feature = "rust1", since = "1.0.0")]
269 pub fn from_utf16(v: &[u16]) -> Result<String, FromUtf16Error> {
270 decode_utf16(v.iter().cloned()).collect::<Result<_, _>>().map_err(|_| FromUtf16Error(()))
271 }
272
273 /// Decode a UTF-16 encoded vector `v` into a string, replacing
274 /// invalid data with the replacement character (U+FFFD).
275 ///
276 /// # Examples
277 ///
278 /// ```
279 /// // 𝄞mus<invalid>ic<invalid>
280 /// let v = &[0xD834, 0xDD1E, 0x006d, 0x0075,
281 /// 0x0073, 0xDD1E, 0x0069, 0x0063,
282 /// 0xD834];
283 ///
284 /// assert_eq!(String::from_utf16_lossy(v),
285 /// "𝄞mus\u{FFFD}ic\u{FFFD}".to_string());
286 /// ```
287 #[inline]
288 #[stable(feature = "rust1", since = "1.0.0")]
289 pub fn from_utf16_lossy(v: &[u16]) -> String {
290 decode_utf16(v.iter().cloned()).map(|r| r.unwrap_or(REPLACEMENT_CHARACTER)).collect()
291 }
292
293 /// Creates a new `String` from a length, capacity, and pointer.
294 ///
295 /// # Unsafety
296 ///
297 /// This is _very_ unsafe because:
298 ///
299 /// * We call `Vec::from_raw_parts` to get a `Vec<u8>`. Therefore, this
300 /// function inherits all of its unsafety, see [its
301 /// documentation](../vec/struct.Vec.html#method.from_raw_parts)
302 /// for the invariants it expects, they also apply to this function.
303 /// * We assume that the `Vec` contains valid UTF-8.
304 #[inline]
305 #[stable(feature = "rust1", since = "1.0.0")]
306 pub unsafe fn from_raw_parts(buf: *mut u8, length: usize, capacity: usize) -> String {
307 String {
308 vec: Vec::from_raw_parts(buf, length, capacity),
309 }
310 }
311
312 /// Converts a vector of bytes to a new `String` without checking if
313 /// it contains valid UTF-8. This is unsafe because it assumes that
314 /// the UTF-8-ness of the vector has already been validated.
315 #[inline]
316 #[stable(feature = "rust1", since = "1.0.0")]
317 pub unsafe fn from_utf8_unchecked(bytes: Vec<u8>) -> String {
318 String { vec: bytes }
319 }
320
321 /// Returns the underlying byte buffer, encoded as UTF-8.
322 ///
323 /// # Examples
324 ///
325 /// ```
326 /// let s = String::from("hello");
327 /// let bytes = s.into_bytes();
328 /// assert_eq!(bytes, [104, 101, 108, 108, 111]);
329 /// ```
330 #[inline]
331 #[stable(feature = "rust1", since = "1.0.0")]
332 pub fn into_bytes(self) -> Vec<u8> {
333 self.vec
334 }
335
336 /// Extracts a string slice containing the entire string.
337 #[inline]
338 #[unstable(feature = "convert",
339 reason = "waiting on RFC revision",
340 issue = "27729")]
341 pub fn as_str(&self) -> &str {
342 self
343 }
344
345 /// Pushes the given string onto this string buffer.
346 ///
347 /// # Examples
348 ///
349 /// ```
350 /// let mut s = String::from("foo");
351 /// s.push_str("bar");
352 /// assert_eq!(s, "foobar");
353 /// ```
354 #[inline]
355 #[stable(feature = "rust1", since = "1.0.0")]
356 pub fn push_str(&mut self, string: &str) {
357 self.vec.push_all(string.as_bytes())
358 }
359
360 /// Returns the number of bytes that this string buffer can hold without
361 /// reallocating.
362 ///
363 /// # Examples
364 ///
365 /// ```
366 /// let s = String::with_capacity(10);
367 /// assert!(s.capacity() >= 10);
368 /// ```
369 #[inline]
370 #[stable(feature = "rust1", since = "1.0.0")]
371 pub fn capacity(&self) -> usize {
372 self.vec.capacity()
373 }
374
375 /// Reserves capacity for at least `additional` more bytes to be inserted
376 /// in the given `String`. The collection may reserve more space to avoid
377 /// frequent reallocations.
378 ///
379 /// # Panics
380 ///
381 /// Panics if the new capacity overflows `usize`.
382 ///
383 /// # Examples
384 ///
385 /// ```
386 /// let mut s = String::new();
387 /// s.reserve(10);
388 /// assert!(s.capacity() >= 10);
389 /// ```
390 #[inline]
391 #[stable(feature = "rust1", since = "1.0.0")]
392 pub fn reserve(&mut self, additional: usize) {
393 self.vec.reserve(additional)
394 }
395
396 /// Reserves the minimum capacity for exactly `additional` more bytes to be
397 /// inserted in the given `String`. Does nothing if the capacity is already
398 /// sufficient.
399 ///
400 /// Note that the allocator may give the collection more space than it
401 /// requests. Therefore capacity can not be relied upon to be precisely
402 /// minimal. Prefer `reserve` if future insertions are expected.
403 ///
404 /// # Panics
405 ///
406 /// Panics if the new capacity overflows `usize`.
407 ///
408 /// # Examples
409 ///
410 /// ```
411 /// let mut s = String::new();
412 /// s.reserve_exact(10);
413 /// assert!(s.capacity() >= 10);
414 /// ```
415 #[inline]
416 #[stable(feature = "rust1", since = "1.0.0")]
417 pub fn reserve_exact(&mut self, additional: usize) {
418 self.vec.reserve_exact(additional)
419 }
420
421 /// Shrinks the capacity of this string buffer to match its length.
422 ///
423 /// # Examples
424 ///
425 /// ```
426 /// let mut s = String::from("foo");
427 /// s.reserve(100);
428 /// assert!(s.capacity() >= 100);
429 /// s.shrink_to_fit();
430 /// assert_eq!(s.capacity(), 3);
431 /// ```
432 #[inline]
433 #[stable(feature = "rust1", since = "1.0.0")]
434 pub fn shrink_to_fit(&mut self) {
435 self.vec.shrink_to_fit()
436 }
437
438 /// Adds the given character to the end of the string.
439 ///
440 /// # Examples
441 ///
442 /// ```
443 /// let mut s = String::from("abc");
444 /// s.push('1');
445 /// s.push('2');
446 /// s.push('3');
447 /// assert_eq!(s, "abc123");
448 /// ```
449 #[inline]
450 #[stable(feature = "rust1", since = "1.0.0")]
451 pub fn push(&mut self, ch: char) {
452 match ch.len_utf8() {
453 1 => self.vec.push(ch as u8),
454 ch_len => {
455 let cur_len = self.len();
456 // This may use up to 4 bytes.
457 self.vec.reserve(ch_len);
458
459 unsafe {
460 // Attempt to not use an intermediate buffer by just pushing bytes
461 // directly onto this string.
462 let slice = slice::from_raw_parts_mut (
463 self.vec.as_mut_ptr().offset(cur_len as isize),
464 ch_len
465 );
466 let used = ch.encode_utf8(slice).unwrap_or(0);
467 self.vec.set_len(cur_len + used);
468 }
469 }
470 }
471 }
472
473 /// Works with the underlying buffer as a byte slice.
474 ///
475 /// # Examples
476 ///
477 /// ```
478 /// let s = String::from("hello");
479 /// assert_eq!(s.as_bytes(), [104, 101, 108, 108, 111]);
480 /// ```
481 #[inline]
482 #[stable(feature = "rust1", since = "1.0.0")]
483 pub fn as_bytes(&self) -> &[u8] {
484 &self.vec
485 }
486
487 /// Shortens a string to the specified length.
488 ///
489 /// # Panics
490 ///
491 /// Panics if `new_len` > current length,
492 /// or if `new_len` is not a character boundary.
493 ///
494 /// # Examples
495 ///
496 /// ```
497 /// let mut s = String::from("hello");
498 /// s.truncate(2);
499 /// assert_eq!(s, "he");
500 /// ```
501 #[inline]
502 #[stable(feature = "rust1", since = "1.0.0")]
503 pub fn truncate(&mut self, new_len: usize) {
504 assert!(self.is_char_boundary(new_len));
505 self.vec.truncate(new_len)
506 }
507
508 /// Removes the last character from the string buffer and returns it.
509 /// Returns `None` if this string buffer is empty.
510 ///
511 /// # Examples
512 ///
513 /// ```
514 /// let mut s = String::from("foo");
515 /// assert_eq!(s.pop(), Some('o'));
516 /// assert_eq!(s.pop(), Some('o'));
517 /// assert_eq!(s.pop(), Some('f'));
518 /// assert_eq!(s.pop(), None);
519 /// ```
520 #[inline]
521 #[stable(feature = "rust1", since = "1.0.0")]
522 pub fn pop(&mut self) -> Option<char> {
523 let len = self.len();
524 if len == 0 {
525 return None
526 }
527
528 let ch = self.char_at_reverse(len);
529 unsafe {
530 self.vec.set_len(len - ch.len_utf8());
531 }
532 Some(ch)
533 }
534
535 /// Removes the character from the string buffer at byte position `idx` and
536 /// returns it.
537 ///
538 /// # Warning
539 ///
540 /// This is an O(n) operation as it requires copying every element in the
541 /// buffer.
542 ///
543 /// # Panics
544 ///
545 /// If `idx` does not lie on a character boundary, or if it is out of
546 /// bounds, then this function will panic.
547 ///
548 /// # Examples
549 ///
550 /// ```
551 /// let mut s = String::from("foo");
552 /// assert_eq!(s.remove(0), 'f');
553 /// assert_eq!(s.remove(1), 'o');
554 /// assert_eq!(s.remove(0), 'o');
555 /// ```
556 #[inline]
557 #[stable(feature = "rust1", since = "1.0.0")]
558 pub fn remove(&mut self, idx: usize) -> char {
559 let len = self.len();
560 assert!(idx <= len);
561
562 let ch = self.char_at(idx);
563 let next = idx + ch.len_utf8();
564 unsafe {
565 ptr::copy(self.vec.as_ptr().offset(next as isize),
566 self.vec.as_mut_ptr().offset(idx as isize),
567 len - next);
568 self.vec.set_len(len - (next - idx));
569 }
570 ch
571 }
572
573 /// Inserts a character into the string buffer at byte position `idx`.
574 ///
575 /// # Warning
576 ///
577 /// This is an O(n) operation as it requires copying every element in the
578 /// buffer.
579 ///
580 /// # Panics
581 ///
582 /// If `idx` does not lie on a character boundary or is out of bounds, then
583 /// this function will panic.
584 #[inline]
585 #[stable(feature = "rust1", since = "1.0.0")]
586 pub fn insert(&mut self, idx: usize, ch: char) {
587 let len = self.len();
588 assert!(idx <= len);
589 assert!(self.is_char_boundary(idx));
590 self.vec.reserve(4);
591 let mut bits = [0; 4];
592 let amt = ch.encode_utf8(&mut bits).unwrap();
593
594 unsafe {
595 ptr::copy(self.vec.as_ptr().offset(idx as isize),
596 self.vec.as_mut_ptr().offset((idx + amt) as isize),
597 len - idx);
598 ptr::copy(bits.as_ptr(),
599 self.vec.as_mut_ptr().offset(idx as isize),
600 amt);
601 self.vec.set_len(len + amt);
602 }
603 }
604
605 /// Views the string buffer as a mutable sequence of bytes.
606 ///
607 /// This is unsafe because it does not check
608 /// to ensure that the resulting string will be valid UTF-8.
609 ///
610 /// # Examples
611 ///
612 /// ```
613 /// let mut s = String::from("hello");
614 /// unsafe {
615 /// let vec = s.as_mut_vec();
616 /// assert!(vec == &[104, 101, 108, 108, 111]);
617 /// vec.reverse();
618 /// }
619 /// assert_eq!(s, "olleh");
620 /// ```
621 #[inline]
622 #[stable(feature = "rust1", since = "1.0.0")]
623 pub unsafe fn as_mut_vec(&mut self) -> &mut Vec<u8> {
624 &mut self.vec
625 }
626
627 /// Returns the number of bytes in this string.
628 ///
629 /// # Examples
630 ///
631 /// ```
632 /// let a = "foo".to_string();
633 /// assert_eq!(a.len(), 3);
634 /// ```
635 #[inline]
636 #[stable(feature = "rust1", since = "1.0.0")]
637 pub fn len(&self) -> usize { self.vec.len() }
638
639 /// Returns true if the string contains no bytes
640 ///
641 /// # Examples
642 ///
643 /// ```
644 /// let mut v = String::new();
645 /// assert!(v.is_empty());
646 /// v.push('a');
647 /// assert!(!v.is_empty());
648 /// ```
649 #[inline]
650 #[stable(feature = "rust1", since = "1.0.0")]
651 pub fn is_empty(&self) -> bool { self.len() == 0 }
652
653 /// Truncates the string, returning it to 0 length.
654 ///
655 /// # Examples
656 ///
657 /// ```
658 /// let mut s = "foo".to_string();
659 /// s.clear();
660 /// assert!(s.is_empty());
661 /// ```
662 #[inline]
663 #[stable(feature = "rust1", since = "1.0.0")]
664 pub fn clear(&mut self) {
665 self.vec.clear()
666 }
667
668 /// Create a draining iterator that removes the specified range in the string
669 /// and yields the removed chars from start to end. The element range is
670 /// removed even if the iterator is not consumed until the end.
671 ///
672 /// # Panics
673 ///
674 /// Panics if the starting point or end point are not on character boundaries,
675 /// or if they are out of bounds.
676 ///
677 /// # Examples
678 ///
679 /// ```
680 /// #![feature(drain)]
681 ///
682 /// let mut s = String::from("α is alpha, β is beta");
683 /// let beta_offset = s.find('β').unwrap_or(s.len());
684 ///
685 /// // Remove the range up until the β from the string
686 /// let t: String = s.drain(..beta_offset).collect();
687 /// assert_eq!(t, "α is alpha, ");
688 /// assert_eq!(s, "β is beta");
689 ///
690 /// // A full range clears the string
691 /// s.drain(..);
692 /// assert_eq!(s, "");
693 /// ```
694 #[unstable(feature = "drain",
695 reason = "recently added, matches RFC",
696 issue = "27711")]
697 pub fn drain<R>(&mut self, range: R) -> Drain where R: RangeArgument<usize> {
698 // Memory safety
699 //
700 // The String version of Drain does not have the memory safety issues
701 // of the vector version. The data is just plain bytes.
702 // Because the range removal happens in Drop, if the Drain iterator is leaked,
703 // the removal will not happen.
704 let len = self.len();
705 let start = *range.start().unwrap_or(&0);
706 let end = *range.end().unwrap_or(&len);
707
708 // Take out two simultaneous borrows. The &mut String won't be accessed
709 // until iteration is over, in Drop.
710 let self_ptr = self as *mut _;
711 // slicing does the appropriate bounds checks
712 let chars_iter = self[start..end].chars();
713
714 Drain {
715 start: start,
716 end: end,
717 iter: chars_iter,
718 string: self_ptr,
719 }
720 }
721
722 /// Converts the string into `Box<str>`.
723 ///
724 /// Note that this will drop any excess capacity.
725 #[stable(feature = "box_str", since = "1.4.0")]
726 pub fn into_boxed_str(self) -> Box<str> {
727 let slice = self.vec.into_boxed_slice();
728 unsafe { mem::transmute::<Box<[u8]>, Box<str>>(slice) }
729 }
730
731 /// Converts the string into `Box<str>`.
732 ///
733 /// Note that this will drop any excess capacity.
734 #[unstable(feature = "box_str2",
735 reason = "recently added, matches RFC",
736 issue = "27785")]
737 #[deprecated(since = "1.4.0", reason = "renamed to `into_boxed_str`")]
738 pub fn into_boxed_slice(self) -> Box<str> {
739 self.into_boxed_str()
740 }
741 }
742
743 impl FromUtf8Error {
744 /// Consumes this error, returning the bytes that were attempted to make a
745 /// `String` with.
746 #[stable(feature = "rust1", since = "1.0.0")]
747 pub fn into_bytes(self) -> Vec<u8> { self.bytes }
748
749 /// Access the underlying UTF8-error that was the cause of this error.
750 #[stable(feature = "rust1", since = "1.0.0")]
751 pub fn utf8_error(&self) -> Utf8Error { self.error }
752 }
753
754 #[stable(feature = "rust1", since = "1.0.0")]
755 impl fmt::Display for FromUtf8Error {
756 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
757 fmt::Display::fmt(&self.error, f)
758 }
759 }
760
761 #[stable(feature = "rust1", since = "1.0.0")]
762 impl fmt::Display for FromUtf16Error {
763 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
764 fmt::Display::fmt("invalid utf-16: lone surrogate found", f)
765 }
766 }
767
768 #[stable(feature = "rust1", since = "1.0.0")]
769 impl FromIterator<char> for String {
770 fn from_iter<I: IntoIterator<Item=char>>(iterable: I) -> String {
771 let mut buf = String::new();
772 buf.extend(iterable);
773 buf
774 }
775 }
776
777 #[stable(feature = "rust1", since = "1.0.0")]
778 impl<'a> FromIterator<&'a str> for String {
779 fn from_iter<I: IntoIterator<Item=&'a str>>(iterable: I) -> String {
780 let mut buf = String::new();
781 buf.extend(iterable);
782 buf
783 }
784 }
785
786 #[stable(feature = "extend_string", since = "1.4.0")]
787 impl FromIterator<String> for String {
788 fn from_iter<I: IntoIterator<Item=String>>(iterable: I) -> String {
789 let mut buf = String::new();
790 buf.extend(iterable);
791 buf
792 }
793 }
794
795 #[stable(feature = "rust1", since = "1.0.0")]
796 impl Extend<char> for String {
797 fn extend<I: IntoIterator<Item=char>>(&mut self, iterable: I) {
798 let iterator = iterable.into_iter();
799 let (lower_bound, _) = iterator.size_hint();
800 self.reserve(lower_bound);
801 for ch in iterator {
802 self.push(ch)
803 }
804 }
805 }
806
807 #[stable(feature = "extend_ref", since = "1.2.0")]
808 impl<'a> Extend<&'a char> for String {
809 fn extend<I: IntoIterator<Item=&'a char>>(&mut self, iterable: I) {
810 self.extend(iterable.into_iter().cloned());
811 }
812 }
813
814 #[stable(feature = "rust1", since = "1.0.0")]
815 impl<'a> Extend<&'a str> for String {
816 fn extend<I: IntoIterator<Item=&'a str>>(&mut self, iterable: I) {
817 for s in iterable {
818 self.push_str(s)
819 }
820 }
821 }
822
823 #[stable(feature = "extend_string", since = "1.4.0")]
824 impl Extend<String> for String {
825 fn extend<I: IntoIterator<Item=String>>(&mut self, iterable: I) {
826 for s in iterable {
827 self.push_str(&s)
828 }
829 }
830 }
831
832 /// A convenience impl that delegates to the impl for `&str`
833 impl<'a, 'b> Pattern<'a> for &'b String {
834 type Searcher = <&'b str as Pattern<'a>>::Searcher;
835
836 fn into_searcher(self, haystack: &'a str) -> <&'b str as Pattern<'a>>::Searcher {
837 self[..].into_searcher(haystack)
838 }
839
840 #[inline]
841 fn is_contained_in(self, haystack: &'a str) -> bool {
842 self[..].is_contained_in(haystack)
843 }
844
845 #[inline]
846 fn is_prefix_of(self, haystack: &'a str) -> bool {
847 self[..].is_prefix_of(haystack)
848 }
849 }
850
851 #[stable(feature = "rust1", since = "1.0.0")]
852 impl PartialEq for String {
853 #[inline]
854 fn eq(&self, other: &String) -> bool { PartialEq::eq(&self[..], &other[..]) }
855 #[inline]
856 fn ne(&self, other: &String) -> bool { PartialEq::ne(&self[..], &other[..]) }
857 }
858
859 macro_rules! impl_eq {
860 ($lhs:ty, $rhs: ty) => {
861 #[stable(feature = "rust1", since = "1.0.0")]
862 impl<'a> PartialEq<$rhs> for $lhs {
863 #[inline]
864 fn eq(&self, other: &$rhs) -> bool { PartialEq::eq(&self[..], &other[..]) }
865 #[inline]
866 fn ne(&self, other: &$rhs) -> bool { PartialEq::ne(&self[..], &other[..]) }
867 }
868
869 #[stable(feature = "rust1", since = "1.0.0")]
870 impl<'a> PartialEq<$lhs> for $rhs {
871 #[inline]
872 fn eq(&self, other: &$lhs) -> bool { PartialEq::eq(&self[..], &other[..]) }
873 #[inline]
874 fn ne(&self, other: &$lhs) -> bool { PartialEq::ne(&self[..], &other[..]) }
875 }
876
877 }
878 }
879
880 impl_eq! { String, str }
881 impl_eq! { String, &'a str }
882 impl_eq! { Cow<'a, str>, str }
883 impl_eq! { Cow<'a, str>, String }
884
885 #[stable(feature = "rust1", since = "1.0.0")]
886 impl<'a, 'b> PartialEq<&'b str> for Cow<'a, str> {
887 #[inline]
888 fn eq(&self, other: &&'b str) -> bool { PartialEq::eq(&self[..], &other[..]) }
889 #[inline]
890 fn ne(&self, other: &&'b str) -> bool { PartialEq::ne(&self[..], &other[..]) }
891 }
892
893 #[stable(feature = "rust1", since = "1.0.0")]
894 impl<'a, 'b> PartialEq<Cow<'a, str>> for &'b str {
895 #[inline]
896 fn eq(&self, other: &Cow<'a, str>) -> bool { PartialEq::eq(&self[..], &other[..]) }
897 #[inline]
898 fn ne(&self, other: &Cow<'a, str>) -> bool { PartialEq::ne(&self[..], &other[..]) }
899 }
900
901 #[stable(feature = "rust1", since = "1.0.0")]
902 impl Default for String {
903 #[inline]
904 #[stable(feature = "rust1", since = "1.0.0")]
905 fn default() -> String {
906 String::new()
907 }
908 }
909
910 #[stable(feature = "rust1", since = "1.0.0")]
911 impl fmt::Display for String {
912 #[inline]
913 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
914 fmt::Display::fmt(&**self, f)
915 }
916 }
917
918 #[stable(feature = "rust1", since = "1.0.0")]
919 impl fmt::Debug for String {
920 #[inline]
921 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
922 fmt::Debug::fmt(&**self, f)
923 }
924 }
925
926 #[stable(feature = "rust1", since = "1.0.0")]
927 impl hash::Hash for String {
928 #[inline]
929 fn hash<H: hash::Hasher>(&self, hasher: &mut H) {
930 (**self).hash(hasher)
931 }
932 }
933
934 #[stable(feature = "rust1", since = "1.0.0")]
935 impl<'a> Add<&'a str> for String {
936 type Output = String;
937
938 #[inline]
939 fn add(mut self, other: &str) -> String {
940 self.push_str(other);
941 self
942 }
943 }
944
945 #[stable(feature = "rust1", since = "1.0.0")]
946 impl ops::Index<ops::Range<usize>> for String {
947 type Output = str;
948
949 #[inline]
950 fn index(&self, index: ops::Range<usize>) -> &str {
951 &self[..][index]
952 }
953 }
954 #[stable(feature = "rust1", since = "1.0.0")]
955 impl ops::Index<ops::RangeTo<usize>> for String {
956 type Output = str;
957
958 #[inline]
959 fn index(&self, index: ops::RangeTo<usize>) -> &str {
960 &self[..][index]
961 }
962 }
963 #[stable(feature = "rust1", since = "1.0.0")]
964 impl ops::Index<ops::RangeFrom<usize>> for String {
965 type Output = str;
966
967 #[inline]
968 fn index(&self, index: ops::RangeFrom<usize>) -> &str {
969 &self[..][index]
970 }
971 }
972 #[stable(feature = "rust1", since = "1.0.0")]
973 impl ops::Index<ops::RangeFull> for String {
974 type Output = str;
975
976 #[inline]
977 fn index(&self, _index: ops::RangeFull) -> &str {
978 unsafe { str::from_utf8_unchecked(&self.vec) }
979 }
980 }
981
982 #[stable(feature = "derefmut_for_string", since = "1.2.0")]
983 impl ops::IndexMut<ops::Range<usize>> for String {
984 #[inline]
985 fn index_mut(&mut self, index: ops::Range<usize>) -> &mut str {
986 &mut self[..][index]
987 }
988 }
989 #[stable(feature = "derefmut_for_string", since = "1.2.0")]
990 impl ops::IndexMut<ops::RangeTo<usize>> for String {
991 #[inline]
992 fn index_mut(&mut self, index: ops::RangeTo<usize>) -> &mut str {
993 &mut self[..][index]
994 }
995 }
996 #[stable(feature = "derefmut_for_string", since = "1.2.0")]
997 impl ops::IndexMut<ops::RangeFrom<usize>> for String {
998 #[inline]
999 fn index_mut(&mut self, index: ops::RangeFrom<usize>) -> &mut str {
1000 &mut self[..][index]
1001 }
1002 }
1003 #[stable(feature = "derefmut_for_string", since = "1.2.0")]
1004 impl ops::IndexMut<ops::RangeFull> for String {
1005 #[inline]
1006 fn index_mut(&mut self, _index: ops::RangeFull) -> &mut str {
1007 unsafe { mem::transmute(&mut *self.vec) }
1008 }
1009 }
1010
1011 #[stable(feature = "rust1", since = "1.0.0")]
1012 impl ops::Deref for String {
1013 type Target = str;
1014
1015 #[inline]
1016 fn deref(&self) -> &str {
1017 unsafe { str::from_utf8_unchecked(&self.vec) }
1018 }
1019 }
1020
1021 #[stable(feature = "derefmut_for_string", since = "1.2.0")]
1022 impl ops::DerefMut for String {
1023 #[inline]
1024 fn deref_mut(&mut self) -> &mut str {
1025 unsafe { mem::transmute(&mut *self.vec) }
1026 }
1027 }
1028
1029 /// Error returned from `String::from`
1030 #[unstable(feature = "str_parse_error", reason = "may want to be replaced with \
1031 Void if it ever exists",
1032 issue = "27734")]
1033 #[derive(Copy, Clone, Debug, Eq, PartialEq)]
1034 pub struct ParseError(());
1035
1036 #[stable(feature = "rust1", since = "1.0.0")]
1037 impl FromStr for String {
1038 type Err = ParseError;
1039 #[inline]
1040 fn from_str(s: &str) -> Result<String, ParseError> {
1041 Ok(String::from(s))
1042 }
1043 }
1044
1045 /// A generic trait for converting a value to a string
1046 #[stable(feature = "rust1", since = "1.0.0")]
1047 pub trait ToString {
1048 /// Converts the value of `self` to an owned string
1049 #[stable(feature = "rust1", since = "1.0.0")]
1050 fn to_string(&self) -> String;
1051 }
1052
1053 #[stable(feature = "rust1", since = "1.0.0")]
1054 impl<T: fmt::Display + ?Sized> ToString for T {
1055 #[inline]
1056 fn to_string(&self) -> String {
1057 use core::fmt::Write;
1058 let mut buf = String::new();
1059 let _ = buf.write_fmt(format_args!("{}", self));
1060 buf.shrink_to_fit();
1061 buf
1062 }
1063 }
1064
1065 #[stable(feature = "rust1", since = "1.0.0")]
1066 impl AsRef<str> for String {
1067 #[inline]
1068 fn as_ref(&self) -> &str {
1069 self
1070 }
1071 }
1072
1073 #[stable(feature = "rust1", since = "1.0.0")]
1074 impl AsRef<[u8]> for String {
1075 #[inline]
1076 fn as_ref(&self) -> &[u8] {
1077 self.as_bytes()
1078 }
1079 }
1080
1081 #[stable(feature = "rust1", since = "1.0.0")]
1082 impl<'a> From<&'a str> for String {
1083 #[cfg(not(test))]
1084 #[inline]
1085 fn from(s: &'a str) -> String {
1086 String { vec: <[_]>::to_vec(s.as_bytes()) }
1087 }
1088
1089 // HACK(japaric): with cfg(test) the inherent `[T]::to_vec` method, which is
1090 // required for this method definition, is not available. Since we don't
1091 // require this method for testing purposes, I'll just stub it
1092 // NB see the slice::hack module in slice.rs for more information
1093 #[inline]
1094 #[cfg(test)]
1095 fn from(_: &str) -> String {
1096 panic!("not available with cfg(test)");
1097 }
1098 }
1099
1100 #[stable(feature = "rust1", since = "1.0.0")]
1101 impl<'a> From<&'a str> for Cow<'a, str> {
1102 #[inline]
1103 fn from(s: &'a str) -> Cow<'a, str> {
1104 Cow::Borrowed(s)
1105 }
1106 }
1107
1108 #[stable(feature = "rust1", since = "1.0.0")]
1109 impl<'a> From<String> for Cow<'a, str> {
1110 #[inline]
1111 fn from(s: String) -> Cow<'a, str> {
1112 Cow::Owned(s)
1113 }
1114 }
1115
1116 #[stable(feature = "rust1", since = "1.0.0")]
1117 impl Into<Vec<u8>> for String {
1118 fn into(self) -> Vec<u8> {
1119 self.into_bytes()
1120 }
1121 }
1122
1123 #[unstable(feature = "into_cow", reason = "may be replaced by `convert::Into`",
1124 issue= "27735")]
1125 impl IntoCow<'static, str> for String {
1126 #[inline]
1127 fn into_cow(self) -> Cow<'static, str> {
1128 Cow::Owned(self)
1129 }
1130 }
1131
1132 #[unstable(feature = "into_cow", reason = "may be replaced by `convert::Into`",
1133 issue = "27735")]
1134 impl<'a> IntoCow<'a, str> for &'a str {
1135 #[inline]
1136 fn into_cow(self) -> Cow<'a, str> {
1137 Cow::Borrowed(self)
1138 }
1139 }
1140
1141 #[stable(feature = "rust1", since = "1.0.0")]
1142 impl fmt::Write for String {
1143 #[inline]
1144 fn write_str(&mut self, s: &str) -> fmt::Result {
1145 self.push_str(s);
1146 Ok(())
1147 }
1148
1149 #[inline]
1150 fn write_char(&mut self, c: char) -> fmt::Result {
1151 self.push(c);
1152 Ok(())
1153 }
1154 }
1155
1156 /// A draining iterator for `String`.
1157 #[unstable(feature = "drain", reason = "recently added", issue = "27711")]
1158 pub struct Drain<'a> {
1159 /// Will be used as &'a mut String in the destructor
1160 string: *mut String,
1161 /// Start of part to remove
1162 start: usize,
1163 /// End of part to remove
1164 end: usize,
1165 /// Current remaining range to remove
1166 iter: Chars<'a>,
1167 }
1168
1169 unsafe impl<'a> Sync for Drain<'a> {}
1170 unsafe impl<'a> Send for Drain<'a> {}
1171
1172 #[unstable(feature = "drain", reason = "recently added", issue = "27711")]
1173 impl<'a> Drop for Drain<'a> {
1174 fn drop(&mut self) {
1175 unsafe {
1176 // Use Vec::drain. "Reaffirm" the bounds checks to avoid
1177 // panic code being inserted again.
1178 let self_vec = (*self.string).as_mut_vec();
1179 if self.start <= self.end && self.end <= self_vec.len() {
1180 self_vec.drain(self.start..self.end);
1181 }
1182 }
1183 }
1184 }
1185
1186 #[unstable(feature = "drain", reason = "recently added", issue = "27711")]
1187 impl<'a> Iterator for Drain<'a> {
1188 type Item = char;
1189
1190 #[inline]
1191 fn next(&mut self) -> Option<char> {
1192 self.iter.next()
1193 }
1194
1195 fn size_hint(&self) -> (usize, Option<usize>) {
1196 self.iter.size_hint()
1197 }
1198 }
1199
1200 #[unstable(feature = "drain", reason = "recently added", issue = "27711")]
1201 impl<'a> DoubleEndedIterator for Drain<'a> {
1202 #[inline]
1203 fn next_back(&mut self) -> Option<char> {
1204 self.iter.next_back()
1205 }
1206 }