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 //! Unicode string slices
13 //! *[See also the `str` primitive type](../primitive.str.html).*
16 #![stable(feature = "rust1", since = "1.0.0")]
18 // Many of the usings in this module are only used in the test configuration.
19 // It's cleaner to just turn off the unused_imports warning than to fix them.
20 #![allow(unused_imports)]
22 use core::clone::Clone;
23 use core::iter::{Iterator, Extend};
24 use core::option::Option::{self, Some, None};
25 use core::result::Result;
26 use core::str as core_str;
27 use core::str::pattern::Pattern;
28 use core::str::pattern::{Searcher, ReverseSearcher, DoubleEndedSearcher};
30 use rustc_unicode::str::{UnicodeStr, Utf16Encoder};
32 use vec_deque::VecDeque;
33 use borrow::{Borrow, ToOwned};
37 use slice::SliceConcatExt;
40 #[stable(feature = "rust1", since = "1.0.0")]
41 pub use core::str::{FromStr, Utf8Error};
43 #[stable(feature = "rust1", since = "1.0.0")]
44 pub use core::str::{Lines, LinesAny, CharRange};
45 #[stable(feature = "rust1", since = "1.0.0")]
46 pub use core::str::{Split, RSplit};
47 #[stable(feature = "rust1", since = "1.0.0")]
48 pub use core::str::{SplitN, RSplitN};
49 #[stable(feature = "rust1", since = "1.0.0")]
50 pub use core::str::{SplitTerminator, RSplitTerminator};
51 #[stable(feature = "rust1", since = "1.0.0")]
52 pub use core::str::{Matches, RMatches};
53 #[stable(feature = "rust1", since = "1.0.0")]
54 pub use core::str::{MatchIndices, RMatchIndices};
55 #[stable(feature = "rust1", since = "1.0.0")]
56 pub use core::str::{from_utf8, Chars, CharIndices, Bytes};
57 #[stable(feature = "rust1", since = "1.0.0")]
58 pub use core::str::{from_utf8_unchecked, ParseBoolError};
59 #[stable(feature = "rust1", since = "1.0.0")]
60 pub use rustc_unicode::str::SplitWhitespace;
61 #[stable(feature = "rust1", since = "1.0.0")]
62 pub use core::str::pattern;
64 #[unstable(feature = "slice_concat_ext",
65 reason = "trait should not have to exist",
67 impl<S: Borrow<str>> SliceConcatExt<str> for [S] {
70 fn concat(&self) -> String {
75 // `len` calculation may overflow but push_str will check boundaries
76 let len = self.iter().map(|s| s.borrow().len()).sum();
77 let mut result = String::with_capacity(len);
80 result.push_str(s.borrow())
86 fn join(&self, sep: &str) -> String {
96 // this is wrong without the guarantee that `self` is non-empty
97 // `len` calculation may overflow but push_str but will check boundaries
98 let len = sep.len() * (self.len() - 1) +
99 self.iter().map(|s| s.borrow().len()).sum::<usize>();
100 let mut result = String::with_capacity(len);
101 let mut first = true;
107 result.push_str(sep);
109 result.push_str(s.borrow());
114 fn connect(&self, sep: &str) -> String {
119 /// External iterator for a string's UTF-16 code units.
121 /// For use with the `std::iter` module.
123 #[unstable(feature = "str_utf16", issue = "27714")]
124 pub struct Utf16Units<'a> {
125 encoder: Utf16Encoder<Chars<'a>>,
128 #[stable(feature = "rust1", since = "1.0.0")]
129 impl<'a> Iterator for Utf16Units<'a> {
133 fn next(&mut self) -> Option<u16> {
138 fn size_hint(&self) -> (usize, Option<usize>) {
139 self.encoder.size_hint()
143 // Return the initial codepoint accumulator for the first byte.
144 // The first byte is special, only want bottom 5 bits for width 2, 4 bits
145 // for width 3, and 3 bits for width 4
146 macro_rules! utf8_first_byte {
147 ($byte:expr, $width:expr) => (($byte & (0x7F >> $width)) as u32)
150 // return the value of $ch updated with continuation byte $byte
151 macro_rules! utf8_acc_cont_byte {
152 ($ch:expr, $byte:expr) => (($ch << 6) | ($byte & 63) as u32)
155 #[stable(feature = "rust1", since = "1.0.0")]
156 impl Borrow<str> for String {
158 fn borrow(&self) -> &str {
163 #[stable(feature = "rust1", since = "1.0.0")]
164 impl ToOwned for str {
166 fn to_owned(&self) -> String {
167 unsafe { String::from_utf8_unchecked(self.as_bytes().to_owned()) }
171 /// Methods for string slices.
175 /// Returns the length of `self`.
177 /// This length is in bytes, not [`char`]s or graphemes. In other words,
178 /// it may not be what a human considers the length of the string.
180 /// [`char`]: primitive.char.html
187 /// let len = "foo".len();
188 /// assert_eq!(3, len);
190 /// let len = "ƒoo".len(); // fancy f!
191 /// assert_eq!(4, len);
193 #[stable(feature = "rust1", since = "1.0.0")]
195 pub fn len(&self) -> usize {
196 core_str::StrExt::len(self)
199 /// Returns true if this slice has a length of zero bytes.
207 /// assert!(s.is_empty());
209 /// let s = "not empty";
210 /// assert!(!s.is_empty());
213 #[stable(feature = "rust1", since = "1.0.0")]
214 pub fn is_empty(&self) -> bool {
215 core_str::StrExt::is_empty(self)
218 /// Checks that `index`-th byte lies at the start and/or end of a
219 /// UTF-8 code point sequence.
221 /// The start and end of the string (when `index == self.len()`) are
225 /// Returns `false` if `index` is greater than `self.len()`.
230 /// #![feature(str_char)]
232 /// let s = "Löwe 老虎 Léopard";
233 /// assert!(s.is_char_boundary(0));
235 /// assert!(s.is_char_boundary(6));
236 /// assert!(s.is_char_boundary(s.len()));
238 /// // second byte of `ö`
239 /// assert!(!s.is_char_boundary(2));
241 /// // third byte of `老`
242 /// assert!(!s.is_char_boundary(8));
244 #[unstable(feature = "str_char",
245 reason = "it is unclear whether this method pulls its weight \
246 with the existence of the char_indices iterator or \
247 this method may want to be replaced with checked \
251 pub fn is_char_boundary(&self, index: usize) -> bool {
252 core_str::StrExt::is_char_boundary(self, index)
255 /// Converts a string slice to a byte slice.
262 /// let bytes = "bors".as_bytes();
263 /// assert_eq!(b"bors", bytes);
265 #[stable(feature = "rust1", since = "1.0.0")]
267 pub fn as_bytes(&self) -> &[u8] {
268 core_str::StrExt::as_bytes(self)
271 /// Converts a string slice to a raw pointer.
273 /// As string slices are a slice of bytes, the raw pointer points to a
274 /// `u8`. This pointer will be pointing to the first byte of the string
283 /// let ptr = s.as_ptr();
285 #[stable(feature = "rust1", since = "1.0.0")]
287 pub fn as_ptr(&self) -> *const u8 {
288 core_str::StrExt::as_ptr(self)
291 /// Creates a string slice from another string slice, bypassing safety
294 /// This new slice goes from `begin` to `end`, including `begin` but
297 /// To get a mutable string slice instead, see the
298 /// [`slice_mut_unchecked()`] method.
300 /// [`slice_mut_unchecked()`]: #method.slice_mut_unchecked
304 /// Callers of this function are responsible that three preconditions are
307 /// * `begin` must come before `end`.
308 /// * `begin` and `end` must be byte positions within the string slice.
309 /// * `begin` and `end` must lie on UTF-8 sequence boundaries.
316 /// let s = "Löwe 老虎 Léopard";
319 /// assert_eq!("Löwe 老虎 Léopard", s.slice_unchecked(0, 21));
322 /// let s = "Hello, world!";
325 /// assert_eq!("world", s.slice_unchecked(7, 12));
328 #[stable(feature = "rust1", since = "1.0.0")]
330 pub unsafe fn slice_unchecked(&self, begin: usize, end: usize) -> &str {
331 core_str::StrExt::slice_unchecked(self, begin, end)
334 /// Creates a string slice from another string slice, bypassing safety
337 /// This new slice goes from `begin` to `end`, including `begin` but
340 /// To get an immutable string slice instead, see the
341 /// [`slice_unchecked()`] method.
343 /// [`slice_unchecked()`]: #method.slice_unchecked
347 /// Callers of this function are responsible that three preconditions are
350 /// * `begin` must come before `end`.
351 /// * `begin` and `end` must be byte positions within the string slice.
352 /// * `begin` and `end` must lie on UTF-8 sequence boundaries.
353 #[stable(feature = "str_slice_mut", since = "1.5.0")]
355 pub unsafe fn slice_mut_unchecked(&mut self, begin: usize, end: usize) -> &mut str {
356 core_str::StrExt::slice_mut_unchecked(self, begin, end)
359 /// Given a byte position, returns the next `char` and its index.
363 /// If `i` is greater than or equal to the length of the string.
364 /// If `i` is not the index of the beginning of a valid UTF-8 sequence.
368 /// This example manually iterates through the code points of a string;
369 /// this should normally be
370 /// done by `.chars()` or `.char_indices()`.
373 /// #![feature(str_char)]
375 /// use std::str::CharRange;
377 /// let s = "中华Việt Nam";
379 /// while i < s.len() {
380 /// let CharRange {ch, next} = s.char_range_at(i);
381 /// println!("{}: {}", i, ch);
402 #[unstable(feature = "str_char",
403 reason = "often replaced by char_indices, this method may \
404 be removed in favor of just char_at() or eventually \
408 pub fn char_range_at(&self, start: usize) -> CharRange {
409 core_str::StrExt::char_range_at(self, start)
412 /// Given a byte position, returns the previous `char` and its position.
414 /// Note that Unicode has many features, such as combining marks, ligatures,
415 /// and direction marks, that need to be taken into account to correctly reverse a string.
417 /// Returns 0 for next index if called on start index 0.
421 /// If `i` is greater than the length of the string.
422 /// If `i` is not an index following a valid UTF-8 sequence.
426 /// This example manually iterates through the code points of a string;
427 /// this should normally be
428 /// done by `.chars().rev()` or `.char_indices()`.
431 /// #![feature(str_char)]
433 /// use std::str::CharRange;
435 /// let s = "中华Việt Nam";
436 /// let mut i = s.len();
438 /// let CharRange {ch, next} = s.char_range_at_reverse(i);
439 /// println!("{}: {}", i, ch);
460 #[unstable(feature = "str_char",
461 reason = "often replaced by char_indices, this method may \
462 be removed in favor of just char_at_reverse() or \
463 eventually removed altogether",
466 pub fn char_range_at_reverse(&self, start: usize) -> CharRange {
467 core_str::StrExt::char_range_at_reverse(self, start)
470 /// Given a byte position, returns the `char` at that position.
474 /// If `i` is greater than or equal to the length of the string.
475 /// If `i` is not the index of the beginning of a valid UTF-8 sequence.
480 /// #![feature(str_char)]
483 /// assert_eq!(s.char_at(1), 'b');
484 /// assert_eq!(s.char_at(2), 'π');
485 /// assert_eq!(s.char_at(4), 'c');
487 #[unstable(feature = "str_char",
488 reason = "frequently replaced by the chars() iterator, this \
489 method may be removed or possibly renamed in the \
490 future; it is normally replaced by chars/char_indices \
491 iterators or by getting the first char from a \
495 pub fn char_at(&self, i: usize) -> char {
496 core_str::StrExt::char_at(self, i)
499 /// Given a byte position, returns the `char` at that position, counting
504 /// If `i` is greater than the length of the string.
505 /// If `i` is not an index following a valid UTF-8 sequence.
510 /// #![feature(str_char)]
513 /// assert_eq!(s.char_at_reverse(1), 'a');
514 /// assert_eq!(s.char_at_reverse(2), 'b');
515 /// assert_eq!(s.char_at_reverse(3), 'π');
517 #[unstable(feature = "str_char",
518 reason = "see char_at for more details, but reverse semantics \
519 are also somewhat unclear, especially with which \
520 cases generate panics",
523 pub fn char_at_reverse(&self, i: usize) -> char {
524 core_str::StrExt::char_at_reverse(self, i)
527 /// Retrieves the first `char` from a `&str` and returns it.
529 /// Note that a single Unicode character (grapheme cluster)
530 /// can be composed of multiple `char`s.
532 /// This does not allocate a new string; instead, it returns a slice that
533 /// points one code point beyond the code point that was shifted.
535 /// `None` is returned if the slice is empty.
540 /// #![feature(str_char)]
542 /// let s = "Łódź"; // \u{141}o\u{301}dz\u{301}
543 /// let (c, s1) = s.slice_shift_char().unwrap();
545 /// assert_eq!(c, 'Ł');
546 /// assert_eq!(s1, "ódź");
548 /// let (c, s2) = s1.slice_shift_char().unwrap();
550 /// assert_eq!(c, 'o');
551 /// assert_eq!(s2, "\u{301}dz\u{301}");
553 #[unstable(feature = "str_char",
554 reason = "awaiting conventions about shifting and slices and \
555 may not be warranted with the existence of the chars \
556 and/or char_indices iterators",
559 pub fn slice_shift_char(&self) -> Option<(char, &str)> {
560 core_str::StrExt::slice_shift_char(self)
563 /// Divide one string slice into two at an index.
565 /// The argument, `mid`, should be a byte offset from the start of the
566 /// string. It must also be on the boundary of a UTF-8 code point.
568 /// The two slices returned go from the start of the string slice to `mid`,
569 /// and from `mid` to the end of the string slice.
571 /// To get mutable string slices instead, see the [`split_at_mut()`]
574 /// [`split_at_mut()`]: #method.split_at_mut
578 /// Panics if `mid` is not on a UTF-8 code point boundary, or if it is
579 /// beyond the last code point of the string slice.
586 /// let s = "Per Martin-Löf";
588 /// let (first, last) = s.split_at(3);
590 /// assert_eq!("Per", first);
591 /// assert_eq!(" Martin-Löf", last);
594 #[stable(feature = "str_split_at", since = "1.4.0")]
595 pub fn split_at(&self, mid: usize) -> (&str, &str) {
596 core_str::StrExt::split_at(self, mid)
599 /// Divide one mutable string slice into two at an index.
601 /// The argument, `mid`, should be a byte offset from the start of the
602 /// string. It must also be on the boundary of a UTF-8 code point.
604 /// The two slices returned go from the start of the string slice to `mid`,
605 /// and from `mid` to the end of the string slice.
607 /// To get immutable string slices instead, see the [`split_at()`] method.
609 /// [`split_at()`]: #method.split_at
613 /// Panics if `mid` is not on a UTF-8 code point boundary, or if it is
614 /// beyond the last code point of the string slice.
621 /// let s = "Per Martin-Löf";
623 /// let (first, last) = s.split_at(3);
625 /// assert_eq!("Per", first);
626 /// assert_eq!(" Martin-Löf", last);
629 #[stable(feature = "str_split_at", since = "1.4.0")]
630 pub fn split_at_mut(&mut self, mid: usize) -> (&mut str, &mut str) {
631 core_str::StrExt::split_at_mut(self, mid)
634 /// Returns an iterator over the `char`s of a string slice.
636 /// As a string slice consists of valid UTF-8, we can iterate through a
637 /// string slice by [`char`]. This method returns such an iterator.
639 /// It's important to remember that [`char`] represents a Unicode Scalar
640 /// Value, and may not match your idea of what a 'character' is. Iteration
641 /// over grapheme clusters may be what you actually want.
643 /// [`char`]: primitive.char.html
650 /// let word = "goodbye";
652 /// let count = word.chars().count();
653 /// assert_eq!(7, count);
655 /// let mut chars = word.chars();
657 /// assert_eq!(Some('g'), chars.next());
658 /// assert_eq!(Some('o'), chars.next());
659 /// assert_eq!(Some('o'), chars.next());
660 /// assert_eq!(Some('d'), chars.next());
661 /// assert_eq!(Some('b'), chars.next());
662 /// assert_eq!(Some('y'), chars.next());
663 /// assert_eq!(Some('e'), chars.next());
665 /// assert_eq!(None, chars.next());
668 /// Remember, `char`s may not match your human intuition about characters:
673 /// let mut chars = y.chars();
675 /// assert_eq!(Some('y'), chars.next()); // not 'y̆'
676 /// assert_eq!(Some('\u{0306}'), chars.next());
678 /// assert_eq!(None, chars.next());
680 #[stable(feature = "rust1", since = "1.0.0")]
682 pub fn chars(&self) -> Chars {
683 core_str::StrExt::chars(self)
685 /// Returns an iterator over the `char`s of a string slice, and their
688 /// As a string slice consists of valid UTF-8, we can iterate through a
689 /// string slice by `char`. This method returns an iterator of both
690 /// these `char`s, as well as their byte positions.
692 /// The iterator yields tuples. The position is first, the `char` is
700 /// let word = "goodbye";
702 /// let count = word.char_indices().count();
703 /// assert_eq!(7, count);
705 /// let mut char_indices = word.char_indices();
707 /// assert_eq!(Some((0, 'g')), char_indices.next());
708 /// assert_eq!(Some((1, 'o')), char_indices.next());
709 /// assert_eq!(Some((2, 'o')), char_indices.next());
710 /// assert_eq!(Some((3, 'd')), char_indices.next());
711 /// assert_eq!(Some((4, 'b')), char_indices.next());
712 /// assert_eq!(Some((5, 'y')), char_indices.next());
713 /// assert_eq!(Some((6, 'e')), char_indices.next());
715 /// assert_eq!(None, char_indices.next());
718 /// Remember, `char`s may not match your human intuition about characters:
723 /// let mut char_indices = y.char_indices();
725 /// assert_eq!(Some((0, 'y')), char_indices.next()); // not (0, 'y̆')
726 /// assert_eq!(Some((1, '\u{0306}')), char_indices.next());
728 /// assert_eq!(None, char_indices.next());
730 #[stable(feature = "rust1", since = "1.0.0")]
732 pub fn char_indices(&self) -> CharIndices {
733 core_str::StrExt::char_indices(self)
736 /// An iterator over the bytes of a string slice.
738 /// As a string slice consists of a sequence of bytes, we can iterate
739 /// through a string slice by byte. This method returns such an iterator.
746 /// let mut bytes = "bors".bytes();
748 /// assert_eq!(Some(b'b'), bytes.next());
749 /// assert_eq!(Some(b'o'), bytes.next());
750 /// assert_eq!(Some(b'r'), bytes.next());
751 /// assert_eq!(Some(b's'), bytes.next());
753 /// assert_eq!(None, bytes.next());
755 #[stable(feature = "rust1", since = "1.0.0")]
757 pub fn bytes(&self) -> Bytes {
758 core_str::StrExt::bytes(self)
761 /// Split a string slice by whitespace.
763 /// The iterator returned will return string slices that are sub-slices of
764 /// the original string slice, separated by any amount of whitespace.
766 /// 'Whitespace' is defined according to the terms of the Unicode Derived
767 /// Core Property `White_Space`.
774 /// let mut iter = "A few words".split_whitespace();
776 /// assert_eq!(Some("A"), iter.next());
777 /// assert_eq!(Some("few"), iter.next());
778 /// assert_eq!(Some("words"), iter.next());
780 /// assert_eq!(None, iter.next());
783 /// All kinds of whitespace are considered:
786 /// let mut iter = " Mary had\ta\u{2009}little \n\t lamb".split_whitespace();
787 /// assert_eq!(Some("Mary"), iter.next());
788 /// assert_eq!(Some("had"), iter.next());
789 /// assert_eq!(Some("a"), iter.next());
790 /// assert_eq!(Some("little"), iter.next());
791 /// assert_eq!(Some("lamb"), iter.next());
793 /// assert_eq!(None, iter.next());
795 #[stable(feature = "split_whitespace", since = "1.1.0")]
797 pub fn split_whitespace(&self) -> SplitWhitespace {
798 UnicodeStr::split_whitespace(self)
801 /// An iterator over the lines of a string, as string slices.
803 /// Lines are ended with either a newline (`\n`) or a carriage return with
804 /// a line feed (`\r\n`).
806 /// The final line ending is optional.
813 /// let text = "foo\r\nbar\n\nbaz\n";
814 /// let mut lines = text.lines();
816 /// assert_eq!(Some("foo"), lines.next());
817 /// assert_eq!(Some("bar"), lines.next());
818 /// assert_eq!(Some(""), lines.next());
819 /// assert_eq!(Some("baz"), lines.next());
821 /// assert_eq!(None, lines.next());
824 /// The final line ending isn't required:
827 /// let text = "foo\nbar\n\r\nbaz";
828 /// let mut lines = text.lines();
830 /// assert_eq!(Some("foo"), lines.next());
831 /// assert_eq!(Some("bar"), lines.next());
832 /// assert_eq!(Some(""), lines.next());
833 /// assert_eq!(Some("baz"), lines.next());
835 /// assert_eq!(None, lines.next());
837 #[stable(feature = "rust1", since = "1.0.0")]
839 pub fn lines(&self) -> Lines {
840 core_str::StrExt::lines(self)
843 /// An iterator over the lines of a string.
844 #[stable(feature = "rust1", since = "1.0.0")]
845 #[rustc_deprecated(since = "1.4.0", reason = "use lines() instead now")]
848 pub fn lines_any(&self) -> LinesAny {
849 core_str::StrExt::lines_any(self)
852 /// Returns an iterator of `u16` over the string encoded as UTF-16.
853 #[unstable(feature = "str_utf16",
854 reason = "this functionality may only be provided by libunicode",
856 pub fn utf16_units(&self) -> Utf16Units {
857 Utf16Units { encoder: Utf16Encoder::new(self[..].chars()) }
860 /// Returns `true` if the given pattern matches a sub-slice of
861 /// this string slice.
863 /// Returns `false` if it does not.
870 /// let bananas = "bananas";
872 /// assert!(bananas.contains("nana"));
873 /// assert!(!bananas.contains("apples"));
875 #[stable(feature = "rust1", since = "1.0.0")]
876 pub fn contains<'a, P: Pattern<'a>>(&'a self, pat: P) -> bool {
877 core_str::StrExt::contains(self, pat)
880 /// Returns `true` if the given pattern matches a prefix of this
883 /// Returns `false` if it does not.
890 /// let bananas = "bananas";
892 /// assert!(bananas.starts_with("bana"));
893 /// assert!(!bananas.starts_with("nana"));
895 #[stable(feature = "rust1", since = "1.0.0")]
896 pub fn starts_with<'a, P: Pattern<'a>>(&'a self, pat: P) -> bool {
897 core_str::StrExt::starts_with(self, pat)
900 /// Returns `true` if the given pattern matches a suffix of this
903 /// Returns `false` if it does not.
910 /// let bananas = "bananas";
912 /// assert!(bananas.ends_with("anas"));
913 /// assert!(!bananas.ends_with("nana"));
915 #[stable(feature = "rust1", since = "1.0.0")]
916 pub fn ends_with<'a, P: Pattern<'a>>(&'a self, pat: P) -> bool
917 where P::Searcher: ReverseSearcher<'a>
919 core_str::StrExt::ends_with(self, pat)
922 /// Returns the byte index of the first character of this string slice that
923 /// matches the pattern.
925 /// Returns `None` if the pattern doesn't match.
927 /// The pattern can be a `&str`, [`char`], or a closure that determines if
928 /// a character matches.
930 /// [`char`]: primitive.char.html
937 /// let s = "Löwe 老虎 Léopard";
939 /// assert_eq!(s.find('L'), Some(0));
940 /// assert_eq!(s.find('é'), Some(14));
941 /// assert_eq!(s.find("Léopard"), Some(13));
944 /// More complex patterns with closures:
947 /// let s = "Löwe 老虎 Léopard";
949 /// assert_eq!(s.find(char::is_whitespace), Some(5));
950 /// assert_eq!(s.find(char::is_lowercase), Some(1));
953 /// Not finding the pattern:
956 /// let s = "Löwe 老虎 Léopard";
957 /// let x: &[_] = &['1', '2'];
959 /// assert_eq!(s.find(x), None);
961 #[stable(feature = "rust1", since = "1.0.0")]
962 pub fn find<'a, P: Pattern<'a>>(&'a self, pat: P) -> Option<usize> {
963 core_str::StrExt::find(self, pat)
966 /// Returns the byte index of the last character of this string slice that
967 /// matches the pattern.
969 /// Returns `None` if the pattern doesn't match.
971 /// The pattern can be a `&str`, [`char`], or a closure that determines if
972 /// a character matches.
974 /// [`char`]: primitive.char.html
981 /// let s = "Löwe 老虎 Léopard";
983 /// assert_eq!(s.rfind('L'), Some(13));
984 /// assert_eq!(s.rfind('é'), Some(14));
987 /// More complex patterns with closures:
990 /// let s = "Löwe 老虎 Léopard";
992 /// assert_eq!(s.rfind(char::is_whitespace), Some(12));
993 /// assert_eq!(s.rfind(char::is_lowercase), Some(20));
996 /// Not finding the pattern:
999 /// let s = "Löwe 老虎 Léopard";
1000 /// let x: &[_] = &['1', '2'];
1002 /// assert_eq!(s.rfind(x), None);
1004 #[stable(feature = "rust1", since = "1.0.0")]
1005 pub fn rfind<'a, P: Pattern<'a>>(&'a self, pat: P) -> Option<usize>
1006 where P::Searcher: ReverseSearcher<'a>
1008 core_str::StrExt::rfind(self, pat)
1011 /// An iterator over substrings of this string slice, separated by
1012 /// characters matched by a pattern.
1014 /// The pattern can be a `&str`, [`char`], or a closure that determines the
1017 /// # Iterator behavior
1019 /// The returned iterator will be a [`DoubleEndedIterator`] if the pattern
1020 /// allows a reverse search and forward/reverse search yields the same
1021 /// elements. This is true for, eg, [`char`] but not for `&str`.
1023 /// [`DoubleEndedIterator`]: iter/trait.DoubleEndedIterator.html
1025 /// If the pattern allows a reverse search but its results might differ
1026 /// from a forward search, the [`rsplit()`] method can be used.
1028 /// [`char`]: primitive.char.html
1029 /// [`rsplit()`]: #method.rsplit
1033 /// Simple patterns:
1036 /// let v: Vec<&str> = "Mary had a little lamb".split(' ').collect();
1037 /// assert_eq!(v, ["Mary", "had", "a", "little", "lamb"]);
1039 /// let v: Vec<&str> = "".split('X').collect();
1040 /// assert_eq!(v, [""]);
1042 /// let v: Vec<&str> = "lionXXtigerXleopard".split('X').collect();
1043 /// assert_eq!(v, ["lion", "", "tiger", "leopard"]);
1045 /// let v: Vec<&str> = "lion::tiger::leopard".split("::").collect();
1046 /// assert_eq!(v, ["lion", "tiger", "leopard"]);
1048 /// let v: Vec<&str> = "abc1def2ghi".split(char::is_numeric).collect();
1049 /// assert_eq!(v, ["abc", "def", "ghi"]);
1051 /// let v: Vec<&str> = "lionXtigerXleopard".split(char::is_uppercase).collect();
1052 /// assert_eq!(v, ["lion", "tiger", "leopard"]);
1055 /// A more complex pattern, using a closure:
1058 /// let v: Vec<&str> = "abc1defXghi".split(|c| c == '1' || c == 'X').collect();
1059 /// assert_eq!(v, ["abc", "def", "ghi"]);
1062 /// If a string contains multiple contiguous separators, you will end up
1063 /// with empty strings in the output:
1066 /// let x = "||||a||b|c".to_string();
1067 /// let d: Vec<_> = x.split('|').collect();
1069 /// assert_eq!(d, &["", "", "", "", "a", "", "b", "c"]);
1072 /// This can lead to possibly surprising behavior when whitespace is used
1073 /// as the separator. This code is correct:
1076 /// let x = " a b c".to_string();
1077 /// let d: Vec<_> = x.split(' ').collect();
1079 /// assert_eq!(d, &["", "", "", "", "a", "", "b", "c"]);
1082 /// It does _not_ give you:
1085 /// assert_eq!(d, &["a", "b", "c"]);
1088 /// Use [`split_whitespace()`] for this behavior.
1090 /// [`split_whitespace()`]: #method.split_whitespace
1091 #[stable(feature = "rust1", since = "1.0.0")]
1092 pub fn split<'a, P: Pattern<'a>>(&'a self, pat: P) -> Split<'a, P> {
1093 core_str::StrExt::split(self, pat)
1096 /// An iterator over substrings of the given string slice, separated by
1097 /// characters matched by a pattern and yielded in reverse order.
1099 /// The pattern can be a `&str`, [`char`], or a closure that determines the
1102 /// [`char`]: primitive.char.html
1104 /// # Iterator behavior
1106 /// The returned iterator requires that the pattern supports a reverse
1107 /// search, and it will be a [`DoubleEndedIterator`] if a forward/reverse
1108 /// search yields the same elements.
1110 /// [`DoubleEndedIterator`]: iter/trait.DoubleEndedIterator.html
1112 /// For iterating from the front, the [`split()`] method can be used.
1114 /// [`split()`]: #method.split
1118 /// Simple patterns:
1121 /// let v: Vec<&str> = "Mary had a little lamb".rsplit(' ').collect();
1122 /// assert_eq!(v, ["lamb", "little", "a", "had", "Mary"]);
1124 /// let v: Vec<&str> = "".rsplit('X').collect();
1125 /// assert_eq!(v, [""]);
1127 /// let v: Vec<&str> = "lionXXtigerXleopard".rsplit('X').collect();
1128 /// assert_eq!(v, ["leopard", "tiger", "", "lion"]);
1130 /// let v: Vec<&str> = "lion::tiger::leopard".rsplit("::").collect();
1131 /// assert_eq!(v, ["leopard", "tiger", "lion"]);
1134 /// A more complex pattern, using a closure:
1137 /// let v: Vec<&str> = "abc1defXghi".rsplit(|c| c == '1' || c == 'X').collect();
1138 /// assert_eq!(v, ["ghi", "def", "abc"]);
1140 #[stable(feature = "rust1", since = "1.0.0")]
1141 pub fn rsplit<'a, P: Pattern<'a>>(&'a self, pat: P) -> RSplit<'a, P>
1142 where P::Searcher: ReverseSearcher<'a>
1144 core_str::StrExt::rsplit(self, pat)
1147 /// An iterator over substrings of the given string slice, separated by
1148 /// characters matched by a pattern.
1150 /// The pattern can be a `&str`, [`char`], or a closure that determines the
1153 /// Equivalent to [`split()`], except that the trailing substring
1154 /// is skipped if empty.
1156 /// [`split()`]: #method.split
1158 /// This method can be used for string data that is _terminated_,
1159 /// rather than _separated_ by a pattern.
1161 /// # Iterator behavior
1163 /// The returned iterator will be a [`DoubleEndedIterator`] if the pattern
1164 /// allows a reverse search and forward/reverse search yields the same
1165 /// elements. This is true for, eg, [`char`] but not for `&str`.
1167 /// [`DoubleEndedIterator`]: iter/trait.DoubleEndedIterator.html
1168 /// [`char`]: primitive.char.html
1170 /// If the pattern allows a reverse search but its results might differ
1171 /// from a forward search, the [`rsplit_terminator()`] method can be used.
1173 /// [`rsplit_terminator()`]: #method.rsplit_terminator
1180 /// let v: Vec<&str> = "A.B.".split_terminator('.').collect();
1181 /// assert_eq!(v, ["A", "B"]);
1183 /// let v: Vec<&str> = "A..B..".split_terminator(".").collect();
1184 /// assert_eq!(v, ["A", "", "B", ""]);
1186 #[stable(feature = "rust1", since = "1.0.0")]
1187 pub fn split_terminator<'a, P: Pattern<'a>>(&'a self, pat: P) -> SplitTerminator<'a, P> {
1188 core_str::StrExt::split_terminator(self, pat)
1191 /// An iterator over substrings of `self`, separated by characters
1192 /// matched by a pattern and yielded in reverse order.
1194 /// The pattern can be a simple `&str`, `char`, or a closure that
1195 /// determines the split.
1196 /// Additional libraries might provide more complex patterns like
1197 /// regular expressions.
1199 /// Equivalent to `split`, except that the trailing substring is
1200 /// skipped if empty.
1202 /// This method can be used for string data that is _terminated_,
1203 /// rather than _separated_ by a pattern.
1205 /// # Iterator behavior
1207 /// The returned iterator requires that the pattern supports a
1208 /// reverse search, and it will be double ended if a forward/reverse
1209 /// search yields the same elements.
1211 /// For iterating from the front, the [`split_terminator()`] method can be
1214 /// [`split_terminator()`]: #method.split_terminator
1219 /// let v: Vec<&str> = "A.B.".rsplit_terminator('.').collect();
1220 /// assert_eq!(v, ["B", "A"]);
1222 /// let v: Vec<&str> = "A..B..".rsplit_terminator(".").collect();
1223 /// assert_eq!(v, ["", "B", "", "A"]);
1225 #[stable(feature = "rust1", since = "1.0.0")]
1226 pub fn rsplit_terminator<'a, P: Pattern<'a>>(&'a self, pat: P) -> RSplitTerminator<'a, P>
1227 where P::Searcher: ReverseSearcher<'a>
1229 core_str::StrExt::rsplit_terminator(self, pat)
1232 /// An iterator over substrings of the given string slice, separated by a
1233 /// pattern, restricted to returning at most `count` items.
1235 /// The last element returned, if any, will contain the remainder of the
1238 /// The pattern can be a `&str`, [`char`], or a closure that determines the
1241 /// [`char`]: primitive.char.html
1243 /// # Iterator behavior
1245 /// The returned iterator will not be double ended, because it is
1246 /// not efficient to support.
1248 /// If the pattern allows a reverse search, the [`rsplitn()`] method can be
1251 /// [`rsplitn()`]: #method.rsplitn
1255 /// Simple patterns:
1258 /// let v: Vec<&str> = "Mary had a little lambda".splitn(3, ' ').collect();
1259 /// assert_eq!(v, ["Mary", "had", "a little lambda"]);
1261 /// let v: Vec<&str> = "lionXXtigerXleopard".splitn(3, "X").collect();
1262 /// assert_eq!(v, ["lion", "", "tigerXleopard"]);
1264 /// let v: Vec<&str> = "abcXdef".splitn(1, 'X').collect();
1265 /// assert_eq!(v, ["abcXdef"]);
1267 /// let v: Vec<&str> = "".splitn(1, 'X').collect();
1268 /// assert_eq!(v, [""]);
1271 /// A more complex pattern, using a closure:
1274 /// let v: Vec<&str> = "abc1defXghi".splitn(2, |c| c == '1' || c == 'X').collect();
1275 /// assert_eq!(v, ["abc", "defXghi"]);
1277 #[stable(feature = "rust1", since = "1.0.0")]
1278 pub fn splitn<'a, P: Pattern<'a>>(&'a self, count: usize, pat: P) -> SplitN<'a, P> {
1279 core_str::StrExt::splitn(self, count, pat)
1282 /// An iterator over substrings of this string slice, separated by a
1283 /// pattern, starting from the end of the string, restricted to returning
1284 /// at most `count` items.
1286 /// The last element returned, if any, will contain the remainder of the
1289 /// The pattern can be a `&str`, [`char`], or a closure that
1290 /// determines the split.
1292 /// [`char`]: primitive.char.html
1294 /// # Iterator behavior
1296 /// The returned iterator will not be double ended, because it is not
1297 /// efficient to support.
1299 /// For splitting from the front, the [`splitn()`] method can be used.
1301 /// [`splitn()`]: #method.splitn
1305 /// Simple patterns:
1308 /// let v: Vec<&str> = "Mary had a little lamb".rsplitn(3, ' ').collect();
1309 /// assert_eq!(v, ["lamb", "little", "Mary had a"]);
1311 /// let v: Vec<&str> = "lionXXtigerXleopard".rsplitn(3, 'X').collect();
1312 /// assert_eq!(v, ["leopard", "tiger", "lionX"]);
1314 /// let v: Vec<&str> = "lion::tiger::leopard".rsplitn(2, "::").collect();
1315 /// assert_eq!(v, ["leopard", "lion::tiger"]);
1318 /// A more complex pattern, using a closure:
1321 /// let v: Vec<&str> = "abc1defXghi".rsplitn(2, |c| c == '1' || c == 'X').collect();
1322 /// assert_eq!(v, ["ghi", "abc1def"]);
1324 #[stable(feature = "rust1", since = "1.0.0")]
1325 pub fn rsplitn<'a, P: Pattern<'a>>(&'a self, count: usize, pat: P) -> RSplitN<'a, P>
1326 where P::Searcher: ReverseSearcher<'a>
1328 core_str::StrExt::rsplitn(self, count, pat)
1331 /// An iterator over the matches of a pattern within the given string
1334 /// The pattern can be a `&str`, [`char`], or a closure that
1335 /// determines if a character matches.
1337 /// [`char`]: primitive.char.html
1339 /// # Iterator behavior
1341 /// The returned iterator will be a [`DoubleEndedIterator`] if the pattern
1342 /// allows a reverse search and forward/reverse search yields the same
1343 /// elements. This is true for, eg, [`char`] but not for `&str`.
1345 /// [`DoubleEndedIterator`]: iter/trait.DoubleEndedIterator.html
1346 /// [`char`]: primitive.char.html
1348 /// If the pattern allows a reverse search but its results might differ
1349 /// from a forward search, the [`rmatches()`] method can be used.
1351 /// [`rmatches()`]: #method.rmatches
1358 /// let v: Vec<&str> = "abcXXXabcYYYabc".matches("abc").collect();
1359 /// assert_eq!(v, ["abc", "abc", "abc"]);
1361 /// let v: Vec<&str> = "1abc2abc3".matches(char::is_numeric).collect();
1362 /// assert_eq!(v, ["1", "2", "3"]);
1364 #[stable(feature = "str_matches", since = "1.2.0")]
1365 pub fn matches<'a, P: Pattern<'a>>(&'a self, pat: P) -> Matches<'a, P> {
1366 core_str::StrExt::matches(self, pat)
1369 /// An iterator over the matches of a pattern within this string slice,
1370 /// yielded in reverse order.
1372 /// The pattern can be a `&str`, [`char`], or a closure that determines if
1373 /// a character matches.
1375 /// [`char`]: primitive.char.html
1377 /// # Iterator behavior
1379 /// The returned iterator requires that the pattern supports a reverse
1380 /// search, and it will be a [`DoubleEndedIterator`] if a forward/reverse
1381 /// search yields the same elements.
1383 /// [`DoubleEndedIterator`]: iter/trait.DoubleEndedIterator.html
1385 /// For iterating from the front, the [`matches()`] method can be used.
1387 /// [`matches`]: #method.matches
1394 /// let v: Vec<&str> = "abcXXXabcYYYabc".rmatches("abc").collect();
1395 /// assert_eq!(v, ["abc", "abc", "abc"]);
1397 /// let v: Vec<&str> = "1abc2abc3".rmatches(char::is_numeric).collect();
1398 /// assert_eq!(v, ["3", "2", "1"]);
1400 #[stable(feature = "str_matches", since = "1.2.0")]
1401 pub fn rmatches<'a, P: Pattern<'a>>(&'a self, pat: P) -> RMatches<'a, P>
1402 where P::Searcher: ReverseSearcher<'a>
1404 core_str::StrExt::rmatches(self, pat)
1407 /// An iterator over the disjoint matches of a pattern within this string
1408 /// slice as well as the index that the match starts at.
1410 /// For matches of `pat` within `self` that overlap, only the indices
1411 /// corresponding to the first match are returned.
1413 /// The pattern can be a `&str`, [`char`], or a closure that determines
1414 /// if a character matches.
1416 /// [`char`]: primitive.char.html
1418 /// # Iterator behavior
1420 /// The returned iterator will be a [`DoubleEndedIterator`] if the pattern
1421 /// allows a reverse search and forward/reverse search yields the same
1422 /// elements. This is true for, eg, [`char`] but not for `&str`.
1424 /// [`DoubleEndedIterator`]: iter/trait.DoubleEndedIterator.html
1426 /// If the pattern allows a reverse search but its results might differ
1427 /// from a forward search, the [`rmatch_indices()`] method can be used.
1429 /// [`rmatch_indices()`]: #method.rmatch_indices
1436 /// let v: Vec<_> = "abcXXXabcYYYabc".match_indices("abc").collect();
1437 /// assert_eq!(v, [(0, "abc"), (6, "abc"), (12, "abc")]);
1439 /// let v: Vec<_> = "1abcabc2".match_indices("abc").collect();
1440 /// assert_eq!(v, [(1, "abc"), (4, "abc")]);
1442 /// let v: Vec<_> = "ababa".match_indices("aba").collect();
1443 /// assert_eq!(v, [(0, "aba")]); // only the first `aba`
1445 #[stable(feature = "str_match_indices", since = "1.5.0")]
1446 pub fn match_indices<'a, P: Pattern<'a>>(&'a self, pat: P) -> MatchIndices<'a, P> {
1447 core_str::StrExt::match_indices(self, pat)
1450 /// An iterator over the disjoint matches of a pattern within `self`,
1451 /// yielded in reverse order along with the index of the match.
1453 /// For matches of `pat` within `self` that overlap, only the indices
1454 /// corresponding to the last match are returned.
1456 /// The pattern can be a `&str`, [`char`], or a closure that determines if a
1457 /// character matches.
1459 /// [`char`]: primitive.char.html
1461 /// # Iterator behavior
1463 /// The returned iterator requires that the pattern supports a reverse
1464 /// search, and it will be a `[DoubleEndedIterator]` if a forward/reverse
1465 /// search yields the same elements.
1467 /// [`DoubleEndedIterator`]: iter/trait.DoubleEndedIterator.html
1469 /// For iterating from the front, the [`match_indices()`] method can be used.
1471 /// [`match_indices()`]: #method.match_indices
1478 /// let v: Vec<_> = "abcXXXabcYYYabc".rmatch_indices("abc").collect();
1479 /// assert_eq!(v, [(12, "abc"), (6, "abc"), (0, "abc")]);
1481 /// let v: Vec<_> = "1abcabc2".rmatch_indices("abc").collect();
1482 /// assert_eq!(v, [(4, "abc"), (1, "abc")]);
1484 /// let v: Vec<_> = "ababa".rmatch_indices("aba").collect();
1485 /// assert_eq!(v, [(2, "aba")]); // only the last `aba`
1487 #[stable(feature = "str_match_indices", since = "1.5.0")]
1488 pub fn rmatch_indices<'a, P: Pattern<'a>>(&'a self, pat: P) -> RMatchIndices<'a, P>
1489 where P::Searcher: ReverseSearcher<'a>
1491 core_str::StrExt::rmatch_indices(self, pat)
1494 /// Returns a string slice with leading and trailing whitespace removed.
1496 /// 'Whitespace' is defined according to the terms of the Unicode Derived
1497 /// Core Property `White_Space`.
1504 /// let s = " Hello\tworld\t";
1506 /// assert_eq!("Hello\tworld", s.trim());
1508 #[stable(feature = "rust1", since = "1.0.0")]
1509 pub fn trim(&self) -> &str {
1510 UnicodeStr::trim(self)
1513 /// Returns a string slice with leading whitespace removed.
1515 /// 'Whitespace' is defined according to the terms of the Unicode Derived
1516 /// Core Property `White_Space`.
1523 /// let s = " Hello\tworld\t";
1525 /// assert_eq!("Hello\tworld\t", s.trim_left());
1527 #[stable(feature = "rust1", since = "1.0.0")]
1528 pub fn trim_left(&self) -> &str {
1529 UnicodeStr::trim_left(self)
1532 /// Returns a string slice with trailing whitespace removed.
1534 /// 'Whitespace' is defined according to the terms of the Unicode Derived
1535 /// Core Property `White_Space`.
1542 /// let s = " Hello\tworld\t";
1544 /// assert_eq!(" Hello\tworld", s.trim_right());
1546 #[stable(feature = "rust1", since = "1.0.0")]
1547 pub fn trim_right(&self) -> &str {
1548 UnicodeStr::trim_right(self)
1551 /// Returns a string slice with all prefixes and suffixes that match a
1552 /// pattern repeatedly removed.
1554 /// The pattern can be a `&str`, [`char`], or a closure that determines
1555 /// if a character matches.
1557 /// [`char`]: primitive.char.html
1561 /// Simple patterns:
1564 /// assert_eq!("11foo1bar11".trim_matches('1'), "foo1bar");
1565 /// assert_eq!("123foo1bar123".trim_matches(char::is_numeric), "foo1bar");
1567 /// let x: &[_] = &['1', '2'];
1568 /// assert_eq!("12foo1bar12".trim_matches(x), "foo1bar");
1571 /// A more complex pattern, using a closure:
1574 /// assert_eq!("1foo1barXX".trim_matches(|c| c == '1' || c == 'X'), "foo1bar");
1576 #[stable(feature = "rust1", since = "1.0.0")]
1577 pub fn trim_matches<'a, P: Pattern<'a>>(&'a self, pat: P) -> &'a str
1578 where P::Searcher: DoubleEndedSearcher<'a>
1580 core_str::StrExt::trim_matches(self, pat)
1583 /// Returns a string slice with all prefixes that match a pattern
1584 /// repeatedly removed.
1586 /// The pattern can be a `&str`, [`char`], or a closure that determines if
1587 /// a character matches.
1589 /// [`char`]: primitive.char.html
1596 /// assert_eq!("11foo1bar11".trim_left_matches('1'), "foo1bar11");
1597 /// assert_eq!("123foo1bar123".trim_left_matches(char::is_numeric), "foo1bar123");
1599 /// let x: &[_] = &['1', '2'];
1600 /// assert_eq!("12foo1bar12".trim_left_matches(x), "foo1bar12");
1602 #[stable(feature = "rust1", since = "1.0.0")]
1603 pub fn trim_left_matches<'a, P: Pattern<'a>>(&'a self, pat: P) -> &'a str {
1604 core_str::StrExt::trim_left_matches(self, pat)
1607 /// Returns a string slice with all suffixes that match a pattern
1608 /// repeatedly removed.
1610 /// The pattern can be a `&str`, [`char`], or a closure that
1611 /// determines if a character matches.
1613 /// [`char`]: primitive.char.html
1617 /// Simple patterns:
1620 /// assert_eq!("11foo1bar11".trim_right_matches('1'), "11foo1bar");
1621 /// assert_eq!("123foo1bar123".trim_right_matches(char::is_numeric), "123foo1bar");
1623 /// let x: &[_] = &['1', '2'];
1624 /// assert_eq!("12foo1bar12".trim_right_matches(x), "12foo1bar");
1627 /// A more complex pattern, using a closure:
1630 /// assert_eq!("1fooX".trim_left_matches(|c| c == '1' || c == 'X'), "fooX");
1632 #[stable(feature = "rust1", since = "1.0.0")]
1633 pub fn trim_right_matches<'a, P: Pattern<'a>>(&'a self, pat: P) -> &'a str
1634 where P::Searcher: ReverseSearcher<'a>
1636 core_str::StrExt::trim_right_matches(self, pat)
1639 /// Parses this string slice into another type.
1641 /// Because `parse()` is so general, it can cause problems with type
1642 /// inference. As such, `parse()` is one of the few times you'll see
1643 /// the syntax affectionately known as the 'turbofish': `::<>`. This
1644 /// helps the inference algorithm understand specifically which type
1645 /// you're trying to parse into.
1647 /// `parse()` can parse any type that implements the [`FromStr`] trait.
1649 /// [`FromStr`]: str/trait.FromStr.html
1653 /// Will return `Err` if it's not possible to parse this string slice into
1654 /// the desired type.
1661 /// let four: u32 = "4".parse().unwrap();
1663 /// assert_eq!(4, four);
1666 /// Using the 'turbofish' instead of annotationg `four`:
1669 /// let four = "4".parse::<u32>();
1671 /// assert_eq!(Ok(4), four);
1674 /// Failing to parse:
1677 /// let nope = "j".parse::<u32>();
1679 /// assert!(nope.is_err());
1682 #[stable(feature = "rust1", since = "1.0.0")]
1683 pub fn parse<F: FromStr>(&self) -> Result<F, F::Err> {
1684 core_str::StrExt::parse(self)
1687 /// Replaces all matches of a pattern with another string.
1689 /// `replace` creates a new [`String`], and copies the data from this string slice into it.
1690 /// While doing so, it attempts to find matches of a pattern. If it finds any, it
1691 /// replaces them with the replacement string slice.
1693 /// [`String`]: string/struct.String.html
1700 /// let s = "this is old";
1702 /// assert_eq!("this is new", s.replace("old", "new"));
1705 /// When the pattern doesn't match:
1708 /// let s = "this is old";
1709 /// assert_eq!(s, s.replace("cookie monster", "little lamb"));
1711 #[stable(feature = "rust1", since = "1.0.0")]
1712 pub fn replace<'a, P: Pattern<'a>>(&'a self, from: P, to: &str) -> String {
1713 let mut result = String::new();
1714 let mut last_end = 0;
1715 for (start, part) in self.match_indices(from) {
1716 result.push_str(unsafe { self.slice_unchecked(last_end, start) });
1717 result.push_str(to);
1718 last_end = start + part.len();
1720 result.push_str(unsafe { self.slice_unchecked(last_end, self.len()) });
1724 /// Returns the lowercase equivalent of this string slice, as a new `String`.
1726 /// 'Lowercase' is defined according to the terms of the Unicode Derived Core Property
1734 /// let s = "HELLO";
1736 /// assert_eq!("hello", s.to_lowercase());
1739 /// A tricky example, with sigma:
1742 /// let sigma = "Σ";
1744 /// assert_eq!("σ", sigma.to_lowercase());
1746 /// // but at the end of a word, it's ς, not σ:
1747 /// let odysseus = "ὈΔΥΣΣΕΎΣ";
1749 /// assert_eq!("ὀδυσσεύς", odysseus.to_lowercase());
1752 /// Languages without case are not changed:
1755 /// let new_year = "农历新年";
1757 /// assert_eq!(new_year, new_year.to_lowercase());
1759 #[stable(feature = "unicode_case_mapping", since = "1.2.0")]
1760 pub fn to_lowercase(&self) -> String {
1761 let mut s = String::with_capacity(self.len());
1762 for (i, c) in self[..].char_indices() {
1764 // Σ maps to σ, except at the end of a word where it maps to ς.
1765 // This is the only conditional (contextual) but language-independent mapping
1766 // in `SpecialCasing.txt`,
1767 // so hard-code it rather than have a generic "condition" mechanim.
1768 // See https://github.com/rust-lang/rust/issues/26035
1769 map_uppercase_sigma(self, i, &mut s)
1771 s.extend(c.to_lowercase());
1776 fn map_uppercase_sigma(from: &str, i: usize, to: &mut String) {
1777 // See http://www.unicode.org/versions/Unicode7.0.0/ch03.pdf#G33992
1778 // for the definition of `Final_Sigma`.
1779 debug_assert!('Σ'.len_utf8() == 2);
1780 let is_word_final = case_ignoreable_then_cased(from[..i].chars().rev()) &&
1781 !case_ignoreable_then_cased(from[i + 2..].chars());
1782 to.push_str(if is_word_final {
1789 fn case_ignoreable_then_cased<I: Iterator<Item = char>>(iter: I) -> bool {
1790 use rustc_unicode::derived_property::{Cased, Case_Ignorable};
1791 match iter.skip_while(|&c| Case_Ignorable(c)).next() {
1792 Some(c) => Cased(c),
1798 /// Returns the uppercase equivalent of this string slice, as a new `String`.
1800 /// 'Uppercase' is defined according to the terms of the Unicode Derived Core Property
1808 /// let s = "hello";
1810 /// assert_eq!("HELLO", s.to_uppercase());
1813 /// Scripts without case are not changed:
1816 /// let new_year = "农历新年";
1818 /// assert_eq!(new_year, new_year.to_uppercase());
1820 #[stable(feature = "unicode_case_mapping", since = "1.2.0")]
1821 pub fn to_uppercase(&self) -> String {
1822 let mut s = String::with_capacity(self.len());
1823 s.extend(self.chars().flat_map(|c| c.to_uppercase()));
1827 /// Escapes each char in `s` with `char::escape_default`.
1828 #[unstable(feature = "str_escape",
1829 reason = "return type may change to be an iterator",
1831 pub fn escape_default(&self) -> String {
1832 self.chars().flat_map(|c| c.escape_default()).collect()
1835 /// Escapes each char in `s` with `char::escape_unicode`.
1836 #[unstable(feature = "str_escape",
1837 reason = "return type may change to be an iterator",
1839 pub fn escape_unicode(&self) -> String {
1840 self.chars().flat_map(|c| c.escape_unicode()).collect()
1843 /// Converts a `Box<str>` into a `String` without copying or allocating.
1850 /// let string = String::from("birthday gift");
1851 /// let boxed_str = string.clone().into_boxed_str();
1853 /// assert_eq!(boxed_str.into_string(), string);
1855 #[stable(feature = "box_str", since = "1.4.0")]
1856 pub fn into_string(self: Box<str>) -> String {
1858 let slice = mem::transmute::<Box<str>, Box<[u8]>>(self);
1859 String::from_utf8_unchecked(slice.into_vec())