1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! String manipulation
13 //! For more details, see std::str
15 #![stable(feature = "rust1", since = "1.0.0")]
17 use self::pattern::Pattern;
18 use self::pattern::{Searcher, ReverseSearcher, DoubleEndedSearcher};
23 use iter::{Map, Cloned, FusedIterator};
25 use slice::{self, SliceIndex};
29 /// A trait to abstract the idea of creating a new instance of a type from a
32 /// `FromStr`'s [`from_str`] method is often used implicitly, through
33 /// [`str`]'s [`parse`] method. See [`parse`]'s documentation for examples.
35 /// [`from_str`]: #tymethod.from_str
36 /// [`str`]: ../../std/primitive.str.html
37 /// [`parse`]: ../../std/primitive.str.html#method.parse
41 /// Basic implementation of `FromStr` on an example `Point` type:
44 /// use std::str::FromStr;
45 /// use std::num::ParseIntError;
47 /// #[derive(Debug, PartialEq)]
53 /// impl FromStr for Point {
54 /// type Err = ParseIntError;
56 /// fn from_str(s: &str) -> Result<Self, Self::Err> {
57 /// let coords: Vec<&str> = s.trim_matches(|p| p == '(' || p == ')' )
61 /// let x_fromstr = coords[0].parse::<i32>()?;
62 /// let y_fromstr = coords[1].parse::<i32>()?;
64 /// Ok(Point { x: x_fromstr, y: y_fromstr })
68 /// let p = Point::from_str("(1,2)");
69 /// assert_eq!(p.unwrap(), Point{ x: 1, y: 2} )
71 #[stable(feature = "rust1", since = "1.0.0")]
72 pub trait FromStr: Sized {
73 /// The associated error which can be returned from parsing.
74 #[stable(feature = "rust1", since = "1.0.0")]
77 /// Parses a string `s` to return a value of this type.
79 /// If parsing succeeds, return the value inside `Ok`, otherwise
80 /// when the string is ill-formatted return an error specific to the
81 /// inside `Err`. The error type is specific to implementation of the trait.
85 /// Basic usage with [`i32`][ithirtytwo], a type that implements `FromStr`:
87 /// [ithirtytwo]: ../../std/primitive.i32.html
90 /// use std::str::FromStr;
93 /// let x = i32::from_str(s).unwrap();
97 #[stable(feature = "rust1", since = "1.0.0")]
98 fn from_str(s: &str) -> Result<Self, Self::Err>;
101 #[stable(feature = "rust1", since = "1.0.0")]
102 impl FromStr for bool {
103 type Err = ParseBoolError;
105 /// Parse a `bool` from a string.
107 /// Yields a `Result<bool, ParseBoolError>`, because `s` may or may not
108 /// actually be parseable.
113 /// use std::str::FromStr;
115 /// assert_eq!(FromStr::from_str("true"), Ok(true));
116 /// assert_eq!(FromStr::from_str("false"), Ok(false));
117 /// assert!(<bool as FromStr>::from_str("not even a boolean").is_err());
120 /// Note, in many cases, the `.parse()` method on `str` is more proper.
123 /// assert_eq!("true".parse(), Ok(true));
124 /// assert_eq!("false".parse(), Ok(false));
125 /// assert!("not even a boolean".parse::<bool>().is_err());
128 fn from_str(s: &str) -> Result<bool, ParseBoolError> {
131 "false" => Ok(false),
132 _ => Err(ParseBoolError { _priv: () }),
137 /// An error returned when parsing a `bool` using [`from_str`] fails
139 /// [`from_str`]: ../../std/primitive.bool.html#method.from_str
140 #[derive(Debug, Clone, PartialEq, Eq)]
141 #[stable(feature = "rust1", since = "1.0.0")]
142 pub struct ParseBoolError { _priv: () }
144 #[stable(feature = "rust1", since = "1.0.0")]
145 impl fmt::Display for ParseBoolError {
146 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
147 "provided string was not `true` or `false`".fmt(f)
152 Section: Creating a string
155 /// Errors which can occur when attempting to interpret a sequence of [`u8`]
158 /// [`u8`]: ../../std/primitive.u8.html
160 /// As such, the `from_utf8` family of functions and methods for both [`String`]s
161 /// and [`&str`]s make use of this error, for example.
163 /// [`String`]: ../../std/string/struct.String.html#method.from_utf8
164 /// [`&str`]: ../../std/str/fn.from_utf8.html
165 #[derive(Copy, Eq, PartialEq, Clone, Debug)]
166 #[stable(feature = "rust1", since = "1.0.0")]
167 pub struct Utf8Error {
169 error_len: Option<u8>,
173 /// Returns the index in the given string up to which valid UTF-8 was
176 /// It is the maximum index such that `from_utf8(&input[..index])`
177 /// would return `Ok(_)`.
186 /// // some invalid bytes, in a vector
187 /// let sparkle_heart = vec![0, 159, 146, 150];
189 /// // std::str::from_utf8 returns a Utf8Error
190 /// let error = str::from_utf8(&sparkle_heart).unwrap_err();
192 /// // the second byte is invalid here
193 /// assert_eq!(1, error.valid_up_to());
195 #[stable(feature = "utf8_error", since = "1.5.0")]
196 pub fn valid_up_to(&self) -> usize { self.valid_up_to }
198 /// Provide more information about the failure:
200 /// * `None`: the end of the input was reached unexpectedly.
201 /// `self.valid_up_to()` is 1 to 3 bytes from the end of the input.
202 /// If a byte stream (such as a file or a network socket) is being decoded incrementally,
203 /// this could be a valid `char` whose UTF-8 byte sequence is spanning multiple chunks.
205 /// * `Some(len)`: an unexpected byte was encountered.
206 /// The length provided is that of the invalid byte sequence
207 /// that starts at the index given by `valid_up_to()`.
208 /// Decoding should resume after that sequence
209 /// (after inserting a U+FFFD REPLACEMENT CHARACTER) in case of lossy decoding.
210 #[unstable(feature = "utf8_error_error_len", reason ="new", issue = "40494")]
211 pub fn error_len(&self) -> Option<usize> {
212 self.error_len.map(|len| len as usize)
216 /// Converts a slice of bytes to a string slice.
218 /// A string slice (`&str`) is made of bytes (`u8`), and a byte slice (`&[u8]`)
219 /// is made of bytes, so this function converts between the two. Not all byte
220 /// slices are valid string slices, however: `&str` requires that it is valid
221 /// UTF-8. `from_utf8()` checks to ensure that the bytes are valid UTF-8, and
222 /// then does the conversion.
224 /// If you are sure that the byte slice is valid UTF-8, and you don't want to
225 /// incur the overhead of the validity check, there is an unsafe version of
226 /// this function, [`from_utf8_unchecked`][fromutf8u], which has the same
227 /// behavior but skips the check.
229 /// [fromutf8u]: fn.from_utf8_unchecked.html
231 /// If you need a `String` instead of a `&str`, consider
232 /// [`String::from_utf8`][string].
234 /// [string]: ../../std/string/struct.String.html#method.from_utf8
236 /// Because you can stack-allocate a `[u8; N]`, and you can take a `&[u8]` of
237 /// it, this function is one way to have a stack-allocated string. There is
238 /// an example of this in the examples section below.
242 /// Returns `Err` if the slice is not UTF-8 with a description as to why the
243 /// provided slice is not UTF-8.
252 /// // some bytes, in a vector
253 /// let sparkle_heart = vec![240, 159, 146, 150];
255 /// // We know these bytes are valid, so just use `unwrap()`.
256 /// let sparkle_heart = str::from_utf8(&sparkle_heart).unwrap();
258 /// assert_eq!("💖", sparkle_heart);
266 /// // some invalid bytes, in a vector
267 /// let sparkle_heart = vec![0, 159, 146, 150];
269 /// assert!(str::from_utf8(&sparkle_heart).is_err());
272 /// See the docs for [`Utf8Error`][error] for more details on the kinds of
273 /// errors that can be returned.
275 /// [error]: struct.Utf8Error.html
277 /// A "stack allocated string":
282 /// // some bytes, in a stack-allocated array
283 /// let sparkle_heart = [240, 159, 146, 150];
285 /// // We know these bytes are valid, so just use `unwrap()`.
286 /// let sparkle_heart = str::from_utf8(&sparkle_heart).unwrap();
288 /// assert_eq!("💖", sparkle_heart);
290 #[stable(feature = "rust1", since = "1.0.0")]
291 pub fn from_utf8(v: &[u8]) -> Result<&str, Utf8Error> {
292 run_utf8_validation(v)?;
293 Ok(unsafe { from_utf8_unchecked(v) })
296 /// Forms a str from a pointer and a length.
298 /// The `len` argument is the number of bytes in the string.
302 /// This function is unsafe as there is no guarantee that the given pointer is
303 /// valid for `len` bytes, nor whether the lifetime inferred is a suitable
304 /// lifetime for the returned str.
306 /// The data must be valid UTF-8
308 /// `p` must be non-null, even for zero-length str.
312 /// The lifetime for the returned str is inferred from its usage. To
313 /// prevent accidental misuse, it's suggested to tie the lifetime to whichever
314 /// source lifetime is safe in the context, such as by providing a helper
315 /// function taking the lifetime of a host value for the str, or by explicit
317 /// Performs the same functionality as `from_raw_parts`, except that a mutable
320 unsafe fn from_raw_parts_mut<'a>(p: *mut u8, len: usize) -> &'a mut str {
321 mem::transmute::<&mut [u8], &mut str>(slice::from_raw_parts_mut(p, len))
324 /// Converts a slice of bytes to a string slice without checking
325 /// that the string contains valid UTF-8.
327 /// See the safe version, [`from_utf8`][fromutf8], for more information.
329 /// [fromutf8]: fn.from_utf8.html
333 /// This function is unsafe because it does not check that the bytes passed to
334 /// it are valid UTF-8. If this constraint is violated, undefined behavior
335 /// results, as the rest of Rust assumes that [`&str`]s are valid UTF-8.
337 /// [`&str`]: ../../std/primitive.str.html
346 /// // some bytes, in a vector
347 /// let sparkle_heart = vec![240, 159, 146, 150];
349 /// let sparkle_heart = unsafe {
350 /// str::from_utf8_unchecked(&sparkle_heart)
353 /// assert_eq!("💖", sparkle_heart);
356 #[stable(feature = "rust1", since = "1.0.0")]
357 pub unsafe fn from_utf8_unchecked(v: &[u8]) -> &str {
361 #[stable(feature = "rust1", since = "1.0.0")]
362 impl fmt::Display for Utf8Error {
363 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
364 if let Some(error_len) = self.error_len {
365 write!(f, "invalid utf-8 sequence of {} bytes from index {}",
366 error_len, self.valid_up_to)
368 write!(f, "incomplete utf-8 byte sequence from index {}", self.valid_up_to)
377 /// An iterator over the [`char`]s of a string slice.
379 /// [`char`]: ../../std/primitive.char.html
381 /// This struct is created by the [`chars`] method on [`str`].
382 /// See its documentation for more.
384 /// [`chars`]: ../../std/primitive.str.html#method.chars
385 /// [`str`]: ../../std/primitive.str.html
386 #[derive(Clone, Debug)]
387 #[stable(feature = "rust1", since = "1.0.0")]
388 pub struct Chars<'a> {
389 iter: slice::Iter<'a, u8>
392 /// Returns the initial codepoint accumulator for the first byte.
393 /// The first byte is special, only want bottom 5 bits for width 2, 4 bits
394 /// for width 3, and 3 bits for width 4.
396 fn utf8_first_byte(byte: u8, width: u32) -> u32 { (byte & (0x7F >> width)) as u32 }
398 /// Returns the value of `ch` updated with continuation byte `byte`.
400 fn utf8_acc_cont_byte(ch: u32, byte: u8) -> u32 { (ch << 6) | (byte & CONT_MASK) as u32 }
402 /// Checks whether the byte is a UTF-8 continuation byte (i.e. starts with the
405 fn utf8_is_cont_byte(byte: u8) -> bool { (byte & !CONT_MASK) == TAG_CONT_U8 }
408 fn unwrap_or_0(opt: Option<&u8>) -> u8 {
415 /// Reads the next code point out of a byte iterator (assuming a
416 /// UTF-8-like encoding).
417 #[unstable(feature = "str_internals", issue = "0")]
419 pub fn next_code_point<'a, I: Iterator<Item = &'a u8>>(bytes: &mut I) -> Option<u32> {
421 let x = match bytes.next() {
423 Some(&next_byte) if next_byte < 128 => return Some(next_byte as u32),
424 Some(&next_byte) => next_byte,
427 // Multibyte case follows
428 // Decode from a byte combination out of: [[[x y] z] w]
429 // NOTE: Performance is sensitive to the exact formulation here
430 let init = utf8_first_byte(x, 2);
431 let y = unwrap_or_0(bytes.next());
432 let mut ch = utf8_acc_cont_byte(init, y);
435 // 5th bit in 0xE0 .. 0xEF is always clear, so `init` is still valid
436 let z = unwrap_or_0(bytes.next());
437 let y_z = utf8_acc_cont_byte((y & CONT_MASK) as u32, z);
438 ch = init << 12 | y_z;
441 // use only the lower 3 bits of `init`
442 let w = unwrap_or_0(bytes.next());
443 ch = (init & 7) << 18 | utf8_acc_cont_byte(y_z, w);
450 /// Reads the last code point out of a byte iterator (assuming a
451 /// UTF-8-like encoding).
453 fn next_code_point_reverse<'a, I>(bytes: &mut I) -> Option<u32>
454 where I: DoubleEndedIterator<Item = &'a u8>,
457 let w = match bytes.next_back() {
459 Some(&next_byte) if next_byte < 128 => return Some(next_byte as u32),
460 Some(&back_byte) => back_byte,
463 // Multibyte case follows
464 // Decode from a byte combination out of: [x [y [z w]]]
466 let z = unwrap_or_0(bytes.next_back());
467 ch = utf8_first_byte(z, 2);
468 if utf8_is_cont_byte(z) {
469 let y = unwrap_or_0(bytes.next_back());
470 ch = utf8_first_byte(y, 3);
471 if utf8_is_cont_byte(y) {
472 let x = unwrap_or_0(bytes.next_back());
473 ch = utf8_first_byte(x, 4);
474 ch = utf8_acc_cont_byte(ch, y);
476 ch = utf8_acc_cont_byte(ch, z);
478 ch = utf8_acc_cont_byte(ch, w);
483 #[stable(feature = "rust1", since = "1.0.0")]
484 impl<'a> Iterator for Chars<'a> {
488 fn next(&mut self) -> Option<char> {
489 next_code_point(&mut self.iter).map(|ch| {
490 // str invariant says `ch` is a valid Unicode Scalar Value
492 char::from_u32_unchecked(ch)
498 fn count(self) -> usize {
499 // length in `char` is equal to the number of non-continuation bytes
500 let bytes_len = self.iter.len();
501 let mut cont_bytes = 0;
502 for &byte in self.iter {
503 cont_bytes += utf8_is_cont_byte(byte) as usize;
505 bytes_len - cont_bytes
509 fn size_hint(&self) -> (usize, Option<usize>) {
510 let len = self.iter.len();
511 // `(len + 3)` can't overflow, because we know that the `slice::Iter`
512 // belongs to a slice in memory which has a maximum length of
513 // `isize::MAX` (that's well below `usize::MAX`).
514 ((len + 3) / 4, Some(len))
518 fn last(mut self) -> Option<char> {
519 // No need to go through the entire string.
524 #[stable(feature = "rust1", since = "1.0.0")]
525 impl<'a> DoubleEndedIterator for Chars<'a> {
527 fn next_back(&mut self) -> Option<char> {
528 next_code_point_reverse(&mut self.iter).map(|ch| {
529 // str invariant says `ch` is a valid Unicode Scalar Value
531 char::from_u32_unchecked(ch)
537 #[unstable(feature = "fused", issue = "35602")]
538 impl<'a> FusedIterator for Chars<'a> {}
541 /// View the underlying data as a subslice of the original data.
543 /// This has the same lifetime as the original slice, and so the
544 /// iterator can continue to be used while this exists.
549 /// let mut chars = "abc".chars();
551 /// assert_eq!(chars.as_str(), "abc");
553 /// assert_eq!(chars.as_str(), "bc");
556 /// assert_eq!(chars.as_str(), "");
558 #[stable(feature = "iter_to_slice", since = "1.4.0")]
560 pub fn as_str(&self) -> &'a str {
561 unsafe { from_utf8_unchecked(self.iter.as_slice()) }
565 /// An iterator over the [`char`]s of a string slice, and their positions.
567 /// [`char`]: ../../std/primitive.char.html
569 /// This struct is created by the [`char_indices`] method on [`str`].
570 /// See its documentation for more.
572 /// [`char_indices`]: ../../std/primitive.str.html#method.char_indices
573 /// [`str`]: ../../std/primitive.str.html
574 #[derive(Clone, Debug)]
575 #[stable(feature = "rust1", since = "1.0.0")]
576 pub struct CharIndices<'a> {
581 #[stable(feature = "rust1", since = "1.0.0")]
582 impl<'a> Iterator for CharIndices<'a> {
583 type Item = (usize, char);
586 fn next(&mut self) -> Option<(usize, char)> {
587 let pre_len = self.iter.iter.len();
588 match self.iter.next() {
591 let index = self.front_offset;
592 let len = self.iter.iter.len();
593 self.front_offset += pre_len - len;
600 fn count(self) -> usize {
605 fn size_hint(&self) -> (usize, Option<usize>) {
606 self.iter.size_hint()
610 fn last(mut self) -> Option<(usize, char)> {
611 // No need to go through the entire string.
616 #[stable(feature = "rust1", since = "1.0.0")]
617 impl<'a> DoubleEndedIterator for CharIndices<'a> {
619 fn next_back(&mut self) -> Option<(usize, char)> {
620 match self.iter.next_back() {
623 let index = self.front_offset + self.iter.iter.len();
630 #[unstable(feature = "fused", issue = "35602")]
631 impl<'a> FusedIterator for CharIndices<'a> {}
633 impl<'a> CharIndices<'a> {
634 /// View the underlying data as a subslice of the original data.
636 /// This has the same lifetime as the original slice, and so the
637 /// iterator can continue to be used while this exists.
638 #[stable(feature = "iter_to_slice", since = "1.4.0")]
640 pub fn as_str(&self) -> &'a str {
645 /// An iterator over the bytes of a string slice.
647 /// This struct is created by the [`bytes`] method on [`str`].
648 /// See its documentation for more.
650 /// [`bytes`]: ../../std/primitive.str.html#method.bytes
651 /// [`str`]: ../../std/primitive.str.html
652 #[stable(feature = "rust1", since = "1.0.0")]
653 #[derive(Clone, Debug)]
654 pub struct Bytes<'a>(Cloned<slice::Iter<'a, u8>>);
656 #[stable(feature = "rust1", since = "1.0.0")]
657 impl<'a> Iterator for Bytes<'a> {
661 fn next(&mut self) -> Option<u8> {
666 fn size_hint(&self) -> (usize, Option<usize>) {
671 fn count(self) -> usize {
676 fn last(self) -> Option<Self::Item> {
681 fn nth(&mut self, n: usize) -> Option<Self::Item> {
686 #[stable(feature = "rust1", since = "1.0.0")]
687 impl<'a> DoubleEndedIterator for Bytes<'a> {
689 fn next_back(&mut self) -> Option<u8> {
694 #[stable(feature = "rust1", since = "1.0.0")]
695 impl<'a> ExactSizeIterator for Bytes<'a> {
697 fn len(&self) -> usize {
702 fn is_empty(&self) -> bool {
707 #[unstable(feature = "fused", issue = "35602")]
708 impl<'a> FusedIterator for Bytes<'a> {}
710 /// This macro generates a Clone impl for string pattern API
711 /// wrapper types of the form X<'a, P>
712 macro_rules! derive_pattern_clone {
713 (clone $t:ident with |$s:ident| $e:expr) => {
714 impl<'a, P: Pattern<'a>> Clone for $t<'a, P>
715 where P::Searcher: Clone
717 fn clone(&self) -> Self {
725 /// This macro generates two public iterator structs
726 /// wrapping a private internal one that makes use of the `Pattern` API.
728 /// For all patterns `P: Pattern<'a>` the following items will be
729 /// generated (generics omitted):
731 /// struct $forward_iterator($internal_iterator);
732 /// struct $reverse_iterator($internal_iterator);
734 /// impl Iterator for $forward_iterator
735 /// { /* internal ends up calling Searcher::next_match() */ }
737 /// impl DoubleEndedIterator for $forward_iterator
738 /// where P::Searcher: DoubleEndedSearcher
739 /// { /* internal ends up calling Searcher::next_match_back() */ }
741 /// impl Iterator for $reverse_iterator
742 /// where P::Searcher: ReverseSearcher
743 /// { /* internal ends up calling Searcher::next_match_back() */ }
745 /// impl DoubleEndedIterator for $reverse_iterator
746 /// where P::Searcher: DoubleEndedSearcher
747 /// { /* internal ends up calling Searcher::next_match() */ }
749 /// The internal one is defined outside the macro, and has almost the same
750 /// semantic as a DoubleEndedIterator by delegating to `pattern::Searcher` and
751 /// `pattern::ReverseSearcher` for both forward and reverse iteration.
753 /// "Almost", because a `Searcher` and a `ReverseSearcher` for a given
754 /// `Pattern` might not return the same elements, so actually implementing
755 /// `DoubleEndedIterator` for it would be incorrect.
756 /// (See the docs in `str::pattern` for more details)
758 /// However, the internal struct still represents a single ended iterator from
759 /// either end, and depending on pattern is also a valid double ended iterator,
760 /// so the two wrapper structs implement `Iterator`
761 /// and `DoubleEndedIterator` depending on the concrete pattern type, leading
762 /// to the complex impls seen above.
763 macro_rules! generate_pattern_iterators {
767 $(#[$forward_iterator_attribute:meta])*
768 struct $forward_iterator:ident;
772 $(#[$reverse_iterator_attribute:meta])*
773 struct $reverse_iterator:ident;
775 // Stability of all generated items
777 $(#[$common_stability_attribute:meta])*
779 // Internal almost-iterator that is being delegated to
781 $internal_iterator:ident yielding ($iterty:ty);
783 // Kind of delgation - either single ended or double ended
786 $(#[$forward_iterator_attribute])*
787 $(#[$common_stability_attribute])*
788 pub struct $forward_iterator<'a, P: Pattern<'a>>($internal_iterator<'a, P>);
790 $(#[$common_stability_attribute])*
791 impl<'a, P: Pattern<'a>> fmt::Debug for $forward_iterator<'a, P>
792 where P::Searcher: fmt::Debug
794 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
795 f.debug_tuple(stringify!($forward_iterator))
801 $(#[$common_stability_attribute])*
802 impl<'a, P: Pattern<'a>> Iterator for $forward_iterator<'a, P> {
806 fn next(&mut self) -> Option<$iterty> {
811 $(#[$common_stability_attribute])*
812 impl<'a, P: Pattern<'a>> Clone for $forward_iterator<'a, P>
813 where P::Searcher: Clone
815 fn clone(&self) -> Self {
816 $forward_iterator(self.0.clone())
820 $(#[$reverse_iterator_attribute])*
821 $(#[$common_stability_attribute])*
822 pub struct $reverse_iterator<'a, P: Pattern<'a>>($internal_iterator<'a, P>);
824 $(#[$common_stability_attribute])*
825 impl<'a, P: Pattern<'a>> fmt::Debug for $reverse_iterator<'a, P>
826 where P::Searcher: fmt::Debug
828 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
829 f.debug_tuple(stringify!($reverse_iterator))
835 $(#[$common_stability_attribute])*
836 impl<'a, P: Pattern<'a>> Iterator for $reverse_iterator<'a, P>
837 where P::Searcher: ReverseSearcher<'a>
842 fn next(&mut self) -> Option<$iterty> {
847 $(#[$common_stability_attribute])*
848 impl<'a, P: Pattern<'a>> Clone for $reverse_iterator<'a, P>
849 where P::Searcher: Clone
851 fn clone(&self) -> Self {
852 $reverse_iterator(self.0.clone())
856 #[unstable(feature = "fused", issue = "35602")]
857 impl<'a, P: Pattern<'a>> FusedIterator for $forward_iterator<'a, P> {}
859 #[unstable(feature = "fused", issue = "35602")]
860 impl<'a, P: Pattern<'a>> FusedIterator for $reverse_iterator<'a, P>
861 where P::Searcher: ReverseSearcher<'a> {}
863 generate_pattern_iterators!($($t)* with $(#[$common_stability_attribute])*,
865 $reverse_iterator, $iterty);
868 double ended; with $(#[$common_stability_attribute:meta])*,
869 $forward_iterator:ident,
870 $reverse_iterator:ident, $iterty:ty
872 $(#[$common_stability_attribute])*
873 impl<'a, P: Pattern<'a>> DoubleEndedIterator for $forward_iterator<'a, P>
874 where P::Searcher: DoubleEndedSearcher<'a>
877 fn next_back(&mut self) -> Option<$iterty> {
882 $(#[$common_stability_attribute])*
883 impl<'a, P: Pattern<'a>> DoubleEndedIterator for $reverse_iterator<'a, P>
884 where P::Searcher: DoubleEndedSearcher<'a>
887 fn next_back(&mut self) -> Option<$iterty> {
893 single ended; with $(#[$common_stability_attribute:meta])*,
894 $forward_iterator:ident,
895 $reverse_iterator:ident, $iterty:ty
899 derive_pattern_clone!{
901 with |s| SplitInternal { matcher: s.matcher.clone(), ..*s }
904 struct SplitInternal<'a, P: Pattern<'a>> {
907 matcher: P::Searcher,
908 allow_trailing_empty: bool,
912 impl<'a, P: Pattern<'a>> fmt::Debug for SplitInternal<'a, P> where P::Searcher: fmt::Debug {
913 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
914 f.debug_struct("SplitInternal")
915 .field("start", &self.start)
916 .field("end", &self.end)
917 .field("matcher", &self.matcher)
918 .field("allow_trailing_empty", &self.allow_trailing_empty)
919 .field("finished", &self.finished)
924 impl<'a, P: Pattern<'a>> SplitInternal<'a, P> {
926 fn get_end(&mut self) -> Option<&'a str> {
927 if !self.finished && (self.allow_trailing_empty || self.end - self.start > 0) {
928 self.finished = true;
930 let string = self.matcher.haystack().slice_unchecked(self.start, self.end);
939 fn next(&mut self) -> Option<&'a str> {
940 if self.finished { return None }
942 let haystack = self.matcher.haystack();
943 match self.matcher.next_match() {
944 Some((a, b)) => unsafe {
945 let elt = haystack.slice_unchecked(self.start, a);
949 None => self.get_end(),
954 fn next_back(&mut self) -> Option<&'a str>
955 where P::Searcher: ReverseSearcher<'a>
957 if self.finished { return None }
959 if !self.allow_trailing_empty {
960 self.allow_trailing_empty = true;
961 match self.next_back() {
962 Some(elt) if !elt.is_empty() => return Some(elt),
963 _ => if self.finished { return None }
967 let haystack = self.matcher.haystack();
968 match self.matcher.next_match_back() {
969 Some((a, b)) => unsafe {
970 let elt = haystack.slice_unchecked(b, self.end);
975 self.finished = true;
976 Some(haystack.slice_unchecked(self.start, self.end))
982 generate_pattern_iterators! {
984 /// Created with the method [`split`].
986 /// [`split`]: ../../std/primitive.str.html#method.split
989 /// Created with the method [`rsplit`].
991 /// [`rsplit`]: ../../std/primitive.str.html#method.rsplit
994 #[stable(feature = "rust1", since = "1.0.0")]
996 SplitInternal yielding (&'a str);
997 delegate double ended;
1000 generate_pattern_iterators! {
1002 /// Created with the method [`split_terminator`].
1004 /// [`split_terminator`]: ../../std/primitive.str.html#method.split_terminator
1005 struct SplitTerminator;
1007 /// Created with the method [`rsplit_terminator`].
1009 /// [`rsplit_terminator`]: ../../std/primitive.str.html#method.rsplit_terminator
1010 struct RSplitTerminator;
1012 #[stable(feature = "rust1", since = "1.0.0")]
1014 SplitInternal yielding (&'a str);
1015 delegate double ended;
1018 derive_pattern_clone!{
1019 clone SplitNInternal
1020 with |s| SplitNInternal { iter: s.iter.clone(), ..*s }
1023 struct SplitNInternal<'a, P: Pattern<'a>> {
1024 iter: SplitInternal<'a, P>,
1025 /// The number of splits remaining
1029 impl<'a, P: Pattern<'a>> fmt::Debug for SplitNInternal<'a, P> where P::Searcher: fmt::Debug {
1030 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1031 f.debug_struct("SplitNInternal")
1032 .field("iter", &self.iter)
1033 .field("count", &self.count)
1038 impl<'a, P: Pattern<'a>> SplitNInternal<'a, P> {
1040 fn next(&mut self) -> Option<&'a str> {
1043 1 => { self.count = 0; self.iter.get_end() }
1044 _ => { self.count -= 1; self.iter.next() }
1049 fn next_back(&mut self) -> Option<&'a str>
1050 where P::Searcher: ReverseSearcher<'a>
1054 1 => { self.count = 0; self.iter.get_end() }
1055 _ => { self.count -= 1; self.iter.next_back() }
1060 generate_pattern_iterators! {
1062 /// Created with the method [`splitn`].
1064 /// [`splitn`]: ../../std/primitive.str.html#method.splitn
1067 /// Created with the method [`rsplitn`].
1069 /// [`rsplitn`]: ../../std/primitive.str.html#method.rsplitn
1072 #[stable(feature = "rust1", since = "1.0.0")]
1074 SplitNInternal yielding (&'a str);
1075 delegate single ended;
1078 derive_pattern_clone!{
1079 clone MatchIndicesInternal
1080 with |s| MatchIndicesInternal(s.0.clone())
1083 struct MatchIndicesInternal<'a, P: Pattern<'a>>(P::Searcher);
1085 impl<'a, P: Pattern<'a>> fmt::Debug for MatchIndicesInternal<'a, P> where P::Searcher: fmt::Debug {
1086 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1087 f.debug_tuple("MatchIndicesInternal")
1093 impl<'a, P: Pattern<'a>> MatchIndicesInternal<'a, P> {
1095 fn next(&mut self) -> Option<(usize, &'a str)> {
1096 self.0.next_match().map(|(start, end)| unsafe {
1097 (start, self.0.haystack().slice_unchecked(start, end))
1102 fn next_back(&mut self) -> Option<(usize, &'a str)>
1103 where P::Searcher: ReverseSearcher<'a>
1105 self.0.next_match_back().map(|(start, end)| unsafe {
1106 (start, self.0.haystack().slice_unchecked(start, end))
1111 generate_pattern_iterators! {
1113 /// Created with the method [`match_indices`].
1115 /// [`match_indices`]: ../../std/primitive.str.html#method.match_indices
1116 struct MatchIndices;
1118 /// Created with the method [`rmatch_indices`].
1120 /// [`rmatch_indices`]: ../../std/primitive.str.html#method.rmatch_indices
1121 struct RMatchIndices;
1123 #[stable(feature = "str_match_indices", since = "1.5.0")]
1125 MatchIndicesInternal yielding ((usize, &'a str));
1126 delegate double ended;
1129 derive_pattern_clone!{
1130 clone MatchesInternal
1131 with |s| MatchesInternal(s.0.clone())
1134 struct MatchesInternal<'a, P: Pattern<'a>>(P::Searcher);
1136 impl<'a, P: Pattern<'a>> fmt::Debug for MatchesInternal<'a, P> where P::Searcher: fmt::Debug {
1137 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1138 f.debug_tuple("MatchesInternal")
1144 impl<'a, P: Pattern<'a>> MatchesInternal<'a, P> {
1146 fn next(&mut self) -> Option<&'a str> {
1147 self.0.next_match().map(|(a, b)| unsafe {
1148 // Indices are known to be on utf8 boundaries
1149 self.0.haystack().slice_unchecked(a, b)
1154 fn next_back(&mut self) -> Option<&'a str>
1155 where P::Searcher: ReverseSearcher<'a>
1157 self.0.next_match_back().map(|(a, b)| unsafe {
1158 // Indices are known to be on utf8 boundaries
1159 self.0.haystack().slice_unchecked(a, b)
1164 generate_pattern_iterators! {
1166 /// Created with the method [`matches`].
1168 /// [`matches`]: ../../std/primitive.str.html#method.matches
1171 /// Created with the method [`rmatches`].
1173 /// [`rmatches`]: ../../std/primitive.str.html#method.rmatches
1176 #[stable(feature = "str_matches", since = "1.2.0")]
1178 MatchesInternal yielding (&'a str);
1179 delegate double ended;
1182 /// An iterator over the lines of a string, as string slices.
1184 /// This struct is created with the [`lines`] method on [`str`].
1185 /// See its documentation for more.
1187 /// [`lines`]: ../../std/primitive.str.html#method.lines
1188 /// [`str`]: ../../std/primitive.str.html
1189 #[stable(feature = "rust1", since = "1.0.0")]
1190 #[derive(Clone, Debug)]
1191 pub struct Lines<'a>(Map<SplitTerminator<'a, char>, LinesAnyMap>);
1193 #[stable(feature = "rust1", since = "1.0.0")]
1194 impl<'a> Iterator for Lines<'a> {
1195 type Item = &'a str;
1198 fn next(&mut self) -> Option<&'a str> {
1203 fn size_hint(&self) -> (usize, Option<usize>) {
1208 #[stable(feature = "rust1", since = "1.0.0")]
1209 impl<'a> DoubleEndedIterator for Lines<'a> {
1211 fn next_back(&mut self) -> Option<&'a str> {
1216 #[unstable(feature = "fused", issue = "35602")]
1217 impl<'a> FusedIterator for Lines<'a> {}
1219 /// Created with the method [`lines_any`].
1221 /// [`lines_any`]: ../../std/primitive.str.html#method.lines_any
1222 #[stable(feature = "rust1", since = "1.0.0")]
1223 #[rustc_deprecated(since = "1.4.0", reason = "use lines()/Lines instead now")]
1224 #[derive(Clone, Debug)]
1225 #[allow(deprecated)]
1226 pub struct LinesAny<'a>(Lines<'a>);
1228 /// A nameable, cloneable fn type
1232 impl<'a> Fn<(&'a str,)> for LinesAnyMap {
1234 extern "rust-call" fn call(&self, (line,): (&'a str,)) -> &'a str {
1236 if l > 0 && line.as_bytes()[l - 1] == b'\r' { &line[0 .. l - 1] }
1241 impl<'a> FnMut<(&'a str,)> for LinesAnyMap {
1243 extern "rust-call" fn call_mut(&mut self, (line,): (&'a str,)) -> &'a str {
1244 Fn::call(&*self, (line,))
1248 impl<'a> FnOnce<(&'a str,)> for LinesAnyMap {
1249 type Output = &'a str;
1252 extern "rust-call" fn call_once(self, (line,): (&'a str,)) -> &'a str {
1253 Fn::call(&self, (line,))
1257 #[stable(feature = "rust1", since = "1.0.0")]
1258 #[allow(deprecated)]
1259 impl<'a> Iterator for LinesAny<'a> {
1260 type Item = &'a str;
1263 fn next(&mut self) -> Option<&'a str> {
1268 fn size_hint(&self) -> (usize, Option<usize>) {
1273 #[stable(feature = "rust1", since = "1.0.0")]
1274 #[allow(deprecated)]
1275 impl<'a> DoubleEndedIterator for LinesAny<'a> {
1277 fn next_back(&mut self) -> Option<&'a str> {
1282 #[unstable(feature = "fused", issue = "35602")]
1283 #[allow(deprecated)]
1284 impl<'a> FusedIterator for LinesAny<'a> {}
1287 Section: Comparing strings
1290 /// Bytewise slice equality
1291 /// NOTE: This function is (ab)used in rustc::middle::trans::_match
1292 /// to compare &[u8] byte slices that are not necessarily valid UTF-8.
1295 fn eq_slice(a: &str, b: &str) -> bool {
1296 a.as_bytes() == b.as_bytes()
1300 Section: UTF-8 validation
1303 // use truncation to fit u64 into usize
1304 const NONASCII_MASK: usize = 0x80808080_80808080u64 as usize;
1306 /// Returns `true` if any byte in the word `x` is nonascii (>= 128).
1308 fn contains_nonascii(x: usize) -> bool {
1309 (x & NONASCII_MASK) != 0
1312 /// Walks through `iter` checking that it's a valid UTF-8 sequence,
1313 /// returning `true` in that case, or, if it is invalid, `false` with
1314 /// `iter` reset such that it is pointing at the first byte in the
1315 /// invalid sequence.
1317 fn run_utf8_validation(v: &[u8]) -> Result<(), Utf8Error> {
1321 let usize_bytes = mem::size_of::<usize>();
1322 let ascii_block_size = 2 * usize_bytes;
1323 let blocks_end = if len >= ascii_block_size { len - ascii_block_size + 1 } else { 0 };
1326 let old_offset = index;
1328 ($error_len: expr) => {
1329 return Err(Utf8Error {
1330 valid_up_to: old_offset,
1331 error_len: $error_len,
1336 macro_rules! next { () => {{
1338 // we needed data, but there was none: error!
1345 let first = v[index];
1347 let w = UTF8_CHAR_WIDTH[first as usize];
1348 // 2-byte encoding is for codepoints \u{0080} to \u{07ff}
1349 // first C2 80 last DF BF
1350 // 3-byte encoding is for codepoints \u{0800} to \u{ffff}
1351 // first E0 A0 80 last EF BF BF
1352 // excluding surrogates codepoints \u{d800} to \u{dfff}
1353 // ED A0 80 to ED BF BF
1354 // 4-byte encoding is for codepoints \u{1000}0 to \u{10ff}ff
1355 // first F0 90 80 80 last F4 8F BF BF
1357 // Use the UTF-8 syntax from the RFC
1359 // https://tools.ietf.org/html/rfc3629
1361 // UTF8-2 = %xC2-DF UTF8-tail
1362 // UTF8-3 = %xE0 %xA0-BF UTF8-tail / %xE1-EC 2( UTF8-tail ) /
1363 // %xED %x80-9F UTF8-tail / %xEE-EF 2( UTF8-tail )
1364 // UTF8-4 = %xF0 %x90-BF 2( UTF8-tail ) / %xF1-F3 3( UTF8-tail ) /
1365 // %xF4 %x80-8F 2( UTF8-tail )
1367 2 => if next!() & !CONT_MASK != TAG_CONT_U8 {
1371 match (first, next!()) {
1372 (0xE0 , 0xA0 ... 0xBF) |
1373 (0xE1 ... 0xEC, 0x80 ... 0xBF) |
1374 (0xED , 0x80 ... 0x9F) |
1375 (0xEE ... 0xEF, 0x80 ... 0xBF) => {}
1378 if next!() & !CONT_MASK != TAG_CONT_U8 {
1383 match (first, next!()) {
1384 (0xF0 , 0x90 ... 0xBF) |
1385 (0xF1 ... 0xF3, 0x80 ... 0xBF) |
1386 (0xF4 , 0x80 ... 0x8F) => {}
1389 if next!() & !CONT_MASK != TAG_CONT_U8 {
1392 if next!() & !CONT_MASK != TAG_CONT_U8 {
1400 // Ascii case, try to skip forward quickly.
1401 // When the pointer is aligned, read 2 words of data per iteration
1402 // until we find a word containing a non-ascii byte.
1403 let ptr = v.as_ptr();
1404 let align = (ptr as usize + index) & (usize_bytes - 1);
1406 while index < blocks_end {
1408 let block = ptr.offset(index as isize) as *const usize;
1409 // break if there is a nonascii byte
1410 let zu = contains_nonascii(*block);
1411 let zv = contains_nonascii(*block.offset(1));
1416 index += ascii_block_size;
1418 // step from the point where the wordwise loop stopped
1419 while index < len && v[index] < 128 {
1431 // https://tools.ietf.org/html/rfc3629
1432 static UTF8_CHAR_WIDTH: [u8; 256] = [
1433 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1434 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 0x1F
1435 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1436 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 0x3F
1437 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1438 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 0x5F
1439 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1440 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 0x7F
1441 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1442 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 0x9F
1443 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1444 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 0xBF
1445 0,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
1446 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, // 0xDF
1447 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3, // 0xEF
1448 4,4,4,4,4,0,0,0,0,0,0,0,0,0,0,0, // 0xFF
1451 /// Given a first byte, determines how many bytes are in this UTF-8 character.
1452 #[unstable(feature = "str_internals", issue = "0")]
1454 pub fn utf8_char_width(b: u8) -> usize {
1455 return UTF8_CHAR_WIDTH[b as usize] as usize;
1458 /// Mask of the value bits of a continuation byte.
1459 const CONT_MASK: u8 = 0b0011_1111;
1460 /// Value of the tag bits (tag mask is !CONT_MASK) of a continuation byte.
1461 const TAG_CONT_U8: u8 = 0b1000_0000;
1464 Section: Trait implementations
1471 use slice::{self, SliceIndex};
1474 /// Implements ordering of strings.
1476 /// Strings are ordered lexicographically by their byte values. This orders Unicode code
1477 /// points based on their positions in the code charts. This is not necessarily the same as
1478 /// "alphabetical" order, which varies by language and locale. Sorting strings according to
1479 /// culturally-accepted standards requires locale-specific data that is outside the scope of
1481 #[stable(feature = "rust1", since = "1.0.0")]
1484 fn cmp(&self, other: &str) -> Ordering {
1485 self.as_bytes().cmp(other.as_bytes())
1489 #[stable(feature = "rust1", since = "1.0.0")]
1490 impl PartialEq for str {
1492 fn eq(&self, other: &str) -> bool {
1493 eq_slice(self, other)
1496 fn ne(&self, other: &str) -> bool { !(*self).eq(other) }
1499 #[stable(feature = "rust1", since = "1.0.0")]
1502 /// Implements comparison operations on strings.
1504 /// Strings are compared lexicographically by their byte values. This compares Unicode code
1505 /// points based on their positions in the code charts. This is not necessarily the same as
1506 /// "alphabetical" order, which varies by language and locale. Comparing strings according to
1507 /// culturally-accepted standards requires locale-specific data that is outside the scope of
1509 #[stable(feature = "rust1", since = "1.0.0")]
1510 impl PartialOrd for str {
1512 fn partial_cmp(&self, other: &str) -> Option<Ordering> {
1513 Some(self.cmp(other))
1517 /// Implements substring slicing with syntax `&self[begin .. end]`.
1519 /// Returns a slice of the given string from the byte range
1520 /// [`begin`..`end`).
1522 /// This operation is `O(1)`.
1526 /// Panics if `begin` or `end` does not point to the starting
1527 /// byte offset of a character (as defined by `is_char_boundary`).
1528 /// Requires that `begin <= end` and `end <= len` where `len` is the
1529 /// length of the string.
1534 /// let s = "Löwe 老虎 Léopard";
1535 /// assert_eq!(&s[0 .. 1], "L");
1537 /// assert_eq!(&s[1 .. 9], "öwe 老");
1539 /// // these will panic:
1540 /// // byte 2 lies within `ö`:
1543 /// // byte 8 lies within `老`
1546 /// // byte 100 is outside the string
1547 /// // &s[3 .. 100];
1549 #[stable(feature = "rust1", since = "1.0.0")]
1550 impl ops::Index<ops::Range<usize>> for str {
1553 fn index(&self, index: ops::Range<usize>) -> &str {
1558 /// Implements mutable substring slicing with syntax
1559 /// `&mut self[begin .. end]`.
1561 /// Returns a mutable slice of the given string from the byte range
1562 /// [`begin`..`end`).
1564 /// This operation is `O(1)`.
1568 /// Panics if `begin` or `end` does not point to the starting
1569 /// byte offset of a character (as defined by `is_char_boundary`).
1570 /// Requires that `begin <= end` and `end <= len` where `len` is the
1571 /// length of the string.
1572 #[stable(feature = "derefmut_for_string", since = "1.2.0")]
1573 impl ops::IndexMut<ops::Range<usize>> for str {
1575 fn index_mut(&mut self, index: ops::Range<usize>) -> &mut str {
1576 index.index_mut(self)
1580 /// Implements substring slicing with syntax `&self[.. end]`.
1582 /// Returns a slice of the string from the beginning to byte offset
1585 /// Equivalent to `&self[0 .. end]`.
1586 #[stable(feature = "rust1", since = "1.0.0")]
1587 impl ops::Index<ops::RangeTo<usize>> for str {
1591 fn index(&self, index: ops::RangeTo<usize>) -> &str {
1592 // is_char_boundary checks that the index is in [0, .len()]
1593 if self.is_char_boundary(index.end) {
1594 unsafe { self.slice_unchecked(0, index.end) }
1596 super::slice_error_fail(self, 0, index.end)
1601 /// Implements mutable substring slicing with syntax `&mut self[.. end]`.
1603 /// Returns a mutable slice of the string from the beginning to byte offset
1606 /// Equivalent to `&mut self[0 .. end]`.
1607 #[stable(feature = "derefmut_for_string", since = "1.2.0")]
1608 impl ops::IndexMut<ops::RangeTo<usize>> for str {
1610 fn index_mut(&mut self, index: ops::RangeTo<usize>) -> &mut str {
1611 // is_char_boundary checks that the index is in [0, .len()]
1612 if self.is_char_boundary(index.end) {
1613 unsafe { self.slice_mut_unchecked(0, index.end) }
1615 super::slice_error_fail(self, 0, index.end)
1620 /// Implements substring slicing with syntax `&self[begin ..]`.
1622 /// Returns a slice of the string from byte offset `begin`
1623 /// to the end of the string.
1625 /// Equivalent to `&self[begin .. len]`.
1626 #[stable(feature = "rust1", since = "1.0.0")]
1627 impl ops::Index<ops::RangeFrom<usize>> for str {
1631 fn index(&self, index: ops::RangeFrom<usize>) -> &str {
1632 // is_char_boundary checks that the index is in [0, .len()]
1633 if self.is_char_boundary(index.start) {
1634 unsafe { self.slice_unchecked(index.start, self.len()) }
1636 super::slice_error_fail(self, index.start, self.len())
1641 /// Implements mutable substring slicing with syntax `&mut self[begin ..]`.
1643 /// Returns a mutable slice of the string from byte offset `begin`
1644 /// to the end of the string.
1646 /// Equivalent to `&mut self[begin .. len]`.
1647 #[stable(feature = "derefmut_for_string", since = "1.2.0")]
1648 impl ops::IndexMut<ops::RangeFrom<usize>> for str {
1650 fn index_mut(&mut self, index: ops::RangeFrom<usize>) -> &mut str {
1651 // is_char_boundary checks that the index is in [0, .len()]
1652 if self.is_char_boundary(index.start) {
1653 let len = self.len();
1654 unsafe { self.slice_mut_unchecked(index.start, len) }
1656 super::slice_error_fail(self, index.start, self.len())
1661 /// Implements substring slicing with syntax `&self[..]`.
1663 /// Returns a slice of the whole string. This operation can
1666 /// Equivalent to `&self[0 .. len]`.
1667 #[stable(feature = "rust1", since = "1.0.0")]
1668 impl ops::Index<ops::RangeFull> for str {
1672 fn index(&self, _index: ops::RangeFull) -> &str {
1677 /// Implements mutable substring slicing with syntax `&mut self[..]`.
1679 /// Returns a mutable slice of the whole string. This operation can
1682 /// Equivalent to `&mut self[0 .. len]`.
1683 #[stable(feature = "derefmut_for_string", since = "1.2.0")]
1684 impl ops::IndexMut<ops::RangeFull> for str {
1686 fn index_mut(&mut self, _index: ops::RangeFull) -> &mut str {
1691 #[unstable(feature = "inclusive_range",
1692 reason = "recently added, follows RFC",
1694 impl ops::Index<ops::RangeInclusive<usize>> for str {
1698 fn index(&self, index: ops::RangeInclusive<usize>) -> &str {
1700 ops::RangeInclusive::Empty { .. } => "",
1701 ops::RangeInclusive::NonEmpty { end, .. } if end == usize::max_value() =>
1702 panic!("attempted to index slice up to maximum usize"),
1703 ops::RangeInclusive::NonEmpty { start, end } =>
1704 self.index(start .. end+1)
1708 #[unstable(feature = "inclusive_range",
1709 reason = "recently added, follows RFC",
1711 impl ops::Index<ops::RangeToInclusive<usize>> for str {
1715 fn index(&self, index: ops::RangeToInclusive<usize>) -> &str {
1716 self.index(0...index.end)
1720 #[unstable(feature = "inclusive_range",
1721 reason = "recently added, follows RFC",
1723 impl ops::IndexMut<ops::RangeInclusive<usize>> for str {
1725 fn index_mut(&mut self, index: ops::RangeInclusive<usize>) -> &mut str {
1727 ops::RangeInclusive::Empty { .. } => &mut self[0..0], // `&mut ""` doesn't work
1728 ops::RangeInclusive::NonEmpty { end, .. } if end == usize::max_value() =>
1729 panic!("attempted to index str up to maximum usize"),
1730 ops::RangeInclusive::NonEmpty { start, end } =>
1731 self.index_mut(start .. end+1)
1735 #[unstable(feature = "inclusive_range",
1736 reason = "recently added, follows RFC",
1738 impl ops::IndexMut<ops::RangeToInclusive<usize>> for str {
1740 fn index_mut(&mut self, index: ops::RangeToInclusive<usize>) -> &mut str {
1741 self.index_mut(0...index.end)
1745 #[unstable(feature = "str_checked_slicing", issue = "39932")]
1746 impl SliceIndex<str> for ops::RangeFull {
1749 fn get(self, slice: &str) -> Option<&Self::Output> {
1753 fn get_mut(self, slice: &mut str) -> Option<&mut Self::Output> {
1757 unsafe fn get_unchecked(self, slice: &str) -> &Self::Output {
1761 unsafe fn get_unchecked_mut(self, slice: &mut str) -> &mut Self::Output {
1765 fn index(self, slice: &str) -> &Self::Output {
1769 fn index_mut(self, slice: &mut str) -> &mut Self::Output {
1774 #[unstable(feature = "str_checked_slicing", issue = "39932")]
1775 impl SliceIndex<str> for ops::Range<usize> {
1778 fn get(self, slice: &str) -> Option<&Self::Output> {
1779 if self.start <= self.end &&
1780 slice.is_char_boundary(self.start) &&
1781 slice.is_char_boundary(self.end) {
1782 Some(unsafe { self.get_unchecked(slice) })
1788 fn get_mut(self, slice: &mut str) -> Option<&mut Self::Output> {
1789 if self.start <= self.end &&
1790 slice.is_char_boundary(self.start) &&
1791 slice.is_char_boundary(self.end) {
1792 Some(unsafe { self.get_unchecked_mut(slice) })
1798 unsafe fn get_unchecked(self, slice: &str) -> &Self::Output {
1799 let ptr = slice.as_ptr().offset(self.start as isize);
1800 let len = self.end - self.start;
1801 super::from_utf8_unchecked(slice::from_raw_parts(ptr, len))
1804 unsafe fn get_unchecked_mut(self, slice: &mut str) -> &mut Self::Output {
1805 let ptr = slice.as_ptr().offset(self.start as isize);
1806 let len = self.end - self.start;
1807 mem::transmute(slice::from_raw_parts_mut(ptr as *mut u8, len))
1810 fn index(self, slice: &str) -> &Self::Output {
1811 let (start, end) = (self.start, self.end);
1812 self.get(slice).unwrap_or_else(|| super::slice_error_fail(slice, start, end))
1815 fn index_mut(self, slice: &mut str) -> &mut Self::Output {
1816 // is_char_boundary checks that the index is in [0, .len()]
1817 // canot reuse `get` as above, because of NLL trouble
1818 if self.start <= self.end &&
1819 slice.is_char_boundary(self.start) &&
1820 slice.is_char_boundary(self.end) {
1821 unsafe { self.get_unchecked_mut(slice) }
1823 super::slice_error_fail(slice, self.start, self.end)
1828 #[unstable(feature = "str_checked_slicing", issue = "39932")]
1829 impl SliceIndex<str> for ops::RangeTo<usize> {
1832 fn get(self, slice: &str) -> Option<&Self::Output> {
1833 if slice.is_char_boundary(self.end) {
1834 Some(unsafe { self.get_unchecked(slice) })
1840 fn get_mut(self, slice: &mut str) -> Option<&mut Self::Output> {
1841 if slice.is_char_boundary(self.end) {
1842 Some(unsafe { self.get_unchecked_mut(slice) })
1848 unsafe fn get_unchecked(self, slice: &str) -> &Self::Output {
1849 let ptr = slice.as_ptr();
1850 super::from_utf8_unchecked(slice::from_raw_parts(ptr, self.end))
1853 unsafe fn get_unchecked_mut(self, slice: &mut str) -> &mut Self::Output {
1854 let ptr = slice.as_ptr();
1855 mem::transmute(slice::from_raw_parts_mut(ptr as *mut u8, self.end))
1858 fn index(self, slice: &str) -> &Self::Output {
1860 self.get(slice).unwrap_or_else(|| super::slice_error_fail(slice, 0, end))
1863 fn index_mut(self, slice: &mut str) -> &mut Self::Output {
1864 if slice.is_char_boundary(self.end) {
1865 unsafe { self.get_unchecked_mut(slice) }
1867 super::slice_error_fail(slice, 0, self.end)
1872 #[unstable(feature = "str_checked_slicing", issue = "39932")]
1873 impl SliceIndex<str> for ops::RangeFrom<usize> {
1876 fn get(self, slice: &str) -> Option<&Self::Output> {
1877 if slice.is_char_boundary(self.start) {
1878 Some(unsafe { self.get_unchecked(slice) })
1884 fn get_mut(self, slice: &mut str) -> Option<&mut Self::Output> {
1885 if slice.is_char_boundary(self.start) {
1886 Some(unsafe { self.get_unchecked_mut(slice) })
1892 unsafe fn get_unchecked(self, slice: &str) -> &Self::Output {
1893 let ptr = slice.as_ptr().offset(self.start as isize);
1894 let len = slice.len() - self.start;
1895 super::from_utf8_unchecked(slice::from_raw_parts(ptr, len))
1898 unsafe fn get_unchecked_mut(self, slice: &mut str) -> &mut Self::Output {
1899 let ptr = slice.as_ptr().offset(self.start as isize);
1900 let len = slice.len() - self.start;
1901 mem::transmute(slice::from_raw_parts_mut(ptr as *mut u8, len))
1904 fn index(self, slice: &str) -> &Self::Output {
1905 let (start, end) = (self.start, slice.len());
1906 self.get(slice).unwrap_or_else(|| super::slice_error_fail(slice, start, end))
1909 fn index_mut(self, slice: &mut str) -> &mut Self::Output {
1910 if slice.is_char_boundary(self.start) {
1911 unsafe { self.get_unchecked_mut(slice) }
1913 super::slice_error_fail(slice, self.start, slice.len())
1918 #[unstable(feature = "str_checked_slicing", issue = "39932")]
1919 impl SliceIndex<str> for ops::RangeInclusive<usize> {
1922 fn get(self, slice: &str) -> Option<&Self::Output> {
1924 ops::RangeInclusive::Empty { .. } => 0..0,
1925 ops::RangeInclusive::NonEmpty { start, end } => start..end+1,
1929 fn get_mut(self, slice: &mut str) -> Option<&mut Self::Output> {
1931 ops::RangeInclusive::Empty { .. } => 0..0,
1932 ops::RangeInclusive::NonEmpty { start, end } => start..end+1,
1936 unsafe fn get_unchecked(self, slice: &str) -> &Self::Output {
1938 ops::RangeInclusive::Empty { .. } => 0..0,
1939 ops::RangeInclusive::NonEmpty { start, end } => start..end+1,
1940 }.get_unchecked(slice)
1943 unsafe fn get_unchecked_mut(self, slice: &mut str) -> &mut Self::Output {
1945 ops::RangeInclusive::Empty { .. } => 0..0,
1946 ops::RangeInclusive::NonEmpty { start, end } => start..end+1,
1947 }.get_unchecked_mut(slice)
1950 fn index(self, slice: &str) -> &Self::Output {
1952 ops::RangeInclusive::Empty { .. } => 0..0,
1953 ops::RangeInclusive::NonEmpty { start, end } => start..end+1,
1957 fn index_mut(self, slice: &mut str) -> &mut Self::Output {
1959 ops::RangeInclusive::Empty { .. } => 0..0,
1960 ops::RangeInclusive::NonEmpty { start, end } => start..end+1,
1967 #[unstable(feature = "str_checked_slicing", issue = "39932")]
1968 impl SliceIndex<str> for ops::RangeToInclusive<usize> {
1971 fn get(self, slice: &str) -> Option<&Self::Output> {
1972 if slice.is_char_boundary(self.end + 1) {
1973 Some(unsafe { self.get_unchecked(slice) })
1979 fn get_mut(self, slice: &mut str) -> Option<&mut Self::Output> {
1980 if slice.is_char_boundary(self.end + 1) {
1981 Some(unsafe { self.get_unchecked_mut(slice) })
1987 unsafe fn get_unchecked(self, slice: &str) -> &Self::Output {
1988 let ptr = slice.as_ptr();
1989 super::from_utf8_unchecked(slice::from_raw_parts(ptr, self.end + 1))
1992 unsafe fn get_unchecked_mut(self, slice: &mut str) -> &mut Self::Output {
1993 let ptr = slice.as_ptr();
1994 mem::transmute(slice::from_raw_parts_mut(ptr as *mut u8, self.end + 1))
1997 fn index(self, slice: &str) -> &Self::Output {
1998 let end = self.end + 1;
1999 self.get(slice).unwrap_or_else(|| super::slice_error_fail(slice, 0, end))
2002 fn index_mut(self, slice: &mut str) -> &mut Self::Output {
2003 if slice.is_char_boundary(self.end) {
2004 unsafe { self.get_unchecked_mut(slice) }
2006 super::slice_error_fail(slice, 0, self.end + 1)
2014 /// Methods for string slices
2015 #[allow(missing_docs)]
2017 #[unstable(feature = "core_str_ext",
2018 reason = "stable interface provided by `impl str` in later crates",
2021 // NB there are no docs here are they're all located on the StrExt trait in
2022 // libcollections, not here.
2024 #[stable(feature = "core", since = "1.6.0")]
2025 fn contains<'a, P: Pattern<'a>>(&'a self, pat: P) -> bool;
2026 #[stable(feature = "core", since = "1.6.0")]
2027 fn chars(&self) -> Chars;
2028 #[stable(feature = "core", since = "1.6.0")]
2029 fn bytes(&self) -> Bytes;
2030 #[stable(feature = "core", since = "1.6.0")]
2031 fn char_indices(&self) -> CharIndices;
2032 #[stable(feature = "core", since = "1.6.0")]
2033 fn split<'a, P: Pattern<'a>>(&'a self, pat: P) -> Split<'a, P>;
2034 #[stable(feature = "core", since = "1.6.0")]
2035 fn rsplit<'a, P: Pattern<'a>>(&'a self, pat: P) -> RSplit<'a, P>
2036 where P::Searcher: ReverseSearcher<'a>;
2037 #[stable(feature = "core", since = "1.6.0")]
2038 fn splitn<'a, P: Pattern<'a>>(&'a self, count: usize, pat: P) -> SplitN<'a, P>;
2039 #[stable(feature = "core", since = "1.6.0")]
2040 fn rsplitn<'a, P: Pattern<'a>>(&'a self, count: usize, pat: P) -> RSplitN<'a, P>
2041 where P::Searcher: ReverseSearcher<'a>;
2042 #[stable(feature = "core", since = "1.6.0")]
2043 fn split_terminator<'a, P: Pattern<'a>>(&'a self, pat: P) -> SplitTerminator<'a, P>;
2044 #[stable(feature = "core", since = "1.6.0")]
2045 fn rsplit_terminator<'a, P: Pattern<'a>>(&'a self, pat: P) -> RSplitTerminator<'a, P>
2046 where P::Searcher: ReverseSearcher<'a>;
2047 #[stable(feature = "core", since = "1.6.0")]
2048 fn matches<'a, P: Pattern<'a>>(&'a self, pat: P) -> Matches<'a, P>;
2049 #[stable(feature = "core", since = "1.6.0")]
2050 fn rmatches<'a, P: Pattern<'a>>(&'a self, pat: P) -> RMatches<'a, P>
2051 where P::Searcher: ReverseSearcher<'a>;
2052 #[stable(feature = "core", since = "1.6.0")]
2053 fn match_indices<'a, P: Pattern<'a>>(&'a self, pat: P) -> MatchIndices<'a, P>;
2054 #[stable(feature = "core", since = "1.6.0")]
2055 fn rmatch_indices<'a, P: Pattern<'a>>(&'a self, pat: P) -> RMatchIndices<'a, P>
2056 where P::Searcher: ReverseSearcher<'a>;
2057 #[stable(feature = "core", since = "1.6.0")]
2058 fn lines(&self) -> Lines;
2059 #[stable(feature = "core", since = "1.6.0")]
2060 #[rustc_deprecated(since = "1.6.0", reason = "use lines() instead now")]
2061 #[allow(deprecated)]
2062 fn lines_any(&self) -> LinesAny;
2063 #[unstable(feature = "str_checked_slicing", issue = "39932")]
2064 fn get<I: SliceIndex<str>>(&self, i: I) -> Option<&I::Output>;
2065 #[unstable(feature = "str_checked_slicing", issue = "39932")]
2066 fn get_mut<I: SliceIndex<str>>(&mut self, i: I) -> Option<&mut I::Output>;
2067 #[unstable(feature = "str_checked_slicing", issue = "39932")]
2068 unsafe fn get_unchecked<I: SliceIndex<str>>(&self, i: I) -> &I::Output;
2069 #[unstable(feature = "str_checked_slicing", issue = "39932")]
2070 unsafe fn get_unchecked_mut<I: SliceIndex<str>>(&mut self, i: I) -> &mut I::Output;
2071 #[stable(feature = "core", since = "1.6.0")]
2072 unsafe fn slice_unchecked(&self, begin: usize, end: usize) -> &str;
2073 #[stable(feature = "core", since = "1.6.0")]
2074 unsafe fn slice_mut_unchecked(&mut self, begin: usize, end: usize) -> &mut str;
2075 #[stable(feature = "core", since = "1.6.0")]
2076 fn starts_with<'a, P: Pattern<'a>>(&'a self, pat: P) -> bool;
2077 #[stable(feature = "core", since = "1.6.0")]
2078 fn ends_with<'a, P: Pattern<'a>>(&'a self, pat: P) -> bool
2079 where P::Searcher: ReverseSearcher<'a>;
2080 #[stable(feature = "core", since = "1.6.0")]
2081 fn trim_matches<'a, P: Pattern<'a>>(&'a self, pat: P) -> &'a str
2082 where P::Searcher: DoubleEndedSearcher<'a>;
2083 #[stable(feature = "core", since = "1.6.0")]
2084 fn trim_left_matches<'a, P: Pattern<'a>>(&'a self, pat: P) -> &'a str;
2085 #[stable(feature = "core", since = "1.6.0")]
2086 fn trim_right_matches<'a, P: Pattern<'a>>(&'a self, pat: P) -> &'a str
2087 where P::Searcher: ReverseSearcher<'a>;
2088 #[stable(feature = "is_char_boundary", since = "1.9.0")]
2089 fn is_char_boundary(&self, index: usize) -> bool;
2090 #[stable(feature = "core", since = "1.6.0")]
2091 fn as_bytes(&self) -> &[u8];
2092 #[stable(feature = "core", since = "1.6.0")]
2093 fn find<'a, P: Pattern<'a>>(&'a self, pat: P) -> Option<usize>;
2094 #[stable(feature = "core", since = "1.6.0")]
2095 fn rfind<'a, P: Pattern<'a>>(&'a self, pat: P) -> Option<usize>
2096 where P::Searcher: ReverseSearcher<'a>;
2097 fn find_str<'a, P: Pattern<'a>>(&'a self, pat: P) -> Option<usize>;
2098 #[stable(feature = "core", since = "1.6.0")]
2099 fn split_at(&self, mid: usize) -> (&str, &str);
2100 #[stable(feature = "core", since = "1.6.0")]
2101 fn split_at_mut(&mut self, mid: usize) -> (&mut str, &mut str);
2102 #[stable(feature = "core", since = "1.6.0")]
2103 fn as_ptr(&self) -> *const u8;
2104 #[stable(feature = "core", since = "1.6.0")]
2105 fn len(&self) -> usize;
2106 #[stable(feature = "core", since = "1.6.0")]
2107 fn is_empty(&self) -> bool;
2108 #[stable(feature = "core", since = "1.6.0")]
2109 fn parse<'a, T: TryFrom<&'a str>>(&'a self) -> Result<T, T::Error>;
2112 // truncate `&str` to length at most equal to `max`
2113 // return `true` if it were truncated, and the new str.
2114 fn truncate_to_char_boundary(s: &str, mut max: usize) -> (bool, &str) {
2118 while !s.is_char_boundary(max) {
2127 fn slice_error_fail(s: &str, begin: usize, end: usize) -> ! {
2128 const MAX_DISPLAY_LENGTH: usize = 256;
2129 let (truncated, s_trunc) = truncate_to_char_boundary(s, MAX_DISPLAY_LENGTH);
2130 let ellipsis = if truncated { "[...]" } else { "" };
2133 if begin > s.len() || end > s.len() {
2134 let oob_index = if begin > s.len() { begin } else { end };
2135 panic!("byte index {} is out of bounds of `{}`{}", oob_index, s_trunc, ellipsis);
2139 assert!(begin <= end, "begin <= end ({} <= {}) when slicing `{}`{}",
2140 begin, end, s_trunc, ellipsis);
2142 // 3. character boundary
2143 let index = if !s.is_char_boundary(begin) { begin } else { end };
2144 // find the character
2145 let mut char_start = index;
2146 while !s.is_char_boundary(char_start) {
2149 // `char_start` must be less than len and a char boundary
2150 let ch = s[char_start..].chars().next().unwrap();
2151 let char_range = char_start .. char_start + ch.len_utf8();
2152 panic!("byte index {} is not a char boundary; it is inside {:?} (bytes {:?}) of `{}`{}",
2153 index, ch, char_range, s_trunc, ellipsis);
2156 #[stable(feature = "core", since = "1.6.0")]
2157 impl StrExt for str {
2159 fn contains<'a, P: Pattern<'a>>(&'a self, pat: P) -> bool {
2160 pat.is_contained_in(self)
2164 fn chars(&self) -> Chars {
2165 Chars{iter: self.as_bytes().iter()}
2169 fn bytes(&self) -> Bytes {
2170 Bytes(self.as_bytes().iter().cloned())
2174 fn char_indices(&self) -> CharIndices {
2175 CharIndices { front_offset: 0, iter: self.chars() }
2179 fn split<'a, P: Pattern<'a>>(&'a self, pat: P) -> Split<'a, P> {
2180 Split(SplitInternal {
2183 matcher: pat.into_searcher(self),
2184 allow_trailing_empty: true,
2190 fn rsplit<'a, P: Pattern<'a>>(&'a self, pat: P) -> RSplit<'a, P>
2191 where P::Searcher: ReverseSearcher<'a>
2193 RSplit(self.split(pat).0)
2197 fn splitn<'a, P: Pattern<'a>>(&'a self, count: usize, pat: P) -> SplitN<'a, P> {
2198 SplitN(SplitNInternal {
2199 iter: self.split(pat).0,
2205 fn rsplitn<'a, P: Pattern<'a>>(&'a self, count: usize, pat: P) -> RSplitN<'a, P>
2206 where P::Searcher: ReverseSearcher<'a>
2208 RSplitN(self.splitn(count, pat).0)
2212 fn split_terminator<'a, P: Pattern<'a>>(&'a self, pat: P) -> SplitTerminator<'a, P> {
2213 SplitTerminator(SplitInternal {
2214 allow_trailing_empty: false,
2220 fn rsplit_terminator<'a, P: Pattern<'a>>(&'a self, pat: P) -> RSplitTerminator<'a, P>
2221 where P::Searcher: ReverseSearcher<'a>
2223 RSplitTerminator(self.split_terminator(pat).0)
2227 fn matches<'a, P: Pattern<'a>>(&'a self, pat: P) -> Matches<'a, P> {
2228 Matches(MatchesInternal(pat.into_searcher(self)))
2232 fn rmatches<'a, P: Pattern<'a>>(&'a self, pat: P) -> RMatches<'a, P>
2233 where P::Searcher: ReverseSearcher<'a>
2235 RMatches(self.matches(pat).0)
2239 fn match_indices<'a, P: Pattern<'a>>(&'a self, pat: P) -> MatchIndices<'a, P> {
2240 MatchIndices(MatchIndicesInternal(pat.into_searcher(self)))
2244 fn rmatch_indices<'a, P: Pattern<'a>>(&'a self, pat: P) -> RMatchIndices<'a, P>
2245 where P::Searcher: ReverseSearcher<'a>
2247 RMatchIndices(self.match_indices(pat).0)
2250 fn lines(&self) -> Lines {
2251 Lines(self.split_terminator('\n').map(LinesAnyMap))
2255 #[allow(deprecated)]
2256 fn lines_any(&self) -> LinesAny {
2257 LinesAny(self.lines())
2261 fn get<I: SliceIndex<str>>(&self, i: I) -> Option<&I::Output> {
2266 fn get_mut<I: SliceIndex<str>>(&mut self, i: I) -> Option<&mut I::Output> {
2271 unsafe fn get_unchecked<I: SliceIndex<str>>(&self, i: I) -> &I::Output {
2272 i.get_unchecked(self)
2276 unsafe fn get_unchecked_mut<I: SliceIndex<str>>(&mut self, i: I) -> &mut I::Output {
2277 i.get_unchecked_mut(self)
2281 unsafe fn slice_unchecked(&self, begin: usize, end: usize) -> &str {
2282 (begin..end).get_unchecked(self)
2286 unsafe fn slice_mut_unchecked(&mut self, begin: usize, end: usize) -> &mut str {
2287 (begin..end).get_unchecked_mut(self)
2291 fn starts_with<'a, P: Pattern<'a>>(&'a self, pat: P) -> bool {
2292 pat.is_prefix_of(self)
2296 fn ends_with<'a, P: Pattern<'a>>(&'a self, pat: P) -> bool
2297 where P::Searcher: ReverseSearcher<'a>
2299 pat.is_suffix_of(self)
2303 fn trim_matches<'a, P: Pattern<'a>>(&'a self, pat: P) -> &'a str
2304 where P::Searcher: DoubleEndedSearcher<'a>
2308 let mut matcher = pat.into_searcher(self);
2309 if let Some((a, b)) = matcher.next_reject() {
2311 j = b; // Remember earliest known match, correct it below if
2312 // last match is different
2314 if let Some((_, b)) = matcher.next_reject_back() {
2318 // Searcher is known to return valid indices
2319 self.slice_unchecked(i, j)
2324 fn trim_left_matches<'a, P: Pattern<'a>>(&'a self, pat: P) -> &'a str {
2325 let mut i = self.len();
2326 let mut matcher = pat.into_searcher(self);
2327 if let Some((a, _)) = matcher.next_reject() {
2331 // Searcher is known to return valid indices
2332 self.slice_unchecked(i, self.len())
2337 fn trim_right_matches<'a, P: Pattern<'a>>(&'a self, pat: P) -> &'a str
2338 where P::Searcher: ReverseSearcher<'a>
2341 let mut matcher = pat.into_searcher(self);
2342 if let Some((_, b)) = matcher.next_reject_back() {
2346 // Searcher is known to return valid indices
2347 self.slice_unchecked(0, j)
2352 fn is_char_boundary(&self, index: usize) -> bool {
2353 // 0 and len are always ok.
2354 // Test for 0 explicitly so that it can optimize out the check
2355 // easily and skip reading string data for that case.
2356 if index == 0 || index == self.len() { return true; }
2357 match self.as_bytes().get(index) {
2359 // This is bit magic equivalent to: b < 128 || b >= 192
2360 Some(&b) => (b as i8) >= -0x40,
2365 fn as_bytes(&self) -> &[u8] {
2366 unsafe { mem::transmute(self) }
2369 fn find<'a, P: Pattern<'a>>(&'a self, pat: P) -> Option<usize> {
2370 pat.into_searcher(self).next_match().map(|(i, _)| i)
2373 fn rfind<'a, P: Pattern<'a>>(&'a self, pat: P) -> Option<usize>
2374 where P::Searcher: ReverseSearcher<'a>
2376 pat.into_searcher(self).next_match_back().map(|(i, _)| i)
2379 fn find_str<'a, P: Pattern<'a>>(&'a self, pat: P) -> Option<usize> {
2384 fn split_at(&self, mid: usize) -> (&str, &str) {
2385 // is_char_boundary checks that the index is in [0, .len()]
2386 if self.is_char_boundary(mid) {
2388 (self.slice_unchecked(0, mid),
2389 self.slice_unchecked(mid, self.len()))
2392 slice_error_fail(self, 0, mid)
2396 fn split_at_mut(&mut self, mid: usize) -> (&mut str, &mut str) {
2397 // is_char_boundary checks that the index is in [0, .len()]
2398 if self.is_char_boundary(mid) {
2399 let len = self.len();
2400 let ptr = self.as_ptr() as *mut u8;
2402 (from_raw_parts_mut(ptr, mid),
2403 from_raw_parts_mut(ptr.offset(mid as isize), len - mid))
2406 slice_error_fail(self, 0, mid)
2411 fn as_ptr(&self) -> *const u8 {
2412 self as *const str as *const u8
2416 fn len(&self) -> usize {
2417 self.as_bytes().len()
2421 fn is_empty(&self) -> bool { self.len() == 0 }
2424 fn parse<'a, T>(&'a self) -> Result<T, T::Error> where T: TryFrom<&'a str> {
2429 #[stable(feature = "rust1", since = "1.0.0")]
2430 impl AsRef<[u8]> for str {
2432 fn as_ref(&self) -> &[u8] {
2437 #[stable(feature = "rust1", since = "1.0.0")]
2438 impl<'a> Default for &'a str {
2439 /// Creates an empty str
2440 fn default() -> &'a str { "" }