]> git.proxmox.com Git - rustc.git/blob - src/libcollections/string.rs
New upstream version 1.12.0+dfsg1
[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 //! A UTF-8 encoded, growable string.
12 //!
13 //! This module contains the [`String`] type, a trait for converting
14 //! [`ToString`]s, and several error types that may result from working with
15 //! [`String`]s.
16 //!
17 //! [`String`]: struct.String.html
18 //! [`ToString`]: trait.ToString.html
19 //!
20 //! # Examples
21 //!
22 //! There are multiple ways to create a new `String` from a string literal:
23 //!
24 //! ```rust
25 //! let s = "Hello".to_string();
26 //!
27 //! let s = String::from("world");
28 //! let s: String = "also this".into();
29 //! ```
30 //!
31 //! You can create a new `String` from an existing one by concatenating with
32 //! `+`:
33 //!
34 //! ```rust
35 //! let s = "Hello".to_string();
36 //!
37 //! let message = s + " world!";
38 //! ```
39 //!
40 //! If you have a vector of valid UTF-8 bytes, you can make a `String` out of
41 //! it. You can do the reverse too.
42 //!
43 //! ```rust
44 //! let sparkle_heart = vec![240, 159, 146, 150];
45 //!
46 //! // We know these bytes are valid, so we'll use `unwrap()`.
47 //! let sparkle_heart = String::from_utf8(sparkle_heart).unwrap();
48 //!
49 //! assert_eq!("💖", sparkle_heart);
50 //!
51 //! let bytes = sparkle_heart.into_bytes();
52 //!
53 //! assert_eq!(bytes, [240, 159, 146, 150]);
54 //! ```
55
56 #![stable(feature = "rust1", since = "1.0.0")]
57
58 use core::fmt;
59 use core::hash;
60 use core::iter::FromIterator;
61 use core::mem;
62 use core::ops::{self, Add, AddAssign, Index, IndexMut};
63 use core::ptr;
64 use core::str::pattern::Pattern;
65 use rustc_unicode::char::{decode_utf16, REPLACEMENT_CHARACTER};
66 use rustc_unicode::str as unicode_str;
67
68 use borrow::{Cow, ToOwned};
69 use range::RangeArgument;
70 use str::{self, FromStr, Utf8Error, Chars};
71 use vec::Vec;
72 use boxed::Box;
73
74 /// A UTF-8 encoded, growable string.
75 ///
76 /// The `String` type is the most common string type that has ownership over the
77 /// contents of the string. It has a close relationship with its borrowed
78 /// counterpart, the primitive [`str`].
79 ///
80 /// [`str`]: ../../std/primitive.str.html
81 ///
82 /// # Examples
83 ///
84 /// You can create a `String` from a literal string with `String::from`:
85 ///
86 /// ```
87 /// let hello = String::from("Hello, world!");
88 /// ```
89 ///
90 /// You can append a [`char`] to a `String` with the [`push()`] method, and
91 /// append a [`&str`] with the [`push_str()`] method:
92 ///
93 /// ```
94 /// let mut hello = String::from("Hello, ");
95 ///
96 /// hello.push('w');
97 /// hello.push_str("orld!");
98 /// ```
99 ///
100 /// [`char`]: ../../std/primitive.char.html
101 /// [`push()`]: #method.push
102 /// [`push_str()`]: #method.push_str
103 ///
104 /// If you have a vector of UTF-8 bytes, you can create a `String` from it with
105 /// the [`from_utf8()`] method:
106 ///
107 /// ```
108 /// // some bytes, in a vector
109 /// let sparkle_heart = vec![240, 159, 146, 150];
110 ///
111 /// // We know these bytes are valid, so we'll use `unwrap()`.
112 /// let sparkle_heart = String::from_utf8(sparkle_heart).unwrap();
113 ///
114 /// assert_eq!("💖", sparkle_heart);
115 /// ```
116 ///
117 /// [`from_utf8()`]: #method.from_utf8
118 ///
119 /// # UTF-8
120 ///
121 /// `String`s are always valid UTF-8. This has a few implications, the first of
122 /// which is that if you need a non-UTF-8 string, consider [`OsString`]. It is
123 /// similar, but without the UTF-8 constraint. The second implication is that
124 /// you cannot index into a `String`:
125 ///
126 /// ```ignore
127 /// let s = "hello";
128 ///
129 /// println!("The first letter of s is {}", s[0]); // ERROR!!!
130 /// ```
131 ///
132 /// [`OsString`]: ../../std/ffi/struct.OsString.html
133 ///
134 /// Indexing is intended to be a constant-time operation, but UTF-8 encoding
135 /// does not allow us to do this. Furtheremore, it's not clear what sort of
136 /// thing the index should return: a byte, a codepoint, or a grapheme cluster.
137 /// The [`as_bytes()`] and [`chars()`] methods return iterators over the first
138 /// two, respectively.
139 ///
140 /// [`as_bytes()`]: #method.as_bytes
141 /// [`chars()`]: #method.chars
142 ///
143 /// # Deref
144 ///
145 /// `String`s implement [`Deref`]`<Target=str>`, and so inherit all of [`str`]'s
146 /// methods. In addition, this means that you can pass a `String` to any
147 /// function which takes a [`&str`] by using an ampersand (`&`):
148 ///
149 /// ```
150 /// fn takes_str(s: &str) { }
151 ///
152 /// let s = String::from("Hello");
153 ///
154 /// takes_str(&s);
155 /// ```
156 ///
157 /// [`&str`]: ../../std/primitive.str.html
158 /// [`Deref`]: ../../std/ops/trait.Deref.html
159 ///
160 /// This will create a [`&str`] from the `String` and pass it in. This
161 /// conversion is very inexpensive, and so generally, functions will accept
162 /// [`&str`]s as arguments unless they need a `String` for some specific reason.
163 ///
164 ///
165 /// # Representation
166 ///
167 /// A `String` is made up of three components: a pointer to some bytes, a
168 /// length, and a capacity. The pointer points to an internal buffer `String`
169 /// uses to store its data. The length is the number of bytes currently stored
170 /// in the buffer, and the capacity is the size of the buffer in bytes. As such,
171 /// the length will always be less than or equal to the capacity.
172 ///
173 /// This buffer is always stored on the heap.
174 ///
175 /// You can look at these with the [`as_ptr()`], [`len()`], and [`capacity()`]
176 /// methods:
177 ///
178 /// ```
179 /// use std::mem;
180 ///
181 /// let story = String::from("Once upon a time...");
182 ///
183 /// let ptr = story.as_ptr();
184 /// let len = story.len();
185 /// let capacity = story.capacity();
186 ///
187 /// // story has nineteen bytes
188 /// assert_eq!(19, len);
189 ///
190 /// // Now that we have our parts, we throw the story away.
191 /// mem::forget(story);
192 ///
193 /// // We can re-build a String out of ptr, len, and capacity. This is all
194 /// // unsafe because we are responsible for making sure the components are
195 /// // valid:
196 /// let s = unsafe { String::from_raw_parts(ptr as *mut _, len, capacity) } ;
197 ///
198 /// assert_eq!(String::from("Once upon a time..."), s);
199 /// ```
200 ///
201 /// [`as_ptr()`]: #method.as_ptr
202 /// [`len()`]: #method.len
203 /// [`capacity()`]: #method.capacity
204 ///
205 /// If a `String` has enough capacity, adding elements to it will not
206 /// re-allocate. For example, consider this program:
207 ///
208 /// ```
209 /// let mut s = String::new();
210 ///
211 /// println!("{}", s.capacity());
212 ///
213 /// for _ in 0..5 {
214 /// s.push_str("hello");
215 /// println!("{}", s.capacity());
216 /// }
217 /// ```
218 ///
219 /// This will output the following:
220 ///
221 /// ```text
222 /// 0
223 /// 5
224 /// 10
225 /// 20
226 /// 20
227 /// 40
228 /// ```
229 ///
230 /// At first, we have no memory allocated at all, but as we append to the
231 /// string, it increases its capacity appropriately. If we instead use the
232 /// [`with_capacity()`] method to allocate the correct capacity initially:
233 ///
234 /// ```
235 /// let mut s = String::with_capacity(25);
236 ///
237 /// println!("{}", s.capacity());
238 ///
239 /// for _ in 0..5 {
240 /// s.push_str("hello");
241 /// println!("{}", s.capacity());
242 /// }
243 /// ```
244 ///
245 /// [`with_capacity()`]: #method.with_capacity
246 ///
247 /// We end up with a different output:
248 ///
249 /// ```text
250 /// 25
251 /// 25
252 /// 25
253 /// 25
254 /// 25
255 /// 25
256 /// ```
257 ///
258 /// Here, there's no need to allocate more memory inside the loop.
259 #[derive(PartialOrd, Eq, Ord)]
260 #[stable(feature = "rust1", since = "1.0.0")]
261 pub struct String {
262 vec: Vec<u8>,
263 }
264
265 /// A possible error value when converting a `String` from a UTF-8 byte vector.
266 ///
267 /// This type is the error type for the [`from_utf8()`] method on [`String`]. It
268 /// is designed in such a way to carefully avoid reallocations: the
269 /// [`into_bytes()`] method will give back the byte vector that was used in the
270 /// conversion attempt.
271 ///
272 /// [`from_utf8()`]: struct.String.html#method.from_utf8
273 /// [`String`]: struct.String.html
274 /// [`into_bytes()`]: struct.FromUtf8Error.html#method.into_bytes
275 ///
276 /// The [`Utf8Error`] type provided by [`std::str`] represents an error that may
277 /// occur when converting a slice of [`u8`]s to a [`&str`]. In this sense, it's
278 /// an analogue to `FromUtf8Error`, and you can get one from a `FromUtf8Error`
279 /// through the [`utf8_error()`] method.
280 ///
281 /// [`Utf8Error`]: ../../std/str/struct.Utf8Error.html
282 /// [`std::str`]: ../../std/str/index.html
283 /// [`u8`]: ../../std/primitive.u8.html
284 /// [`&str`]: ../../std/primitive.str.html
285 /// [`utf8_error()`]: #method.utf8_error
286 ///
287 /// # Examples
288 ///
289 /// Basic usage:
290 ///
291 /// ```
292 /// // some invalid bytes, in a vector
293 /// let bytes = vec![0, 159];
294 ///
295 /// let value = String::from_utf8(bytes);
296 ///
297 /// assert!(value.is_err());
298 /// assert_eq!(vec![0, 159], value.unwrap_err().into_bytes());
299 /// ```
300 #[stable(feature = "rust1", since = "1.0.0")]
301 #[derive(Debug)]
302 pub struct FromUtf8Error {
303 bytes: Vec<u8>,
304 error: Utf8Error,
305 }
306
307 /// A possible error value when converting a `String` from a UTF-16 byte slice.
308 ///
309 /// This type is the error type for the [`from_utf16()`] method on [`String`].
310 ///
311 /// [`from_utf16()`]: struct.String.html#method.from_utf16
312 /// [`String`]: struct.String.html
313 ///
314 /// # Examples
315 ///
316 /// Basic usage:
317 ///
318 /// ```
319 /// // 𝄞mu<invalid>ic
320 /// let v = &[0xD834, 0xDD1E, 0x006d, 0x0075,
321 /// 0xD800, 0x0069, 0x0063];
322 ///
323 /// assert!(String::from_utf16(v).is_err());
324 /// ```
325 #[stable(feature = "rust1", since = "1.0.0")]
326 #[derive(Debug)]
327 pub struct FromUtf16Error(());
328
329 impl String {
330 /// Creates a new empty `String`.
331 ///
332 /// Given that the `String` is empty, this will not allocate any initial
333 /// buffer. While that means that this initial operation is very
334 /// inexpensive, but may cause excessive allocation later, when you add
335 /// data. If you have an idea of how much data the `String` will hold,
336 /// consider the [`with_capacity()`] method to prevent excessive
337 /// re-allocation.
338 ///
339 /// [`with_capacity()`]: #method.with_capacity
340 ///
341 /// # Examples
342 ///
343 /// Basic usage:
344 ///
345 /// ```
346 /// let s = String::new();
347 /// ```
348 #[inline]
349 #[stable(feature = "rust1", since = "1.0.0")]
350 pub fn new() -> String {
351 String { vec: Vec::new() }
352 }
353
354 /// Creates a new empty `String` with a particular capacity.
355 ///
356 /// `String`s have an internal buffer to hold their data. The capacity is
357 /// the length of that buffer, and can be queried with the [`capacity()`]
358 /// method. This method creates an empty `String`, but one with an initial
359 /// buffer that can hold `capacity` bytes. This is useful when you may be
360 /// appending a bunch of data to the `String`, reducing the number of
361 /// reallocations it needs to do.
362 ///
363 /// [`capacity()`]: #method.capacity
364 ///
365 /// If the given capacity is `0`, no allocation will occur, and this method
366 /// is identical to the [`new()`] method.
367 ///
368 /// [`new()`]: #method.new
369 ///
370 /// # Examples
371 ///
372 /// Basic usage:
373 ///
374 /// ```
375 /// let mut s = String::with_capacity(10);
376 ///
377 /// // The String contains no chars, even though it has capacity for more
378 /// assert_eq!(s.len(), 0);
379 ///
380 /// // These are all done without reallocating...
381 /// let cap = s.capacity();
382 /// for i in 0..10 {
383 /// s.push('a');
384 /// }
385 ///
386 /// assert_eq!(s.capacity(), cap);
387 ///
388 /// // ...but this may make the vector reallocate
389 /// s.push('a');
390 /// ```
391 #[inline]
392 #[stable(feature = "rust1", since = "1.0.0")]
393 pub fn with_capacity(capacity: usize) -> String {
394 String { vec: Vec::with_capacity(capacity) }
395 }
396
397 // HACK(japaric): with cfg(test) the inherent `[T]::to_vec` method, which is
398 // required for this method definition, is not available. Since we don't
399 // require this method for testing purposes, I'll just stub it
400 // NB see the slice::hack module in slice.rs for more information
401 #[inline]
402 #[cfg(test)]
403 pub fn from_str(_: &str) -> String {
404 panic!("not available with cfg(test)");
405 }
406
407 /// Converts a vector of bytes to a `String`.
408 ///
409 /// A string slice ([`&str`]) is made of bytes ([`u8`]), and a vector of bytes
410 /// ([`Vec<u8>`]) is made of bytes, so this function converts between the
411 /// two. Not all byte slices are valid `String`s, however: `String`
412 /// requires that it is valid UTF-8. `from_utf8()` checks to ensure that
413 /// the bytes are valid UTF-8, and then does the conversion.
414 ///
415 /// [`&str`]: ../../std/primitive.str.html
416 /// [`u8`]: ../../std/primitive.u8.html
417 /// [`Vec<u8>`]: ../../std/vec/struct.Vec.html
418 ///
419 /// If you are sure that the byte slice is valid UTF-8, and you don't want
420 /// to incur the overhead of the validity check, there is an unsafe version
421 /// of this function, [`from_utf8_unchecked()`], which has the same behavior
422 /// but skips the check.
423 ///
424 /// [`from_utf8_unchecked()`]: struct.String.html#method.from_utf8_unchecked
425 ///
426 /// This method will take care to not copy the vector, for efficiency's
427 /// sake.
428 ///
429 /// If you need a `&str` instead of a `String`, consider
430 /// [`str::from_utf8()`].
431 ///
432 /// [`str::from_utf8()`]: ../../std/str/fn.from_utf8.html
433 ///
434 /// # Errors
435 ///
436 /// Returns `Err` if the slice is not UTF-8 with a description as to why the
437 /// provided bytes are not UTF-8. The vector you moved in is also included.
438 ///
439 /// # Examples
440 ///
441 /// Basic usage:
442 ///
443 /// ```
444 /// // some bytes, in a vector
445 /// let sparkle_heart = vec![240, 159, 146, 150];
446 ///
447 /// // We know these bytes are valid, so we'll use `unwrap()`.
448 /// let sparkle_heart = String::from_utf8(sparkle_heart).unwrap();
449 ///
450 /// assert_eq!("💖", sparkle_heart);
451 /// ```
452 ///
453 /// Incorrect bytes:
454 ///
455 /// ```
456 /// // some invalid bytes, in a vector
457 /// let sparkle_heart = vec![0, 159, 146, 150];
458 ///
459 /// assert!(String::from_utf8(sparkle_heart).is_err());
460 /// ```
461 ///
462 /// See the docs for [`FromUtf8Error`] for more details on what you can do
463 /// with this error.
464 ///
465 /// [`FromUtf8Error`]: struct.FromUtf8Error.html
466 #[inline]
467 #[stable(feature = "rust1", since = "1.0.0")]
468 pub fn from_utf8(vec: Vec<u8>) -> Result<String, FromUtf8Error> {
469 match str::from_utf8(&vec) {
470 Ok(..) => Ok(String { vec: vec }),
471 Err(e) => {
472 Err(FromUtf8Error {
473 bytes: vec,
474 error: e,
475 })
476 }
477 }
478 }
479
480 /// Converts a slice of bytes to a string, including invalid characters.
481 ///
482 /// Strings are made of bytes ([`u8`]), and a slice of bytes
483 /// ([`&[u8]`][byteslice]) is made of bytes, so this function converts
484 /// between the two. Not all byte slices are valid strings, however: strings
485 /// are required to be valid UTF-8. During this conversion,
486 /// `from_utf8_lossy()` will replace any invalid UTF-8 sequences with
487 /// `U+FFFD REPLACEMENT CHARACTER`, which looks like this: �
488 ///
489 /// [`u8`]: ../../std/primitive.u8.html
490 /// [byteslice]: ../../std/primitive.slice.html
491 ///
492 /// If you are sure that the byte slice is valid UTF-8, and you don't want
493 /// to incur the overhead of the conversion, there is an unsafe version
494 /// of this function, [`from_utf8_unchecked()`], which has the same behavior
495 /// but skips the checks.
496 ///
497 /// [`from_utf8_unchecked()`]: struct.String.html#method.from_utf8_unchecked
498 ///
499 /// This function returns a [`Cow<'a, str>`]. If our byte slice is invalid
500 /// UTF-8, then we need to insert the replacement characters, which will
501 /// change the size of the string, and hence, require a `String`. But if
502 /// it's already valid UTF-8, we don't need a new allocation. This return
503 /// type allows us to handle both cases.
504 ///
505 /// [`Cow<'a, str>`]: ../../std/borrow/enum.Cow.html
506 ///
507 /// # Examples
508 ///
509 /// Basic usage:
510 ///
511 /// ```
512 /// // some bytes, in a vector
513 /// let sparkle_heart = vec![240, 159, 146, 150];
514 ///
515 /// let sparkle_heart = String::from_utf8_lossy(&sparkle_heart);
516 ///
517 /// assert_eq!("💖", sparkle_heart);
518 /// ```
519 ///
520 /// Incorrect bytes:
521 ///
522 /// ```
523 /// // some invalid bytes
524 /// let input = b"Hello \xF0\x90\x80World";
525 /// let output = String::from_utf8_lossy(input);
526 ///
527 /// assert_eq!("Hello �World", output);
528 /// ```
529 #[stable(feature = "rust1", since = "1.0.0")]
530 pub fn from_utf8_lossy<'a>(v: &'a [u8]) -> Cow<'a, str> {
531 let mut i;
532 match str::from_utf8(v) {
533 Ok(s) => return Cow::Borrowed(s),
534 Err(e) => i = e.valid_up_to(),
535 }
536
537 const TAG_CONT_U8: u8 = 128;
538 const REPLACEMENT: &'static [u8] = b"\xEF\xBF\xBD"; // U+FFFD in UTF-8
539 let total = v.len();
540 fn unsafe_get(xs: &[u8], i: usize) -> u8 {
541 unsafe { *xs.get_unchecked(i) }
542 }
543 fn safe_get(xs: &[u8], i: usize, total: usize) -> u8 {
544 if i >= total {
545 0
546 } else {
547 unsafe_get(xs, i)
548 }
549 }
550
551 let mut res = String::with_capacity(total);
552
553 if i > 0 {
554 unsafe { res.as_mut_vec().extend_from_slice(&v[..i]) };
555 }
556
557 // subseqidx is the index of the first byte of the subsequence we're
558 // looking at. It's used to copy a bunch of contiguous good codepoints
559 // at once instead of copying them one by one.
560 let mut subseqidx = i;
561
562 while i < total {
563 let i_ = i;
564 let byte = unsafe_get(v, i);
565 i += 1;
566
567 macro_rules! error { () => ({
568 unsafe {
569 if subseqidx != i_ {
570 res.as_mut_vec().extend_from_slice(&v[subseqidx..i_]);
571 }
572 subseqidx = i;
573 res.as_mut_vec().extend_from_slice(REPLACEMENT);
574 }
575 })}
576
577 if byte < 128 {
578 // subseqidx handles this
579 } else {
580 let w = unicode_str::utf8_char_width(byte);
581
582 match w {
583 2 => {
584 if safe_get(v, i, total) & 192 != TAG_CONT_U8 {
585 error!();
586 continue;
587 }
588 i += 1;
589 }
590 3 => {
591 match (byte, safe_get(v, i, total)) {
592 (0xE0, 0xA0...0xBF) => (),
593 (0xE1...0xEC, 0x80...0xBF) => (),
594 (0xED, 0x80...0x9F) => (),
595 (0xEE...0xEF, 0x80...0xBF) => (),
596 _ => {
597 error!();
598 continue;
599 }
600 }
601 i += 1;
602 if safe_get(v, i, total) & 192 != TAG_CONT_U8 {
603 error!();
604 continue;
605 }
606 i += 1;
607 }
608 4 => {
609 match (byte, safe_get(v, i, total)) {
610 (0xF0, 0x90...0xBF) => (),
611 (0xF1...0xF3, 0x80...0xBF) => (),
612 (0xF4, 0x80...0x8F) => (),
613 _ => {
614 error!();
615 continue;
616 }
617 }
618 i += 1;
619 if safe_get(v, i, total) & 192 != TAG_CONT_U8 {
620 error!();
621 continue;
622 }
623 i += 1;
624 if safe_get(v, i, total) & 192 != TAG_CONT_U8 {
625 error!();
626 continue;
627 }
628 i += 1;
629 }
630 _ => {
631 error!();
632 continue;
633 }
634 }
635 }
636 }
637 if subseqidx < total {
638 unsafe { res.as_mut_vec().extend_from_slice(&v[subseqidx..total]) };
639 }
640 Cow::Owned(res)
641 }
642
643 /// Decode a UTF-16 encoded vector `v` into a `String`, returning `Err`
644 /// if `v` contains any invalid data.
645 ///
646 /// # Examples
647 ///
648 /// Basic usage:
649 ///
650 /// ```
651 /// // 𝄞music
652 /// let v = &[0xD834, 0xDD1E, 0x006d, 0x0075,
653 /// 0x0073, 0x0069, 0x0063];
654 /// assert_eq!(String::from("𝄞music"),
655 /// String::from_utf16(v).unwrap());
656 ///
657 /// // 𝄞mu<invalid>ic
658 /// let v = &[0xD834, 0xDD1E, 0x006d, 0x0075,
659 /// 0xD800, 0x0069, 0x0063];
660 /// assert!(String::from_utf16(v).is_err());
661 /// ```
662 #[stable(feature = "rust1", since = "1.0.0")]
663 pub fn from_utf16(v: &[u16]) -> Result<String, FromUtf16Error> {
664 decode_utf16(v.iter().cloned()).collect::<Result<_, _>>().map_err(|_| FromUtf16Error(()))
665 }
666
667 /// Decode a UTF-16 encoded vector `v` into a string, replacing
668 /// invalid data with the replacement character (U+FFFD).
669 ///
670 /// # Examples
671 ///
672 /// Basic usage:
673 ///
674 /// ```
675 /// // 𝄞mus<invalid>ic<invalid>
676 /// let v = &[0xD834, 0xDD1E, 0x006d, 0x0075,
677 /// 0x0073, 0xDD1E, 0x0069, 0x0063,
678 /// 0xD834];
679 ///
680 /// assert_eq!(String::from("𝄞mus\u{FFFD}ic\u{FFFD}"),
681 /// String::from_utf16_lossy(v));
682 /// ```
683 #[inline]
684 #[stable(feature = "rust1", since = "1.0.0")]
685 pub fn from_utf16_lossy(v: &[u16]) -> String {
686 decode_utf16(v.iter().cloned()).map(|r| r.unwrap_or(REPLACEMENT_CHARACTER)).collect()
687 }
688
689 /// Creates a new `String` from a length, capacity, and pointer.
690 ///
691 /// # Safety
692 ///
693 /// This is highly unsafe, due to the number of invariants that aren't
694 /// checked:
695 ///
696 /// * The memory at `ptr` needs to have been previously allocated by the
697 /// same allocator the standard library uses.
698 /// * `length` needs to be less than or equal to `capacity`.
699 /// * `capacity` needs to be the correct value.
700 ///
701 /// Violating these may cause problems like corrupting the allocator's
702 /// internal datastructures.
703 ///
704 /// The ownership of `ptr` is effectively transferred to the
705 /// `String` which may then deallocate, reallocate or change the
706 /// contents of memory pointed to by the pointer at will. Ensure
707 /// that nothing else uses the pointer after calling this
708 /// function.
709 ///
710 /// # Examples
711 ///
712 /// Basic usage:
713 ///
714 /// ```
715 /// use std::mem;
716 ///
717 /// unsafe {
718 /// let s = String::from("hello");
719 /// let ptr = s.as_ptr();
720 /// let len = s.len();
721 /// let capacity = s.capacity();
722 ///
723 /// mem::forget(s);
724 ///
725 /// let s = String::from_raw_parts(ptr as *mut _, len, capacity);
726 ///
727 /// assert_eq!(String::from("hello"), s);
728 /// }
729 /// ```
730 #[inline]
731 #[stable(feature = "rust1", since = "1.0.0")]
732 pub unsafe fn from_raw_parts(buf: *mut u8, length: usize, capacity: usize) -> String {
733 String { vec: Vec::from_raw_parts(buf, length, capacity) }
734 }
735
736 /// Converts a vector of bytes to a `String` without checking that the
737 /// string contains valid UTF-8.
738 ///
739 /// See the safe version, [`from_utf8()`], for more details.
740 ///
741 /// [`from_utf8()`]: struct.String.html#method.from_utf8
742 ///
743 /// # Safety
744 ///
745 /// This function is unsafe because it does not check that the bytes passed
746 /// to it are valid UTF-8. If this constraint is violated, it may cause
747 /// memory unsafety issues with future users of the `String`, as the rest of
748 /// the standard library assumes that `String`s are valid UTF-8.
749 ///
750 /// # Examples
751 ///
752 /// Basic usage:
753 ///
754 /// ```
755 /// // some bytes, in a vector
756 /// let sparkle_heart = vec![240, 159, 146, 150];
757 ///
758 /// let sparkle_heart = unsafe {
759 /// String::from_utf8_unchecked(sparkle_heart)
760 /// };
761 ///
762 /// assert_eq!("💖", sparkle_heart);
763 /// ```
764 #[inline]
765 #[stable(feature = "rust1", since = "1.0.0")]
766 pub unsafe fn from_utf8_unchecked(bytes: Vec<u8>) -> String {
767 String { vec: bytes }
768 }
769
770 /// Converts a `String` into a byte vector.
771 ///
772 /// This consumes the `String`, so we do not need to copy its contents.
773 ///
774 /// # Examples
775 ///
776 /// Basic usage:
777 ///
778 /// ```
779 /// let s = String::from("hello");
780 /// let bytes = s.into_bytes();
781 ///
782 /// assert_eq!(&[104, 101, 108, 108, 111][..], &bytes[..]);
783 /// ```
784 #[inline]
785 #[stable(feature = "rust1", since = "1.0.0")]
786 pub fn into_bytes(self) -> Vec<u8> {
787 self.vec
788 }
789
790 /// Extracts a string slice containing the entire string.
791 #[inline]
792 #[stable(feature = "string_as_str", since = "1.7.0")]
793 pub fn as_str(&self) -> &str {
794 self
795 }
796
797 /// Extracts a string slice containing the entire string.
798 #[inline]
799 #[stable(feature = "string_as_str", since = "1.7.0")]
800 pub fn as_mut_str(&mut self) -> &mut str {
801 self
802 }
803
804 /// Appends a given string slice onto the end of this `String`.
805 ///
806 /// # Examples
807 ///
808 /// Basic usage:
809 ///
810 /// ```
811 /// let mut s = String::from("foo");
812 ///
813 /// s.push_str("bar");
814 ///
815 /// assert_eq!("foobar", s);
816 /// ```
817 #[inline]
818 #[stable(feature = "rust1", since = "1.0.0")]
819 pub fn push_str(&mut self, string: &str) {
820 self.vec.extend_from_slice(string.as_bytes())
821 }
822
823 /// Returns this `String`'s capacity, in bytes.
824 ///
825 /// # Examples
826 ///
827 /// Basic usage:
828 ///
829 /// ```
830 /// let s = String::with_capacity(10);
831 ///
832 /// assert!(s.capacity() >= 10);
833 /// ```
834 #[inline]
835 #[stable(feature = "rust1", since = "1.0.0")]
836 pub fn capacity(&self) -> usize {
837 self.vec.capacity()
838 }
839
840 /// Ensures that this `String`'s capacity is at least `additional` bytes
841 /// larger than its length.
842 ///
843 /// The capacity may be increased by more than `additional` bytes if it
844 /// chooses, to prevent frequent reallocations.
845 ///
846 /// If you do not want this "at least" behavior, see the [`reserve_exact()`]
847 /// method.
848 ///
849 /// [`reserve_exact()`]: #method.reserve_exact
850 ///
851 /// # Panics
852 ///
853 /// Panics if the new capacity overflows `usize`.
854 ///
855 /// # Examples
856 ///
857 /// Basic usage:
858 ///
859 /// ```
860 /// let mut s = String::new();
861 ///
862 /// s.reserve(10);
863 ///
864 /// assert!(s.capacity() >= 10);
865 /// ```
866 ///
867 /// This may not actually increase the capacity:
868 ///
869 /// ```
870 /// let mut s = String::with_capacity(10);
871 /// s.push('a');
872 /// s.push('b');
873 ///
874 /// // s now has a length of 2 and a capacity of 10
875 /// assert_eq!(2, s.len());
876 /// assert_eq!(10, s.capacity());
877 ///
878 /// // Since we already have an extra 8 capacity, calling this...
879 /// s.reserve(8);
880 ///
881 /// // ... doesn't actually increase.
882 /// assert_eq!(10, s.capacity());
883 /// ```
884 #[inline]
885 #[stable(feature = "rust1", since = "1.0.0")]
886 pub fn reserve(&mut self, additional: usize) {
887 self.vec.reserve(additional)
888 }
889
890 /// Ensures that this `String`'s capacity is `additional` bytes
891 /// larger than its length.
892 ///
893 /// Consider using the [`reserve()`] method unless you absolutely know
894 /// better than the allocator.
895 ///
896 /// [`reserve()`]: #method.reserve
897 ///
898 /// # Panics
899 ///
900 /// Panics if the new capacity overflows `usize`.
901 ///
902 /// # Examples
903 ///
904 /// Basic usage:
905 ///
906 /// ```
907 /// let mut s = String::new();
908 ///
909 /// s.reserve_exact(10);
910 ///
911 /// assert!(s.capacity() >= 10);
912 /// ```
913 ///
914 /// This may not actually increase the capacity:
915 ///
916 /// ```
917 /// let mut s = String::with_capacity(10);
918 /// s.push('a');
919 /// s.push('b');
920 ///
921 /// // s now has a length of 2 and a capacity of 10
922 /// assert_eq!(2, s.len());
923 /// assert_eq!(10, s.capacity());
924 ///
925 /// // Since we already have an extra 8 capacity, calling this...
926 /// s.reserve_exact(8);
927 ///
928 /// // ... doesn't actually increase.
929 /// assert_eq!(10, s.capacity());
930 /// ```
931 #[inline]
932 #[stable(feature = "rust1", since = "1.0.0")]
933 pub fn reserve_exact(&mut self, additional: usize) {
934 self.vec.reserve_exact(additional)
935 }
936
937 /// Shrinks the capacity of this `String` to match its length.
938 ///
939 /// # Examples
940 ///
941 /// Basic usage:
942 ///
943 /// ```
944 /// let mut s = String::from("foo");
945 ///
946 /// s.reserve(100);
947 /// assert!(s.capacity() >= 100);
948 ///
949 /// s.shrink_to_fit();
950 /// assert_eq!(3, s.capacity());
951 /// ```
952 #[inline]
953 #[stable(feature = "rust1", since = "1.0.0")]
954 pub fn shrink_to_fit(&mut self) {
955 self.vec.shrink_to_fit()
956 }
957
958 /// Appends the given `char` to the end of this `String`.
959 ///
960 /// # Examples
961 ///
962 /// Basic usage:
963 ///
964 /// ```
965 /// let mut s = String::from("abc");
966 ///
967 /// s.push('1');
968 /// s.push('2');
969 /// s.push('3');
970 ///
971 /// assert_eq!("abc123", s);
972 /// ```
973 #[inline]
974 #[stable(feature = "rust1", since = "1.0.0")]
975 pub fn push(&mut self, ch: char) {
976 match ch.len_utf8() {
977 1 => self.vec.push(ch as u8),
978 _ => self.vec.extend_from_slice(ch.encode_utf8().as_slice()),
979 }
980 }
981
982 /// Returns a byte slice of this `String`'s contents.
983 ///
984 /// # Examples
985 ///
986 /// Basic usage:
987 ///
988 /// ```
989 /// let s = String::from("hello");
990 ///
991 /// assert_eq!(&[104, 101, 108, 108, 111], s.as_bytes());
992 /// ```
993 #[inline]
994 #[stable(feature = "rust1", since = "1.0.0")]
995 pub fn as_bytes(&self) -> &[u8] {
996 &self.vec
997 }
998
999 /// Shortens this `String` to the specified length.
1000 ///
1001 /// If `new_len` is greater than the string's current length, this has no
1002 /// effect.
1003 ///
1004 /// # Panics
1005 ///
1006 /// Panics if `new_len` does not lie on a [`char`] boundary.
1007 ///
1008 /// [`char`]: ../../std/primitive.char.html
1009 ///
1010 /// # Examples
1011 ///
1012 /// Basic usage:
1013 ///
1014 /// ```
1015 /// let mut s = String::from("hello");
1016 ///
1017 /// s.truncate(2);
1018 ///
1019 /// assert_eq!("he", s);
1020 /// ```
1021 #[inline]
1022 #[stable(feature = "rust1", since = "1.0.0")]
1023 pub fn truncate(&mut self, new_len: usize) {
1024 if new_len <= self.len() {
1025 assert!(self.is_char_boundary(new_len));
1026 self.vec.truncate(new_len)
1027 }
1028 }
1029
1030 /// Removes the last character from the string buffer and returns it.
1031 ///
1032 /// Returns `None` if this `String` is empty.
1033 ///
1034 /// # Examples
1035 ///
1036 /// Basic usage:
1037 ///
1038 /// ```
1039 /// let mut s = String::from("foo");
1040 ///
1041 /// assert_eq!(s.pop(), Some('o'));
1042 /// assert_eq!(s.pop(), Some('o'));
1043 /// assert_eq!(s.pop(), Some('f'));
1044 ///
1045 /// assert_eq!(s.pop(), None);
1046 /// ```
1047 #[inline]
1048 #[stable(feature = "rust1", since = "1.0.0")]
1049 pub fn pop(&mut self) -> Option<char> {
1050 let ch = match self.chars().rev().next() {
1051 Some(ch) => ch,
1052 None => return None,
1053 };
1054 let newlen = self.len() - ch.len_utf8();
1055 unsafe {
1056 self.vec.set_len(newlen);
1057 }
1058 Some(ch)
1059 }
1060
1061 /// Removes a `char` from this `String` at a byte position and returns it.
1062 ///
1063 /// This is an `O(n)` operation, as it requires copying every element in the
1064 /// buffer.
1065 ///
1066 /// # Panics
1067 ///
1068 /// Panics if `idx` is larger than or equal to the `String`'s length,
1069 /// or if it does not lie on a [`char`] boundary.
1070 ///
1071 /// [`char`]: ../../std/primitive.char.html
1072 ///
1073 /// # Examples
1074 ///
1075 /// Basic usage:
1076 ///
1077 /// ```
1078 /// let mut s = String::from("foo");
1079 ///
1080 /// assert_eq!(s.remove(0), 'f');
1081 /// assert_eq!(s.remove(1), 'o');
1082 /// assert_eq!(s.remove(0), 'o');
1083 /// ```
1084 #[inline]
1085 #[stable(feature = "rust1", since = "1.0.0")]
1086 pub fn remove(&mut self, idx: usize) -> char {
1087 let ch = match self[idx..].chars().next() {
1088 Some(ch) => ch,
1089 None => panic!("cannot remove a char from the end of a string"),
1090 };
1091
1092 let next = idx + ch.len_utf8();
1093 let len = self.len();
1094 unsafe {
1095 ptr::copy(self.vec.as_ptr().offset(next as isize),
1096 self.vec.as_mut_ptr().offset(idx as isize),
1097 len - next);
1098 self.vec.set_len(len - (next - idx));
1099 }
1100 ch
1101 }
1102
1103 /// Inserts a character into this `String` at a byte position.
1104 ///
1105 /// This is an `O(n)` operation as it requires copying every element in the
1106 /// buffer.
1107 ///
1108 /// # Panics
1109 ///
1110 /// Panics if `idx` is larger than the `String`'s length, or if it does not
1111 /// lie on a [`char`] boundary.
1112 ///
1113 /// [`char`]: ../../std/primitive.char.html
1114 ///
1115 /// # Examples
1116 ///
1117 /// Basic usage:
1118 ///
1119 /// ```
1120 /// let mut s = String::with_capacity(3);
1121 ///
1122 /// s.insert(0, 'f');
1123 /// s.insert(1, 'o');
1124 /// s.insert(2, 'o');
1125 ///
1126 /// assert_eq!("foo", s);
1127 /// ```
1128 #[inline]
1129 #[stable(feature = "rust1", since = "1.0.0")]
1130 pub fn insert(&mut self, idx: usize, ch: char) {
1131 let len = self.len();
1132 assert!(idx <= len);
1133 assert!(self.is_char_boundary(idx));
1134 let bits = ch.encode_utf8();
1135
1136 unsafe {
1137 self.insert_bytes(idx, bits.as_slice());
1138 }
1139 }
1140
1141 unsafe fn insert_bytes(&mut self, idx: usize, bytes: &[u8]) {
1142 let len = self.len();
1143 let amt = bytes.len();
1144 self.vec.reserve(amt);
1145
1146 ptr::copy(self.vec.as_ptr().offset(idx as isize),
1147 self.vec.as_mut_ptr().offset((idx + amt) as isize),
1148 len - idx);
1149 ptr::copy(bytes.as_ptr(),
1150 self.vec.as_mut_ptr().offset(idx as isize),
1151 amt);
1152 self.vec.set_len(len + amt);
1153 }
1154
1155 /// Inserts a string slice into this `String` at a byte position.
1156 ///
1157 /// This is an `O(n)` operation as it requires copying every element in the
1158 /// buffer.
1159 ///
1160 /// # Panics
1161 ///
1162 /// Panics if `idx` is larger than the `String`'s length, or if it does not
1163 /// lie on a [`char`] boundary.
1164 ///
1165 /// [`char`]: ../../std/primitive.char.html
1166 ///
1167 /// # Examples
1168 ///
1169 /// Basic usage:
1170 ///
1171 /// ```
1172 /// #![feature(insert_str)]
1173 ///
1174 /// let mut s = String::from("bar");
1175 ///
1176 /// s.insert_str(0, "foo");
1177 ///
1178 /// assert_eq!("foobar", s);
1179 /// ```
1180 #[inline]
1181 #[unstable(feature = "insert_str",
1182 reason = "recent addition",
1183 issue = "35553")]
1184 pub fn insert_str(&mut self, idx: usize, string: &str) {
1185 assert!(idx <= self.len());
1186 assert!(self.is_char_boundary(idx));
1187
1188 unsafe {
1189 self.insert_bytes(idx, string.as_bytes());
1190 }
1191 }
1192
1193 /// Returns a mutable reference to the contents of this `String`.
1194 ///
1195 /// # Safety
1196 ///
1197 /// This function is unsafe because it does not check that the bytes passed
1198 /// to it are valid UTF-8. If this constraint is violated, it may cause
1199 /// memory unsafety issues with future users of the `String`, as the rest of
1200 /// the standard library assumes that `String`s are valid UTF-8.
1201 ///
1202 /// # Examples
1203 ///
1204 /// Basic usage:
1205 ///
1206 /// ```
1207 /// let mut s = String::from("hello");
1208 ///
1209 /// unsafe {
1210 /// let vec = s.as_mut_vec();
1211 /// assert_eq!(&[104, 101, 108, 108, 111][..], &vec[..]);
1212 ///
1213 /// vec.reverse();
1214 /// }
1215 /// assert_eq!(s, "olleh");
1216 /// ```
1217 #[inline]
1218 #[stable(feature = "rust1", since = "1.0.0")]
1219 pub unsafe fn as_mut_vec(&mut self) -> &mut Vec<u8> {
1220 &mut self.vec
1221 }
1222
1223 /// Returns the length of this `String`, in bytes.
1224 ///
1225 /// # Examples
1226 ///
1227 /// Basic usage:
1228 ///
1229 /// ```
1230 /// let a = String::from("foo");
1231 ///
1232 /// assert_eq!(a.len(), 3);
1233 /// ```
1234 #[inline]
1235 #[stable(feature = "rust1", since = "1.0.0")]
1236 pub fn len(&self) -> usize {
1237 self.vec.len()
1238 }
1239
1240 /// Returns `true` if this `String` has a length of zero.
1241 ///
1242 /// Returns `false` otherwise.
1243 ///
1244 /// # Examples
1245 ///
1246 /// Basic usage:
1247 ///
1248 /// ```
1249 /// let mut v = String::new();
1250 /// assert!(v.is_empty());
1251 ///
1252 /// v.push('a');
1253 /// assert!(!v.is_empty());
1254 /// ```
1255 #[inline]
1256 #[stable(feature = "rust1", since = "1.0.0")]
1257 pub fn is_empty(&self) -> bool {
1258 self.len() == 0
1259 }
1260
1261 /// Truncates this `String`, removing all contents.
1262 ///
1263 /// While this means the `String` will have a length of zero, it does not
1264 /// touch its capacity.
1265 ///
1266 /// # Examples
1267 ///
1268 /// Basic usage:
1269 ///
1270 /// ```
1271 /// let mut s = String::from("foo");
1272 ///
1273 /// s.clear();
1274 ///
1275 /// assert!(s.is_empty());
1276 /// assert_eq!(0, s.len());
1277 /// assert_eq!(3, s.capacity());
1278 /// ```
1279 #[inline]
1280 #[stable(feature = "rust1", since = "1.0.0")]
1281 pub fn clear(&mut self) {
1282 self.vec.clear()
1283 }
1284
1285 /// Create a draining iterator that removes the specified range in the string
1286 /// and yields the removed chars.
1287 ///
1288 /// Note: The element range is removed even if the iterator is not
1289 /// consumed until the end.
1290 ///
1291 /// # Panics
1292 ///
1293 /// Panics if the starting point or end point do not lie on a [`char`]
1294 /// boundary, or if they're out of bounds.
1295 ///
1296 /// [`char`]: ../../std/primitive.char.html
1297 ///
1298 /// # Examples
1299 ///
1300 /// Basic usage:
1301 ///
1302 /// ```
1303 /// let mut s = String::from("α is alpha, β is beta");
1304 /// let beta_offset = s.find('β').unwrap_or(s.len());
1305 ///
1306 /// // Remove the range up until the β from the string
1307 /// let t: String = s.drain(..beta_offset).collect();
1308 /// assert_eq!(t, "α is alpha, ");
1309 /// assert_eq!(s, "β is beta");
1310 ///
1311 /// // A full range clears the string
1312 /// s.drain(..);
1313 /// assert_eq!(s, "");
1314 /// ```
1315 #[stable(feature = "drain", since = "1.6.0")]
1316 pub fn drain<R>(&mut self, range: R) -> Drain
1317 where R: RangeArgument<usize>
1318 {
1319 // Memory safety
1320 //
1321 // The String version of Drain does not have the memory safety issues
1322 // of the vector version. The data is just plain bytes.
1323 // Because the range removal happens in Drop, if the Drain iterator is leaked,
1324 // the removal will not happen.
1325 let len = self.len();
1326 let start = *range.start().unwrap_or(&0);
1327 let end = *range.end().unwrap_or(&len);
1328
1329 // Take out two simultaneous borrows. The &mut String won't be accessed
1330 // until iteration is over, in Drop.
1331 let self_ptr = self as *mut _;
1332 // slicing does the appropriate bounds checks
1333 let chars_iter = self[start..end].chars();
1334
1335 Drain {
1336 start: start,
1337 end: end,
1338 iter: chars_iter,
1339 string: self_ptr,
1340 }
1341 }
1342
1343 /// Converts this `String` into a `Box<str>`.
1344 ///
1345 /// This will drop any excess capacity.
1346 ///
1347 /// # Examples
1348 ///
1349 /// Basic usage:
1350 ///
1351 /// ```
1352 /// let s = String::from("hello");
1353 ///
1354 /// let b = s.into_boxed_str();
1355 /// ```
1356 #[stable(feature = "box_str", since = "1.4.0")]
1357 pub fn into_boxed_str(self) -> Box<str> {
1358 let slice = self.vec.into_boxed_slice();
1359 unsafe { mem::transmute::<Box<[u8]>, Box<str>>(slice) }
1360 }
1361 }
1362
1363 impl FromUtf8Error {
1364 /// Returns the bytes that were attempted to convert to a `String`.
1365 ///
1366 /// This method is carefully constructed to avoid allocation. It will
1367 /// consume the error, moving out the bytes, so that a copy of the bytes
1368 /// does not need to be made.
1369 ///
1370 /// # Examples
1371 ///
1372 /// Basic usage:
1373 ///
1374 /// ```
1375 /// // some invalid bytes, in a vector
1376 /// let bytes = vec![0, 159];
1377 ///
1378 /// let value = String::from_utf8(bytes);
1379 ///
1380 /// assert_eq!(vec![0, 159], value.unwrap_err().into_bytes());
1381 /// ```
1382 #[stable(feature = "rust1", since = "1.0.0")]
1383 pub fn into_bytes(self) -> Vec<u8> {
1384 self.bytes
1385 }
1386
1387 /// Fetch a `Utf8Error` to get more details about the conversion failure.
1388 ///
1389 /// The [`Utf8Error`] type provided by [`std::str`] represents an error that may
1390 /// occur when converting a slice of [`u8`]s to a [`&str`]. In this sense, it's
1391 /// an analogue to `FromUtf8Error`. See its documentation for more details
1392 /// on using it.
1393 ///
1394 /// [`Utf8Error`]: ../../std/str/struct.Utf8Error.html
1395 /// [`std::str`]: ../../std/str/index.html
1396 /// [`u8`]: ../../std/primitive.u8.html
1397 /// [`&str`]: ../../std/primitive.str.html
1398 ///
1399 /// # Examples
1400 ///
1401 /// Basic usage:
1402 ///
1403 /// ```
1404 /// // some invalid bytes, in a vector
1405 /// let bytes = vec![0, 159];
1406 ///
1407 /// let error = String::from_utf8(bytes).unwrap_err().utf8_error();
1408 ///
1409 /// // the first byte is invalid here
1410 /// assert_eq!(1, error.valid_up_to());
1411 /// ```
1412 #[stable(feature = "rust1", since = "1.0.0")]
1413 pub fn utf8_error(&self) -> Utf8Error {
1414 self.error
1415 }
1416 }
1417
1418 #[stable(feature = "rust1", since = "1.0.0")]
1419 impl fmt::Display for FromUtf8Error {
1420 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1421 fmt::Display::fmt(&self.error, f)
1422 }
1423 }
1424
1425 #[stable(feature = "rust1", since = "1.0.0")]
1426 impl fmt::Display for FromUtf16Error {
1427 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1428 fmt::Display::fmt("invalid utf-16: lone surrogate found", f)
1429 }
1430 }
1431
1432 #[stable(feature = "rust1", since = "1.0.0")]
1433 impl Clone for String {
1434 fn clone(&self) -> Self {
1435 String { vec: self.vec.clone() }
1436 }
1437
1438 fn clone_from(&mut self, source: &Self) {
1439 self.vec.clone_from(&source.vec);
1440 }
1441 }
1442
1443 #[stable(feature = "rust1", since = "1.0.0")]
1444 impl FromIterator<char> for String {
1445 fn from_iter<I: IntoIterator<Item = char>>(iter: I) -> String {
1446 let mut buf = String::new();
1447 buf.extend(iter);
1448 buf
1449 }
1450 }
1451
1452 #[stable(feature = "rust1", since = "1.0.0")]
1453 impl<'a> FromIterator<&'a str> for String {
1454 fn from_iter<I: IntoIterator<Item = &'a str>>(iter: I) -> String {
1455 let mut buf = String::new();
1456 buf.extend(iter);
1457 buf
1458 }
1459 }
1460
1461 #[stable(feature = "extend_string", since = "1.4.0")]
1462 impl FromIterator<String> for String {
1463 fn from_iter<I: IntoIterator<Item = String>>(iter: I) -> String {
1464 let mut buf = String::new();
1465 buf.extend(iter);
1466 buf
1467 }
1468 }
1469
1470 #[stable(feature = "rust1", since = "1.0.0")]
1471 impl Extend<char> for String {
1472 fn extend<I: IntoIterator<Item = char>>(&mut self, iter: I) {
1473 let iterator = iter.into_iter();
1474 let (lower_bound, _) = iterator.size_hint();
1475 self.reserve(lower_bound);
1476 for ch in iterator {
1477 self.push(ch)
1478 }
1479 }
1480 }
1481
1482 #[stable(feature = "extend_ref", since = "1.2.0")]
1483 impl<'a> Extend<&'a char> for String {
1484 fn extend<I: IntoIterator<Item = &'a char>>(&mut self, iter: I) {
1485 self.extend(iter.into_iter().cloned());
1486 }
1487 }
1488
1489 #[stable(feature = "rust1", since = "1.0.0")]
1490 impl<'a> Extend<&'a str> for String {
1491 fn extend<I: IntoIterator<Item = &'a str>>(&mut self, iter: I) {
1492 for s in iter {
1493 self.push_str(s)
1494 }
1495 }
1496 }
1497
1498 #[stable(feature = "extend_string", since = "1.4.0")]
1499 impl Extend<String> for String {
1500 fn extend<I: IntoIterator<Item = String>>(&mut self, iter: I) {
1501 for s in iter {
1502 self.push_str(&s)
1503 }
1504 }
1505 }
1506
1507 /// A convenience impl that delegates to the impl for `&str`
1508 #[unstable(feature = "pattern",
1509 reason = "API not fully fleshed out and ready to be stabilized",
1510 issue = "27721")]
1511 impl<'a, 'b> Pattern<'a> for &'b String {
1512 type Searcher = <&'b str as Pattern<'a>>::Searcher;
1513
1514 fn into_searcher(self, haystack: &'a str) -> <&'b str as Pattern<'a>>::Searcher {
1515 self[..].into_searcher(haystack)
1516 }
1517
1518 #[inline]
1519 fn is_contained_in(self, haystack: &'a str) -> bool {
1520 self[..].is_contained_in(haystack)
1521 }
1522
1523 #[inline]
1524 fn is_prefix_of(self, haystack: &'a str) -> bool {
1525 self[..].is_prefix_of(haystack)
1526 }
1527 }
1528
1529 #[stable(feature = "rust1", since = "1.0.0")]
1530 impl PartialEq for String {
1531 #[inline]
1532 fn eq(&self, other: &String) -> bool {
1533 PartialEq::eq(&self[..], &other[..])
1534 }
1535 #[inline]
1536 fn ne(&self, other: &String) -> bool {
1537 PartialEq::ne(&self[..], &other[..])
1538 }
1539 }
1540
1541 macro_rules! impl_eq {
1542 ($lhs:ty, $rhs: ty) => {
1543 #[stable(feature = "rust1", since = "1.0.0")]
1544 impl<'a, 'b> PartialEq<$rhs> for $lhs {
1545 #[inline]
1546 fn eq(&self, other: &$rhs) -> bool { PartialEq::eq(&self[..], &other[..]) }
1547 #[inline]
1548 fn ne(&self, other: &$rhs) -> bool { PartialEq::ne(&self[..], &other[..]) }
1549 }
1550
1551 #[stable(feature = "rust1", since = "1.0.0")]
1552 impl<'a, 'b> PartialEq<$lhs> for $rhs {
1553 #[inline]
1554 fn eq(&self, other: &$lhs) -> bool { PartialEq::eq(&self[..], &other[..]) }
1555 #[inline]
1556 fn ne(&self, other: &$lhs) -> bool { PartialEq::ne(&self[..], &other[..]) }
1557 }
1558
1559 }
1560 }
1561
1562 impl_eq! { String, str }
1563 impl_eq! { String, &'a str }
1564 impl_eq! { Cow<'a, str>, str }
1565 impl_eq! { Cow<'a, str>, &'b str }
1566 impl_eq! { Cow<'a, str>, String }
1567
1568 #[stable(feature = "rust1", since = "1.0.0")]
1569 impl Default for String {
1570 #[inline]
1571 fn default() -> String {
1572 String::new()
1573 }
1574 }
1575
1576 #[stable(feature = "rust1", since = "1.0.0")]
1577 impl fmt::Display for String {
1578 #[inline]
1579 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1580 fmt::Display::fmt(&**self, f)
1581 }
1582 }
1583
1584 #[stable(feature = "rust1", since = "1.0.0")]
1585 impl fmt::Debug for String {
1586 #[inline]
1587 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1588 fmt::Debug::fmt(&**self, f)
1589 }
1590 }
1591
1592 #[stable(feature = "rust1", since = "1.0.0")]
1593 impl hash::Hash for String {
1594 #[inline]
1595 fn hash<H: hash::Hasher>(&self, hasher: &mut H) {
1596 (**self).hash(hasher)
1597 }
1598 }
1599
1600 #[stable(feature = "rust1", since = "1.0.0")]
1601 impl<'a> Add<&'a str> for String {
1602 type Output = String;
1603
1604 #[inline]
1605 fn add(mut self, other: &str) -> String {
1606 self.push_str(other);
1607 self
1608 }
1609 }
1610
1611 #[stable(feature = "stringaddassign", since = "1.12.0")]
1612 impl<'a> AddAssign<&'a str> for String {
1613 #[inline]
1614 fn add_assign(&mut self, other: &str) {
1615 self.push_str(other);
1616 }
1617 }
1618
1619 #[stable(feature = "rust1", since = "1.0.0")]
1620 impl ops::Index<ops::Range<usize>> for String {
1621 type Output = str;
1622
1623 #[inline]
1624 fn index(&self, index: ops::Range<usize>) -> &str {
1625 &self[..][index]
1626 }
1627 }
1628 #[stable(feature = "rust1", since = "1.0.0")]
1629 impl ops::Index<ops::RangeTo<usize>> for String {
1630 type Output = str;
1631
1632 #[inline]
1633 fn index(&self, index: ops::RangeTo<usize>) -> &str {
1634 &self[..][index]
1635 }
1636 }
1637 #[stable(feature = "rust1", since = "1.0.0")]
1638 impl ops::Index<ops::RangeFrom<usize>> for String {
1639 type Output = str;
1640
1641 #[inline]
1642 fn index(&self, index: ops::RangeFrom<usize>) -> &str {
1643 &self[..][index]
1644 }
1645 }
1646 #[stable(feature = "rust1", since = "1.0.0")]
1647 impl ops::Index<ops::RangeFull> for String {
1648 type Output = str;
1649
1650 #[inline]
1651 fn index(&self, _index: ops::RangeFull) -> &str {
1652 unsafe { str::from_utf8_unchecked(&self.vec) }
1653 }
1654 }
1655 #[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")]
1656 impl ops::Index<ops::RangeInclusive<usize>> for String {
1657 type Output = str;
1658
1659 #[inline]
1660 fn index(&self, index: ops::RangeInclusive<usize>) -> &str {
1661 Index::index(&**self, index)
1662 }
1663 }
1664 #[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")]
1665 impl ops::Index<ops::RangeToInclusive<usize>> for String {
1666 type Output = str;
1667
1668 #[inline]
1669 fn index(&self, index: ops::RangeToInclusive<usize>) -> &str {
1670 Index::index(&**self, index)
1671 }
1672 }
1673
1674 #[stable(feature = "derefmut_for_string", since = "1.2.0")]
1675 impl ops::IndexMut<ops::Range<usize>> for String {
1676 #[inline]
1677 fn index_mut(&mut self, index: ops::Range<usize>) -> &mut str {
1678 &mut self[..][index]
1679 }
1680 }
1681 #[stable(feature = "derefmut_for_string", since = "1.2.0")]
1682 impl ops::IndexMut<ops::RangeTo<usize>> for String {
1683 #[inline]
1684 fn index_mut(&mut self, index: ops::RangeTo<usize>) -> &mut str {
1685 &mut self[..][index]
1686 }
1687 }
1688 #[stable(feature = "derefmut_for_string", since = "1.2.0")]
1689 impl ops::IndexMut<ops::RangeFrom<usize>> for String {
1690 #[inline]
1691 fn index_mut(&mut self, index: ops::RangeFrom<usize>) -> &mut str {
1692 &mut self[..][index]
1693 }
1694 }
1695 #[stable(feature = "derefmut_for_string", since = "1.2.0")]
1696 impl ops::IndexMut<ops::RangeFull> for String {
1697 #[inline]
1698 fn index_mut(&mut self, _index: ops::RangeFull) -> &mut str {
1699 unsafe { mem::transmute(&mut *self.vec) }
1700 }
1701 }
1702 #[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")]
1703 impl ops::IndexMut<ops::RangeInclusive<usize>> for String {
1704 #[inline]
1705 fn index_mut(&mut self, index: ops::RangeInclusive<usize>) -> &mut str {
1706 IndexMut::index_mut(&mut **self, index)
1707 }
1708 }
1709 #[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")]
1710 impl ops::IndexMut<ops::RangeToInclusive<usize>> for String {
1711 #[inline]
1712 fn index_mut(&mut self, index: ops::RangeToInclusive<usize>) -> &mut str {
1713 IndexMut::index_mut(&mut **self, index)
1714 }
1715 }
1716
1717 #[stable(feature = "rust1", since = "1.0.0")]
1718 impl ops::Deref for String {
1719 type Target = str;
1720
1721 #[inline]
1722 fn deref(&self) -> &str {
1723 unsafe { str::from_utf8_unchecked(&self.vec) }
1724 }
1725 }
1726
1727 #[stable(feature = "derefmut_for_string", since = "1.2.0")]
1728 impl ops::DerefMut for String {
1729 #[inline]
1730 fn deref_mut(&mut self) -> &mut str {
1731 unsafe { mem::transmute(&mut *self.vec) }
1732 }
1733 }
1734
1735 /// An error when parsing a `String`.
1736 ///
1737 /// This `enum` is slightly awkward: it will never actually exist. This error is
1738 /// part of the type signature of the implementation of [`FromStr`] on
1739 /// [`String`]. The return type of [`from_str()`], requires that an error be
1740 /// defined, but, given that a [`String`] can always be made into a new
1741 /// [`String`] without error, this type will never actually be returned. As
1742 /// such, it is only here to satisfy said signature, and is useless otherwise.
1743 ///
1744 /// [`FromStr`]: ../../std/str/trait.FromStr.html
1745 /// [`String`]: struct.String.html
1746 /// [`from_str()`]: ../../std/str/trait.FromStr.html#tymethod.from_str
1747 #[stable(feature = "str_parse_error", since = "1.5.0")]
1748 #[derive(Copy)]
1749 pub enum ParseError {}
1750
1751 #[stable(feature = "rust1", since = "1.0.0")]
1752 impl FromStr for String {
1753 type Err = ParseError;
1754 #[inline]
1755 fn from_str(s: &str) -> Result<String, ParseError> {
1756 Ok(String::from(s))
1757 }
1758 }
1759
1760 #[stable(feature = "str_parse_error", since = "1.5.0")]
1761 impl Clone for ParseError {
1762 fn clone(&self) -> ParseError {
1763 match *self {}
1764 }
1765 }
1766
1767 #[stable(feature = "str_parse_error", since = "1.5.0")]
1768 impl fmt::Debug for ParseError {
1769 fn fmt(&self, _: &mut fmt::Formatter) -> fmt::Result {
1770 match *self {}
1771 }
1772 }
1773
1774 #[stable(feature = "str_parse_error2", since = "1.8.0")]
1775 impl fmt::Display for ParseError {
1776 fn fmt(&self, _: &mut fmt::Formatter) -> fmt::Result {
1777 match *self {}
1778 }
1779 }
1780
1781 #[stable(feature = "str_parse_error", since = "1.5.0")]
1782 impl PartialEq for ParseError {
1783 fn eq(&self, _: &ParseError) -> bool {
1784 match *self {}
1785 }
1786 }
1787
1788 #[stable(feature = "str_parse_error", since = "1.5.0")]
1789 impl Eq for ParseError {}
1790
1791 /// A trait for converting a value to a `String`.
1792 ///
1793 /// This trait is automatically implemented for any type which implements the
1794 /// [`Display`] trait. As such, `ToString` shouldn't be implemented directly:
1795 /// [`Display`] should be implemented instead, and you get the `ToString`
1796 /// implementation for free.
1797 ///
1798 /// [`Display`]: ../../std/fmt/trait.Display.html
1799 #[stable(feature = "rust1", since = "1.0.0")]
1800 pub trait ToString {
1801 /// Converts the given value to a `String`.
1802 ///
1803 /// # Examples
1804 ///
1805 /// Basic usage:
1806 ///
1807 /// ```
1808 /// let i = 5;
1809 /// let five = String::from("5");
1810 ///
1811 /// assert_eq!(five, i.to_string());
1812 /// ```
1813 #[stable(feature = "rust1", since = "1.0.0")]
1814 fn to_string(&self) -> String;
1815 }
1816
1817 #[stable(feature = "rust1", since = "1.0.0")]
1818 impl<T: fmt::Display + ?Sized> ToString for T {
1819 #[inline]
1820 default fn to_string(&self) -> String {
1821 use core::fmt::Write;
1822 let mut buf = String::new();
1823 let _ = buf.write_fmt(format_args!("{}", self));
1824 buf.shrink_to_fit();
1825 buf
1826 }
1827 }
1828
1829 #[stable(feature = "str_to_string_specialization", since = "1.9.0")]
1830 impl ToString for str {
1831 #[inline]
1832 fn to_string(&self) -> String {
1833 String::from(self)
1834 }
1835 }
1836
1837 #[stable(feature = "rust1", since = "1.0.0")]
1838 impl AsRef<str> for String {
1839 #[inline]
1840 fn as_ref(&self) -> &str {
1841 self
1842 }
1843 }
1844
1845 #[stable(feature = "rust1", since = "1.0.0")]
1846 impl AsRef<[u8]> for String {
1847 #[inline]
1848 fn as_ref(&self) -> &[u8] {
1849 self.as_bytes()
1850 }
1851 }
1852
1853 #[stable(feature = "rust1", since = "1.0.0")]
1854 impl<'a> From<&'a str> for String {
1855 fn from(s: &'a str) -> String {
1856 s.to_owned()
1857 }
1858 }
1859
1860 #[stable(feature = "rust1", since = "1.0.0")]
1861 impl<'a> From<&'a str> for Cow<'a, str> {
1862 #[inline]
1863 fn from(s: &'a str) -> Cow<'a, str> {
1864 Cow::Borrowed(s)
1865 }
1866 }
1867
1868 #[stable(feature = "rust1", since = "1.0.0")]
1869 impl<'a> From<String> for Cow<'a, str> {
1870 #[inline]
1871 fn from(s: String) -> Cow<'a, str> {
1872 Cow::Owned(s)
1873 }
1874 }
1875
1876 #[stable(feature = "cow_str_from_iter", since = "1.12.0")]
1877 impl<'a> FromIterator<char> for Cow<'a, str> {
1878 fn from_iter<I: IntoIterator<Item = char>>(it: I) -> Cow<'a, str> {
1879 Cow::Owned(FromIterator::from_iter(it))
1880 }
1881 }
1882
1883 #[stable(feature = "cow_str_from_iter", since = "1.12.0")]
1884 impl<'a, 'b> FromIterator<&'b str> for Cow<'a, str> {
1885 fn from_iter<I: IntoIterator<Item = &'b str>>(it: I) -> Cow<'a, str> {
1886 Cow::Owned(FromIterator::from_iter(it))
1887 }
1888 }
1889
1890 #[stable(feature = "cow_str_from_iter", since = "1.12.0")]
1891 impl<'a> FromIterator<String> for Cow<'a, str> {
1892 fn from_iter<I: IntoIterator<Item = String>>(it: I) -> Cow<'a, str> {
1893 Cow::Owned(FromIterator::from_iter(it))
1894 }
1895 }
1896
1897 #[stable(feature = "rust1", since = "1.0.0")]
1898 impl Into<Vec<u8>> for String {
1899 fn into(self) -> Vec<u8> {
1900 self.into_bytes()
1901 }
1902 }
1903
1904 #[stable(feature = "rust1", since = "1.0.0")]
1905 impl fmt::Write for String {
1906 #[inline]
1907 fn write_str(&mut self, s: &str) -> fmt::Result {
1908 self.push_str(s);
1909 Ok(())
1910 }
1911
1912 #[inline]
1913 fn write_char(&mut self, c: char) -> fmt::Result {
1914 self.push(c);
1915 Ok(())
1916 }
1917 }
1918
1919 /// A draining iterator for `String`.
1920 ///
1921 /// This struct is created by the [`drain()`] method on [`String`]. See its
1922 /// documentation for more.
1923 ///
1924 /// [`drain()`]: struct.String.html#method.drain
1925 /// [`String`]: struct.String.html
1926 #[stable(feature = "drain", since = "1.6.0")]
1927 pub struct Drain<'a> {
1928 /// Will be used as &'a mut String in the destructor
1929 string: *mut String,
1930 /// Start of part to remove
1931 start: usize,
1932 /// End of part to remove
1933 end: usize,
1934 /// Current remaining range to remove
1935 iter: Chars<'a>,
1936 }
1937
1938 #[stable(feature = "drain", since = "1.6.0")]
1939 unsafe impl<'a> Sync for Drain<'a> {}
1940 #[stable(feature = "drain", since = "1.6.0")]
1941 unsafe impl<'a> Send for Drain<'a> {}
1942
1943 #[stable(feature = "drain", since = "1.6.0")]
1944 impl<'a> Drop for Drain<'a> {
1945 fn drop(&mut self) {
1946 unsafe {
1947 // Use Vec::drain. "Reaffirm" the bounds checks to avoid
1948 // panic code being inserted again.
1949 let self_vec = (*self.string).as_mut_vec();
1950 if self.start <= self.end && self.end <= self_vec.len() {
1951 self_vec.drain(self.start..self.end);
1952 }
1953 }
1954 }
1955 }
1956
1957 #[stable(feature = "drain", since = "1.6.0")]
1958 impl<'a> Iterator for Drain<'a> {
1959 type Item = char;
1960
1961 #[inline]
1962 fn next(&mut self) -> Option<char> {
1963 self.iter.next()
1964 }
1965
1966 fn size_hint(&self) -> (usize, Option<usize>) {
1967 self.iter.size_hint()
1968 }
1969 }
1970
1971 #[stable(feature = "drain", since = "1.6.0")]
1972 impl<'a> DoubleEndedIterator for Drain<'a> {
1973 #[inline]
1974 fn next_back(&mut self) -> Option<char> {
1975 self.iter.next_back()
1976 }
1977 }