]> git.lizzy.rs Git - rust.git/blob - src/libcore/str/mod.rs
Rollup merge of #70038 - DutchGhost:const-forget-tests, r=RalfJung
[rust.git] / src / libcore / str / mod.rs
1 // ignore-tidy-filelength
2
3 //! String manipulation.
4 //!
5 //! For more details, see the [`std::str`] module.
6 //!
7 //! [`std::str`]: ../../std/str/index.html
8
9 #![stable(feature = "rust1", since = "1.0.0")]
10
11 use self::pattern::Pattern;
12 use self::pattern::{DoubleEndedSearcher, ReverseSearcher, SearchStep, Searcher};
13
14 use crate::char;
15 use crate::fmt::{self, Write};
16 use crate::iter::{Chain, FlatMap, Flatten};
17 use crate::iter::{Copied, Filter, FusedIterator, Map, TrustedLen, TrustedRandomAccess};
18 use crate::mem;
19 use crate::ops::Try;
20 use crate::option;
21 use crate::slice::{self, SliceIndex, Split as SliceSplit};
22
23 pub mod pattern;
24
25 #[unstable(feature = "str_internals", issue = "none")]
26 #[allow(missing_docs)]
27 pub mod lossy;
28
29 /// Parse a value from a string
30 ///
31 /// `FromStr`'s [`from_str`] method is often used implicitly, through
32 /// [`str`]'s [`parse`] method. See [`parse`]'s documentation for examples.
33 ///
34 /// [`from_str`]: #tymethod.from_str
35 /// [`str`]: ../../std/primitive.str.html
36 /// [`parse`]: ../../std/primitive.str.html#method.parse
37 ///
38 /// `FromStr` does not have a lifetime parameter, and so you can only parse types
39 /// that do not contain a lifetime parameter themselves. In other words, you can
40 /// parse an `i32` with `FromStr`, but not a `&i32`. You can parse a struct that
41 /// contains an `i32`, but not one that contains an `&i32`.
42 ///
43 /// # Examples
44 ///
45 /// Basic implementation of `FromStr` on an example `Point` type:
46 ///
47 /// ```
48 /// use std::str::FromStr;
49 /// use std::num::ParseIntError;
50 ///
51 /// #[derive(Debug, PartialEq)]
52 /// struct Point {
53 ///     x: i32,
54 ///     y: i32
55 /// }
56 ///
57 /// impl FromStr for Point {
58 ///     type Err = ParseIntError;
59 ///
60 ///     fn from_str(s: &str) -> Result<Self, Self::Err> {
61 ///         let coords: Vec<&str> = s.trim_matches(|p| p == '(' || p == ')' )
62 ///                                  .split(',')
63 ///                                  .collect();
64 ///
65 ///         let x_fromstr = coords[0].parse::<i32>()?;
66 ///         let y_fromstr = coords[1].parse::<i32>()?;
67 ///
68 ///         Ok(Point { x: x_fromstr, y: y_fromstr })
69 ///     }
70 /// }
71 ///
72 /// let p = Point::from_str("(1,2)");
73 /// assert_eq!(p.unwrap(), Point{ x: 1, y: 2} )
74 /// ```
75 #[stable(feature = "rust1", since = "1.0.0")]
76 pub trait FromStr: Sized {
77     /// The associated error which can be returned from parsing.
78     #[stable(feature = "rust1", since = "1.0.0")]
79     type Err;
80
81     /// Parses a string `s` to return a value of this type.
82     ///
83     /// If parsing succeeds, return the value inside [`Ok`], otherwise
84     /// when the string is ill-formatted return an error specific to the
85     /// inside [`Err`]. The error type is specific to implementation of the trait.
86     ///
87     /// [`Ok`]: ../../std/result/enum.Result.html#variant.Ok
88     /// [`Err`]: ../../std/result/enum.Result.html#variant.Err
89     ///
90     /// # Examples
91     ///
92     /// Basic usage with [`i32`][ithirtytwo], a type that implements `FromStr`:
93     ///
94     /// [ithirtytwo]: ../../std/primitive.i32.html
95     ///
96     /// ```
97     /// use std::str::FromStr;
98     ///
99     /// let s = "5";
100     /// let x = i32::from_str(s).unwrap();
101     ///
102     /// assert_eq!(5, x);
103     /// ```
104     #[stable(feature = "rust1", since = "1.0.0")]
105     fn from_str(s: &str) -> Result<Self, Self::Err>;
106 }
107
108 #[stable(feature = "rust1", since = "1.0.0")]
109 impl FromStr for bool {
110     type Err = ParseBoolError;
111
112     /// Parse a `bool` from a string.
113     ///
114     /// Yields a `Result<bool, ParseBoolError>`, because `s` may or may not
115     /// actually be parseable.
116     ///
117     /// # Examples
118     ///
119     /// ```
120     /// use std::str::FromStr;
121     ///
122     /// assert_eq!(FromStr::from_str("true"), Ok(true));
123     /// assert_eq!(FromStr::from_str("false"), Ok(false));
124     /// assert!(<bool as FromStr>::from_str("not even a boolean").is_err());
125     /// ```
126     ///
127     /// Note, in many cases, the `.parse()` method on `str` is more proper.
128     ///
129     /// ```
130     /// assert_eq!("true".parse(), Ok(true));
131     /// assert_eq!("false".parse(), Ok(false));
132     /// assert!("not even a boolean".parse::<bool>().is_err());
133     /// ```
134     #[inline]
135     fn from_str(s: &str) -> Result<bool, ParseBoolError> {
136         match s {
137             "true" => Ok(true),
138             "false" => Ok(false),
139             _ => Err(ParseBoolError { _priv: () }),
140         }
141     }
142 }
143
144 /// An error returned when parsing a `bool` using [`from_str`] fails
145 ///
146 /// [`from_str`]: ../../std/primitive.bool.html#method.from_str
147 #[derive(Debug, Clone, PartialEq, Eq)]
148 #[stable(feature = "rust1", since = "1.0.0")]
149 pub struct ParseBoolError {
150     _priv: (),
151 }
152
153 #[stable(feature = "rust1", since = "1.0.0")]
154 impl fmt::Display for ParseBoolError {
155     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
156         "provided string was not `true` or `false`".fmt(f)
157     }
158 }
159
160 /*
161 Section: Creating a string
162 */
163
164 /// Errors which can occur when attempting to interpret a sequence of [`u8`]
165 /// as a string.
166 ///
167 /// [`u8`]: ../../std/primitive.u8.html
168 ///
169 /// As such, the `from_utf8` family of functions and methods for both [`String`]s
170 /// and [`&str`]s make use of this error, for example.
171 ///
172 /// [`String`]: ../../std/string/struct.String.html#method.from_utf8
173 /// [`&str`]: ../../std/str/fn.from_utf8.html
174 ///
175 /// # Examples
176 ///
177 /// This error type’s methods can be used to create functionality
178 /// similar to `String::from_utf8_lossy` without allocating heap memory:
179 ///
180 /// ```
181 /// fn from_utf8_lossy<F>(mut input: &[u8], mut push: F) where F: FnMut(&str) {
182 ///     loop {
183 ///         match std::str::from_utf8(input) {
184 ///             Ok(valid) => {
185 ///                 push(valid);
186 ///                 break
187 ///             }
188 ///             Err(error) => {
189 ///                 let (valid, after_valid) = input.split_at(error.valid_up_to());
190 ///                 unsafe {
191 ///                     push(std::str::from_utf8_unchecked(valid))
192 ///                 }
193 ///                 push("\u{FFFD}");
194 ///
195 ///                 if let Some(invalid_sequence_length) = error.error_len() {
196 ///                     input = &after_valid[invalid_sequence_length..]
197 ///                 } else {
198 ///                     break
199 ///                 }
200 ///             }
201 ///         }
202 ///     }
203 /// }
204 /// ```
205 #[derive(Copy, Eq, PartialEq, Clone, Debug)]
206 #[stable(feature = "rust1", since = "1.0.0")]
207 pub struct Utf8Error {
208     valid_up_to: usize,
209     error_len: Option<u8>,
210 }
211
212 impl Utf8Error {
213     /// Returns the index in the given string up to which valid UTF-8 was
214     /// verified.
215     ///
216     /// It is the maximum index such that `from_utf8(&input[..index])`
217     /// would return `Ok(_)`.
218     ///
219     /// # Examples
220     ///
221     /// Basic usage:
222     ///
223     /// ```
224     /// use std::str;
225     ///
226     /// // some invalid bytes, in a vector
227     /// let sparkle_heart = vec![0, 159, 146, 150];
228     ///
229     /// // std::str::from_utf8 returns a Utf8Error
230     /// let error = str::from_utf8(&sparkle_heart).unwrap_err();
231     ///
232     /// // the second byte is invalid here
233     /// assert_eq!(1, error.valid_up_to());
234     /// ```
235     #[stable(feature = "utf8_error", since = "1.5.0")]
236     pub fn valid_up_to(&self) -> usize {
237         self.valid_up_to
238     }
239
240     /// Provides more information about the failure:
241     ///
242     /// * `None`: the end of the input was reached unexpectedly.
243     ///   `self.valid_up_to()` is 1 to 3 bytes from the end of the input.
244     ///   If a byte stream (such as a file or a network socket) is being decoded incrementally,
245     ///   this could be a valid `char` whose UTF-8 byte sequence is spanning multiple chunks.
246     ///
247     /// * `Some(len)`: an unexpected byte was encountered.
248     ///   The length provided is that of the invalid byte sequence
249     ///   that starts at the index given by `valid_up_to()`.
250     ///   Decoding should resume after that sequence
251     ///   (after inserting a [`U+FFFD REPLACEMENT CHARACTER`][U+FFFD]) in case of
252     ///   lossy decoding.
253     ///
254     /// [U+FFFD]: ../../std/char/constant.REPLACEMENT_CHARACTER.html
255     #[stable(feature = "utf8_error_error_len", since = "1.20.0")]
256     pub fn error_len(&self) -> Option<usize> {
257         self.error_len.map(|len| len as usize)
258     }
259 }
260
261 /// Converts a slice of bytes to a string slice.
262 ///
263 /// A string slice ([`&str`]) is made of bytes ([`u8`]), and a byte slice
264 /// ([`&[u8]`][byteslice]) is made of bytes, so this function converts between
265 /// the two. Not all byte slices are valid string slices, however: [`&str`] requires
266 /// that it is valid UTF-8. `from_utf8()` checks to ensure that the bytes are valid
267 /// UTF-8, and then does the conversion.
268 ///
269 /// [`&str`]: ../../std/primitive.str.html
270 /// [`u8`]: ../../std/primitive.u8.html
271 /// [byteslice]: ../../std/primitive.slice.html
272 ///
273 /// If you are sure that the byte slice is valid UTF-8, and you don't want to
274 /// incur the overhead of the validity check, there is an unsafe version of
275 /// this function, [`from_utf8_unchecked`][fromutf8u], which has the same
276 /// behavior but skips the check.
277 ///
278 /// [fromutf8u]: fn.from_utf8_unchecked.html
279 ///
280 /// If you need a `String` instead of a `&str`, consider
281 /// [`String::from_utf8`][string].
282 ///
283 /// [string]: ../../std/string/struct.String.html#method.from_utf8
284 ///
285 /// Because you can stack-allocate a `[u8; N]`, and you can take a
286 /// [`&[u8]`][byteslice] of it, this function is one way to have a
287 /// stack-allocated string. There is an example of this in the
288 /// examples section below.
289 ///
290 /// [byteslice]: ../../std/primitive.slice.html
291 ///
292 /// # Errors
293 ///
294 /// Returns `Err` if the slice is not UTF-8 with a description as to why the
295 /// provided slice is not UTF-8.
296 ///
297 /// # Examples
298 ///
299 /// Basic usage:
300 ///
301 /// ```
302 /// use std::str;
303 ///
304 /// // some bytes, in a vector
305 /// let sparkle_heart = vec![240, 159, 146, 150];
306 ///
307 /// // We know these bytes are valid, so just use `unwrap()`.
308 /// let sparkle_heart = str::from_utf8(&sparkle_heart).unwrap();
309 ///
310 /// assert_eq!("💖", sparkle_heart);
311 /// ```
312 ///
313 /// Incorrect bytes:
314 ///
315 /// ```
316 /// use std::str;
317 ///
318 /// // some invalid bytes, in a vector
319 /// let sparkle_heart = vec![0, 159, 146, 150];
320 ///
321 /// assert!(str::from_utf8(&sparkle_heart).is_err());
322 /// ```
323 ///
324 /// See the docs for [`Utf8Error`][error] for more details on the kinds of
325 /// errors that can be returned.
326 ///
327 /// [error]: struct.Utf8Error.html
328 ///
329 /// A "stack allocated string":
330 ///
331 /// ```
332 /// use std::str;
333 ///
334 /// // some bytes, in a stack-allocated array
335 /// let sparkle_heart = [240, 159, 146, 150];
336 ///
337 /// // We know these bytes are valid, so just use `unwrap()`.
338 /// let sparkle_heart = str::from_utf8(&sparkle_heart).unwrap();
339 ///
340 /// assert_eq!("💖", sparkle_heart);
341 /// ```
342 #[stable(feature = "rust1", since = "1.0.0")]
343 pub fn from_utf8(v: &[u8]) -> Result<&str, Utf8Error> {
344     run_utf8_validation(v)?;
345     // SAFETY: Just ran validation.
346     Ok(unsafe { from_utf8_unchecked(v) })
347 }
348
349 /// Converts a mutable slice of bytes to a mutable string slice.
350 ///
351 /// # Examples
352 ///
353 /// Basic usage:
354 ///
355 /// ```
356 /// use std::str;
357 ///
358 /// // "Hello, Rust!" as a mutable vector
359 /// let mut hellorust = vec![72, 101, 108, 108, 111, 44, 32, 82, 117, 115, 116, 33];
360 ///
361 /// // As we know these bytes are valid, we can use `unwrap()`
362 /// let outstr = str::from_utf8_mut(&mut hellorust).unwrap();
363 ///
364 /// assert_eq!("Hello, Rust!", outstr);
365 /// ```
366 ///
367 /// Incorrect bytes:
368 ///
369 /// ```
370 /// use std::str;
371 ///
372 /// // Some invalid bytes in a mutable vector
373 /// let mut invalid = vec![128, 223];
374 ///
375 /// assert!(str::from_utf8_mut(&mut invalid).is_err());
376 /// ```
377 /// See the docs for [`Utf8Error`][error] for more details on the kinds of
378 /// errors that can be returned.
379 ///
380 /// [error]: struct.Utf8Error.html
381 #[stable(feature = "str_mut_extras", since = "1.20.0")]
382 pub fn from_utf8_mut(v: &mut [u8]) -> Result<&mut str, Utf8Error> {
383     run_utf8_validation(v)?;
384     // SAFETY: Just ran validation.
385     Ok(unsafe { from_utf8_unchecked_mut(v) })
386 }
387
388 /// Converts a slice of bytes to a string slice without checking
389 /// that the string contains valid UTF-8.
390 ///
391 /// See the safe version, [`from_utf8`][fromutf8], for more information.
392 ///
393 /// [fromutf8]: fn.from_utf8.html
394 ///
395 /// # Safety
396 ///
397 /// This function is unsafe because it does not check that the bytes passed to
398 /// it are valid UTF-8. If this constraint is violated, undefined behavior
399 /// results, as the rest of Rust assumes that [`&str`]s are valid UTF-8.
400 ///
401 /// [`&str`]: ../../std/primitive.str.html
402 ///
403 /// # Examples
404 ///
405 /// Basic usage:
406 ///
407 /// ```
408 /// use std::str;
409 ///
410 /// // some bytes, in a vector
411 /// let sparkle_heart = vec![240, 159, 146, 150];
412 ///
413 /// let sparkle_heart = unsafe {
414 ///     str::from_utf8_unchecked(&sparkle_heart)
415 /// };
416 ///
417 /// assert_eq!("💖", sparkle_heart);
418 /// ```
419 #[inline]
420 #[stable(feature = "rust1", since = "1.0.0")]
421 pub unsafe fn from_utf8_unchecked(v: &[u8]) -> &str {
422     &*(v as *const [u8] as *const str)
423 }
424
425 /// Converts a slice of bytes to a string slice without checking
426 /// that the string contains valid UTF-8; mutable version.
427 ///
428 /// See the immutable version, [`from_utf8_unchecked()`][fromutf8], for more information.
429 ///
430 /// [fromutf8]: fn.from_utf8_unchecked.html
431 ///
432 /// # Examples
433 ///
434 /// Basic usage:
435 ///
436 /// ```
437 /// use std::str;
438 ///
439 /// let mut heart = vec![240, 159, 146, 150];
440 /// let heart = unsafe { str::from_utf8_unchecked_mut(&mut heart) };
441 ///
442 /// assert_eq!("💖", heart);
443 /// ```
444 #[inline]
445 #[stable(feature = "str_mut_extras", since = "1.20.0")]
446 pub unsafe fn from_utf8_unchecked_mut(v: &mut [u8]) -> &mut str {
447     &mut *(v as *mut [u8] as *mut str)
448 }
449
450 #[stable(feature = "rust1", since = "1.0.0")]
451 impl fmt::Display for Utf8Error {
452     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
453         if let Some(error_len) = self.error_len {
454             write!(
455                 f,
456                 "invalid utf-8 sequence of {} bytes from index {}",
457                 error_len, self.valid_up_to
458             )
459         } else {
460             write!(f, "incomplete utf-8 byte sequence from index {}", self.valid_up_to)
461         }
462     }
463 }
464
465 /*
466 Section: Iterators
467 */
468
469 /// An iterator over the [`char`]s of a string slice.
470 ///
471 /// [`char`]: ../../std/primitive.char.html
472 ///
473 /// This struct is created by the [`chars`] method on [`str`].
474 /// See its documentation for more.
475 ///
476 /// [`chars`]: ../../std/primitive.str.html#method.chars
477 /// [`str`]: ../../std/primitive.str.html
478 #[derive(Clone)]
479 #[stable(feature = "rust1", since = "1.0.0")]
480 pub struct Chars<'a> {
481     iter: slice::Iter<'a, u8>,
482 }
483
484 /// Returns the initial codepoint accumulator for the first byte.
485 /// The first byte is special, only want bottom 5 bits for width 2, 4 bits
486 /// for width 3, and 3 bits for width 4.
487 #[inline]
488 fn utf8_first_byte(byte: u8, width: u32) -> u32 {
489     (byte & (0x7F >> width)) as u32
490 }
491
492 /// Returns the value of `ch` updated with continuation byte `byte`.
493 #[inline]
494 fn utf8_acc_cont_byte(ch: u32, byte: u8) -> u32 {
495     (ch << 6) | (byte & CONT_MASK) as u32
496 }
497
498 /// Checks whether the byte is a UTF-8 continuation byte (i.e., starts with the
499 /// bits `10`).
500 #[inline]
501 fn utf8_is_cont_byte(byte: u8) -> bool {
502     (byte & !CONT_MASK) == TAG_CONT_U8
503 }
504
505 #[inline]
506 fn unwrap_or_0(opt: Option<&u8>) -> u8 {
507     match opt {
508         Some(&byte) => byte,
509         None => 0,
510     }
511 }
512
513 /// Reads the next code point out of a byte iterator (assuming a
514 /// UTF-8-like encoding).
515 #[unstable(feature = "str_internals", issue = "none")]
516 #[inline]
517 pub fn next_code_point<'a, I: Iterator<Item = &'a u8>>(bytes: &mut I) -> Option<u32> {
518     // Decode UTF-8
519     let x = *bytes.next()?;
520     if x < 128 {
521         return Some(x as u32);
522     }
523
524     // Multibyte case follows
525     // Decode from a byte combination out of: [[[x y] z] w]
526     // NOTE: Performance is sensitive to the exact formulation here
527     let init = utf8_first_byte(x, 2);
528     let y = unwrap_or_0(bytes.next());
529     let mut ch = utf8_acc_cont_byte(init, y);
530     if x >= 0xE0 {
531         // [[x y z] w] case
532         // 5th bit in 0xE0 .. 0xEF is always clear, so `init` is still valid
533         let z = unwrap_or_0(bytes.next());
534         let y_z = utf8_acc_cont_byte((y & CONT_MASK) as u32, z);
535         ch = init << 12 | y_z;
536         if x >= 0xF0 {
537             // [x y z w] case
538             // use only the lower 3 bits of `init`
539             let w = unwrap_or_0(bytes.next());
540             ch = (init & 7) << 18 | utf8_acc_cont_byte(y_z, w);
541         }
542     }
543
544     Some(ch)
545 }
546
547 /// Reads the last code point out of a byte iterator (assuming a
548 /// UTF-8-like encoding).
549 #[inline]
550 fn next_code_point_reverse<'a, I>(bytes: &mut I) -> Option<u32>
551 where
552     I: DoubleEndedIterator<Item = &'a u8>,
553 {
554     // Decode UTF-8
555     let w = match *bytes.next_back()? {
556         next_byte if next_byte < 128 => return Some(next_byte as u32),
557         back_byte => back_byte,
558     };
559
560     // Multibyte case follows
561     // Decode from a byte combination out of: [x [y [z w]]]
562     let mut ch;
563     let z = unwrap_or_0(bytes.next_back());
564     ch = utf8_first_byte(z, 2);
565     if utf8_is_cont_byte(z) {
566         let y = unwrap_or_0(bytes.next_back());
567         ch = utf8_first_byte(y, 3);
568         if utf8_is_cont_byte(y) {
569             let x = unwrap_or_0(bytes.next_back());
570             ch = utf8_first_byte(x, 4);
571             ch = utf8_acc_cont_byte(ch, y);
572         }
573         ch = utf8_acc_cont_byte(ch, z);
574     }
575     ch = utf8_acc_cont_byte(ch, w);
576
577     Some(ch)
578 }
579
580 #[stable(feature = "rust1", since = "1.0.0")]
581 impl<'a> Iterator for Chars<'a> {
582     type Item = char;
583
584     #[inline]
585     fn next(&mut self) -> Option<char> {
586         next_code_point(&mut self.iter).map(|ch| {
587             // SAFETY: `str` invariant says `ch` is a valid Unicode Scalar Value.
588             unsafe { char::from_u32_unchecked(ch) }
589         })
590     }
591
592     #[inline]
593     fn count(self) -> usize {
594         // length in `char` is equal to the number of non-continuation bytes
595         let bytes_len = self.iter.len();
596         let mut cont_bytes = 0;
597         for &byte in self.iter {
598             cont_bytes += utf8_is_cont_byte(byte) as usize;
599         }
600         bytes_len - cont_bytes
601     }
602
603     #[inline]
604     fn size_hint(&self) -> (usize, Option<usize>) {
605         let len = self.iter.len();
606         // `(len + 3)` can't overflow, because we know that the `slice::Iter`
607         // belongs to a slice in memory which has a maximum length of
608         // `isize::MAX` (that's well below `usize::MAX`).
609         ((len + 3) / 4, Some(len))
610     }
611
612     #[inline]
613     fn last(mut self) -> Option<char> {
614         // No need to go through the entire string.
615         self.next_back()
616     }
617 }
618
619 #[stable(feature = "chars_debug_impl", since = "1.38.0")]
620 impl fmt::Debug for Chars<'_> {
621     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
622         write!(f, "Chars(")?;
623         f.debug_list().entries(self.clone()).finish()?;
624         write!(f, ")")?;
625         Ok(())
626     }
627 }
628
629 #[stable(feature = "rust1", since = "1.0.0")]
630 impl<'a> DoubleEndedIterator for Chars<'a> {
631     #[inline]
632     fn next_back(&mut self) -> Option<char> {
633         next_code_point_reverse(&mut self.iter).map(|ch| {
634             // SAFETY: `str` invariant says `ch` is a valid Unicode Scalar Value.
635             unsafe { char::from_u32_unchecked(ch) }
636         })
637     }
638 }
639
640 #[stable(feature = "fused", since = "1.26.0")]
641 impl FusedIterator for Chars<'_> {}
642
643 impl<'a> Chars<'a> {
644     /// Views the underlying data as a subslice of the original data.
645     ///
646     /// This has the same lifetime as the original slice, and so the
647     /// iterator can continue to be used while this exists.
648     ///
649     /// # Examples
650     ///
651     /// ```
652     /// let mut chars = "abc".chars();
653     ///
654     /// assert_eq!(chars.as_str(), "abc");
655     /// chars.next();
656     /// assert_eq!(chars.as_str(), "bc");
657     /// chars.next();
658     /// chars.next();
659     /// assert_eq!(chars.as_str(), "");
660     /// ```
661     #[stable(feature = "iter_to_slice", since = "1.4.0")]
662     #[inline]
663     pub fn as_str(&self) -> &'a str {
664         // SAFETY: `Chars` is only made from a str, which guarantees the iter is valid UTF-8.
665         unsafe { from_utf8_unchecked(self.iter.as_slice()) }
666     }
667 }
668
669 /// An iterator over the [`char`]s of a string slice, and their positions.
670 ///
671 /// [`char`]: ../../std/primitive.char.html
672 ///
673 /// This struct is created by the [`char_indices`] method on [`str`].
674 /// See its documentation for more.
675 ///
676 /// [`char_indices`]: ../../std/primitive.str.html#method.char_indices
677 /// [`str`]: ../../std/primitive.str.html
678 #[derive(Clone, Debug)]
679 #[stable(feature = "rust1", since = "1.0.0")]
680 pub struct CharIndices<'a> {
681     front_offset: usize,
682     iter: Chars<'a>,
683 }
684
685 #[stable(feature = "rust1", since = "1.0.0")]
686 impl<'a> Iterator for CharIndices<'a> {
687     type Item = (usize, char);
688
689     #[inline]
690     fn next(&mut self) -> Option<(usize, char)> {
691         let pre_len = self.iter.iter.len();
692         match self.iter.next() {
693             None => None,
694             Some(ch) => {
695                 let index = self.front_offset;
696                 let len = self.iter.iter.len();
697                 self.front_offset += pre_len - len;
698                 Some((index, ch))
699             }
700         }
701     }
702
703     #[inline]
704     fn count(self) -> usize {
705         self.iter.count()
706     }
707
708     #[inline]
709     fn size_hint(&self) -> (usize, Option<usize>) {
710         self.iter.size_hint()
711     }
712
713     #[inline]
714     fn last(mut self) -> Option<(usize, char)> {
715         // No need to go through the entire string.
716         self.next_back()
717     }
718 }
719
720 #[stable(feature = "rust1", since = "1.0.0")]
721 impl<'a> DoubleEndedIterator for CharIndices<'a> {
722     #[inline]
723     fn next_back(&mut self) -> Option<(usize, char)> {
724         self.iter.next_back().map(|ch| {
725             let index = self.front_offset + self.iter.iter.len();
726             (index, ch)
727         })
728     }
729 }
730
731 #[stable(feature = "fused", since = "1.26.0")]
732 impl FusedIterator for CharIndices<'_> {}
733
734 impl<'a> CharIndices<'a> {
735     /// Views the underlying data as a subslice of the original data.
736     ///
737     /// This has the same lifetime as the original slice, and so the
738     /// iterator can continue to be used while this exists.
739     #[stable(feature = "iter_to_slice", since = "1.4.0")]
740     #[inline]
741     pub fn as_str(&self) -> &'a str {
742         self.iter.as_str()
743     }
744 }
745
746 /// An iterator over the bytes of a string slice.
747 ///
748 /// This struct is created by the [`bytes`] method on [`str`].
749 /// See its documentation for more.
750 ///
751 /// [`bytes`]: ../../std/primitive.str.html#method.bytes
752 /// [`str`]: ../../std/primitive.str.html
753 #[stable(feature = "rust1", since = "1.0.0")]
754 #[derive(Clone, Debug)]
755 pub struct Bytes<'a>(Copied<slice::Iter<'a, u8>>);
756
757 #[stable(feature = "rust1", since = "1.0.0")]
758 impl Iterator for Bytes<'_> {
759     type Item = u8;
760
761     #[inline]
762     fn next(&mut self) -> Option<u8> {
763         self.0.next()
764     }
765
766     #[inline]
767     fn size_hint(&self) -> (usize, Option<usize>) {
768         self.0.size_hint()
769     }
770
771     #[inline]
772     fn count(self) -> usize {
773         self.0.count()
774     }
775
776     #[inline]
777     fn last(self) -> Option<Self::Item> {
778         self.0.last()
779     }
780
781     #[inline]
782     fn nth(&mut self, n: usize) -> Option<Self::Item> {
783         self.0.nth(n)
784     }
785
786     #[inline]
787     fn all<F>(&mut self, f: F) -> bool
788     where
789         F: FnMut(Self::Item) -> bool,
790     {
791         self.0.all(f)
792     }
793
794     #[inline]
795     fn any<F>(&mut self, f: F) -> bool
796     where
797         F: FnMut(Self::Item) -> bool,
798     {
799         self.0.any(f)
800     }
801
802     #[inline]
803     fn find<P>(&mut self, predicate: P) -> Option<Self::Item>
804     where
805         P: FnMut(&Self::Item) -> bool,
806     {
807         self.0.find(predicate)
808     }
809
810     #[inline]
811     fn position<P>(&mut self, predicate: P) -> Option<usize>
812     where
813         P: FnMut(Self::Item) -> bool,
814     {
815         self.0.position(predicate)
816     }
817
818     #[inline]
819     fn rposition<P>(&mut self, predicate: P) -> Option<usize>
820     where
821         P: FnMut(Self::Item) -> bool,
822     {
823         self.0.rposition(predicate)
824     }
825 }
826
827 #[stable(feature = "rust1", since = "1.0.0")]
828 impl DoubleEndedIterator for Bytes<'_> {
829     #[inline]
830     fn next_back(&mut self) -> Option<u8> {
831         self.0.next_back()
832     }
833
834     #[inline]
835     fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
836         self.0.nth_back(n)
837     }
838
839     #[inline]
840     fn rfind<P>(&mut self, predicate: P) -> Option<Self::Item>
841     where
842         P: FnMut(&Self::Item) -> bool,
843     {
844         self.0.rfind(predicate)
845     }
846 }
847
848 #[stable(feature = "rust1", since = "1.0.0")]
849 impl ExactSizeIterator for Bytes<'_> {
850     #[inline]
851     fn len(&self) -> usize {
852         self.0.len()
853     }
854
855     #[inline]
856     fn is_empty(&self) -> bool {
857         self.0.is_empty()
858     }
859 }
860
861 #[stable(feature = "fused", since = "1.26.0")]
862 impl FusedIterator for Bytes<'_> {}
863
864 #[unstable(feature = "trusted_len", issue = "37572")]
865 unsafe impl TrustedLen for Bytes<'_> {}
866
867 #[doc(hidden)]
868 unsafe impl TrustedRandomAccess for Bytes<'_> {
869     unsafe fn get_unchecked(&mut self, i: usize) -> u8 {
870         self.0.get_unchecked(i)
871     }
872     fn may_have_side_effect() -> bool {
873         false
874     }
875 }
876
877 /// This macro generates a Clone impl for string pattern API
878 /// wrapper types of the form X<'a, P>
879 macro_rules! derive_pattern_clone {
880     (clone $t:ident with |$s:ident| $e:expr) => {
881         impl<'a, P> Clone for $t<'a, P>
882         where
883             P: Pattern<'a, Searcher: Clone>,
884         {
885             fn clone(&self) -> Self {
886                 let $s = self;
887                 $e
888             }
889         }
890     };
891 }
892
893 /// This macro generates two public iterator structs
894 /// wrapping a private internal one that makes use of the `Pattern` API.
895 ///
896 /// For all patterns `P: Pattern<'a>` the following items will be
897 /// generated (generics omitted):
898 ///
899 /// struct $forward_iterator($internal_iterator);
900 /// struct $reverse_iterator($internal_iterator);
901 ///
902 /// impl Iterator for $forward_iterator
903 /// { /* internal ends up calling Searcher::next_match() */ }
904 ///
905 /// impl DoubleEndedIterator for $forward_iterator
906 ///       where P::Searcher: DoubleEndedSearcher
907 /// { /* internal ends up calling Searcher::next_match_back() */ }
908 ///
909 /// impl Iterator for $reverse_iterator
910 ///       where P::Searcher: ReverseSearcher
911 /// { /* internal ends up calling Searcher::next_match_back() */ }
912 ///
913 /// impl DoubleEndedIterator for $reverse_iterator
914 ///       where P::Searcher: DoubleEndedSearcher
915 /// { /* internal ends up calling Searcher::next_match() */ }
916 ///
917 /// The internal one is defined outside the macro, and has almost the same
918 /// semantic as a DoubleEndedIterator by delegating to `pattern::Searcher` and
919 /// `pattern::ReverseSearcher` for both forward and reverse iteration.
920 ///
921 /// "Almost", because a `Searcher` and a `ReverseSearcher` for a given
922 /// `Pattern` might not return the same elements, so actually implementing
923 /// `DoubleEndedIterator` for it would be incorrect.
924 /// (See the docs in `str::pattern` for more details)
925 ///
926 /// However, the internal struct still represents a single ended iterator from
927 /// either end, and depending on pattern is also a valid double ended iterator,
928 /// so the two wrapper structs implement `Iterator`
929 /// and `DoubleEndedIterator` depending on the concrete pattern type, leading
930 /// to the complex impls seen above.
931 macro_rules! generate_pattern_iterators {
932     {
933         // Forward iterator
934         forward:
935             $(#[$forward_iterator_attribute:meta])*
936             struct $forward_iterator:ident;
937
938         // Reverse iterator
939         reverse:
940             $(#[$reverse_iterator_attribute:meta])*
941             struct $reverse_iterator:ident;
942
943         // Stability of all generated items
944         stability:
945             $(#[$common_stability_attribute:meta])*
946
947         // Internal almost-iterator that is being delegated to
948         internal:
949             $internal_iterator:ident yielding ($iterty:ty);
950
951         // Kind of delegation - either single ended or double ended
952         delegate $($t:tt)*
953     } => {
954         $(#[$forward_iterator_attribute])*
955         $(#[$common_stability_attribute])*
956         pub struct $forward_iterator<'a, P: Pattern<'a>>($internal_iterator<'a, P>);
957
958         $(#[$common_stability_attribute])*
959         impl<'a, P> fmt::Debug for $forward_iterator<'a, P>
960         where
961             P: Pattern<'a, Searcher: fmt::Debug>,
962         {
963             fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
964                 f.debug_tuple(stringify!($forward_iterator))
965                     .field(&self.0)
966                     .finish()
967             }
968         }
969
970         $(#[$common_stability_attribute])*
971         impl<'a, P: Pattern<'a>> Iterator for $forward_iterator<'a, P> {
972             type Item = $iterty;
973
974             #[inline]
975             fn next(&mut self) -> Option<$iterty> {
976                 self.0.next()
977             }
978         }
979
980         $(#[$common_stability_attribute])*
981         impl<'a, P> Clone for $forward_iterator<'a, P>
982         where
983             P: Pattern<'a, Searcher: Clone>,
984         {
985             fn clone(&self) -> Self {
986                 $forward_iterator(self.0.clone())
987             }
988         }
989
990         $(#[$reverse_iterator_attribute])*
991         $(#[$common_stability_attribute])*
992         pub struct $reverse_iterator<'a, P: Pattern<'a>>($internal_iterator<'a, P>);
993
994         $(#[$common_stability_attribute])*
995         impl<'a, P> fmt::Debug for $reverse_iterator<'a, P>
996         where
997             P: Pattern<'a, Searcher: fmt::Debug>,
998         {
999             fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1000                 f.debug_tuple(stringify!($reverse_iterator))
1001                     .field(&self.0)
1002                     .finish()
1003             }
1004         }
1005
1006         $(#[$common_stability_attribute])*
1007         impl<'a, P> Iterator for $reverse_iterator<'a, P>
1008         where
1009             P: Pattern<'a, Searcher: ReverseSearcher<'a>>,
1010         {
1011             type Item = $iterty;
1012
1013             #[inline]
1014             fn next(&mut self) -> Option<$iterty> {
1015                 self.0.next_back()
1016             }
1017         }
1018
1019         $(#[$common_stability_attribute])*
1020         impl<'a, P> Clone for $reverse_iterator<'a, P>
1021         where
1022             P: Pattern<'a, Searcher: Clone>,
1023         {
1024             fn clone(&self) -> Self {
1025                 $reverse_iterator(self.0.clone())
1026             }
1027         }
1028
1029         #[stable(feature = "fused", since = "1.26.0")]
1030         impl<'a, P: Pattern<'a>> FusedIterator for $forward_iterator<'a, P> {}
1031
1032         #[stable(feature = "fused", since = "1.26.0")]
1033         impl<'a, P> FusedIterator for $reverse_iterator<'a, P>
1034         where
1035             P: Pattern<'a, Searcher: ReverseSearcher<'a>>,
1036         {}
1037
1038         generate_pattern_iterators!($($t)* with $(#[$common_stability_attribute])*,
1039                                                 $forward_iterator,
1040                                                 $reverse_iterator, $iterty);
1041     };
1042     {
1043         double ended; with $(#[$common_stability_attribute:meta])*,
1044                            $forward_iterator:ident,
1045                            $reverse_iterator:ident, $iterty:ty
1046     } => {
1047         $(#[$common_stability_attribute])*
1048         impl<'a, P> DoubleEndedIterator for $forward_iterator<'a, P>
1049         where
1050             P: Pattern<'a, Searcher: DoubleEndedSearcher<'a>>,
1051         {
1052             #[inline]
1053             fn next_back(&mut self) -> Option<$iterty> {
1054                 self.0.next_back()
1055             }
1056         }
1057
1058         $(#[$common_stability_attribute])*
1059         impl<'a, P> DoubleEndedIterator for $reverse_iterator<'a, P>
1060         where
1061             P: Pattern<'a, Searcher: DoubleEndedSearcher<'a>>,
1062         {
1063             #[inline]
1064             fn next_back(&mut self) -> Option<$iterty> {
1065                 self.0.next()
1066             }
1067         }
1068     };
1069     {
1070         single ended; with $(#[$common_stability_attribute:meta])*,
1071                            $forward_iterator:ident,
1072                            $reverse_iterator:ident, $iterty:ty
1073     } => {}
1074 }
1075
1076 derive_pattern_clone! {
1077     clone SplitInternal
1078     with |s| SplitInternal { matcher: s.matcher.clone(), ..*s }
1079 }
1080
1081 struct SplitInternal<'a, P: Pattern<'a>> {
1082     start: usize,
1083     end: usize,
1084     matcher: P::Searcher,
1085     allow_trailing_empty: bool,
1086     finished: bool,
1087 }
1088
1089 impl<'a, P> fmt::Debug for SplitInternal<'a, P>
1090 where
1091     P: Pattern<'a, Searcher: fmt::Debug>,
1092 {
1093     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1094         f.debug_struct("SplitInternal")
1095             .field("start", &self.start)
1096             .field("end", &self.end)
1097             .field("matcher", &self.matcher)
1098             .field("allow_trailing_empty", &self.allow_trailing_empty)
1099             .field("finished", &self.finished)
1100             .finish()
1101     }
1102 }
1103
1104 impl<'a, P: Pattern<'a>> SplitInternal<'a, P> {
1105     #[inline]
1106     fn get_end(&mut self) -> Option<&'a str> {
1107         if !self.finished && (self.allow_trailing_empty || self.end - self.start > 0) {
1108             self.finished = true;
1109             // SAFETY: `self.start` and `self.end` always lie on unicode boundaries.
1110             unsafe {
1111                 let string = self.matcher.haystack().get_unchecked(self.start..self.end);
1112                 Some(string)
1113             }
1114         } else {
1115             None
1116         }
1117     }
1118
1119     #[inline]
1120     fn next(&mut self) -> Option<&'a str> {
1121         if self.finished {
1122             return None;
1123         }
1124
1125         let haystack = self.matcher.haystack();
1126         match self.matcher.next_match() {
1127             // SAFETY: `Searcher` guarantees that `a` and `b` lie on unicode boundaries.
1128             Some((a, b)) => unsafe {
1129                 let elt = haystack.get_unchecked(self.start..a);
1130                 self.start = b;
1131                 Some(elt)
1132             },
1133             None => self.get_end(),
1134         }
1135     }
1136
1137     #[inline]
1138     fn next_inclusive(&mut self) -> Option<&'a str> {
1139         if self.finished {
1140             return None;
1141         }
1142
1143         let haystack = self.matcher.haystack();
1144         match self.matcher.next_match() {
1145             // SAFETY: `Searcher` guarantees that `b` lies on unicode boundary,
1146             // and self.start is either the start of the original string,
1147             // or `b` was assigned to it, so it also lies on unicode boundary.
1148             Some((_, b)) => unsafe {
1149                 let elt = haystack.get_unchecked(self.start..b);
1150                 self.start = b;
1151                 Some(elt)
1152             },
1153             None => self.get_end(),
1154         }
1155     }
1156
1157     #[inline]
1158     fn next_back(&mut self) -> Option<&'a str>
1159     where
1160         P::Searcher: ReverseSearcher<'a>,
1161     {
1162         if self.finished {
1163             return None;
1164         }
1165
1166         if !self.allow_trailing_empty {
1167             self.allow_trailing_empty = true;
1168             match self.next_back() {
1169                 Some(elt) if !elt.is_empty() => return Some(elt),
1170                 _ => {
1171                     if self.finished {
1172                         return None;
1173                     }
1174                 }
1175             }
1176         }
1177
1178         let haystack = self.matcher.haystack();
1179         match self.matcher.next_match_back() {
1180             // SAFETY: `Searcher` guarantees that `a` and `b` lie on unicode boundaries.
1181             Some((a, b)) => unsafe {
1182                 let elt = haystack.get_unchecked(b..self.end);
1183                 self.end = a;
1184                 Some(elt)
1185             },
1186             // SAFETY: `self.start` and `self.end` always lie on unicode boundaries.
1187             None => unsafe {
1188                 self.finished = true;
1189                 Some(haystack.get_unchecked(self.start..self.end))
1190             },
1191         }
1192     }
1193
1194     #[inline]
1195     fn next_back_inclusive(&mut self) -> Option<&'a str>
1196     where
1197         P::Searcher: ReverseSearcher<'a>,
1198     {
1199         if self.finished {
1200             return None;
1201         }
1202
1203         if !self.allow_trailing_empty {
1204             self.allow_trailing_empty = true;
1205             match self.next_back_inclusive() {
1206                 Some(elt) if !elt.is_empty() => return Some(elt),
1207                 _ => {
1208                     if self.finished {
1209                         return None;
1210                     }
1211                 }
1212             }
1213         }
1214
1215         let haystack = self.matcher.haystack();
1216         match self.matcher.next_match_back() {
1217             // SAFETY: `Searcher` guarantees that `b` lies on unicode boundary,
1218             // and self.end is either the end of the original string,
1219             // or `b` was assigned to it, so it also lies on unicode boundary.
1220             Some((_, b)) => unsafe {
1221                 let elt = haystack.get_unchecked(b..self.end);
1222                 self.end = b;
1223                 Some(elt)
1224             },
1225             // SAFETY: self.start is either the start of the original string,
1226             // or start of a substring that represents the part of the string that hasn't
1227             // iterated yet. Either way, it is guaranteed to lie on unicode boundary.
1228             // self.end is either the end of the original string,
1229             // or `b` was assigned to it, so it also lies on unicode boundary.
1230             None => unsafe {
1231                 self.finished = true;
1232                 Some(haystack.get_unchecked(self.start..self.end))
1233             },
1234         }
1235     }
1236 }
1237
1238 generate_pattern_iterators! {
1239     forward:
1240         /// Created with the method [`split`].
1241         ///
1242         /// [`split`]: ../../std/primitive.str.html#method.split
1243         struct Split;
1244     reverse:
1245         /// Created with the method [`rsplit`].
1246         ///
1247         /// [`rsplit`]: ../../std/primitive.str.html#method.rsplit
1248         struct RSplit;
1249     stability:
1250         #[stable(feature = "rust1", since = "1.0.0")]
1251     internal:
1252         SplitInternal yielding (&'a str);
1253     delegate double ended;
1254 }
1255
1256 generate_pattern_iterators! {
1257     forward:
1258         /// Created with the method [`split_terminator`].
1259         ///
1260         /// [`split_terminator`]: ../../std/primitive.str.html#method.split_terminator
1261         struct SplitTerminator;
1262     reverse:
1263         /// Created with the method [`rsplit_terminator`].
1264         ///
1265         /// [`rsplit_terminator`]: ../../std/primitive.str.html#method.rsplit_terminator
1266         struct RSplitTerminator;
1267     stability:
1268         #[stable(feature = "rust1", since = "1.0.0")]
1269     internal:
1270         SplitInternal yielding (&'a str);
1271     delegate double ended;
1272 }
1273
1274 derive_pattern_clone! {
1275     clone SplitNInternal
1276     with |s| SplitNInternal { iter: s.iter.clone(), ..*s }
1277 }
1278
1279 struct SplitNInternal<'a, P: Pattern<'a>> {
1280     iter: SplitInternal<'a, P>,
1281     /// The number of splits remaining
1282     count: usize,
1283 }
1284
1285 impl<'a, P> fmt::Debug for SplitNInternal<'a, P>
1286 where
1287     P: Pattern<'a, Searcher: fmt::Debug>,
1288 {
1289     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1290         f.debug_struct("SplitNInternal")
1291             .field("iter", &self.iter)
1292             .field("count", &self.count)
1293             .finish()
1294     }
1295 }
1296
1297 impl<'a, P: Pattern<'a>> SplitNInternal<'a, P> {
1298     #[inline]
1299     fn next(&mut self) -> Option<&'a str> {
1300         match self.count {
1301             0 => None,
1302             1 => {
1303                 self.count = 0;
1304                 self.iter.get_end()
1305             }
1306             _ => {
1307                 self.count -= 1;
1308                 self.iter.next()
1309             }
1310         }
1311     }
1312
1313     #[inline]
1314     fn next_back(&mut self) -> Option<&'a str>
1315     where
1316         P::Searcher: ReverseSearcher<'a>,
1317     {
1318         match self.count {
1319             0 => None,
1320             1 => {
1321                 self.count = 0;
1322                 self.iter.get_end()
1323             }
1324             _ => {
1325                 self.count -= 1;
1326                 self.iter.next_back()
1327             }
1328         }
1329     }
1330 }
1331
1332 generate_pattern_iterators! {
1333     forward:
1334         /// Created with the method [`splitn`].
1335         ///
1336         /// [`splitn`]: ../../std/primitive.str.html#method.splitn
1337         struct SplitN;
1338     reverse:
1339         /// Created with the method [`rsplitn`].
1340         ///
1341         /// [`rsplitn`]: ../../std/primitive.str.html#method.rsplitn
1342         struct RSplitN;
1343     stability:
1344         #[stable(feature = "rust1", since = "1.0.0")]
1345     internal:
1346         SplitNInternal yielding (&'a str);
1347     delegate single ended;
1348 }
1349
1350 derive_pattern_clone! {
1351     clone MatchIndicesInternal
1352     with |s| MatchIndicesInternal(s.0.clone())
1353 }
1354
1355 struct MatchIndicesInternal<'a, P: Pattern<'a>>(P::Searcher);
1356
1357 impl<'a, P> fmt::Debug for MatchIndicesInternal<'a, P>
1358 where
1359     P: Pattern<'a, Searcher: fmt::Debug>,
1360 {
1361     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1362         f.debug_tuple("MatchIndicesInternal").field(&self.0).finish()
1363     }
1364 }
1365
1366 impl<'a, P: Pattern<'a>> MatchIndicesInternal<'a, P> {
1367     #[inline]
1368     fn next(&mut self) -> Option<(usize, &'a str)> {
1369         self.0
1370             .next_match()
1371             // SAFETY: `Searcher` guarantees that `start` and `end` lie on unicode boundaries.
1372             .map(|(start, end)| unsafe { (start, self.0.haystack().get_unchecked(start..end)) })
1373     }
1374
1375     #[inline]
1376     fn next_back(&mut self) -> Option<(usize, &'a str)>
1377     where
1378         P::Searcher: ReverseSearcher<'a>,
1379     {
1380         self.0
1381             .next_match_back()
1382             // SAFETY: `Searcher` guarantees that `start` and `end` lie on unicode boundaries.
1383             .map(|(start, end)| unsafe { (start, self.0.haystack().get_unchecked(start..end)) })
1384     }
1385 }
1386
1387 generate_pattern_iterators! {
1388     forward:
1389         /// Created with the method [`match_indices`].
1390         ///
1391         /// [`match_indices`]: ../../std/primitive.str.html#method.match_indices
1392         struct MatchIndices;
1393     reverse:
1394         /// Created with the method [`rmatch_indices`].
1395         ///
1396         /// [`rmatch_indices`]: ../../std/primitive.str.html#method.rmatch_indices
1397         struct RMatchIndices;
1398     stability:
1399         #[stable(feature = "str_match_indices", since = "1.5.0")]
1400     internal:
1401         MatchIndicesInternal yielding ((usize, &'a str));
1402     delegate double ended;
1403 }
1404
1405 derive_pattern_clone! {
1406     clone MatchesInternal
1407     with |s| MatchesInternal(s.0.clone())
1408 }
1409
1410 struct MatchesInternal<'a, P: Pattern<'a>>(P::Searcher);
1411
1412 impl<'a, P> fmt::Debug for MatchesInternal<'a, P>
1413 where
1414     P: Pattern<'a, Searcher: fmt::Debug>,
1415 {
1416     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1417         f.debug_tuple("MatchesInternal").field(&self.0).finish()
1418     }
1419 }
1420
1421 impl<'a, P: Pattern<'a>> MatchesInternal<'a, P> {
1422     #[inline]
1423     fn next(&mut self) -> Option<&'a str> {
1424         // SAFETY: `Searcher` guarantees that `start` and `end` lie on unicode boundaries.
1425         self.0.next_match().map(|(a, b)| unsafe {
1426             // Indices are known to be on utf8 boundaries
1427             self.0.haystack().get_unchecked(a..b)
1428         })
1429     }
1430
1431     #[inline]
1432     fn next_back(&mut self) -> Option<&'a str>
1433     where
1434         P::Searcher: ReverseSearcher<'a>,
1435     {
1436         // SAFETY: `Searcher` guarantees that `start` and `end` lie on unicode boundaries.
1437         self.0.next_match_back().map(|(a, b)| unsafe {
1438             // Indices are known to be on utf8 boundaries
1439             self.0.haystack().get_unchecked(a..b)
1440         })
1441     }
1442 }
1443
1444 generate_pattern_iterators! {
1445     forward:
1446         /// Created with the method [`matches`].
1447         ///
1448         /// [`matches`]: ../../std/primitive.str.html#method.matches
1449         struct Matches;
1450     reverse:
1451         /// Created with the method [`rmatches`].
1452         ///
1453         /// [`rmatches`]: ../../std/primitive.str.html#method.rmatches
1454         struct RMatches;
1455     stability:
1456         #[stable(feature = "str_matches", since = "1.2.0")]
1457     internal:
1458         MatchesInternal yielding (&'a str);
1459     delegate double ended;
1460 }
1461
1462 /// An iterator over the lines of a string, as string slices.
1463 ///
1464 /// This struct is created with the [`lines`] method on [`str`].
1465 /// See its documentation for more.
1466 ///
1467 /// [`lines`]: ../../std/primitive.str.html#method.lines
1468 /// [`str`]: ../../std/primitive.str.html
1469 #[stable(feature = "rust1", since = "1.0.0")]
1470 #[derive(Clone, Debug)]
1471 pub struct Lines<'a>(Map<SplitTerminator<'a, char>, LinesAnyMap>);
1472
1473 #[stable(feature = "rust1", since = "1.0.0")]
1474 impl<'a> Iterator for Lines<'a> {
1475     type Item = &'a str;
1476
1477     #[inline]
1478     fn next(&mut self) -> Option<&'a str> {
1479         self.0.next()
1480     }
1481
1482     #[inline]
1483     fn size_hint(&self) -> (usize, Option<usize>) {
1484         self.0.size_hint()
1485     }
1486
1487     #[inline]
1488     fn last(mut self) -> Option<&'a str> {
1489         self.next_back()
1490     }
1491 }
1492
1493 #[stable(feature = "rust1", since = "1.0.0")]
1494 impl<'a> DoubleEndedIterator for Lines<'a> {
1495     #[inline]
1496     fn next_back(&mut self) -> Option<&'a str> {
1497         self.0.next_back()
1498     }
1499 }
1500
1501 #[stable(feature = "fused", since = "1.26.0")]
1502 impl FusedIterator for Lines<'_> {}
1503
1504 /// Created with the method [`lines_any`].
1505 ///
1506 /// [`lines_any`]: ../../std/primitive.str.html#method.lines_any
1507 #[stable(feature = "rust1", since = "1.0.0")]
1508 #[rustc_deprecated(since = "1.4.0", reason = "use lines()/Lines instead now")]
1509 #[derive(Clone, Debug)]
1510 #[allow(deprecated)]
1511 pub struct LinesAny<'a>(Lines<'a>);
1512
1513 impl_fn_for_zst! {
1514     /// A nameable, cloneable fn type
1515     #[derive(Clone)]
1516     struct LinesAnyMap impl<'a> Fn = |line: &'a str| -> &'a str {
1517         let l = line.len();
1518         if l > 0 && line.as_bytes()[l - 1] == b'\r' { &line[0 .. l - 1] }
1519         else { line }
1520     };
1521 }
1522
1523 #[stable(feature = "rust1", since = "1.0.0")]
1524 #[allow(deprecated)]
1525 impl<'a> Iterator for LinesAny<'a> {
1526     type Item = &'a str;
1527
1528     #[inline]
1529     fn next(&mut self) -> Option<&'a str> {
1530         self.0.next()
1531     }
1532
1533     #[inline]
1534     fn size_hint(&self) -> (usize, Option<usize>) {
1535         self.0.size_hint()
1536     }
1537 }
1538
1539 #[stable(feature = "rust1", since = "1.0.0")]
1540 #[allow(deprecated)]
1541 impl<'a> DoubleEndedIterator for LinesAny<'a> {
1542     #[inline]
1543     fn next_back(&mut self) -> Option<&'a str> {
1544         self.0.next_back()
1545     }
1546 }
1547
1548 #[stable(feature = "fused", since = "1.26.0")]
1549 #[allow(deprecated)]
1550 impl FusedIterator for LinesAny<'_> {}
1551
1552 /*
1553 Section: UTF-8 validation
1554 */
1555
1556 // use truncation to fit u64 into usize
1557 const NONASCII_MASK: usize = 0x80808080_80808080u64 as usize;
1558
1559 /// Returns `true` if any byte in the word `x` is nonascii (>= 128).
1560 #[inline]
1561 fn contains_nonascii(x: usize) -> bool {
1562     (x & NONASCII_MASK) != 0
1563 }
1564
1565 /// Walks through `v` checking that it's a valid UTF-8 sequence,
1566 /// returning `Ok(())` in that case, or, if it is invalid, `Err(err)`.
1567 #[inline(always)]
1568 fn run_utf8_validation(v: &[u8]) -> Result<(), Utf8Error> {
1569     let mut index = 0;
1570     let len = v.len();
1571
1572     let usize_bytes = mem::size_of::<usize>();
1573     let ascii_block_size = 2 * usize_bytes;
1574     let blocks_end = if len >= ascii_block_size { len - ascii_block_size + 1 } else { 0 };
1575     let align = v.as_ptr().align_offset(usize_bytes);
1576
1577     while index < len {
1578         let old_offset = index;
1579         macro_rules! err {
1580             ($error_len: expr) => {
1581                 return Err(Utf8Error { valid_up_to: old_offset, error_len: $error_len });
1582             };
1583         }
1584
1585         macro_rules! next {
1586             () => {{
1587                 index += 1;
1588                 // we needed data, but there was none: error!
1589                 if index >= len {
1590                     err!(None)
1591                 }
1592                 v[index]
1593             }};
1594         }
1595
1596         let first = v[index];
1597         if first >= 128 {
1598             let w = UTF8_CHAR_WIDTH[first as usize];
1599             // 2-byte encoding is for codepoints  \u{0080} to  \u{07ff}
1600             //        first  C2 80        last DF BF
1601             // 3-byte encoding is for codepoints  \u{0800} to  \u{ffff}
1602             //        first  E0 A0 80     last EF BF BF
1603             //   excluding surrogates codepoints  \u{d800} to  \u{dfff}
1604             //               ED A0 80 to       ED BF BF
1605             // 4-byte encoding is for codepoints \u{1000}0 to \u{10ff}ff
1606             //        first  F0 90 80 80  last F4 8F BF BF
1607             //
1608             // Use the UTF-8 syntax from the RFC
1609             //
1610             // https://tools.ietf.org/html/rfc3629
1611             // UTF8-1      = %x00-7F
1612             // UTF8-2      = %xC2-DF UTF8-tail
1613             // UTF8-3      = %xE0 %xA0-BF UTF8-tail / %xE1-EC 2( UTF8-tail ) /
1614             //               %xED %x80-9F UTF8-tail / %xEE-EF 2( UTF8-tail )
1615             // UTF8-4      = %xF0 %x90-BF 2( UTF8-tail ) / %xF1-F3 3( UTF8-tail ) /
1616             //               %xF4 %x80-8F 2( UTF8-tail )
1617             match w {
1618                 2 => {
1619                     if next!() & !CONT_MASK != TAG_CONT_U8 {
1620                         err!(Some(1))
1621                     }
1622                 }
1623                 3 => {
1624                     match (first, next!()) {
1625                         (0xE0, 0xA0..=0xBF)
1626                         | (0xE1..=0xEC, 0x80..=0xBF)
1627                         | (0xED, 0x80..=0x9F)
1628                         | (0xEE..=0xEF, 0x80..=0xBF) => {}
1629                         _ => err!(Some(1)),
1630                     }
1631                     if next!() & !CONT_MASK != TAG_CONT_U8 {
1632                         err!(Some(2))
1633                     }
1634                 }
1635                 4 => {
1636                     match (first, next!()) {
1637                         (0xF0, 0x90..=0xBF) | (0xF1..=0xF3, 0x80..=0xBF) | (0xF4, 0x80..=0x8F) => {}
1638                         _ => err!(Some(1)),
1639                     }
1640                     if next!() & !CONT_MASK != TAG_CONT_U8 {
1641                         err!(Some(2))
1642                     }
1643                     if next!() & !CONT_MASK != TAG_CONT_U8 {
1644                         err!(Some(3))
1645                     }
1646                 }
1647                 _ => err!(Some(1)),
1648             }
1649             index += 1;
1650         } else {
1651             // Ascii case, try to skip forward quickly.
1652             // When the pointer is aligned, read 2 words of data per iteration
1653             // until we find a word containing a non-ascii byte.
1654             if align != usize::max_value() && align.wrapping_sub(index) % usize_bytes == 0 {
1655                 let ptr = v.as_ptr();
1656                 while index < blocks_end {
1657                     // SAFETY: since `align - index` and `ascii_block_size` are
1658                     // multiples of `usize_bytes`, `block = ptr.add(index)` is
1659                     // always aligned with a `usize` so it's safe to dereference
1660                     // both `block` and `block.offset(1)`.
1661                     unsafe {
1662                         let block = ptr.add(index) as *const usize;
1663                         // break if there is a nonascii byte
1664                         let zu = contains_nonascii(*block);
1665                         let zv = contains_nonascii(*block.offset(1));
1666                         if zu | zv {
1667                             break;
1668                         }
1669                     }
1670                     index += ascii_block_size;
1671                 }
1672                 // step from the point where the wordwise loop stopped
1673                 while index < len && v[index] < 128 {
1674                     index += 1;
1675                 }
1676             } else {
1677                 index += 1;
1678             }
1679         }
1680     }
1681
1682     Ok(())
1683 }
1684
1685 // https://tools.ietf.org/html/rfc3629
1686 static UTF8_CHAR_WIDTH: [u8; 256] = [
1687     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1688     1, // 0x1F
1689     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1690     1, // 0x3F
1691     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1692     1, // 0x5F
1693     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1694     1, // 0x7F
1695     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1696     0, // 0x9F
1697     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1698     0, // 0xBF
1699     0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
1700     2, // 0xDF
1701     3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 0xEF
1702     4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0xFF
1703 ];
1704
1705 /// Given a first byte, determines how many bytes are in this UTF-8 character.
1706 #[unstable(feature = "str_internals", issue = "none")]
1707 #[inline]
1708 pub fn utf8_char_width(b: u8) -> usize {
1709     UTF8_CHAR_WIDTH[b as usize] as usize
1710 }
1711
1712 /// Mask of the value bits of a continuation byte.
1713 const CONT_MASK: u8 = 0b0011_1111;
1714 /// Value of the tag bits (tag mask is !CONT_MASK) of a continuation byte.
1715 const TAG_CONT_U8: u8 = 0b1000_0000;
1716
1717 /*
1718 Section: Trait implementations
1719 */
1720
1721 mod traits {
1722     use crate::cmp::Ordering;
1723     use crate::ops;
1724     use crate::slice::{self, SliceIndex};
1725
1726     /// Implements ordering of strings.
1727     ///
1728     /// Strings are ordered  lexicographically by their byte values. This orders Unicode code
1729     /// points based on their positions in the code charts. This is not necessarily the same as
1730     /// "alphabetical" order, which varies by language and locale. Sorting strings according to
1731     /// culturally-accepted standards requires locale-specific data that is outside the scope of
1732     /// the `str` type.
1733     #[stable(feature = "rust1", since = "1.0.0")]
1734     impl Ord for str {
1735         #[inline]
1736         fn cmp(&self, other: &str) -> Ordering {
1737             self.as_bytes().cmp(other.as_bytes())
1738         }
1739     }
1740
1741     #[stable(feature = "rust1", since = "1.0.0")]
1742     impl PartialEq for str {
1743         #[inline]
1744         fn eq(&self, other: &str) -> bool {
1745             self.as_bytes() == other.as_bytes()
1746         }
1747         #[inline]
1748         fn ne(&self, other: &str) -> bool {
1749             !(*self).eq(other)
1750         }
1751     }
1752
1753     #[stable(feature = "rust1", since = "1.0.0")]
1754     impl Eq for str {}
1755
1756     /// Implements comparison operations on strings.
1757     ///
1758     /// Strings are compared lexicographically by their byte values. This compares Unicode code
1759     /// points based on their positions in the code charts. This is not necessarily the same as
1760     /// "alphabetical" order, which varies by language and locale. Comparing strings according to
1761     /// culturally-accepted standards requires locale-specific data that is outside the scope of
1762     /// the `str` type.
1763     #[stable(feature = "rust1", since = "1.0.0")]
1764     impl PartialOrd for str {
1765         #[inline]
1766         fn partial_cmp(&self, other: &str) -> Option<Ordering> {
1767             Some(self.cmp(other))
1768         }
1769     }
1770
1771     #[stable(feature = "rust1", since = "1.0.0")]
1772     impl<I> ops::Index<I> for str
1773     where
1774         I: SliceIndex<str>,
1775     {
1776         type Output = I::Output;
1777
1778         #[inline]
1779         fn index(&self, index: I) -> &I::Output {
1780             index.index(self)
1781         }
1782     }
1783
1784     #[stable(feature = "rust1", since = "1.0.0")]
1785     impl<I> ops::IndexMut<I> for str
1786     where
1787         I: SliceIndex<str>,
1788     {
1789         #[inline]
1790         fn index_mut(&mut self, index: I) -> &mut I::Output {
1791             index.index_mut(self)
1792         }
1793     }
1794
1795     #[inline(never)]
1796     #[cold]
1797     fn str_index_overflow_fail() -> ! {
1798         panic!("attempted to index str up to maximum usize");
1799     }
1800
1801     /// Implements substring slicing with syntax `&self[..]` or `&mut self[..]`.
1802     ///
1803     /// Returns a slice of the whole string, i.e., returns `&self` or `&mut
1804     /// self`. Equivalent to `&self[0 .. len]` or `&mut self[0 .. len]`. Unlike
1805     /// other indexing operations, this can never panic.
1806     ///
1807     /// This operation is `O(1)`.
1808     ///
1809     /// Prior to 1.20.0, these indexing operations were still supported by
1810     /// direct implementation of `Index` and `IndexMut`.
1811     ///
1812     /// Equivalent to `&self[0 .. len]` or `&mut self[0 .. len]`.
1813     #[stable(feature = "str_checked_slicing", since = "1.20.0")]
1814     impl SliceIndex<str> for ops::RangeFull {
1815         type Output = str;
1816         #[inline]
1817         fn get(self, slice: &str) -> Option<&Self::Output> {
1818             Some(slice)
1819         }
1820         #[inline]
1821         fn get_mut(self, slice: &mut str) -> Option<&mut Self::Output> {
1822             Some(slice)
1823         }
1824         #[inline]
1825         unsafe fn get_unchecked(self, slice: &str) -> &Self::Output {
1826             slice
1827         }
1828         #[inline]
1829         unsafe fn get_unchecked_mut(self, slice: &mut str) -> &mut Self::Output {
1830             slice
1831         }
1832         #[inline]
1833         fn index(self, slice: &str) -> &Self::Output {
1834             slice
1835         }
1836         #[inline]
1837         fn index_mut(self, slice: &mut str) -> &mut Self::Output {
1838             slice
1839         }
1840     }
1841
1842     /// Implements substring slicing with syntax `&self[begin .. end]` or `&mut
1843     /// self[begin .. end]`.
1844     ///
1845     /// Returns a slice of the given string from the byte range
1846     /// [`begin`, `end`).
1847     ///
1848     /// This operation is `O(1)`.
1849     ///
1850     /// Prior to 1.20.0, these indexing operations were still supported by
1851     /// direct implementation of `Index` and `IndexMut`.
1852     ///
1853     /// # Panics
1854     ///
1855     /// Panics if `begin` or `end` does not point to the starting byte offset of
1856     /// a character (as defined by `is_char_boundary`), if `begin > end`, or if
1857     /// `end > len`.
1858     ///
1859     /// # Examples
1860     ///
1861     /// ```
1862     /// let s = "Löwe 老虎 Léopard";
1863     /// assert_eq!(&s[0 .. 1], "L");
1864     ///
1865     /// assert_eq!(&s[1 .. 9], "öwe 老");
1866     ///
1867     /// // these will panic:
1868     /// // byte 2 lies within `ö`:
1869     /// // &s[2 ..3];
1870     ///
1871     /// // byte 8 lies within `老`
1872     /// // &s[1 .. 8];
1873     ///
1874     /// // byte 100 is outside the string
1875     /// // &s[3 .. 100];
1876     /// ```
1877     #[stable(feature = "str_checked_slicing", since = "1.20.0")]
1878     impl SliceIndex<str> for ops::Range<usize> {
1879         type Output = str;
1880         #[inline]
1881         fn get(self, slice: &str) -> Option<&Self::Output> {
1882             if self.start <= self.end
1883                 && slice.is_char_boundary(self.start)
1884                 && slice.is_char_boundary(self.end)
1885             {
1886                 // SAFETY: just checked that `start` and `end` are on a char boundary.
1887                 Some(unsafe { self.get_unchecked(slice) })
1888             } else {
1889                 None
1890             }
1891         }
1892         #[inline]
1893         fn get_mut(self, slice: &mut str) -> Option<&mut Self::Output> {
1894             if self.start <= self.end
1895                 && slice.is_char_boundary(self.start)
1896                 && slice.is_char_boundary(self.end)
1897             {
1898                 // SAFETY: just checked that `start` and `end` are on a char boundary.
1899                 Some(unsafe { self.get_unchecked_mut(slice) })
1900             } else {
1901                 None
1902             }
1903         }
1904         #[inline]
1905         unsafe fn get_unchecked(self, slice: &str) -> &Self::Output {
1906             let ptr = slice.as_ptr().add(self.start);
1907             let len = self.end - self.start;
1908             super::from_utf8_unchecked(slice::from_raw_parts(ptr, len))
1909         }
1910         #[inline]
1911         unsafe fn get_unchecked_mut(self, slice: &mut str) -> &mut Self::Output {
1912             let ptr = slice.as_mut_ptr().add(self.start);
1913             let len = self.end - self.start;
1914             super::from_utf8_unchecked_mut(slice::from_raw_parts_mut(ptr, len))
1915         }
1916         #[inline]
1917         fn index(self, slice: &str) -> &Self::Output {
1918             let (start, end) = (self.start, self.end);
1919             self.get(slice).unwrap_or_else(|| super::slice_error_fail(slice, start, end))
1920         }
1921         #[inline]
1922         fn index_mut(self, slice: &mut str) -> &mut Self::Output {
1923             // is_char_boundary checks that the index is in [0, .len()]
1924             // cannot reuse `get` as above, because of NLL trouble
1925             if self.start <= self.end
1926                 && slice.is_char_boundary(self.start)
1927                 && slice.is_char_boundary(self.end)
1928             {
1929                 // SAFETY: just checked that `start` and `end` are on a char boundary.
1930                 unsafe { self.get_unchecked_mut(slice) }
1931             } else {
1932                 super::slice_error_fail(slice, self.start, self.end)
1933             }
1934         }
1935     }
1936
1937     /// Implements substring slicing with syntax `&self[.. end]` or `&mut
1938     /// self[.. end]`.
1939     ///
1940     /// Returns a slice of the given string from the byte range [`0`, `end`).
1941     /// Equivalent to `&self[0 .. end]` or `&mut self[0 .. end]`.
1942     ///
1943     /// This operation is `O(1)`.
1944     ///
1945     /// Prior to 1.20.0, these indexing operations were still supported by
1946     /// direct implementation of `Index` and `IndexMut`.
1947     ///
1948     /// # Panics
1949     ///
1950     /// Panics if `end` does not point to the starting byte offset of a
1951     /// character (as defined by `is_char_boundary`), or if `end > len`.
1952     #[stable(feature = "str_checked_slicing", since = "1.20.0")]
1953     impl SliceIndex<str> for ops::RangeTo<usize> {
1954         type Output = str;
1955         #[inline]
1956         fn get(self, slice: &str) -> Option<&Self::Output> {
1957             if slice.is_char_boundary(self.end) {
1958                 // SAFETY: just checked that `end` is on a char boundary.
1959                 Some(unsafe { self.get_unchecked(slice) })
1960             } else {
1961                 None
1962             }
1963         }
1964         #[inline]
1965         fn get_mut(self, slice: &mut str) -> Option<&mut Self::Output> {
1966             if slice.is_char_boundary(self.end) {
1967                 // SAFETY: just checked that `end` is on a char boundary.
1968                 Some(unsafe { self.get_unchecked_mut(slice) })
1969             } else {
1970                 None
1971             }
1972         }
1973         #[inline]
1974         unsafe fn get_unchecked(self, slice: &str) -> &Self::Output {
1975             let ptr = slice.as_ptr();
1976             super::from_utf8_unchecked(slice::from_raw_parts(ptr, self.end))
1977         }
1978         #[inline]
1979         unsafe fn get_unchecked_mut(self, slice: &mut str) -> &mut Self::Output {
1980             let ptr = slice.as_mut_ptr();
1981             super::from_utf8_unchecked_mut(slice::from_raw_parts_mut(ptr, self.end))
1982         }
1983         #[inline]
1984         fn index(self, slice: &str) -> &Self::Output {
1985             let end = self.end;
1986             self.get(slice).unwrap_or_else(|| super::slice_error_fail(slice, 0, end))
1987         }
1988         #[inline]
1989         fn index_mut(self, slice: &mut str) -> &mut Self::Output {
1990             if slice.is_char_boundary(self.end) {
1991                 // SAFETY: just checked that `end` is on a char boundary.
1992                 unsafe { self.get_unchecked_mut(slice) }
1993             } else {
1994                 super::slice_error_fail(slice, 0, self.end)
1995             }
1996         }
1997     }
1998
1999     /// Implements substring slicing with syntax `&self[begin ..]` or `&mut
2000     /// self[begin ..]`.
2001     ///
2002     /// Returns a slice of the given string from the byte range [`begin`,
2003     /// `len`). Equivalent to `&self[begin .. len]` or `&mut self[begin ..
2004     /// len]`.
2005     ///
2006     /// This operation is `O(1)`.
2007     ///
2008     /// Prior to 1.20.0, these indexing operations were still supported by
2009     /// direct implementation of `Index` and `IndexMut`.
2010     ///
2011     /// # Panics
2012     ///
2013     /// Panics if `begin` does not point to the starting byte offset of
2014     /// a character (as defined by `is_char_boundary`), or if `begin >= len`.
2015     #[stable(feature = "str_checked_slicing", since = "1.20.0")]
2016     impl SliceIndex<str> for ops::RangeFrom<usize> {
2017         type Output = str;
2018         #[inline]
2019         fn get(self, slice: &str) -> Option<&Self::Output> {
2020             if slice.is_char_boundary(self.start) {
2021                 // SAFETY: just checked that `start` is on a char boundary.
2022                 Some(unsafe { self.get_unchecked(slice) })
2023             } else {
2024                 None
2025             }
2026         }
2027         #[inline]
2028         fn get_mut(self, slice: &mut str) -> Option<&mut Self::Output> {
2029             if slice.is_char_boundary(self.start) {
2030                 // SAFETY: just checked that `start` is on a char boundary.
2031                 Some(unsafe { self.get_unchecked_mut(slice) })
2032             } else {
2033                 None
2034             }
2035         }
2036         #[inline]
2037         unsafe fn get_unchecked(self, slice: &str) -> &Self::Output {
2038             let ptr = slice.as_ptr().add(self.start);
2039             let len = slice.len() - self.start;
2040             super::from_utf8_unchecked(slice::from_raw_parts(ptr, len))
2041         }
2042         #[inline]
2043         unsafe fn get_unchecked_mut(self, slice: &mut str) -> &mut Self::Output {
2044             let ptr = slice.as_mut_ptr().add(self.start);
2045             let len = slice.len() - self.start;
2046             super::from_utf8_unchecked_mut(slice::from_raw_parts_mut(ptr, len))
2047         }
2048         #[inline]
2049         fn index(self, slice: &str) -> &Self::Output {
2050             let (start, end) = (self.start, slice.len());
2051             self.get(slice).unwrap_or_else(|| super::slice_error_fail(slice, start, end))
2052         }
2053         #[inline]
2054         fn index_mut(self, slice: &mut str) -> &mut Self::Output {
2055             if slice.is_char_boundary(self.start) {
2056                 // SAFETY: just checked that `start` is on a char boundary.
2057                 unsafe { self.get_unchecked_mut(slice) }
2058             } else {
2059                 super::slice_error_fail(slice, self.start, slice.len())
2060             }
2061         }
2062     }
2063
2064     /// Implements substring slicing with syntax `&self[begin ..= end]` or `&mut
2065     /// self[begin ..= end]`.
2066     ///
2067     /// Returns a slice of the given string from the byte range
2068     /// [`begin`, `end`]. Equivalent to `&self [begin .. end + 1]` or `&mut
2069     /// self[begin .. end + 1]`, except if `end` has the maximum value for
2070     /// `usize`.
2071     ///
2072     /// This operation is `O(1)`.
2073     ///
2074     /// # Panics
2075     ///
2076     /// Panics if `begin` does not point to the starting byte offset of
2077     /// a character (as defined by `is_char_boundary`), if `end` does not point
2078     /// to the ending byte offset of a character (`end + 1` is either a starting
2079     /// byte offset or equal to `len`), if `begin > end`, or if `end >= len`.
2080     #[stable(feature = "inclusive_range", since = "1.26.0")]
2081     impl SliceIndex<str> for ops::RangeInclusive<usize> {
2082         type Output = str;
2083         #[inline]
2084         fn get(self, slice: &str) -> Option<&Self::Output> {
2085             if *self.end() == usize::max_value() {
2086                 None
2087             } else {
2088                 (*self.start()..self.end() + 1).get(slice)
2089             }
2090         }
2091         #[inline]
2092         fn get_mut(self, slice: &mut str) -> Option<&mut Self::Output> {
2093             if *self.end() == usize::max_value() {
2094                 None
2095             } else {
2096                 (*self.start()..self.end() + 1).get_mut(slice)
2097             }
2098         }
2099         #[inline]
2100         unsafe fn get_unchecked(self, slice: &str) -> &Self::Output {
2101             (*self.start()..self.end() + 1).get_unchecked(slice)
2102         }
2103         #[inline]
2104         unsafe fn get_unchecked_mut(self, slice: &mut str) -> &mut Self::Output {
2105             (*self.start()..self.end() + 1).get_unchecked_mut(slice)
2106         }
2107         #[inline]
2108         fn index(self, slice: &str) -> &Self::Output {
2109             if *self.end() == usize::max_value() {
2110                 str_index_overflow_fail();
2111             }
2112             (*self.start()..self.end() + 1).index(slice)
2113         }
2114         #[inline]
2115         fn index_mut(self, slice: &mut str) -> &mut Self::Output {
2116             if *self.end() == usize::max_value() {
2117                 str_index_overflow_fail();
2118             }
2119             (*self.start()..self.end() + 1).index_mut(slice)
2120         }
2121     }
2122
2123     /// Implements substring slicing with syntax `&self[..= end]` or `&mut
2124     /// self[..= end]`.
2125     ///
2126     /// Returns a slice of the given string from the byte range [0, `end`].
2127     /// Equivalent to `&self [0 .. end + 1]`, except if `end` has the maximum
2128     /// value for `usize`.
2129     ///
2130     /// This operation is `O(1)`.
2131     ///
2132     /// # Panics
2133     ///
2134     /// Panics if `end` does not point to the ending byte offset of a character
2135     /// (`end + 1` is either a starting byte offset as defined by
2136     /// `is_char_boundary`, or equal to `len`), or if `end >= len`.
2137     #[stable(feature = "inclusive_range", since = "1.26.0")]
2138     impl SliceIndex<str> for ops::RangeToInclusive<usize> {
2139         type Output = str;
2140         #[inline]
2141         fn get(self, slice: &str) -> Option<&Self::Output> {
2142             if self.end == usize::max_value() { None } else { (..self.end + 1).get(slice) }
2143         }
2144         #[inline]
2145         fn get_mut(self, slice: &mut str) -> Option<&mut Self::Output> {
2146             if self.end == usize::max_value() { None } else { (..self.end + 1).get_mut(slice) }
2147         }
2148         #[inline]
2149         unsafe fn get_unchecked(self, slice: &str) -> &Self::Output {
2150             (..self.end + 1).get_unchecked(slice)
2151         }
2152         #[inline]
2153         unsafe fn get_unchecked_mut(self, slice: &mut str) -> &mut Self::Output {
2154             (..self.end + 1).get_unchecked_mut(slice)
2155         }
2156         #[inline]
2157         fn index(self, slice: &str) -> &Self::Output {
2158             if self.end == usize::max_value() {
2159                 str_index_overflow_fail();
2160             }
2161             (..self.end + 1).index(slice)
2162         }
2163         #[inline]
2164         fn index_mut(self, slice: &mut str) -> &mut Self::Output {
2165             if self.end == usize::max_value() {
2166                 str_index_overflow_fail();
2167             }
2168             (..self.end + 1).index_mut(slice)
2169         }
2170     }
2171 }
2172
2173 // truncate `&str` to length at most equal to `max`
2174 // return `true` if it were truncated, and the new str.
2175 fn truncate_to_char_boundary(s: &str, mut max: usize) -> (bool, &str) {
2176     if max >= s.len() {
2177         (false, s)
2178     } else {
2179         while !s.is_char_boundary(max) {
2180             max -= 1;
2181         }
2182         (true, &s[..max])
2183     }
2184 }
2185
2186 #[inline(never)]
2187 #[cold]
2188 fn slice_error_fail(s: &str, begin: usize, end: usize) -> ! {
2189     const MAX_DISPLAY_LENGTH: usize = 256;
2190     let (truncated, s_trunc) = truncate_to_char_boundary(s, MAX_DISPLAY_LENGTH);
2191     let ellipsis = if truncated { "[...]" } else { "" };
2192
2193     // 1. out of bounds
2194     if begin > s.len() || end > s.len() {
2195         let oob_index = if begin > s.len() { begin } else { end };
2196         panic!("byte index {} is out of bounds of `{}`{}", oob_index, s_trunc, ellipsis);
2197     }
2198
2199     // 2. begin <= end
2200     assert!(
2201         begin <= end,
2202         "begin <= end ({} <= {}) when slicing `{}`{}",
2203         begin,
2204         end,
2205         s_trunc,
2206         ellipsis
2207     );
2208
2209     // 3. character boundary
2210     let index = if !s.is_char_boundary(begin) { begin } else { end };
2211     // find the character
2212     let mut char_start = index;
2213     while !s.is_char_boundary(char_start) {
2214         char_start -= 1;
2215     }
2216     // `char_start` must be less than len and a char boundary
2217     let ch = s[char_start..].chars().next().unwrap();
2218     let char_range = char_start..char_start + ch.len_utf8();
2219     panic!(
2220         "byte index {} is not a char boundary; it is inside {:?} (bytes {:?}) of `{}`{}",
2221         index, ch, char_range, s_trunc, ellipsis
2222     );
2223 }
2224
2225 #[lang = "str"]
2226 #[cfg(not(test))]
2227 impl str {
2228     /// Returns the length of `self`.
2229     ///
2230     /// This length is in bytes, not [`char`]s or graphemes. In other words,
2231     /// it may not be what a human considers the length of the string.
2232     ///
2233     /// # Examples
2234     ///
2235     /// Basic usage:
2236     ///
2237     /// ```
2238     /// let len = "foo".len();
2239     /// assert_eq!(3, len);
2240     ///
2241     /// assert_eq!("ƒoo".len(), 4); // fancy f!
2242     /// assert_eq!("ƒoo".chars().count(), 3);
2243     /// ```
2244     #[stable(feature = "rust1", since = "1.0.0")]
2245     #[rustc_const_stable(feature = "const_str_len", since = "1.32.0")]
2246     #[inline]
2247     pub const fn len(&self) -> usize {
2248         self.as_bytes().len()
2249     }
2250
2251     /// Returns `true` if `self` has a length of zero bytes.
2252     ///
2253     /// # Examples
2254     ///
2255     /// Basic usage:
2256     ///
2257     /// ```
2258     /// let s = "";
2259     /// assert!(s.is_empty());
2260     ///
2261     /// let s = "not empty";
2262     /// assert!(!s.is_empty());
2263     /// ```
2264     #[inline]
2265     #[stable(feature = "rust1", since = "1.0.0")]
2266     #[rustc_const_stable(feature = "const_str_is_empty", since = "1.32.0")]
2267     pub const fn is_empty(&self) -> bool {
2268         self.len() == 0
2269     }
2270
2271     /// Checks that `index`-th byte lies at the start and/or end of a
2272     /// UTF-8 code point sequence.
2273     ///
2274     /// The start and end of the string (when `index == self.len()`) are
2275     /// considered to be
2276     /// boundaries.
2277     ///
2278     /// Returns `false` if `index` is greater than `self.len()`.
2279     ///
2280     /// # Examples
2281     ///
2282     /// ```
2283     /// let s = "Löwe 老虎 Léopard";
2284     /// assert!(s.is_char_boundary(0));
2285     /// // start of `老`
2286     /// assert!(s.is_char_boundary(6));
2287     /// assert!(s.is_char_boundary(s.len()));
2288     ///
2289     /// // second byte of `ö`
2290     /// assert!(!s.is_char_boundary(2));
2291     ///
2292     /// // third byte of `老`
2293     /// assert!(!s.is_char_boundary(8));
2294     /// ```
2295     #[stable(feature = "is_char_boundary", since = "1.9.0")]
2296     #[inline]
2297     pub fn is_char_boundary(&self, index: usize) -> bool {
2298         // 0 and len are always ok.
2299         // Test for 0 explicitly so that it can optimize out the check
2300         // easily and skip reading string data for that case.
2301         if index == 0 || index == self.len() {
2302             return true;
2303         }
2304         match self.as_bytes().get(index) {
2305             None => false,
2306             // This is bit magic equivalent to: b < 128 || b >= 192
2307             Some(&b) => (b as i8) >= -0x40,
2308         }
2309     }
2310
2311     /// Converts a string slice to a byte slice. To convert the byte slice back
2312     /// into a string slice, use the [`str::from_utf8`] function.
2313     ///
2314     /// [`str::from_utf8`]: ./str/fn.from_utf8.html
2315     ///
2316     /// # Examples
2317     ///
2318     /// Basic usage:
2319     ///
2320     /// ```
2321     /// let bytes = "bors".as_bytes();
2322     /// assert_eq!(b"bors", bytes);
2323     /// ```
2324     #[stable(feature = "rust1", since = "1.0.0")]
2325     #[rustc_const_stable(feature = "str_as_bytes", since = "1.32.0")]
2326     #[inline(always)]
2327     #[allow(unused_attributes)]
2328     #[allow_internal_unstable(const_fn_union)]
2329     pub const fn as_bytes(&self) -> &[u8] {
2330         #[repr(C)]
2331         union Slices<'a> {
2332             str: &'a str,
2333             slice: &'a [u8],
2334         }
2335         // SAFETY: const sound because we transmute two types with the same layout
2336         unsafe { Slices { str: self }.slice }
2337     }
2338
2339     /// Converts a mutable string slice to a mutable byte slice. To convert the
2340     /// mutable byte slice back into a mutable string slice, use the
2341     /// [`str::from_utf8_mut`] function.
2342     ///
2343     /// [`str::from_utf8_mut`]: ./str/fn.from_utf8_mut.html
2344     ///
2345     /// # Examples
2346     ///
2347     /// Basic usage:
2348     ///
2349     /// ```
2350     /// let mut s = String::from("Hello");
2351     /// let bytes = unsafe { s.as_bytes_mut() };
2352     ///
2353     /// assert_eq!(b"Hello", bytes);
2354     /// ```
2355     ///
2356     /// Mutability:
2357     ///
2358     /// ```
2359     /// let mut s = String::from("🗻∈🌏");
2360     ///
2361     /// unsafe {
2362     ///     let bytes = s.as_bytes_mut();
2363     ///
2364     ///     bytes[0] = 0xF0;
2365     ///     bytes[1] = 0x9F;
2366     ///     bytes[2] = 0x8D;
2367     ///     bytes[3] = 0x94;
2368     /// }
2369     ///
2370     /// assert_eq!("🍔∈🌏", s);
2371     /// ```
2372     #[stable(feature = "str_mut_extras", since = "1.20.0")]
2373     #[inline(always)]
2374     pub unsafe fn as_bytes_mut(&mut self) -> &mut [u8] {
2375         &mut *(self as *mut str as *mut [u8])
2376     }
2377
2378     /// Converts a string slice to a raw pointer.
2379     ///
2380     /// As string slices are a slice of bytes, the raw pointer points to a
2381     /// [`u8`]. This pointer will be pointing to the first byte of the string
2382     /// slice.
2383     ///
2384     /// The caller must ensure that the returned pointer is never written to.
2385     /// If you need to mutate the contents of the string slice, use [`as_mut_ptr`].
2386     ///
2387     /// [`u8`]: primitive.u8.html
2388     /// [`as_mut_ptr`]: #method.as_mut_ptr
2389     ///
2390     /// # Examples
2391     ///
2392     /// Basic usage:
2393     ///
2394     /// ```
2395     /// let s = "Hello";
2396     /// let ptr = s.as_ptr();
2397     /// ```
2398     #[stable(feature = "rust1", since = "1.0.0")]
2399     #[rustc_const_stable(feature = "rustc_str_as_ptr", since = "1.32.0")]
2400     #[inline]
2401     pub const fn as_ptr(&self) -> *const u8 {
2402         self as *const str as *const u8
2403     }
2404
2405     /// Converts a mutable string slice to a raw pointer.
2406     ///
2407     /// As string slices are a slice of bytes, the raw pointer points to a
2408     /// [`u8`]. This pointer will be pointing to the first byte of the string
2409     /// slice.
2410     ///
2411     /// It is your responsibility to make sure that the string slice only gets
2412     /// modified in a way that it remains valid UTF-8.
2413     ///
2414     /// [`u8`]: primitive.u8.html
2415     #[stable(feature = "str_as_mut_ptr", since = "1.36.0")]
2416     #[inline]
2417     pub fn as_mut_ptr(&mut self) -> *mut u8 {
2418         self as *mut str as *mut u8
2419     }
2420
2421     /// Returns a subslice of `str`.
2422     ///
2423     /// This is the non-panicking alternative to indexing the `str`. Returns
2424     /// [`None`] whenever equivalent indexing operation would panic.
2425     ///
2426     /// [`None`]: option/enum.Option.html#variant.None
2427     ///
2428     /// # Examples
2429     ///
2430     /// ```
2431     /// let v = String::from("🗻∈🌏");
2432     ///
2433     /// assert_eq!(Some("🗻"), v.get(0..4));
2434     ///
2435     /// // indices not on UTF-8 sequence boundaries
2436     /// assert!(v.get(1..).is_none());
2437     /// assert!(v.get(..8).is_none());
2438     ///
2439     /// // out of bounds
2440     /// assert!(v.get(..42).is_none());
2441     /// ```
2442     #[stable(feature = "str_checked_slicing", since = "1.20.0")]
2443     #[inline]
2444     pub fn get<I: SliceIndex<str>>(&self, i: I) -> Option<&I::Output> {
2445         i.get(self)
2446     }
2447
2448     /// Returns a mutable subslice of `str`.
2449     ///
2450     /// This is the non-panicking alternative to indexing the `str`. Returns
2451     /// [`None`] whenever equivalent indexing operation would panic.
2452     ///
2453     /// [`None`]: option/enum.Option.html#variant.None
2454     ///
2455     /// # Examples
2456     ///
2457     /// ```
2458     /// let mut v = String::from("hello");
2459     /// // correct length
2460     /// assert!(v.get_mut(0..5).is_some());
2461     /// // out of bounds
2462     /// assert!(v.get_mut(..42).is_none());
2463     /// assert_eq!(Some("he"), v.get_mut(0..2).map(|v| &*v));
2464     ///
2465     /// assert_eq!("hello", v);
2466     /// {
2467     ///     let s = v.get_mut(0..2);
2468     ///     let s = s.map(|s| {
2469     ///         s.make_ascii_uppercase();
2470     ///         &*s
2471     ///     });
2472     ///     assert_eq!(Some("HE"), s);
2473     /// }
2474     /// assert_eq!("HEllo", v);
2475     /// ```
2476     #[stable(feature = "str_checked_slicing", since = "1.20.0")]
2477     #[inline]
2478     pub fn get_mut<I: SliceIndex<str>>(&mut self, i: I) -> Option<&mut I::Output> {
2479         i.get_mut(self)
2480     }
2481
2482     /// Returns an unchecked subslice of `str`.
2483     ///
2484     /// This is the unchecked alternative to indexing the `str`.
2485     ///
2486     /// # Safety
2487     ///
2488     /// Callers of this function are responsible that these preconditions are
2489     /// satisfied:
2490     ///
2491     /// * The starting index must not exceed the ending index;
2492     /// * Indexes must be within bounds of the original slice;
2493     /// * Indexes must lie on UTF-8 sequence boundaries.
2494     ///
2495     /// Failing that, the returned string slice may reference invalid memory or
2496     /// violate the invariants communicated by the `str` type.
2497     ///
2498     /// # Examples
2499     ///
2500     /// ```
2501     /// let v = "🗻∈🌏";
2502     /// unsafe {
2503     ///     assert_eq!("🗻", v.get_unchecked(0..4));
2504     ///     assert_eq!("∈", v.get_unchecked(4..7));
2505     ///     assert_eq!("🌏", v.get_unchecked(7..11));
2506     /// }
2507     /// ```
2508     #[stable(feature = "str_checked_slicing", since = "1.20.0")]
2509     #[inline]
2510     pub unsafe fn get_unchecked<I: SliceIndex<str>>(&self, i: I) -> &I::Output {
2511         i.get_unchecked(self)
2512     }
2513
2514     /// Returns a mutable, unchecked subslice of `str`.
2515     ///
2516     /// This is the unchecked alternative to indexing the `str`.
2517     ///
2518     /// # Safety
2519     ///
2520     /// Callers of this function are responsible that these preconditions are
2521     /// satisfied:
2522     ///
2523     /// * The starting index must not exceed the ending index;
2524     /// * Indexes must be within bounds of the original slice;
2525     /// * Indexes must lie on UTF-8 sequence boundaries.
2526     ///
2527     /// Failing that, the returned string slice may reference invalid memory or
2528     /// violate the invariants communicated by the `str` type.
2529     ///
2530     /// # Examples
2531     ///
2532     /// ```
2533     /// let mut v = String::from("🗻∈🌏");
2534     /// unsafe {
2535     ///     assert_eq!("🗻", v.get_unchecked_mut(0..4));
2536     ///     assert_eq!("∈", v.get_unchecked_mut(4..7));
2537     ///     assert_eq!("🌏", v.get_unchecked_mut(7..11));
2538     /// }
2539     /// ```
2540     #[stable(feature = "str_checked_slicing", since = "1.20.0")]
2541     #[inline]
2542     pub unsafe fn get_unchecked_mut<I: SliceIndex<str>>(&mut self, i: I) -> &mut I::Output {
2543         i.get_unchecked_mut(self)
2544     }
2545
2546     /// Creates a string slice from another string slice, bypassing safety
2547     /// checks.
2548     ///
2549     /// This is generally not recommended, use with caution! For a safe
2550     /// alternative see [`str`] and [`Index`].
2551     ///
2552     /// [`str`]: primitive.str.html
2553     /// [`Index`]: ops/trait.Index.html
2554     ///
2555     /// This new slice goes from `begin` to `end`, including `begin` but
2556     /// excluding `end`.
2557     ///
2558     /// To get a mutable string slice instead, see the
2559     /// [`slice_mut_unchecked`] method.
2560     ///
2561     /// [`slice_mut_unchecked`]: #method.slice_mut_unchecked
2562     ///
2563     /// # Safety
2564     ///
2565     /// Callers of this function are responsible that three preconditions are
2566     /// satisfied:
2567     ///
2568     /// * `begin` must not exceed `end`.
2569     /// * `begin` and `end` must be byte positions within the string slice.
2570     /// * `begin` and `end` must lie on UTF-8 sequence boundaries.
2571     ///
2572     /// # Examples
2573     ///
2574     /// Basic usage:
2575     ///
2576     /// ```
2577     /// let s = "Löwe 老虎 Léopard";
2578     ///
2579     /// unsafe {
2580     ///     assert_eq!("Löwe 老虎 Léopard", s.slice_unchecked(0, 21));
2581     /// }
2582     ///
2583     /// let s = "Hello, world!";
2584     ///
2585     /// unsafe {
2586     ///     assert_eq!("world", s.slice_unchecked(7, 12));
2587     /// }
2588     /// ```
2589     #[stable(feature = "rust1", since = "1.0.0")]
2590     #[rustc_deprecated(since = "1.29.0", reason = "use `get_unchecked(begin..end)` instead")]
2591     #[inline]
2592     pub unsafe fn slice_unchecked(&self, begin: usize, end: usize) -> &str {
2593         (begin..end).get_unchecked(self)
2594     }
2595
2596     /// Creates a string slice from another string slice, bypassing safety
2597     /// checks.
2598     /// This is generally not recommended, use with caution! For a safe
2599     /// alternative see [`str`] and [`IndexMut`].
2600     ///
2601     /// [`str`]: primitive.str.html
2602     /// [`IndexMut`]: ops/trait.IndexMut.html
2603     ///
2604     /// This new slice goes from `begin` to `end`, including `begin` but
2605     /// excluding `end`.
2606     ///
2607     /// To get an immutable string slice instead, see the
2608     /// [`slice_unchecked`] method.
2609     ///
2610     /// [`slice_unchecked`]: #method.slice_unchecked
2611     ///
2612     /// # Safety
2613     ///
2614     /// Callers of this function are responsible that three preconditions are
2615     /// satisfied:
2616     ///
2617     /// * `begin` must not exceed `end`.
2618     /// * `begin` and `end` must be byte positions within the string slice.
2619     /// * `begin` and `end` must lie on UTF-8 sequence boundaries.
2620     #[stable(feature = "str_slice_mut", since = "1.5.0")]
2621     #[rustc_deprecated(since = "1.29.0", reason = "use `get_unchecked_mut(begin..end)` instead")]
2622     #[inline]
2623     pub unsafe fn slice_mut_unchecked(&mut self, begin: usize, end: usize) -> &mut str {
2624         (begin..end).get_unchecked_mut(self)
2625     }
2626
2627     /// Divide one string slice into two at an index.
2628     ///
2629     /// The argument, `mid`, should be a byte offset from the start of the
2630     /// string. It must also be on the boundary of a UTF-8 code point.
2631     ///
2632     /// The two slices returned go from the start of the string slice to `mid`,
2633     /// and from `mid` to the end of the string slice.
2634     ///
2635     /// To get mutable string slices instead, see the [`split_at_mut`]
2636     /// method.
2637     ///
2638     /// [`split_at_mut`]: #method.split_at_mut
2639     ///
2640     /// # Panics
2641     ///
2642     /// Panics if `mid` is not on a UTF-8 code point boundary, or if it is
2643     /// beyond the last code point of the string slice.
2644     ///
2645     /// # Examples
2646     ///
2647     /// Basic usage:
2648     ///
2649     /// ```
2650     /// let s = "Per Martin-Löf";
2651     ///
2652     /// let (first, last) = s.split_at(3);
2653     ///
2654     /// assert_eq!("Per", first);
2655     /// assert_eq!(" Martin-Löf", last);
2656     /// ```
2657     #[inline]
2658     #[stable(feature = "str_split_at", since = "1.4.0")]
2659     pub fn split_at(&self, mid: usize) -> (&str, &str) {
2660         // is_char_boundary checks that the index is in [0, .len()]
2661         if self.is_char_boundary(mid) {
2662             // SAFETY: just checked that `mid` is on a char boundary.
2663             unsafe { (self.get_unchecked(0..mid), self.get_unchecked(mid..self.len())) }
2664         } else {
2665             slice_error_fail(self, 0, mid)
2666         }
2667     }
2668
2669     /// Divide one mutable string slice into two at an index.
2670     ///
2671     /// The argument, `mid`, should be a byte offset from the start of the
2672     /// string. It must also be on the boundary of a UTF-8 code point.
2673     ///
2674     /// The two slices returned go from the start of the string slice to `mid`,
2675     /// and from `mid` to the end of the string slice.
2676     ///
2677     /// To get immutable string slices instead, see the [`split_at`] method.
2678     ///
2679     /// [`split_at`]: #method.split_at
2680     ///
2681     /// # Panics
2682     ///
2683     /// Panics if `mid` is not on a UTF-8 code point boundary, or if it is
2684     /// beyond the last code point of the string slice.
2685     ///
2686     /// # Examples
2687     ///
2688     /// Basic usage:
2689     ///
2690     /// ```
2691     /// let mut s = "Per Martin-Löf".to_string();
2692     /// {
2693     ///     let (first, last) = s.split_at_mut(3);
2694     ///     first.make_ascii_uppercase();
2695     ///     assert_eq!("PER", first);
2696     ///     assert_eq!(" Martin-Löf", last);
2697     /// }
2698     /// assert_eq!("PER Martin-Löf", s);
2699     /// ```
2700     #[inline]
2701     #[stable(feature = "str_split_at", since = "1.4.0")]
2702     pub fn split_at_mut(&mut self, mid: usize) -> (&mut str, &mut str) {
2703         // is_char_boundary checks that the index is in [0, .len()]
2704         if self.is_char_boundary(mid) {
2705             let len = self.len();
2706             let ptr = self.as_mut_ptr();
2707             // SAFETY: just checked that `mid` is on a char boundary.
2708             unsafe {
2709                 (
2710                     from_utf8_unchecked_mut(slice::from_raw_parts_mut(ptr, mid)),
2711                     from_utf8_unchecked_mut(slice::from_raw_parts_mut(ptr.add(mid), len - mid)),
2712                 )
2713             }
2714         } else {
2715             slice_error_fail(self, 0, mid)
2716         }
2717     }
2718
2719     /// Returns an iterator over the [`char`]s of a string slice.
2720     ///
2721     /// As a string slice consists of valid UTF-8, we can iterate through a
2722     /// string slice by [`char`]. This method returns such an iterator.
2723     ///
2724     /// It's important to remember that [`char`] represents a Unicode Scalar
2725     /// Value, and may not match your idea of what a 'character' is. Iteration
2726     /// over grapheme clusters may be what you actually want. This functionality
2727     /// is not provided by Rust's standard library, check crates.io instead.
2728     ///
2729     /// # Examples
2730     ///
2731     /// Basic usage:
2732     ///
2733     /// ```
2734     /// let word = "goodbye";
2735     ///
2736     /// let count = word.chars().count();
2737     /// assert_eq!(7, count);
2738     ///
2739     /// let mut chars = word.chars();
2740     ///
2741     /// assert_eq!(Some('g'), chars.next());
2742     /// assert_eq!(Some('o'), chars.next());
2743     /// assert_eq!(Some('o'), chars.next());
2744     /// assert_eq!(Some('d'), chars.next());
2745     /// assert_eq!(Some('b'), chars.next());
2746     /// assert_eq!(Some('y'), chars.next());
2747     /// assert_eq!(Some('e'), chars.next());
2748     ///
2749     /// assert_eq!(None, chars.next());
2750     /// ```
2751     ///
2752     /// Remember, [`char`]s may not match your human intuition about characters:
2753     ///
2754     /// ```
2755     /// let y = "y̆";
2756     ///
2757     /// let mut chars = y.chars();
2758     ///
2759     /// assert_eq!(Some('y'), chars.next()); // not 'y̆'
2760     /// assert_eq!(Some('\u{0306}'), chars.next());
2761     ///
2762     /// assert_eq!(None, chars.next());
2763     /// ```
2764     #[stable(feature = "rust1", since = "1.0.0")]
2765     #[inline]
2766     pub fn chars(&self) -> Chars<'_> {
2767         Chars { iter: self.as_bytes().iter() }
2768     }
2769
2770     /// Returns an iterator over the [`char`]s of a string slice, and their
2771     /// positions.
2772     ///
2773     /// As a string slice consists of valid UTF-8, we can iterate through a
2774     /// string slice by [`char`]. This method returns an iterator of both
2775     /// these [`char`]s, as well as their byte positions.
2776     ///
2777     /// The iterator yields tuples. The position is first, the [`char`] is
2778     /// second.
2779     ///
2780     /// # Examples
2781     ///
2782     /// Basic usage:
2783     ///
2784     /// ```
2785     /// let word = "goodbye";
2786     ///
2787     /// let count = word.char_indices().count();
2788     /// assert_eq!(7, count);
2789     ///
2790     /// let mut char_indices = word.char_indices();
2791     ///
2792     /// assert_eq!(Some((0, 'g')), char_indices.next());
2793     /// assert_eq!(Some((1, 'o')), char_indices.next());
2794     /// assert_eq!(Some((2, 'o')), char_indices.next());
2795     /// assert_eq!(Some((3, 'd')), char_indices.next());
2796     /// assert_eq!(Some((4, 'b')), char_indices.next());
2797     /// assert_eq!(Some((5, 'y')), char_indices.next());
2798     /// assert_eq!(Some((6, 'e')), char_indices.next());
2799     ///
2800     /// assert_eq!(None, char_indices.next());
2801     /// ```
2802     ///
2803     /// Remember, [`char`]s may not match your human intuition about characters:
2804     ///
2805     /// ```
2806     /// let yes = "y̆es";
2807     ///
2808     /// let mut char_indices = yes.char_indices();
2809     ///
2810     /// assert_eq!(Some((0, 'y')), char_indices.next()); // not (0, 'y̆')
2811     /// assert_eq!(Some((1, '\u{0306}')), char_indices.next());
2812     ///
2813     /// // note the 3 here - the last character took up two bytes
2814     /// assert_eq!(Some((3, 'e')), char_indices.next());
2815     /// assert_eq!(Some((4, 's')), char_indices.next());
2816     ///
2817     /// assert_eq!(None, char_indices.next());
2818     /// ```
2819     #[stable(feature = "rust1", since = "1.0.0")]
2820     #[inline]
2821     pub fn char_indices(&self) -> CharIndices<'_> {
2822         CharIndices { front_offset: 0, iter: self.chars() }
2823     }
2824
2825     /// An iterator over the bytes of a string slice.
2826     ///
2827     /// As a string slice consists of a sequence of bytes, we can iterate
2828     /// through a string slice by byte. This method returns such an iterator.
2829     ///
2830     /// # Examples
2831     ///
2832     /// Basic usage:
2833     ///
2834     /// ```
2835     /// let mut bytes = "bors".bytes();
2836     ///
2837     /// assert_eq!(Some(b'b'), bytes.next());
2838     /// assert_eq!(Some(b'o'), bytes.next());
2839     /// assert_eq!(Some(b'r'), bytes.next());
2840     /// assert_eq!(Some(b's'), bytes.next());
2841     ///
2842     /// assert_eq!(None, bytes.next());
2843     /// ```
2844     #[stable(feature = "rust1", since = "1.0.0")]
2845     #[inline]
2846     pub fn bytes(&self) -> Bytes<'_> {
2847         Bytes(self.as_bytes().iter().copied())
2848     }
2849
2850     /// Splits a string slice by whitespace.
2851     ///
2852     /// The iterator returned will return string slices that are sub-slices of
2853     /// the original string slice, separated by any amount of whitespace.
2854     ///
2855     /// 'Whitespace' is defined according to the terms of the Unicode Derived
2856     /// Core Property `White_Space`. If you only want to split on ASCII whitespace
2857     /// instead, use [`split_ascii_whitespace`].
2858     ///
2859     /// [`split_ascii_whitespace`]: #method.split_ascii_whitespace
2860     ///
2861     /// # Examples
2862     ///
2863     /// Basic usage:
2864     ///
2865     /// ```
2866     /// let mut iter = "A few words".split_whitespace();
2867     ///
2868     /// assert_eq!(Some("A"), iter.next());
2869     /// assert_eq!(Some("few"), iter.next());
2870     /// assert_eq!(Some("words"), iter.next());
2871     ///
2872     /// assert_eq!(None, iter.next());
2873     /// ```
2874     ///
2875     /// All kinds of whitespace are considered:
2876     ///
2877     /// ```
2878     /// let mut iter = " Mary   had\ta\u{2009}little  \n\t lamb".split_whitespace();
2879     /// assert_eq!(Some("Mary"), iter.next());
2880     /// assert_eq!(Some("had"), iter.next());
2881     /// assert_eq!(Some("a"), iter.next());
2882     /// assert_eq!(Some("little"), iter.next());
2883     /// assert_eq!(Some("lamb"), iter.next());
2884     ///
2885     /// assert_eq!(None, iter.next());
2886     /// ```
2887     #[stable(feature = "split_whitespace", since = "1.1.0")]
2888     #[inline]
2889     pub fn split_whitespace(&self) -> SplitWhitespace<'_> {
2890         SplitWhitespace { inner: self.split(IsWhitespace).filter(IsNotEmpty) }
2891     }
2892
2893     /// Splits a string slice by ASCII whitespace.
2894     ///
2895     /// The iterator returned will return string slices that are sub-slices of
2896     /// the original string slice, separated by any amount of ASCII whitespace.
2897     ///
2898     /// To split by Unicode `Whitespace` instead, use [`split_whitespace`].
2899     ///
2900     /// [`split_whitespace`]: #method.split_whitespace
2901     ///
2902     /// # Examples
2903     ///
2904     /// Basic usage:
2905     ///
2906     /// ```
2907     /// let mut iter = "A few words".split_ascii_whitespace();
2908     ///
2909     /// assert_eq!(Some("A"), iter.next());
2910     /// assert_eq!(Some("few"), iter.next());
2911     /// assert_eq!(Some("words"), iter.next());
2912     ///
2913     /// assert_eq!(None, iter.next());
2914     /// ```
2915     ///
2916     /// All kinds of ASCII whitespace are considered:
2917     ///
2918     /// ```
2919     /// let mut iter = " Mary   had\ta little  \n\t lamb".split_ascii_whitespace();
2920     /// assert_eq!(Some("Mary"), iter.next());
2921     /// assert_eq!(Some("had"), iter.next());
2922     /// assert_eq!(Some("a"), iter.next());
2923     /// assert_eq!(Some("little"), iter.next());
2924     /// assert_eq!(Some("lamb"), iter.next());
2925     ///
2926     /// assert_eq!(None, iter.next());
2927     /// ```
2928     #[stable(feature = "split_ascii_whitespace", since = "1.34.0")]
2929     #[inline]
2930     pub fn split_ascii_whitespace(&self) -> SplitAsciiWhitespace<'_> {
2931         let inner =
2932             self.as_bytes().split(IsAsciiWhitespace).filter(BytesIsNotEmpty).map(UnsafeBytesToStr);
2933         SplitAsciiWhitespace { inner }
2934     }
2935
2936     /// An iterator over the lines of a string, as string slices.
2937     ///
2938     /// Lines are ended with either a newline (`\n`) or a carriage return with
2939     /// a line feed (`\r\n`).
2940     ///
2941     /// The final line ending is optional.
2942     ///
2943     /// # Examples
2944     ///
2945     /// Basic usage:
2946     ///
2947     /// ```
2948     /// let text = "foo\r\nbar\n\nbaz\n";
2949     /// let mut lines = text.lines();
2950     ///
2951     /// assert_eq!(Some("foo"), lines.next());
2952     /// assert_eq!(Some("bar"), lines.next());
2953     /// assert_eq!(Some(""), lines.next());
2954     /// assert_eq!(Some("baz"), lines.next());
2955     ///
2956     /// assert_eq!(None, lines.next());
2957     /// ```
2958     ///
2959     /// The final line ending isn't required:
2960     ///
2961     /// ```
2962     /// let text = "foo\nbar\n\r\nbaz";
2963     /// let mut lines = text.lines();
2964     ///
2965     /// assert_eq!(Some("foo"), lines.next());
2966     /// assert_eq!(Some("bar"), lines.next());
2967     /// assert_eq!(Some(""), lines.next());
2968     /// assert_eq!(Some("baz"), lines.next());
2969     ///
2970     /// assert_eq!(None, lines.next());
2971     /// ```
2972     #[stable(feature = "rust1", since = "1.0.0")]
2973     #[inline]
2974     pub fn lines(&self) -> Lines<'_> {
2975         Lines(self.split_terminator('\n').map(LinesAnyMap))
2976     }
2977
2978     /// An iterator over the lines of a string.
2979     #[stable(feature = "rust1", since = "1.0.0")]
2980     #[rustc_deprecated(since = "1.4.0", reason = "use lines() instead now")]
2981     #[inline]
2982     #[allow(deprecated)]
2983     pub fn lines_any(&self) -> LinesAny<'_> {
2984         LinesAny(self.lines())
2985     }
2986
2987     /// Returns an iterator of `u16` over the string encoded as UTF-16.
2988     ///
2989     /// # Examples
2990     ///
2991     /// Basic usage:
2992     ///
2993     /// ```
2994     /// let text = "Zażółć gęślą jaźń";
2995     ///
2996     /// let utf8_len = text.len();
2997     /// let utf16_len = text.encode_utf16().count();
2998     ///
2999     /// assert!(utf16_len <= utf8_len);
3000     /// ```
3001     #[stable(feature = "encode_utf16", since = "1.8.0")]
3002     pub fn encode_utf16(&self) -> EncodeUtf16<'_> {
3003         EncodeUtf16 { chars: self.chars(), extra: 0 }
3004     }
3005
3006     /// Returns `true` if the given pattern matches a sub-slice of
3007     /// this string slice.
3008     ///
3009     /// Returns `false` if it does not.
3010     ///
3011     /// # Examples
3012     ///
3013     /// Basic usage:
3014     ///
3015     /// ```
3016     /// let bananas = "bananas";
3017     ///
3018     /// assert!(bananas.contains("nana"));
3019     /// assert!(!bananas.contains("apples"));
3020     /// ```
3021     #[stable(feature = "rust1", since = "1.0.0")]
3022     #[inline]
3023     pub fn contains<'a, P: Pattern<'a>>(&'a self, pat: P) -> bool {
3024         pat.is_contained_in(self)
3025     }
3026
3027     /// Returns `true` if the given pattern matches a prefix of this
3028     /// string slice.
3029     ///
3030     /// Returns `false` if it does not.
3031     ///
3032     /// # Examples
3033     ///
3034     /// Basic usage:
3035     ///
3036     /// ```
3037     /// let bananas = "bananas";
3038     ///
3039     /// assert!(bananas.starts_with("bana"));
3040     /// assert!(!bananas.starts_with("nana"));
3041     /// ```
3042     #[stable(feature = "rust1", since = "1.0.0")]
3043     pub fn starts_with<'a, P: Pattern<'a>>(&'a self, pat: P) -> bool {
3044         pat.is_prefix_of(self)
3045     }
3046
3047     /// Returns `true` if the given pattern matches a suffix of this
3048     /// string slice.
3049     ///
3050     /// Returns `false` if it does not.
3051     ///
3052     /// # Examples
3053     ///
3054     /// Basic usage:
3055     ///
3056     /// ```
3057     /// let bananas = "bananas";
3058     ///
3059     /// assert!(bananas.ends_with("anas"));
3060     /// assert!(!bananas.ends_with("nana"));
3061     /// ```
3062     #[stable(feature = "rust1", since = "1.0.0")]
3063     pub fn ends_with<'a, P>(&'a self, pat: P) -> bool
3064     where
3065         P: Pattern<'a, Searcher: ReverseSearcher<'a>>,
3066     {
3067         pat.is_suffix_of(self)
3068     }
3069
3070     /// Returns the byte index of the first character of this string slice that
3071     /// matches the pattern.
3072     ///
3073     /// Returns [`None`] if the pattern doesn't match.
3074     ///
3075     /// The pattern can be a `&str`, [`char`], or a closure that determines if
3076     /// a character matches.
3077     ///
3078     /// [`None`]: option/enum.Option.html#variant.None
3079     ///
3080     /// # Examples
3081     ///
3082     /// Simple patterns:
3083     ///
3084     /// ```
3085     /// let s = "Löwe 老虎 Léopard";
3086     ///
3087     /// assert_eq!(s.find('L'), Some(0));
3088     /// assert_eq!(s.find('é'), Some(14));
3089     /// assert_eq!(s.find("Léopard"), Some(13));
3090     /// ```
3091     ///
3092     /// More complex patterns using point-free style and closures:
3093     ///
3094     /// ```
3095     /// let s = "Löwe 老虎 Léopard";
3096     ///
3097     /// assert_eq!(s.find(char::is_whitespace), Some(5));
3098     /// assert_eq!(s.find(char::is_lowercase), Some(1));
3099     /// assert_eq!(s.find(|c: char| c.is_whitespace() || c.is_lowercase()), Some(1));
3100     /// assert_eq!(s.find(|c: char| (c < 'o') && (c > 'a')), Some(4));
3101     /// ```
3102     ///
3103     /// Not finding the pattern:
3104     ///
3105     /// ```
3106     /// let s = "Löwe 老虎 Léopard";
3107     /// let x: &[_] = &['1', '2'];
3108     ///
3109     /// assert_eq!(s.find(x), None);
3110     /// ```
3111     #[stable(feature = "rust1", since = "1.0.0")]
3112     #[inline]
3113     pub fn find<'a, P: Pattern<'a>>(&'a self, pat: P) -> Option<usize> {
3114         pat.into_searcher(self).next_match().map(|(i, _)| i)
3115     }
3116
3117     /// Returns the byte index of the last character of this string slice that
3118     /// matches the pattern.
3119     ///
3120     /// Returns [`None`] if the pattern doesn't match.
3121     ///
3122     /// The pattern can be a `&str`, [`char`], or a closure that determines if
3123     /// a character matches.
3124     ///
3125     /// [`None`]: option/enum.Option.html#variant.None
3126     ///
3127     /// # Examples
3128     ///
3129     /// Simple patterns:
3130     ///
3131     /// ```
3132     /// let s = "Löwe 老虎 Léopard";
3133     ///
3134     /// assert_eq!(s.rfind('L'), Some(13));
3135     /// assert_eq!(s.rfind('é'), Some(14));
3136     /// ```
3137     ///
3138     /// More complex patterns with closures:
3139     ///
3140     /// ```
3141     /// let s = "Löwe 老虎 Léopard";
3142     ///
3143     /// assert_eq!(s.rfind(char::is_whitespace), Some(12));
3144     /// assert_eq!(s.rfind(char::is_lowercase), Some(20));
3145     /// ```
3146     ///
3147     /// Not finding the pattern:
3148     ///
3149     /// ```
3150     /// let s = "Löwe 老虎 Léopard";
3151     /// let x: &[_] = &['1', '2'];
3152     ///
3153     /// assert_eq!(s.rfind(x), None);
3154     /// ```
3155     #[stable(feature = "rust1", since = "1.0.0")]
3156     #[inline]
3157     pub fn rfind<'a, P>(&'a self, pat: P) -> Option<usize>
3158     where
3159         P: Pattern<'a, Searcher: ReverseSearcher<'a>>,
3160     {
3161         pat.into_searcher(self).next_match_back().map(|(i, _)| i)
3162     }
3163
3164     /// An iterator over substrings of this string slice, separated by
3165     /// characters matched by a pattern.
3166     ///
3167     /// The pattern can be any type that implements the Pattern trait. Notable
3168     /// examples are `&str`, [`char`], and closures that determines the split.
3169     ///
3170     /// # Iterator behavior
3171     ///
3172     /// The returned iterator will be a [`DoubleEndedIterator`] if the pattern
3173     /// allows a reverse search and forward/reverse search yields the same
3174     /// elements. This is true for, e.g., [`char`], but not for `&str`.
3175     ///
3176     /// [`DoubleEndedIterator`]: iter/trait.DoubleEndedIterator.html
3177     ///
3178     /// If the pattern allows a reverse search but its results might differ
3179     /// from a forward search, the [`rsplit`] method can be used.
3180     ///
3181     /// [`rsplit`]: #method.rsplit
3182     ///
3183     /// # Examples
3184     ///
3185     /// Simple patterns:
3186     ///
3187     /// ```
3188     /// let v: Vec<&str> = "Mary had a little lamb".split(' ').collect();
3189     /// assert_eq!(v, ["Mary", "had", "a", "little", "lamb"]);
3190     ///
3191     /// let v: Vec<&str> = "".split('X').collect();
3192     /// assert_eq!(v, [""]);
3193     ///
3194     /// let v: Vec<&str> = "lionXXtigerXleopard".split('X').collect();
3195     /// assert_eq!(v, ["lion", "", "tiger", "leopard"]);
3196     ///
3197     /// let v: Vec<&str> = "lion::tiger::leopard".split("::").collect();
3198     /// assert_eq!(v, ["lion", "tiger", "leopard"]);
3199     ///
3200     /// let v: Vec<&str> = "abc1def2ghi".split(char::is_numeric).collect();
3201     /// assert_eq!(v, ["abc", "def", "ghi"]);
3202     ///
3203     /// let v: Vec<&str> = "lionXtigerXleopard".split(char::is_uppercase).collect();
3204     /// assert_eq!(v, ["lion", "tiger", "leopard"]);
3205     /// ```
3206     ///
3207     /// A more complex pattern, using a closure:
3208     ///
3209     /// ```
3210     /// let v: Vec<&str> = "abc1defXghi".split(|c| c == '1' || c == 'X').collect();
3211     /// assert_eq!(v, ["abc", "def", "ghi"]);
3212     /// ```
3213     ///
3214     /// If a string contains multiple contiguous separators, you will end up
3215     /// with empty strings in the output:
3216     ///
3217     /// ```
3218     /// let x = "||||a||b|c".to_string();
3219     /// let d: Vec<_> = x.split('|').collect();
3220     ///
3221     /// assert_eq!(d, &["", "", "", "", "a", "", "b", "c"]);
3222     /// ```
3223     ///
3224     /// Contiguous separators are separated by the empty string.
3225     ///
3226     /// ```
3227     /// let x = "(///)".to_string();
3228     /// let d: Vec<_> = x.split('/').collect();
3229     ///
3230     /// assert_eq!(d, &["(", "", "", ")"]);
3231     /// ```
3232     ///
3233     /// Separators at the start or end of a string are neighbored
3234     /// by empty strings.
3235     ///
3236     /// ```
3237     /// let d: Vec<_> = "010".split("0").collect();
3238     /// assert_eq!(d, &["", "1", ""]);
3239     /// ```
3240     ///
3241     /// When the empty string is used as a separator, it separates
3242     /// every character in the string, along with the beginning
3243     /// and end of the string.
3244     ///
3245     /// ```
3246     /// let f: Vec<_> = "rust".split("").collect();
3247     /// assert_eq!(f, &["", "r", "u", "s", "t", ""]);
3248     /// ```
3249     ///
3250     /// Contiguous separators can lead to possibly surprising behavior
3251     /// when whitespace is used as the separator. This code is correct:
3252     ///
3253     /// ```
3254     /// let x = "    a  b c".to_string();
3255     /// let d: Vec<_> = x.split(' ').collect();
3256     ///
3257     /// assert_eq!(d, &["", "", "", "", "a", "", "b", "c"]);
3258     /// ```
3259     ///
3260     /// It does _not_ give you:
3261     ///
3262     /// ```,ignore
3263     /// assert_eq!(d, &["a", "b", "c"]);
3264     /// ```
3265     ///
3266     /// Use [`split_whitespace`] for this behavior.
3267     ///
3268     /// [`split_whitespace`]: #method.split_whitespace
3269     #[stable(feature = "rust1", since = "1.0.0")]
3270     #[inline]
3271     pub fn split<'a, P: Pattern<'a>>(&'a self, pat: P) -> Split<'a, P> {
3272         Split(SplitInternal {
3273             start: 0,
3274             end: self.len(),
3275             matcher: pat.into_searcher(self),
3276             allow_trailing_empty: true,
3277             finished: false,
3278         })
3279     }
3280
3281     /// An iterator over substrings of this string slice, separated by
3282     /// characters matched by a pattern. Differs from the iterator produced by
3283     /// `split` in that `split_inclusive` leaves the matched part as the
3284     /// terminator of the substring.
3285     ///
3286     /// # Examples
3287     ///
3288     /// ```
3289     /// #![feature(split_inclusive)]
3290     /// let v: Vec<&str> = "Mary had a little lamb\nlittle lamb\nlittle lamb."
3291     ///     .split_inclusive('\n').collect();
3292     /// assert_eq!(v, ["Mary had a little lamb\n", "little lamb\n", "little lamb."]);
3293     /// ```
3294     ///
3295     /// If the last element of the string is matched,
3296     /// that element will be considered the terminator of the preceding substring.
3297     /// That substring will be the last item returned by the iterator.
3298     ///
3299     /// ```
3300     /// #![feature(split_inclusive)]
3301     /// let v: Vec<&str> = "Mary had a little lamb\nlittle lamb\nlittle lamb.\n"
3302     ///     .split_inclusive('\n').collect();
3303     /// assert_eq!(v, ["Mary had a little lamb\n", "little lamb\n", "little lamb.\n"]);
3304     /// ```
3305     #[unstable(feature = "split_inclusive", issue = "none")]
3306     #[inline]
3307     pub fn split_inclusive<'a, P: Pattern<'a>>(&'a self, pat: P) -> SplitInclusive<'a, P> {
3308         SplitInclusive(SplitInternal {
3309             start: 0,
3310             end: self.len(),
3311             matcher: pat.into_searcher(self),
3312             allow_trailing_empty: false,
3313             finished: false,
3314         })
3315     }
3316
3317     /// An iterator over substrings of the given string slice, separated by
3318     /// characters matched by a pattern and yielded in reverse order.
3319     ///
3320     /// The pattern can be any type that implements the Pattern trait. Notable
3321     /// examples are `&str`, [`char`], and closures that determines the split.
3322     ///
3323     /// # Iterator behavior
3324     ///
3325     /// The returned iterator requires that the pattern supports a reverse
3326     /// search, and it will be a [`DoubleEndedIterator`] if a forward/reverse
3327     /// search yields the same elements.
3328     ///
3329     /// [`DoubleEndedIterator`]: iter/trait.DoubleEndedIterator.html
3330     ///
3331     /// For iterating from the front, the [`split`] method can be used.
3332     ///
3333     /// [`split`]: #method.split
3334     ///
3335     /// # Examples
3336     ///
3337     /// Simple patterns:
3338     ///
3339     /// ```
3340     /// let v: Vec<&str> = "Mary had a little lamb".rsplit(' ').collect();
3341     /// assert_eq!(v, ["lamb", "little", "a", "had", "Mary"]);
3342     ///
3343     /// let v: Vec<&str> = "".rsplit('X').collect();
3344     /// assert_eq!(v, [""]);
3345     ///
3346     /// let v: Vec<&str> = "lionXXtigerXleopard".rsplit('X').collect();
3347     /// assert_eq!(v, ["leopard", "tiger", "", "lion"]);
3348     ///
3349     /// let v: Vec<&str> = "lion::tiger::leopard".rsplit("::").collect();
3350     /// assert_eq!(v, ["leopard", "tiger", "lion"]);
3351     /// ```
3352     ///
3353     /// A more complex pattern, using a closure:
3354     ///
3355     /// ```
3356     /// let v: Vec<&str> = "abc1defXghi".rsplit(|c| c == '1' || c == 'X').collect();
3357     /// assert_eq!(v, ["ghi", "def", "abc"]);
3358     /// ```
3359     #[stable(feature = "rust1", since = "1.0.0")]
3360     #[inline]
3361     pub fn rsplit<'a, P>(&'a self, pat: P) -> RSplit<'a, P>
3362     where
3363         P: Pattern<'a, Searcher: ReverseSearcher<'a>>,
3364     {
3365         RSplit(self.split(pat).0)
3366     }
3367
3368     /// An iterator over substrings of the given string slice, separated by
3369     /// characters matched by a pattern.
3370     ///
3371     /// The pattern can be any type that implements the Pattern trait. Notable
3372     /// examples are `&str`, [`char`], and closures that determines the split.
3373     ///
3374     /// Equivalent to [`split`], except that the trailing substring
3375     /// is skipped if empty.
3376     ///
3377     /// [`split`]: #method.split
3378     ///
3379     /// This method can be used for string data that is _terminated_,
3380     /// rather than _separated_ by a pattern.
3381     ///
3382     /// # Iterator behavior
3383     ///
3384     /// The returned iterator will be a [`DoubleEndedIterator`] if the pattern
3385     /// allows a reverse search and forward/reverse search yields the same
3386     /// elements. This is true for, e.g., [`char`], but not for `&str`.
3387     ///
3388     /// [`DoubleEndedIterator`]: iter/trait.DoubleEndedIterator.html
3389     ///
3390     /// If the pattern allows a reverse search but its results might differ
3391     /// from a forward search, the [`rsplit_terminator`] method can be used.
3392     ///
3393     /// [`rsplit_terminator`]: #method.rsplit_terminator
3394     ///
3395     /// # Examples
3396     ///
3397     /// Basic usage:
3398     ///
3399     /// ```
3400     /// let v: Vec<&str> = "A.B.".split_terminator('.').collect();
3401     /// assert_eq!(v, ["A", "B"]);
3402     ///
3403     /// let v: Vec<&str> = "A..B..".split_terminator(".").collect();
3404     /// assert_eq!(v, ["A", "", "B", ""]);
3405     /// ```
3406     #[stable(feature = "rust1", since = "1.0.0")]
3407     #[inline]
3408     pub fn split_terminator<'a, P: Pattern<'a>>(&'a self, pat: P) -> SplitTerminator<'a, P> {
3409         SplitTerminator(SplitInternal { allow_trailing_empty: false, ..self.split(pat).0 })
3410     }
3411
3412     /// An iterator over substrings of `self`, separated by characters
3413     /// matched by a pattern and yielded in reverse order.
3414     ///
3415     /// The pattern can be any type that implements the Pattern trait. Notable
3416     /// examples are `&str`, [`char`], and closures that determines the split.
3417     /// Additional libraries might provide more complex patterns like
3418     /// regular expressions.
3419     ///
3420     /// Equivalent to [`split`], except that the trailing substring is
3421     /// skipped if empty.
3422     ///
3423     /// [`split`]: #method.split
3424     ///
3425     /// This method can be used for string data that is _terminated_,
3426     /// rather than _separated_ by a pattern.
3427     ///
3428     /// # Iterator behavior
3429     ///
3430     /// The returned iterator requires that the pattern supports a
3431     /// reverse search, and it will be double ended if a forward/reverse
3432     /// search yields the same elements.
3433     ///
3434     /// For iterating from the front, the [`split_terminator`] method can be
3435     /// used.
3436     ///
3437     /// [`split_terminator`]: #method.split_terminator
3438     ///
3439     /// # Examples
3440     ///
3441     /// ```
3442     /// let v: Vec<&str> = "A.B.".rsplit_terminator('.').collect();
3443     /// assert_eq!(v, ["B", "A"]);
3444     ///
3445     /// let v: Vec<&str> = "A..B..".rsplit_terminator(".").collect();
3446     /// assert_eq!(v, ["", "B", "", "A"]);
3447     /// ```
3448     #[stable(feature = "rust1", since = "1.0.0")]
3449     #[inline]
3450     pub fn rsplit_terminator<'a, P>(&'a self, pat: P) -> RSplitTerminator<'a, P>
3451     where
3452         P: Pattern<'a, Searcher: ReverseSearcher<'a>>,
3453     {
3454         RSplitTerminator(self.split_terminator(pat).0)
3455     }
3456
3457     /// An iterator over substrings of the given string slice, separated by a
3458     /// pattern, restricted to returning at most `n` items.
3459     ///
3460     /// If `n` substrings are returned, the last substring (the `n`th substring)
3461     /// will contain the remainder of the string.
3462     ///
3463     /// The pattern can be any type that implements the Pattern trait. Notable
3464     /// examples are `&str`, [`char`], and closures that determines the split.
3465     ///
3466     /// # Iterator behavior
3467     ///
3468     /// The returned iterator will not be double ended, because it is
3469     /// not efficient to support.
3470     ///
3471     /// If the pattern allows a reverse search, the [`rsplitn`] method can be
3472     /// used.
3473     ///
3474     /// [`rsplitn`]: #method.rsplitn
3475     ///
3476     /// # Examples
3477     ///
3478     /// Simple patterns:
3479     ///
3480     /// ```
3481     /// let v: Vec<&str> = "Mary had a little lambda".splitn(3, ' ').collect();
3482     /// assert_eq!(v, ["Mary", "had", "a little lambda"]);
3483     ///
3484     /// let v: Vec<&str> = "lionXXtigerXleopard".splitn(3, "X").collect();
3485     /// assert_eq!(v, ["lion", "", "tigerXleopard"]);
3486     ///
3487     /// let v: Vec<&str> = "abcXdef".splitn(1, 'X').collect();
3488     /// assert_eq!(v, ["abcXdef"]);
3489     ///
3490     /// let v: Vec<&str> = "".splitn(1, 'X').collect();
3491     /// assert_eq!(v, [""]);
3492     /// ```
3493     ///
3494     /// A more complex pattern, using a closure:
3495     ///
3496     /// ```
3497     /// let v: Vec<&str> = "abc1defXghi".splitn(2, |c| c == '1' || c == 'X').collect();
3498     /// assert_eq!(v, ["abc", "defXghi"]);
3499     /// ```
3500     #[stable(feature = "rust1", since = "1.0.0")]
3501     #[inline]
3502     pub fn splitn<'a, P: Pattern<'a>>(&'a self, n: usize, pat: P) -> SplitN<'a, P> {
3503         SplitN(SplitNInternal { iter: self.split(pat).0, count: n })
3504     }
3505
3506     /// An iterator over substrings of this string slice, separated by a
3507     /// pattern, starting from the end of the string, restricted to returning
3508     /// at most `n` items.
3509     ///
3510     /// If `n` substrings are returned, the last substring (the `n`th substring)
3511     /// will contain the remainder of the string.
3512     ///
3513     /// The pattern can be any type that implements the Pattern trait. Notable
3514     /// examples are `&str`, [`char`], and closures that determines the split.
3515     ///
3516     /// # Iterator behavior
3517     ///
3518     /// The returned iterator will not be double ended, because it is not
3519     /// efficient to support.
3520     ///
3521     /// For splitting from the front, the [`splitn`] method can be used.
3522     ///
3523     /// [`splitn`]: #method.splitn
3524     ///
3525     /// # Examples
3526     ///
3527     /// Simple patterns:
3528     ///
3529     /// ```
3530     /// let v: Vec<&str> = "Mary had a little lamb".rsplitn(3, ' ').collect();
3531     /// assert_eq!(v, ["lamb", "little", "Mary had a"]);
3532     ///
3533     /// let v: Vec<&str> = "lionXXtigerXleopard".rsplitn(3, 'X').collect();
3534     /// assert_eq!(v, ["leopard", "tiger", "lionX"]);
3535     ///
3536     /// let v: Vec<&str> = "lion::tiger::leopard".rsplitn(2, "::").collect();
3537     /// assert_eq!(v, ["leopard", "lion::tiger"]);
3538     /// ```
3539     ///
3540     /// A more complex pattern, using a closure:
3541     ///
3542     /// ```
3543     /// let v: Vec<&str> = "abc1defXghi".rsplitn(2, |c| c == '1' || c == 'X').collect();
3544     /// assert_eq!(v, ["ghi", "abc1def"]);
3545     /// ```
3546     #[stable(feature = "rust1", since = "1.0.0")]
3547     #[inline]
3548     pub fn rsplitn<'a, P>(&'a self, n: usize, pat: P) -> RSplitN<'a, P>
3549     where
3550         P: Pattern<'a, Searcher: ReverseSearcher<'a>>,
3551     {
3552         RSplitN(self.splitn(n, pat).0)
3553     }
3554
3555     /// An iterator over the disjoint matches of a pattern within the given string
3556     /// slice.
3557     ///
3558     /// The pattern can be a `&str`, [`char`], or a closure that determines if
3559     /// a character matches.
3560     ///
3561     /// # Iterator behavior
3562     ///
3563     /// The returned iterator will be a [`DoubleEndedIterator`] if the pattern
3564     /// allows a reverse search and forward/reverse search yields the same
3565     /// elements. This is true for, e.g., [`char`], but not for `&str`.
3566     ///
3567     /// [`DoubleEndedIterator`]: iter/trait.DoubleEndedIterator.html
3568     ///
3569     /// If the pattern allows a reverse search but its results might differ
3570     /// from a forward search, the [`rmatches`] method can be used.
3571     ///
3572     /// [`rmatches`]: #method.rmatches
3573     ///
3574     /// # Examples
3575     ///
3576     /// Basic usage:
3577     ///
3578     /// ```
3579     /// let v: Vec<&str> = "abcXXXabcYYYabc".matches("abc").collect();
3580     /// assert_eq!(v, ["abc", "abc", "abc"]);
3581     ///
3582     /// let v: Vec<&str> = "1abc2abc3".matches(char::is_numeric).collect();
3583     /// assert_eq!(v, ["1", "2", "3"]);
3584     /// ```
3585     #[stable(feature = "str_matches", since = "1.2.0")]
3586     #[inline]
3587     pub fn matches<'a, P: Pattern<'a>>(&'a self, pat: P) -> Matches<'a, P> {
3588         Matches(MatchesInternal(pat.into_searcher(self)))
3589     }
3590
3591     /// An iterator over the disjoint matches of a pattern within this string slice,
3592     /// yielded in reverse order.
3593     ///
3594     /// The pattern can be a `&str`, [`char`], or a closure that determines if
3595     /// a character matches.
3596     ///
3597     /// # Iterator behavior
3598     ///
3599     /// The returned iterator requires that the pattern supports a reverse
3600     /// search, and it will be a [`DoubleEndedIterator`] if a forward/reverse
3601     /// search yields the same elements.
3602     ///
3603     /// [`DoubleEndedIterator`]: iter/trait.DoubleEndedIterator.html
3604     ///
3605     /// For iterating from the front, the [`matches`] method can be used.
3606     ///
3607     /// [`matches`]: #method.matches
3608     ///
3609     /// # Examples
3610     ///
3611     /// Basic usage:
3612     ///
3613     /// ```
3614     /// let v: Vec<&str> = "abcXXXabcYYYabc".rmatches("abc").collect();
3615     /// assert_eq!(v, ["abc", "abc", "abc"]);
3616     ///
3617     /// let v: Vec<&str> = "1abc2abc3".rmatches(char::is_numeric).collect();
3618     /// assert_eq!(v, ["3", "2", "1"]);
3619     /// ```
3620     #[stable(feature = "str_matches", since = "1.2.0")]
3621     #[inline]
3622     pub fn rmatches<'a, P>(&'a self, pat: P) -> RMatches<'a, P>
3623     where
3624         P: Pattern<'a, Searcher: ReverseSearcher<'a>>,
3625     {
3626         RMatches(self.matches(pat).0)
3627     }
3628
3629     /// An iterator over the disjoint matches of a pattern within this string
3630     /// slice as well as the index that the match starts at.
3631     ///
3632     /// For matches of `pat` within `self` that overlap, only the indices
3633     /// corresponding to the first match are returned.
3634     ///
3635     /// The pattern can be a `&str`, [`char`], or a closure that determines
3636     /// if a character matches.
3637     ///
3638     /// # Iterator behavior
3639     ///
3640     /// The returned iterator will be a [`DoubleEndedIterator`] if the pattern
3641     /// allows a reverse search and forward/reverse search yields the same
3642     /// elements. This is true for, e.g., [`char`], but not for `&str`.
3643     ///
3644     /// [`DoubleEndedIterator`]: iter/trait.DoubleEndedIterator.html
3645     ///
3646     /// If the pattern allows a reverse search but its results might differ
3647     /// from a forward search, the [`rmatch_indices`] method can be used.
3648     ///
3649     /// [`rmatch_indices`]: #method.rmatch_indices
3650     ///
3651     /// # Examples
3652     ///
3653     /// Basic usage:
3654     ///
3655     /// ```
3656     /// let v: Vec<_> = "abcXXXabcYYYabc".match_indices("abc").collect();
3657     /// assert_eq!(v, [(0, "abc"), (6, "abc"), (12, "abc")]);
3658     ///
3659     /// let v: Vec<_> = "1abcabc2".match_indices("abc").collect();
3660     /// assert_eq!(v, [(1, "abc"), (4, "abc")]);
3661     ///
3662     /// let v: Vec<_> = "ababa".match_indices("aba").collect();
3663     /// assert_eq!(v, [(0, "aba")]); // only the first `aba`
3664     /// ```
3665     #[stable(feature = "str_match_indices", since = "1.5.0")]
3666     #[inline]
3667     pub fn match_indices<'a, P: Pattern<'a>>(&'a self, pat: P) -> MatchIndices<'a, P> {
3668         MatchIndices(MatchIndicesInternal(pat.into_searcher(self)))
3669     }
3670
3671     /// An iterator over the disjoint matches of a pattern within `self`,
3672     /// yielded in reverse order along with the index of the match.
3673     ///
3674     /// For matches of `pat` within `self` that overlap, only the indices
3675     /// corresponding to the last match are returned.
3676     ///
3677     /// The pattern can be a `&str`, [`char`], or a closure that determines if a
3678     /// character matches.
3679     ///
3680     /// # Iterator behavior
3681     ///
3682     /// The returned iterator requires that the pattern supports a reverse
3683     /// search, and it will be a [`DoubleEndedIterator`] if a forward/reverse
3684     /// search yields the same elements.
3685     ///
3686     /// [`DoubleEndedIterator`]: iter/trait.DoubleEndedIterator.html
3687     ///
3688     /// For iterating from the front, the [`match_indices`] method can be used.
3689     ///
3690     /// [`match_indices`]: #method.match_indices
3691     ///
3692     /// # Examples
3693     ///
3694     /// Basic usage:
3695     ///
3696     /// ```
3697     /// let v: Vec<_> = "abcXXXabcYYYabc".rmatch_indices("abc").collect();
3698     /// assert_eq!(v, [(12, "abc"), (6, "abc"), (0, "abc")]);
3699     ///
3700     /// let v: Vec<_> = "1abcabc2".rmatch_indices("abc").collect();
3701     /// assert_eq!(v, [(4, "abc"), (1, "abc")]);
3702     ///
3703     /// let v: Vec<_> = "ababa".rmatch_indices("aba").collect();
3704     /// assert_eq!(v, [(2, "aba")]); // only the last `aba`
3705     /// ```
3706     #[stable(feature = "str_match_indices", since = "1.5.0")]
3707     #[inline]
3708     pub fn rmatch_indices<'a, P>(&'a self, pat: P) -> RMatchIndices<'a, P>
3709     where
3710         P: Pattern<'a, Searcher: ReverseSearcher<'a>>,
3711     {
3712         RMatchIndices(self.match_indices(pat).0)
3713     }
3714
3715     /// Returns a string slice with leading and trailing whitespace removed.
3716     ///
3717     /// 'Whitespace' is defined according to the terms of the Unicode Derived
3718     /// Core Property `White_Space`.
3719     ///
3720     /// # Examples
3721     ///
3722     /// Basic usage:
3723     ///
3724     /// ```
3725     /// let s = " Hello\tworld\t";
3726     ///
3727     /// assert_eq!("Hello\tworld", s.trim());
3728     /// ```
3729     #[must_use = "this returns the trimmed string as a slice, \
3730                   without modifying the original"]
3731     #[stable(feature = "rust1", since = "1.0.0")]
3732     pub fn trim(&self) -> &str {
3733         self.trim_matches(|c: char| c.is_whitespace())
3734     }
3735
3736     /// Returns a string slice with leading whitespace removed.
3737     ///
3738     /// 'Whitespace' is defined according to the terms of the Unicode Derived
3739     /// Core Property `White_Space`.
3740     ///
3741     /// # Text directionality
3742     ///
3743     /// A string is a sequence of bytes. `start` in this context means the first
3744     /// position of that byte string; for a left-to-right language like English or
3745     /// Russian, this will be left side, and for right-to-left languages like
3746     /// Arabic or Hebrew, this will be the right side.
3747     ///
3748     /// # Examples
3749     ///
3750     /// Basic usage:
3751     ///
3752     /// ```
3753     /// let s = " Hello\tworld\t";
3754     /// assert_eq!("Hello\tworld\t", s.trim_start());
3755     /// ```
3756     ///
3757     /// Directionality:
3758     ///
3759     /// ```
3760     /// let s = "  English  ";
3761     /// assert!(Some('E') == s.trim_start().chars().next());
3762     ///
3763     /// let s = "  עברית  ";
3764     /// assert!(Some('ע') == s.trim_start().chars().next());
3765     /// ```
3766     #[must_use = "this returns the trimmed string as a new slice, \
3767                   without modifying the original"]
3768     #[stable(feature = "trim_direction", since = "1.30.0")]
3769     pub fn trim_start(&self) -> &str {
3770         self.trim_start_matches(|c: char| c.is_whitespace())
3771     }
3772
3773     /// Returns a string slice with trailing whitespace removed.
3774     ///
3775     /// 'Whitespace' is defined according to the terms of the Unicode Derived
3776     /// Core Property `White_Space`.
3777     ///
3778     /// # Text directionality
3779     ///
3780     /// A string is a sequence of bytes. `end` in this context means the last
3781     /// position of that byte string; for a left-to-right language like English or
3782     /// Russian, this will be right side, and for right-to-left languages like
3783     /// Arabic or Hebrew, this will be the left side.
3784     ///
3785     /// # Examples
3786     ///
3787     /// Basic usage:
3788     ///
3789     /// ```
3790     /// let s = " Hello\tworld\t";
3791     /// assert_eq!(" Hello\tworld", s.trim_end());
3792     /// ```
3793     ///
3794     /// Directionality:
3795     ///
3796     /// ```
3797     /// let s = "  English  ";
3798     /// assert!(Some('h') == s.trim_end().chars().rev().next());
3799     ///
3800     /// let s = "  עברית  ";
3801     /// assert!(Some('ת') == s.trim_end().chars().rev().next());
3802     /// ```
3803     #[must_use = "this returns the trimmed string as a new slice, \
3804                   without modifying the original"]
3805     #[stable(feature = "trim_direction", since = "1.30.0")]
3806     pub fn trim_end(&self) -> &str {
3807         self.trim_end_matches(|c: char| c.is_whitespace())
3808     }
3809
3810     /// Returns a string slice with leading whitespace removed.
3811     ///
3812     /// 'Whitespace' is defined according to the terms of the Unicode Derived
3813     /// Core Property `White_Space`.
3814     ///
3815     /// # Text directionality
3816     ///
3817     /// A string is a sequence of bytes. 'Left' in this context means the first
3818     /// position of that byte string; for a language like Arabic or Hebrew
3819     /// which are 'right to left' rather than 'left to right', this will be
3820     /// the _right_ side, not the left.
3821     ///
3822     /// # Examples
3823     ///
3824     /// Basic usage:
3825     ///
3826     /// ```
3827     /// let s = " Hello\tworld\t";
3828     ///
3829     /// assert_eq!("Hello\tworld\t", s.trim_left());
3830     /// ```
3831     ///
3832     /// Directionality:
3833     ///
3834     /// ```
3835     /// let s = "  English";
3836     /// assert!(Some('E') == s.trim_left().chars().next());
3837     ///
3838     /// let s = "  עברית";
3839     /// assert!(Some('ע') == s.trim_left().chars().next());
3840     /// ```
3841     #[stable(feature = "rust1", since = "1.0.0")]
3842     #[rustc_deprecated(
3843         since = "1.33.0",
3844         reason = "superseded by `trim_start`",
3845         suggestion = "trim_start"
3846     )]
3847     pub fn trim_left(&self) -> &str {
3848         self.trim_start()
3849     }
3850
3851     /// Returns a string slice with trailing whitespace removed.
3852     ///
3853     /// 'Whitespace' is defined according to the terms of the Unicode Derived
3854     /// Core Property `White_Space`.
3855     ///
3856     /// # Text directionality
3857     ///
3858     /// A string is a sequence of bytes. 'Right' in this context means the last
3859     /// position of that byte string; for a language like Arabic or Hebrew
3860     /// which are 'right to left' rather than 'left to right', this will be
3861     /// the _left_ side, not the right.
3862     ///
3863     /// # Examples
3864     ///
3865     /// Basic usage:
3866     ///
3867     /// ```
3868     /// let s = " Hello\tworld\t";
3869     ///
3870     /// assert_eq!(" Hello\tworld", s.trim_right());
3871     /// ```
3872     ///
3873     /// Directionality:
3874     ///
3875     /// ```
3876     /// let s = "English  ";
3877     /// assert!(Some('h') == s.trim_right().chars().rev().next());
3878     ///
3879     /// let s = "עברית  ";
3880     /// assert!(Some('ת') == s.trim_right().chars().rev().next());
3881     /// ```
3882     #[stable(feature = "rust1", since = "1.0.0")]
3883     #[rustc_deprecated(
3884         since = "1.33.0",
3885         reason = "superseded by `trim_end`",
3886         suggestion = "trim_end"
3887     )]
3888     pub fn trim_right(&self) -> &str {
3889         self.trim_end()
3890     }
3891
3892     /// Returns a string slice with all prefixes and suffixes that match a
3893     /// pattern repeatedly removed.
3894     ///
3895     /// The pattern can be a [`char`] or a closure that determines if a
3896     /// character matches.
3897     ///
3898     /// # Examples
3899     ///
3900     /// Simple patterns:
3901     ///
3902     /// ```
3903     /// assert_eq!("11foo1bar11".trim_matches('1'), "foo1bar");
3904     /// assert_eq!("123foo1bar123".trim_matches(char::is_numeric), "foo1bar");
3905     ///
3906     /// let x: &[_] = &['1', '2'];
3907     /// assert_eq!("12foo1bar12".trim_matches(x), "foo1bar");
3908     /// ```
3909     ///
3910     /// A more complex pattern, using a closure:
3911     ///
3912     /// ```
3913     /// assert_eq!("1foo1barXX".trim_matches(|c| c == '1' || c == 'X'), "foo1bar");
3914     /// ```
3915     #[must_use = "this returns the trimmed string as a new slice, \
3916                   without modifying the original"]
3917     #[stable(feature = "rust1", since = "1.0.0")]
3918     pub fn trim_matches<'a, P>(&'a self, pat: P) -> &'a str
3919     where
3920         P: Pattern<'a, Searcher: DoubleEndedSearcher<'a>>,
3921     {
3922         let mut i = 0;
3923         let mut j = 0;
3924         let mut matcher = pat.into_searcher(self);
3925         if let Some((a, b)) = matcher.next_reject() {
3926             i = a;
3927             j = b; // Remember earliest known match, correct it below if
3928             // last match is different
3929         }
3930         if let Some((_, b)) = matcher.next_reject_back() {
3931             j = b;
3932         }
3933         // SAFETY: `Searcher` is known to return valid indices.
3934         unsafe { self.get_unchecked(i..j) }
3935     }
3936
3937     /// Returns a string slice with all prefixes that match a pattern
3938     /// repeatedly removed.
3939     ///
3940     /// The pattern can be a `&str`, [`char`], or a closure that determines if
3941     /// a character matches.
3942     ///
3943     /// # Text directionality
3944     ///
3945     /// A string is a sequence of bytes. `start` in this context means the first
3946     /// position of that byte string; for a left-to-right language like English or
3947     /// Russian, this will be left side, and for right-to-left languages like
3948     /// Arabic or Hebrew, this will be the right side.
3949     ///
3950     /// # Examples
3951     ///
3952     /// Basic usage:
3953     ///
3954     /// ```
3955     /// assert_eq!("11foo1bar11".trim_start_matches('1'), "foo1bar11");
3956     /// assert_eq!("123foo1bar123".trim_start_matches(char::is_numeric), "foo1bar123");
3957     ///
3958     /// let x: &[_] = &['1', '2'];
3959     /// assert_eq!("12foo1bar12".trim_start_matches(x), "foo1bar12");
3960     /// ```
3961     #[must_use = "this returns the trimmed string as a new slice, \
3962                   without modifying the original"]
3963     #[stable(feature = "trim_direction", since = "1.30.0")]
3964     pub fn trim_start_matches<'a, P: Pattern<'a>>(&'a self, pat: P) -> &'a str {
3965         let mut i = self.len();
3966         let mut matcher = pat.into_searcher(self);
3967         if let Some((a, _)) = matcher.next_reject() {
3968             i = a;
3969         }
3970         // SAFETY: `Searcher` is known to return valid indices.
3971         unsafe { self.get_unchecked(i..self.len()) }
3972     }
3973
3974     /// Returns a string slice with the prefix removed.
3975     ///
3976     /// If the string starts with the pattern `prefix`, `Some` is returned with the substring where
3977     /// the prefix is removed. Unlike `trim_start_matches`, this method removes the prefix exactly
3978     /// once.
3979     ///
3980     /// If the string does not start with `prefix`, `None` is returned.
3981     ///
3982     /// # Examples
3983     ///
3984     /// ```
3985     /// #![feature(str_strip)]
3986     ///
3987     /// assert_eq!("foobar".strip_prefix("foo"), Some("bar"));
3988     /// assert_eq!("foobar".strip_prefix("bar"), None);
3989     /// assert_eq!("foofoo".strip_prefix("foo"), Some("foo"));
3990     /// ```
3991     #[must_use = "this returns the remaining substring as a new slice, \
3992                   without modifying the original"]
3993     #[unstable(feature = "str_strip", reason = "newly added", issue = "67302")]
3994     pub fn strip_prefix<'a, P: Pattern<'a>>(&'a self, prefix: P) -> Option<&'a str> {
3995         let mut matcher = prefix.into_searcher(self);
3996         if let SearchStep::Match(start, len) = matcher.next() {
3997             debug_assert_eq!(
3998                 start, 0,
3999                 "The first search step from Searcher \
4000                  must include the first character"
4001             );
4002             // SAFETY: `Searcher` is known to return valid indices.
4003             unsafe { Some(self.get_unchecked(len..)) }
4004         } else {
4005             None
4006         }
4007     }
4008
4009     /// Returns a string slice with the suffix removed.
4010     ///
4011     /// If the string ends with the pattern `suffix`, `Some` is returned with the substring where
4012     /// the suffix is removed. Unlike `trim_end_matches`, this method removes the suffix exactly
4013     /// once.
4014     ///
4015     /// If the string does not end with `suffix`, `None` is returned.
4016     ///
4017     /// # Examples
4018     ///
4019     /// ```
4020     /// #![feature(str_strip)]
4021     /// assert_eq!("barfoo".strip_suffix("foo"), Some("bar"));
4022     /// assert_eq!("barfoo".strip_suffix("bar"), None);
4023     /// assert_eq!("foofoo".strip_suffix("foo"), Some("foo"));
4024     /// ```
4025     #[must_use = "this returns the remaining substring as a new slice, \
4026                   without modifying the original"]
4027     #[unstable(feature = "str_strip", reason = "newly added", issue = "67302")]
4028     pub fn strip_suffix<'a, P>(&'a self, suffix: P) -> Option<&'a str>
4029     where
4030         P: Pattern<'a>,
4031         <P as Pattern<'a>>::Searcher: ReverseSearcher<'a>,
4032     {
4033         let mut matcher = suffix.into_searcher(self);
4034         if let SearchStep::Match(start, end) = matcher.next_back() {
4035             debug_assert_eq!(
4036                 end,
4037                 self.len(),
4038                 "The first search step from ReverseSearcher \
4039                  must include the last character"
4040             );
4041             // SAFETY: `Searcher` is known to return valid indices.
4042             unsafe { Some(self.get_unchecked(..start)) }
4043         } else {
4044             None
4045         }
4046     }
4047
4048     /// Returns a string slice with all suffixes that match a pattern
4049     /// repeatedly removed.
4050     ///
4051     /// The pattern can be a `&str`, [`char`], or a closure that
4052     /// determines if a character matches.
4053     ///
4054     /// # Text directionality
4055     ///
4056     /// A string is a sequence of bytes. `end` in this context means the last
4057     /// position of that byte string; for a left-to-right language like English or
4058     /// Russian, this will be right side, and for right-to-left languages like
4059     /// Arabic or Hebrew, this will be the left side.
4060     ///
4061     /// # Examples
4062     ///
4063     /// Simple patterns:
4064     ///
4065     /// ```
4066     /// assert_eq!("11foo1bar11".trim_end_matches('1'), "11foo1bar");
4067     /// assert_eq!("123foo1bar123".trim_end_matches(char::is_numeric), "123foo1bar");
4068     ///
4069     /// let x: &[_] = &['1', '2'];
4070     /// assert_eq!("12foo1bar12".trim_end_matches(x), "12foo1bar");
4071     /// ```
4072     ///
4073     /// A more complex pattern, using a closure:
4074     ///
4075     /// ```
4076     /// assert_eq!("1fooX".trim_end_matches(|c| c == '1' || c == 'X'), "1foo");
4077     /// ```
4078     #[must_use = "this returns the trimmed string as a new slice, \
4079                   without modifying the original"]
4080     #[stable(feature = "trim_direction", since = "1.30.0")]
4081     pub fn trim_end_matches<'a, P>(&'a self, pat: P) -> &'a str
4082     where
4083         P: Pattern<'a, Searcher: ReverseSearcher<'a>>,
4084     {
4085         let mut j = 0;
4086         let mut matcher = pat.into_searcher(self);
4087         if let Some((_, b)) = matcher.next_reject_back() {
4088             j = b;
4089         }
4090         // SAFETY: `Searcher` is known to return valid indices.
4091         unsafe { self.get_unchecked(0..j) }
4092     }
4093
4094     /// Returns a string slice with all prefixes that match a pattern
4095     /// repeatedly removed.
4096     ///
4097     /// The pattern can be a `&str`, [`char`], or a closure that determines if
4098     /// a character matches.
4099     ///
4100     /// [`char`]: primitive.char.html
4101     ///
4102     /// # Text directionality
4103     ///
4104     /// A string is a sequence of bytes. 'Left' in this context means the first
4105     /// position of that byte string; for a language like Arabic or Hebrew
4106     /// which are 'right to left' rather than 'left to right', this will be
4107     /// the _right_ side, not the left.
4108     ///
4109     /// # Examples
4110     ///
4111     /// Basic usage:
4112     ///
4113     /// ```
4114     /// assert_eq!("11foo1bar11".trim_left_matches('1'), "foo1bar11");
4115     /// assert_eq!("123foo1bar123".trim_left_matches(char::is_numeric), "foo1bar123");
4116     ///
4117     /// let x: &[_] = &['1', '2'];
4118     /// assert_eq!("12foo1bar12".trim_left_matches(x), "foo1bar12");
4119     /// ```
4120     #[stable(feature = "rust1", since = "1.0.0")]
4121     #[rustc_deprecated(
4122         since = "1.33.0",
4123         reason = "superseded by `trim_start_matches`",
4124         suggestion = "trim_start_matches"
4125     )]
4126     pub fn trim_left_matches<'a, P: Pattern<'a>>(&'a self, pat: P) -> &'a str {
4127         self.trim_start_matches(pat)
4128     }
4129
4130     /// Returns a string slice with all suffixes that match a pattern
4131     /// repeatedly removed.
4132     ///
4133     /// The pattern can be a `&str`, [`char`], or a closure that
4134     /// determines if a character matches.
4135     ///
4136     /// [`char`]: primitive.char.html
4137     ///
4138     /// # Text directionality
4139     ///
4140     /// A string is a sequence of bytes. 'Right' in this context means the last
4141     /// position of that byte string; for a language like Arabic or Hebrew
4142     /// which are 'right to left' rather than 'left to right', this will be
4143     /// the _left_ side, not the right.
4144     ///
4145     /// # Examples
4146     ///
4147     /// Simple patterns:
4148     ///
4149     /// ```
4150     /// assert_eq!("11foo1bar11".trim_right_matches('1'), "11foo1bar");
4151     /// assert_eq!("123foo1bar123".trim_right_matches(char::is_numeric), "123foo1bar");
4152     ///
4153     /// let x: &[_] = &['1', '2'];
4154     /// assert_eq!("12foo1bar12".trim_right_matches(x), "12foo1bar");
4155     /// ```
4156     ///
4157     /// A more complex pattern, using a closure:
4158     ///
4159     /// ```
4160     /// assert_eq!("1fooX".trim_right_matches(|c| c == '1' || c == 'X'), "1foo");
4161     /// ```
4162     #[stable(feature = "rust1", since = "1.0.0")]
4163     #[rustc_deprecated(
4164         since = "1.33.0",
4165         reason = "superseded by `trim_end_matches`",
4166         suggestion = "trim_end_matches"
4167     )]
4168     pub fn trim_right_matches<'a, P>(&'a self, pat: P) -> &'a str
4169     where
4170         P: Pattern<'a, Searcher: ReverseSearcher<'a>>,
4171     {
4172         self.trim_end_matches(pat)
4173     }
4174
4175     /// Parses this string slice into another type.
4176     ///
4177     /// Because `parse` is so general, it can cause problems with type
4178     /// inference. As such, `parse` is one of the few times you'll see
4179     /// the syntax affectionately known as the 'turbofish': `::<>`. This
4180     /// helps the inference algorithm understand specifically which type
4181     /// you're trying to parse into.
4182     ///
4183     /// `parse` can parse any type that implements the [`FromStr`] trait.
4184     ///
4185     /// [`FromStr`]: str/trait.FromStr.html
4186     ///
4187     /// # Errors
4188     ///
4189     /// Will return [`Err`] if it's not possible to parse this string slice into
4190     /// the desired type.
4191     ///
4192     /// [`Err`]: str/trait.FromStr.html#associatedtype.Err
4193     ///
4194     /// # Examples
4195     ///
4196     /// Basic usage
4197     ///
4198     /// ```
4199     /// let four: u32 = "4".parse().unwrap();
4200     ///
4201     /// assert_eq!(4, four);
4202     /// ```
4203     ///
4204     /// Using the 'turbofish' instead of annotating `four`:
4205     ///
4206     /// ```
4207     /// let four = "4".parse::<u32>();
4208     ///
4209     /// assert_eq!(Ok(4), four);
4210     /// ```
4211     ///
4212     /// Failing to parse:
4213     ///
4214     /// ```
4215     /// let nope = "j".parse::<u32>();
4216     ///
4217     /// assert!(nope.is_err());
4218     /// ```
4219     #[inline]
4220     #[stable(feature = "rust1", since = "1.0.0")]
4221     pub fn parse<F: FromStr>(&self) -> Result<F, F::Err> {
4222         FromStr::from_str(self)
4223     }
4224
4225     /// Checks if all characters in this string are within the ASCII range.
4226     ///
4227     /// # Examples
4228     ///
4229     /// ```
4230     /// let ascii = "hello!\n";
4231     /// let non_ascii = "Grüße, Jürgen ❤";
4232     ///
4233     /// assert!(ascii.is_ascii());
4234     /// assert!(!non_ascii.is_ascii());
4235     /// ```
4236     #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
4237     #[inline]
4238     pub fn is_ascii(&self) -> bool {
4239         // We can treat each byte as character here: all multibyte characters
4240         // start with a byte that is not in the ascii range, so we will stop
4241         // there already.
4242         self.bytes().all(|b| b.is_ascii())
4243     }
4244
4245     /// Checks that two strings are an ASCII case-insensitive match.
4246     ///
4247     /// Same as `to_ascii_lowercase(a) == to_ascii_lowercase(b)`,
4248     /// but without allocating and copying temporaries.
4249     ///
4250     /// # Examples
4251     ///
4252     /// ```
4253     /// assert!("Ferris".eq_ignore_ascii_case("FERRIS"));
4254     /// assert!("Ferrös".eq_ignore_ascii_case("FERRöS"));
4255     /// assert!(!"Ferrös".eq_ignore_ascii_case("FERRÖS"));
4256     /// ```
4257     #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
4258     #[inline]
4259     pub fn eq_ignore_ascii_case(&self, other: &str) -> bool {
4260         self.as_bytes().eq_ignore_ascii_case(other.as_bytes())
4261     }
4262
4263     /// Converts this string to its ASCII upper case equivalent in-place.
4264     ///
4265     /// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z',
4266     /// but non-ASCII letters are unchanged.
4267     ///
4268     /// To return a new uppercased value without modifying the existing one, use
4269     /// [`to_ascii_uppercase`].
4270     ///
4271     /// [`to_ascii_uppercase`]: #method.to_ascii_uppercase
4272     ///
4273     /// # Examples
4274     ///
4275     /// ```
4276     /// let mut s = String::from("Grüße, Jürgen ❤");
4277     ///
4278     /// s.make_ascii_uppercase();
4279     ///
4280     /// assert_eq!("GRüßE, JüRGEN ❤", s);
4281     /// ```
4282     #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
4283     pub fn make_ascii_uppercase(&mut self) {
4284         // SAFETY: safe because we transmute two types with the same layout.
4285         let me = unsafe { self.as_bytes_mut() };
4286         me.make_ascii_uppercase()
4287     }
4288
4289     /// Converts this string to its ASCII lower case equivalent in-place.
4290     ///
4291     /// ASCII letters 'A' to 'Z' are mapped to 'a' to 'z',
4292     /// but non-ASCII letters are unchanged.
4293     ///
4294     /// To return a new lowercased value without modifying the existing one, use
4295     /// [`to_ascii_lowercase`].
4296     ///
4297     /// [`to_ascii_lowercase`]: #method.to_ascii_lowercase
4298     ///
4299     /// # Examples
4300     ///
4301     /// ```
4302     /// let mut s = String::from("GRÜßE, JÜRGEN ❤");
4303     ///
4304     /// s.make_ascii_lowercase();
4305     ///
4306     /// assert_eq!("grÜße, jÜrgen ❤", s);
4307     /// ```
4308     #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
4309     pub fn make_ascii_lowercase(&mut self) {
4310         // SAFETY: safe because we transmute two types with the same layout.
4311         let me = unsafe { self.as_bytes_mut() };
4312         me.make_ascii_lowercase()
4313     }
4314
4315     /// Return an iterator that escapes each char in `self` with [`char::escape_debug`].
4316     ///
4317     /// Note: only extended grapheme codepoints that begin the string will be
4318     /// escaped.
4319     ///
4320     /// [`char::escape_debug`]: ../std/primitive.char.html#method.escape_debug
4321     ///
4322     /// # Examples
4323     ///
4324     /// As an iterator:
4325     ///
4326     /// ```
4327     /// for c in "❤\n!".escape_debug() {
4328     ///     print!("{}", c);
4329     /// }
4330     /// println!();
4331     /// ```
4332     ///
4333     /// Using `println!` directly:
4334     ///
4335     /// ```
4336     /// println!("{}", "❤\n!".escape_debug());
4337     /// ```
4338     ///
4339     ///
4340     /// Both are equivalent to:
4341     ///
4342     /// ```
4343     /// println!("❤\\n!");
4344     /// ```
4345     ///
4346     /// Using `to_string`:
4347     ///
4348     /// ```
4349     /// assert_eq!("❤\n!".escape_debug().to_string(), "❤\\n!");
4350     /// ```
4351     #[stable(feature = "str_escape", since = "1.34.0")]
4352     pub fn escape_debug(&self) -> EscapeDebug<'_> {
4353         let mut chars = self.chars();
4354         EscapeDebug {
4355             inner: chars
4356                 .next()
4357                 .map(|first| first.escape_debug_ext(true))
4358                 .into_iter()
4359                 .flatten()
4360                 .chain(chars.flat_map(CharEscapeDebugContinue)),
4361         }
4362     }
4363
4364     /// Return an iterator that escapes each char in `self` with [`char::escape_default`].
4365     ///
4366     /// [`char::escape_default`]: ../std/primitive.char.html#method.escape_default
4367     ///
4368     /// # Examples
4369     ///
4370     /// As an iterator:
4371     ///
4372     /// ```
4373     /// for c in "❤\n!".escape_default() {
4374     ///     print!("{}", c);
4375     /// }
4376     /// println!();
4377     /// ```
4378     ///
4379     /// Using `println!` directly:
4380     ///
4381     /// ```
4382     /// println!("{}", "❤\n!".escape_default());
4383     /// ```
4384     ///
4385     ///
4386     /// Both are equivalent to:
4387     ///
4388     /// ```
4389     /// println!("\\u{{2764}}\\n!");
4390     /// ```
4391     ///
4392     /// Using `to_string`:
4393     ///
4394     /// ```
4395     /// assert_eq!("❤\n!".escape_default().to_string(), "\\u{2764}\\n!");
4396     /// ```
4397     #[stable(feature = "str_escape", since = "1.34.0")]
4398     pub fn escape_default(&self) -> EscapeDefault<'_> {
4399         EscapeDefault { inner: self.chars().flat_map(CharEscapeDefault) }
4400     }
4401
4402     /// Return an iterator that escapes each char in `self` with [`char::escape_unicode`].
4403     ///
4404     /// [`char::escape_unicode`]: ../std/primitive.char.html#method.escape_unicode
4405     ///
4406     /// # Examples
4407     ///
4408     /// As an iterator:
4409     ///
4410     /// ```
4411     /// for c in "❤\n!".escape_unicode() {
4412     ///     print!("{}", c);
4413     /// }
4414     /// println!();
4415     /// ```
4416     ///
4417     /// Using `println!` directly:
4418     ///
4419     /// ```
4420     /// println!("{}", "❤\n!".escape_unicode());
4421     /// ```
4422     ///
4423     ///
4424     /// Both are equivalent to:
4425     ///
4426     /// ```
4427     /// println!("\\u{{2764}}\\u{{a}}\\u{{21}}");
4428     /// ```
4429     ///
4430     /// Using `to_string`:
4431     ///
4432     /// ```
4433     /// assert_eq!("❤\n!".escape_unicode().to_string(), "\\u{2764}\\u{a}\\u{21}");
4434     /// ```
4435     #[stable(feature = "str_escape", since = "1.34.0")]
4436     pub fn escape_unicode(&self) -> EscapeUnicode<'_> {
4437         EscapeUnicode { inner: self.chars().flat_map(CharEscapeUnicode) }
4438     }
4439 }
4440
4441 impl_fn_for_zst! {
4442     #[derive(Clone)]
4443     struct CharEscapeDebugContinue impl Fn = |c: char| -> char::EscapeDebug {
4444         c.escape_debug_ext(false)
4445     };
4446
4447     #[derive(Clone)]
4448     struct CharEscapeUnicode impl Fn = |c: char| -> char::EscapeUnicode {
4449         c.escape_unicode()
4450     };
4451     #[derive(Clone)]
4452     struct CharEscapeDefault impl Fn = |c: char| -> char::EscapeDefault {
4453         c.escape_default()
4454     };
4455 }
4456
4457 #[stable(feature = "rust1", since = "1.0.0")]
4458 impl AsRef<[u8]> for str {
4459     #[inline]
4460     fn as_ref(&self) -> &[u8] {
4461         self.as_bytes()
4462     }
4463 }
4464
4465 #[stable(feature = "rust1", since = "1.0.0")]
4466 impl Default for &str {
4467     /// Creates an empty str
4468     fn default() -> Self {
4469         ""
4470     }
4471 }
4472
4473 #[stable(feature = "default_mut_str", since = "1.28.0")]
4474 impl Default for &mut str {
4475     /// Creates an empty mutable str
4476     fn default() -> Self {
4477         // SAFETY: The empty string is valid UTF-8.
4478         unsafe { from_utf8_unchecked_mut(&mut []) }
4479     }
4480 }
4481
4482 /// An iterator over the non-whitespace substrings of a string,
4483 /// separated by any amount of whitespace.
4484 ///
4485 /// This struct is created by the [`split_whitespace`] method on [`str`].
4486 /// See its documentation for more.
4487 ///
4488 /// [`split_whitespace`]: ../../std/primitive.str.html#method.split_whitespace
4489 /// [`str`]: ../../std/primitive.str.html
4490 #[stable(feature = "split_whitespace", since = "1.1.0")]
4491 #[derive(Clone, Debug)]
4492 pub struct SplitWhitespace<'a> {
4493     inner: Filter<Split<'a, IsWhitespace>, IsNotEmpty>,
4494 }
4495
4496 /// An iterator over the non-ASCII-whitespace substrings of a string,
4497 /// separated by any amount of ASCII whitespace.
4498 ///
4499 /// This struct is created by the [`split_ascii_whitespace`] method on [`str`].
4500 /// See its documentation for more.
4501 ///
4502 /// [`split_ascii_whitespace`]: ../../std/primitive.str.html#method.split_ascii_whitespace
4503 /// [`str`]: ../../std/primitive.str.html
4504 #[stable(feature = "split_ascii_whitespace", since = "1.34.0")]
4505 #[derive(Clone, Debug)]
4506 pub struct SplitAsciiWhitespace<'a> {
4507     inner: Map<Filter<SliceSplit<'a, u8, IsAsciiWhitespace>, BytesIsNotEmpty>, UnsafeBytesToStr>,
4508 }
4509
4510 /// An iterator over the substrings of a string,
4511 /// terminated by a substring matching to a predicate function
4512 /// Unlike `Split`, it contains the matched part as a terminator
4513 /// of the subslice.
4514 ///
4515 /// This struct is created by the [`split_inclusive`] method on [`str`].
4516 /// See its documentation for more.
4517 ///
4518 /// [`split_inclusive`]: ../../std/primitive.str.html#method.split_inclusive
4519 /// [`str`]: ../../std/primitive.str.html
4520 #[unstable(feature = "split_inclusive", issue = "none")]
4521 pub struct SplitInclusive<'a, P: Pattern<'a>>(SplitInternal<'a, P>);
4522
4523 impl_fn_for_zst! {
4524     #[derive(Clone)]
4525     struct IsWhitespace impl Fn = |c: char| -> bool {
4526         c.is_whitespace()
4527     };
4528
4529     #[derive(Clone)]
4530     struct IsAsciiWhitespace impl Fn = |byte: &u8| -> bool {
4531         byte.is_ascii_whitespace()
4532     };
4533
4534     #[derive(Clone)]
4535     struct IsNotEmpty impl<'a, 'b> Fn = |s: &'a &'b str| -> bool {
4536         !s.is_empty()
4537     };
4538
4539     #[derive(Clone)]
4540     struct BytesIsNotEmpty impl<'a, 'b> Fn = |s: &'a &'b [u8]| -> bool {
4541         !s.is_empty()
4542     };
4543
4544     #[derive(Clone)]
4545     struct UnsafeBytesToStr impl<'a> Fn = |bytes: &'a [u8]| -> &'a str {
4546         // SAFETY: not safe
4547         unsafe { from_utf8_unchecked(bytes) }
4548     };
4549 }
4550
4551 #[stable(feature = "split_whitespace", since = "1.1.0")]
4552 impl<'a> Iterator for SplitWhitespace<'a> {
4553     type Item = &'a str;
4554
4555     #[inline]
4556     fn next(&mut self) -> Option<&'a str> {
4557         self.inner.next()
4558     }
4559
4560     #[inline]
4561     fn size_hint(&self) -> (usize, Option<usize>) {
4562         self.inner.size_hint()
4563     }
4564
4565     #[inline]
4566     fn last(mut self) -> Option<&'a str> {
4567         self.next_back()
4568     }
4569 }
4570
4571 #[stable(feature = "split_whitespace", since = "1.1.0")]
4572 impl<'a> DoubleEndedIterator for SplitWhitespace<'a> {
4573     #[inline]
4574     fn next_back(&mut self) -> Option<&'a str> {
4575         self.inner.next_back()
4576     }
4577 }
4578
4579 #[stable(feature = "fused", since = "1.26.0")]
4580 impl FusedIterator for SplitWhitespace<'_> {}
4581
4582 #[stable(feature = "split_ascii_whitespace", since = "1.34.0")]
4583 impl<'a> Iterator for SplitAsciiWhitespace<'a> {
4584     type Item = &'a str;
4585
4586     #[inline]
4587     fn next(&mut self) -> Option<&'a str> {
4588         self.inner.next()
4589     }
4590
4591     #[inline]
4592     fn size_hint(&self) -> (usize, Option<usize>) {
4593         self.inner.size_hint()
4594     }
4595
4596     #[inline]
4597     fn last(mut self) -> Option<&'a str> {
4598         self.next_back()
4599     }
4600 }
4601
4602 #[stable(feature = "split_ascii_whitespace", since = "1.34.0")]
4603 impl<'a> DoubleEndedIterator for SplitAsciiWhitespace<'a> {
4604     #[inline]
4605     fn next_back(&mut self) -> Option<&'a str> {
4606         self.inner.next_back()
4607     }
4608 }
4609
4610 #[stable(feature = "split_ascii_whitespace", since = "1.34.0")]
4611 impl FusedIterator for SplitAsciiWhitespace<'_> {}
4612
4613 #[unstable(feature = "split_inclusive", issue = "none")]
4614 impl<'a, P: Pattern<'a>> Iterator for SplitInclusive<'a, P> {
4615     type Item = &'a str;
4616
4617     #[inline]
4618     fn next(&mut self) -> Option<&'a str> {
4619         self.0.next_inclusive()
4620     }
4621 }
4622
4623 #[unstable(feature = "split_inclusive", issue = "none")]
4624 impl<'a, P: Pattern<'a, Searcher: fmt::Debug>> fmt::Debug for SplitInclusive<'a, P> {
4625     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4626         f.debug_struct("SplitInclusive").field("0", &self.0).finish()
4627     }
4628 }
4629
4630 // FIXME(#26925) Remove in favor of `#[derive(Clone)]`
4631 #[unstable(feature = "split_inclusive", issue = "none")]
4632 impl<'a, P: Pattern<'a, Searcher: Clone>> Clone for SplitInclusive<'a, P> {
4633     fn clone(&self) -> Self {
4634         SplitInclusive(self.0.clone())
4635     }
4636 }
4637
4638 #[unstable(feature = "split_inclusive", issue = "none")]
4639 impl<'a, P: Pattern<'a, Searcher: ReverseSearcher<'a>>> DoubleEndedIterator
4640     for SplitInclusive<'a, P>
4641 {
4642     #[inline]
4643     fn next_back(&mut self) -> Option<&'a str> {
4644         self.0.next_back_inclusive()
4645     }
4646 }
4647
4648 #[unstable(feature = "split_inclusive", issue = "none")]
4649 impl<'a, P: Pattern<'a>> FusedIterator for SplitInclusive<'a, P> {}
4650
4651 /// An iterator of [`u16`] over the string encoded as UTF-16.
4652 ///
4653 /// [`u16`]: ../../std/primitive.u16.html
4654 ///
4655 /// This struct is created by the [`encode_utf16`] method on [`str`].
4656 /// See its documentation for more.
4657 ///
4658 /// [`encode_utf16`]: ../../std/primitive.str.html#method.encode_utf16
4659 /// [`str`]: ../../std/primitive.str.html
4660 #[derive(Clone)]
4661 #[stable(feature = "encode_utf16", since = "1.8.0")]
4662 pub struct EncodeUtf16<'a> {
4663     chars: Chars<'a>,
4664     extra: u16,
4665 }
4666
4667 #[stable(feature = "collection_debug", since = "1.17.0")]
4668 impl fmt::Debug for EncodeUtf16<'_> {
4669     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4670         f.pad("EncodeUtf16 { .. }")
4671     }
4672 }
4673
4674 #[stable(feature = "encode_utf16", since = "1.8.0")]
4675 impl<'a> Iterator for EncodeUtf16<'a> {
4676     type Item = u16;
4677
4678     #[inline]
4679     fn next(&mut self) -> Option<u16> {
4680         if self.extra != 0 {
4681             let tmp = self.extra;
4682             self.extra = 0;
4683             return Some(tmp);
4684         }
4685
4686         let mut buf = [0; 2];
4687         self.chars.next().map(|ch| {
4688             let n = ch.encode_utf16(&mut buf).len();
4689             if n == 2 {
4690                 self.extra = buf[1];
4691             }
4692             buf[0]
4693         })
4694     }
4695
4696     #[inline]
4697     fn size_hint(&self) -> (usize, Option<usize>) {
4698         let (low, high) = self.chars.size_hint();
4699         // every char gets either one u16 or two u16,
4700         // so this iterator is between 1 or 2 times as
4701         // long as the underlying iterator.
4702         (low, high.and_then(|n| n.checked_mul(2)))
4703     }
4704 }
4705
4706 #[stable(feature = "fused", since = "1.26.0")]
4707 impl FusedIterator for EncodeUtf16<'_> {}
4708
4709 /// The return type of [`str::escape_debug`].
4710 ///
4711 /// [`str::escape_debug`]: ../../std/primitive.str.html#method.escape_debug
4712 #[stable(feature = "str_escape", since = "1.34.0")]
4713 #[derive(Clone, Debug)]
4714 pub struct EscapeDebug<'a> {
4715     inner: Chain<
4716         Flatten<option::IntoIter<char::EscapeDebug>>,
4717         FlatMap<Chars<'a>, char::EscapeDebug, CharEscapeDebugContinue>,
4718     >,
4719 }
4720
4721 /// The return type of [`str::escape_default`].
4722 ///
4723 /// [`str::escape_default`]: ../../std/primitive.str.html#method.escape_default
4724 #[stable(feature = "str_escape", since = "1.34.0")]
4725 #[derive(Clone, Debug)]
4726 pub struct EscapeDefault<'a> {
4727     inner: FlatMap<Chars<'a>, char::EscapeDefault, CharEscapeDefault>,
4728 }
4729
4730 /// The return type of [`str::escape_unicode`].
4731 ///
4732 /// [`str::escape_unicode`]: ../../std/primitive.str.html#method.escape_unicode
4733 #[stable(feature = "str_escape", since = "1.34.0")]
4734 #[derive(Clone, Debug)]
4735 pub struct EscapeUnicode<'a> {
4736     inner: FlatMap<Chars<'a>, char::EscapeUnicode, CharEscapeUnicode>,
4737 }
4738
4739 macro_rules! escape_types_impls {
4740     ($( $Name: ident ),+) => {$(
4741         #[stable(feature = "str_escape", since = "1.34.0")]
4742         impl<'a> fmt::Display for $Name<'a> {
4743             fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4744                 self.clone().try_for_each(|c| f.write_char(c))
4745             }
4746         }
4747
4748         #[stable(feature = "str_escape", since = "1.34.0")]
4749         impl<'a> Iterator for $Name<'a> {
4750             type Item = char;
4751
4752             #[inline]
4753             fn next(&mut self) -> Option<char> { self.inner.next() }
4754
4755             #[inline]
4756             fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() }
4757
4758             #[inline]
4759             fn try_fold<Acc, Fold, R>(&mut self, init: Acc, fold: Fold) -> R where
4760                 Self: Sized, Fold: FnMut(Acc, Self::Item) -> R, R: Try<Ok=Acc>
4761             {
4762                 self.inner.try_fold(init, fold)
4763             }
4764
4765             #[inline]
4766             fn fold<Acc, Fold>(self, init: Acc, fold: Fold) -> Acc
4767                 where Fold: FnMut(Acc, Self::Item) -> Acc,
4768             {
4769                 self.inner.fold(init, fold)
4770             }
4771         }
4772
4773         #[stable(feature = "str_escape", since = "1.34.0")]
4774         impl<'a> FusedIterator for $Name<'a> {}
4775     )+}
4776 }
4777
4778 escape_types_impls!(EscapeDebug, EscapeDefault, EscapeUnicode);