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](../../std/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
::str as core_str
;
23 use core
::str::pattern
::Pattern
;
24 use core
::str::pattern
::{Searcher, ReverseSearcher, DoubleEndedSearcher}
;
26 use rustc_unicode
::str::{UnicodeStr, Utf16Encoder}
;
28 use vec_deque
::VecDeque
;
29 use borrow
::{Borrow, ToOwned}
;
33 use slice
::SliceConcatExt
;
36 #[stable(feature = "rust1", since = "1.0.0")]
37 pub use core
::str::{FromStr, Utf8Error}
;
39 #[stable(feature = "rust1", since = "1.0.0")]
40 pub use core
::str::{Lines, LinesAny}
;
41 #[stable(feature = "rust1", since = "1.0.0")]
42 pub use core
::str::{Split, RSplit}
;
43 #[stable(feature = "rust1", since = "1.0.0")]
44 pub use core
::str::{SplitN, RSplitN}
;
45 #[stable(feature = "rust1", since = "1.0.0")]
46 pub use core
::str::{SplitTerminator, RSplitTerminator}
;
47 #[stable(feature = "rust1", since = "1.0.0")]
48 pub use core
::str::{Matches, RMatches}
;
49 #[stable(feature = "rust1", since = "1.0.0")]
50 pub use core
::str::{MatchIndices, RMatchIndices}
;
51 #[stable(feature = "rust1", since = "1.0.0")]
52 pub use core
::str::{from_utf8, Chars, CharIndices, Bytes}
;
53 #[stable(feature = "rust1", since = "1.0.0")]
54 pub use core
::str::{from_utf8_unchecked, ParseBoolError}
;
55 #[stable(feature = "rust1", since = "1.0.0")]
56 pub use rustc_unicode
::str::SplitWhitespace
;
57 #[stable(feature = "rust1", since = "1.0.0")]
58 pub use core
::str::pattern
;
60 #[unstable(feature = "slice_concat_ext",
61 reason
= "trait should not have to exist",
63 impl<S
: Borrow
<str>> SliceConcatExt
<str> for [S
] {
66 fn concat(&self) -> String
{
71 // `len` calculation may overflow but push_str will check boundaries
72 let len
= self.iter().map(|s
| s
.borrow().len()).sum();
73 let mut result
= String
::with_capacity(len
);
76 result
.push_str(s
.borrow())
82 fn join(&self, sep
: &str) -> String
{
92 // this is wrong without the guarantee that `self` is non-empty
93 // `len` calculation may overflow but push_str but will check boundaries
94 let len
= sep
.len() * (self.len() - 1) +
95 self.iter().map(|s
| s
.borrow().len()).sum
::<usize>();
96 let mut result
= String
::with_capacity(len
);
103 result
.push_str(sep
);
105 result
.push_str(s
.borrow());
110 fn connect(&self, sep
: &str) -> String
{
115 /// External iterator for a string's UTF-16 code units.
117 /// For use with the `std::iter` module.
119 #[stable(feature = "encode_utf16", since = "1.8.0")]
120 pub struct EncodeUtf16
<'a
> {
121 encoder
: Utf16Encoder
<Chars
<'a
>>,
124 #[stable(feature = "rust1", since = "1.0.0")]
125 impl<'a
> Iterator
for EncodeUtf16
<'a
> {
129 fn next(&mut self) -> Option
<u16> {
134 fn size_hint(&self) -> (usize, Option
<usize>) {
135 self.encoder
.size_hint()
139 // Return the initial codepoint accumulator for the first byte.
140 // The first byte is special, only want bottom 5 bits for width 2, 4 bits
141 // for width 3, and 3 bits for width 4
142 macro_rules
! utf8_first_byte
{
143 ($byte
:expr
, $width
:expr
) => (($byte
& (0x7F >> $width
)) as u32)
146 // return the value of $ch updated with continuation byte $byte
147 macro_rules
! utf8_acc_cont_byte
{
148 ($ch
:expr
, $byte
:expr
) => (($ch
<< 6) | ($byte
& 63) as u32)
151 #[stable(feature = "rust1", since = "1.0.0")]
152 impl Borrow
<str> for String
{
154 fn borrow(&self) -> &str {
159 #[stable(feature = "rust1", since = "1.0.0")]
160 impl ToOwned
for str {
162 fn to_owned(&self) -> String
{
163 unsafe { String::from_utf8_unchecked(self.as_bytes().to_owned()) }
167 /// Methods for string slices.
171 /// Returns the length of `self`.
173 /// This length is in bytes, not [`char`]s or graphemes. In other words,
174 /// it may not be what a human considers the length of the string.
176 /// [`char`]: primitive.char.html
183 /// let len = "foo".len();
184 /// assert_eq!(3, len);
186 /// let len = "ƒoo".len(); // fancy f!
187 /// assert_eq!(4, len);
189 #[stable(feature = "rust1", since = "1.0.0")]
191 pub fn len(&self) -> usize {
192 core_str
::StrExt
::len(self)
195 /// Returns true if this slice has a length of zero bytes.
203 /// assert!(s.is_empty());
205 /// let s = "not empty";
206 /// assert!(!s.is_empty());
209 #[stable(feature = "rust1", since = "1.0.0")]
210 pub fn is_empty(&self) -> bool
{
211 core_str
::StrExt
::is_empty(self)
214 /// Checks that `index`-th byte lies at the start and/or end of a
215 /// UTF-8 code point sequence.
217 /// The start and end of the string (when `index == self.len()`) are
221 /// Returns `false` if `index` is greater than `self.len()`.
226 /// let s = "Löwe 老虎 Léopard";
227 /// assert!(s.is_char_boundary(0));
229 /// assert!(s.is_char_boundary(6));
230 /// assert!(s.is_char_boundary(s.len()));
232 /// // second byte of `ö`
233 /// assert!(!s.is_char_boundary(2));
235 /// // third byte of `老`
236 /// assert!(!s.is_char_boundary(8));
238 #[stable(feature = "is_char_boundary", since = "1.9.0")]
240 pub fn is_char_boundary(&self, index
: usize) -> bool
{
241 core_str
::StrExt
::is_char_boundary(self, index
)
244 /// Converts a string slice to a byte slice.
251 /// let bytes = "bors".as_bytes();
252 /// assert_eq!(b"bors", bytes);
254 #[stable(feature = "rust1", since = "1.0.0")]
256 pub fn as_bytes(&self) -> &[u8] {
257 core_str
::StrExt
::as_bytes(self)
260 /// Converts a string slice to a raw pointer.
262 /// As string slices are a slice of bytes, the raw pointer points to a
263 /// [`u8`]. This pointer will be pointing to the first byte of the string
266 /// [`u8`]: primitive.u8.html
274 /// let ptr = s.as_ptr();
276 #[stable(feature = "rust1", since = "1.0.0")]
278 pub fn as_ptr(&self) -> *const u8 {
279 core_str
::StrExt
::as_ptr(self)
282 /// Creates a string slice from another string slice, bypassing safety
285 /// This new slice goes from `begin` to `end`, including `begin` but
288 /// To get a mutable string slice instead, see the
289 /// [`slice_mut_unchecked()`] method.
291 /// [`slice_mut_unchecked()`]: #method.slice_mut_unchecked
295 /// Callers of this function are responsible that three preconditions are
298 /// * `begin` must come before `end`.
299 /// * `begin` and `end` must be byte positions within the string slice.
300 /// * `begin` and `end` must lie on UTF-8 sequence boundaries.
307 /// let s = "Löwe 老虎 Léopard";
310 /// assert_eq!("Löwe 老虎 Léopard", s.slice_unchecked(0, 21));
313 /// let s = "Hello, world!";
316 /// assert_eq!("world", s.slice_unchecked(7, 12));
319 #[stable(feature = "rust1", since = "1.0.0")]
321 pub unsafe fn slice_unchecked(&self, begin
: usize, end
: usize) -> &str {
322 core_str
::StrExt
::slice_unchecked(self, begin
, end
)
325 /// Creates a string slice from another string slice, bypassing safety
328 /// This new slice goes from `begin` to `end`, including `begin` but
331 /// To get an immutable string slice instead, see the
332 /// [`slice_unchecked()`] method.
334 /// [`slice_unchecked()`]: #method.slice_unchecked
338 /// Callers of this function are responsible that three preconditions are
341 /// * `begin` must come before `end`.
342 /// * `begin` and `end` must be byte positions within the string slice.
343 /// * `begin` and `end` must lie on UTF-8 sequence boundaries.
344 #[stable(feature = "str_slice_mut", since = "1.5.0")]
346 pub unsafe fn slice_mut_unchecked(&mut self, begin
: usize, end
: usize) -> &mut str {
347 core_str
::StrExt
::slice_mut_unchecked(self, begin
, end
)
350 /// Divide one string slice into two at an index.
352 /// The argument, `mid`, should be a byte offset from the start of the
353 /// string. It must also be on the boundary of a UTF-8 code point.
355 /// The two slices returned go from the start of the string slice to `mid`,
356 /// and from `mid` to the end of the string slice.
358 /// To get mutable string slices instead, see the [`split_at_mut()`]
361 /// [`split_at_mut()`]: #method.split_at_mut
365 /// Panics if `mid` is not on a UTF-8 code point boundary, or if it is
366 /// beyond the last code point of the string slice.
373 /// let s = "Per Martin-Löf";
375 /// let (first, last) = s.split_at(3);
377 /// assert_eq!("Per", first);
378 /// assert_eq!(" Martin-Löf", last);
381 #[stable(feature = "str_split_at", since = "1.4.0")]
382 pub fn split_at(&self, mid
: usize) -> (&str, &str) {
383 core_str
::StrExt
::split_at(self, mid
)
386 /// Divide one mutable string slice into two at an index.
388 /// The argument, `mid`, should be a byte offset from the start of the
389 /// string. It must also be on the boundary of a UTF-8 code point.
391 /// The two slices returned go from the start of the string slice to `mid`,
392 /// and from `mid` to the end of the string slice.
394 /// To get immutable string slices instead, see the [`split_at()`] method.
396 /// [`split_at()`]: #method.split_at
400 /// Panics if `mid` is not on a UTF-8 code point boundary, or if it is
401 /// beyond the last code point of the string slice.
408 /// let mut s = "Per Martin-Löf".to_string();
410 /// let (first, last) = s.split_at_mut(3);
412 /// assert_eq!("Per", first);
413 /// assert_eq!(" Martin-Löf", last);
416 #[stable(feature = "str_split_at", since = "1.4.0")]
417 pub fn split_at_mut(&mut self, mid
: usize) -> (&mut str, &mut str) {
418 core_str
::StrExt
::split_at_mut(self, mid
)
421 /// Returns an iterator over the `char`s of a string slice.
423 /// As a string slice consists of valid UTF-8, we can iterate through a
424 /// string slice by [`char`]. This method returns such an iterator.
426 /// It's important to remember that [`char`] represents a Unicode Scalar
427 /// Value, and may not match your idea of what a 'character' is. Iteration
428 /// over grapheme clusters may be what you actually want.
430 /// [`char`]: primitive.char.html
437 /// let word = "goodbye";
439 /// let count = word.chars().count();
440 /// assert_eq!(7, count);
442 /// let mut chars = word.chars();
444 /// assert_eq!(Some('g'), chars.next());
445 /// assert_eq!(Some('o'), chars.next());
446 /// assert_eq!(Some('o'), chars.next());
447 /// assert_eq!(Some('d'), chars.next());
448 /// assert_eq!(Some('b'), chars.next());
449 /// assert_eq!(Some('y'), chars.next());
450 /// assert_eq!(Some('e'), chars.next());
452 /// assert_eq!(None, chars.next());
455 /// Remember, [`char`]s may not match your human intuition about characters:
460 /// let mut chars = y.chars();
462 /// assert_eq!(Some('y'), chars.next()); // not 'y̆'
463 /// assert_eq!(Some('\u{0306}'), chars.next());
465 /// assert_eq!(None, chars.next());
467 #[stable(feature = "rust1", since = "1.0.0")]
469 pub fn chars(&self) -> Chars
{
470 core_str
::StrExt
::chars(self)
472 /// Returns an iterator over the [`char`]s of a string slice, and their
475 /// As a string slice consists of valid UTF-8, we can iterate through a
476 /// string slice by [`char`]. This method returns an iterator of both
477 /// these [`char`]s, as well as their byte positions.
479 /// The iterator yields tuples. The position is first, the [`char`] is
482 /// [`char`]: primitive.char.html
489 /// let word = "goodbye";
491 /// let count = word.char_indices().count();
492 /// assert_eq!(7, count);
494 /// let mut char_indices = word.char_indices();
496 /// assert_eq!(Some((0, 'g')), char_indices.next());
497 /// assert_eq!(Some((1, 'o')), char_indices.next());
498 /// assert_eq!(Some((2, 'o')), char_indices.next());
499 /// assert_eq!(Some((3, 'd')), char_indices.next());
500 /// assert_eq!(Some((4, 'b')), char_indices.next());
501 /// assert_eq!(Some((5, 'y')), char_indices.next());
502 /// assert_eq!(Some((6, 'e')), char_indices.next());
504 /// assert_eq!(None, char_indices.next());
507 /// Remember, [`char`]s may not match your human intuition about characters:
512 /// let mut char_indices = y.char_indices();
514 /// assert_eq!(Some((0, 'y')), char_indices.next()); // not (0, 'y̆')
515 /// assert_eq!(Some((1, '\u{0306}')), char_indices.next());
517 /// assert_eq!(None, char_indices.next());
519 #[stable(feature = "rust1", since = "1.0.0")]
521 pub fn char_indices(&self) -> CharIndices
{
522 core_str
::StrExt
::char_indices(self)
525 /// An iterator over the bytes of a string slice.
527 /// As a string slice consists of a sequence of bytes, we can iterate
528 /// through a string slice by byte. This method returns such an iterator.
535 /// let mut bytes = "bors".bytes();
537 /// assert_eq!(Some(b'b'), bytes.next());
538 /// assert_eq!(Some(b'o'), bytes.next());
539 /// assert_eq!(Some(b'r'), bytes.next());
540 /// assert_eq!(Some(b's'), bytes.next());
542 /// assert_eq!(None, bytes.next());
544 #[stable(feature = "rust1", since = "1.0.0")]
546 pub fn bytes(&self) -> Bytes
{
547 core_str
::StrExt
::bytes(self)
550 /// Split a string slice by whitespace.
552 /// The iterator returned will return string slices that are sub-slices of
553 /// the original string slice, separated by any amount of whitespace.
555 /// 'Whitespace' is defined according to the terms of the Unicode Derived
556 /// Core Property `White_Space`.
563 /// let mut iter = "A few words".split_whitespace();
565 /// assert_eq!(Some("A"), iter.next());
566 /// assert_eq!(Some("few"), iter.next());
567 /// assert_eq!(Some("words"), iter.next());
569 /// assert_eq!(None, iter.next());
572 /// All kinds of whitespace are considered:
575 /// let mut iter = " Mary had\ta\u{2009}little \n\t lamb".split_whitespace();
576 /// assert_eq!(Some("Mary"), iter.next());
577 /// assert_eq!(Some("had"), iter.next());
578 /// assert_eq!(Some("a"), iter.next());
579 /// assert_eq!(Some("little"), iter.next());
580 /// assert_eq!(Some("lamb"), iter.next());
582 /// assert_eq!(None, iter.next());
584 #[stable(feature = "split_whitespace", since = "1.1.0")]
586 pub fn split_whitespace(&self) -> SplitWhitespace
{
587 UnicodeStr
::split_whitespace(self)
590 /// An iterator over the lines of a string, as string slices.
592 /// Lines are ended with either a newline (`\n`) or a carriage return with
593 /// a line feed (`\r\n`).
595 /// The final line ending is optional.
602 /// let text = "foo\r\nbar\n\nbaz\n";
603 /// let mut lines = text.lines();
605 /// assert_eq!(Some("foo"), lines.next());
606 /// assert_eq!(Some("bar"), lines.next());
607 /// assert_eq!(Some(""), lines.next());
608 /// assert_eq!(Some("baz"), lines.next());
610 /// assert_eq!(None, lines.next());
613 /// The final line ending isn't required:
616 /// let text = "foo\nbar\n\r\nbaz";
617 /// let mut lines = text.lines();
619 /// assert_eq!(Some("foo"), lines.next());
620 /// assert_eq!(Some("bar"), lines.next());
621 /// assert_eq!(Some(""), lines.next());
622 /// assert_eq!(Some("baz"), lines.next());
624 /// assert_eq!(None, lines.next());
626 #[stable(feature = "rust1", since = "1.0.0")]
628 pub fn lines(&self) -> Lines
{
629 core_str
::StrExt
::lines(self)
632 /// An iterator over the lines of a string.
633 #[stable(feature = "rust1", since = "1.0.0")]
634 #[rustc_deprecated(since = "1.4.0", reason = "use lines() instead now")]
637 pub fn lines_any(&self) -> LinesAny
{
638 core_str
::StrExt
::lines_any(self)
641 /// Returns an iterator of `u16` over the string encoded as UTF-16.
642 #[stable(feature = "encode_utf16", since = "1.8.0")]
643 pub fn encode_utf16(&self) -> EncodeUtf16
{
644 EncodeUtf16 { encoder: Utf16Encoder::new(self[..].chars()) }
647 /// Returns `true` if the given pattern matches a sub-slice of
648 /// this string slice.
650 /// Returns `false` if it does not.
657 /// let bananas = "bananas";
659 /// assert!(bananas.contains("nana"));
660 /// assert!(!bananas.contains("apples"));
662 #[stable(feature = "rust1", since = "1.0.0")]
663 pub fn contains
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> bool
{
664 core_str
::StrExt
::contains(self, pat
)
667 /// Returns `true` if the given pattern matches a prefix of this
670 /// Returns `false` if it does not.
677 /// let bananas = "bananas";
679 /// assert!(bananas.starts_with("bana"));
680 /// assert!(!bananas.starts_with("nana"));
682 #[stable(feature = "rust1", since = "1.0.0")]
683 pub fn starts_with
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> bool
{
684 core_str
::StrExt
::starts_with(self, pat
)
687 /// Returns `true` if the given pattern matches a suffix of this
690 /// Returns `false` if it does not.
697 /// let bananas = "bananas";
699 /// assert!(bananas.ends_with("anas"));
700 /// assert!(!bananas.ends_with("nana"));
702 #[stable(feature = "rust1", since = "1.0.0")]
703 pub fn ends_with
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> bool
704 where P
::Searcher
: ReverseSearcher
<'a
>
706 core_str
::StrExt
::ends_with(self, pat
)
709 /// Returns the byte index of the first character of this string slice that
710 /// matches the pattern.
712 /// Returns [`None`] if the pattern doesn't match.
714 /// The pattern can be a `&str`, [`char`], or a closure that determines if
715 /// a character matches.
717 /// [`char`]: primitive.char.html
718 /// [`None`]: option/enum.Option.html#variant.None
725 /// let s = "Löwe 老虎 Léopard";
727 /// assert_eq!(s.find('L'), Some(0));
728 /// assert_eq!(s.find('é'), Some(14));
729 /// assert_eq!(s.find("Léopard"), Some(13));
732 /// More complex patterns with closures:
735 /// let s = "Löwe 老虎 Léopard";
737 /// assert_eq!(s.find(char::is_whitespace), Some(5));
738 /// assert_eq!(s.find(char::is_lowercase), Some(1));
741 /// Not finding the pattern:
744 /// let s = "Löwe 老虎 Léopard";
745 /// let x: &[_] = &['1', '2'];
747 /// assert_eq!(s.find(x), None);
749 #[stable(feature = "rust1", since = "1.0.0")]
750 pub fn find
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> Option
<usize> {
751 core_str
::StrExt
::find(self, pat
)
754 /// Returns the byte index of the last character of this string slice that
755 /// matches the pattern.
757 /// Returns [`None`] if the pattern doesn't match.
759 /// The pattern can be a `&str`, [`char`], or a closure that determines if
760 /// a character matches.
762 /// [`char`]: primitive.char.html
763 /// [`None`]: option/enum.Option.html#variant.None
770 /// let s = "Löwe 老虎 Léopard";
772 /// assert_eq!(s.rfind('L'), Some(13));
773 /// assert_eq!(s.rfind('é'), Some(14));
776 /// More complex patterns with closures:
779 /// let s = "Löwe 老虎 Léopard";
781 /// assert_eq!(s.rfind(char::is_whitespace), Some(12));
782 /// assert_eq!(s.rfind(char::is_lowercase), Some(20));
785 /// Not finding the pattern:
788 /// let s = "Löwe 老虎 Léopard";
789 /// let x: &[_] = &['1', '2'];
791 /// assert_eq!(s.rfind(x), None);
793 #[stable(feature = "rust1", since = "1.0.0")]
794 pub fn rfind
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> Option
<usize>
795 where P
::Searcher
: ReverseSearcher
<'a
>
797 core_str
::StrExt
::rfind(self, pat
)
800 /// An iterator over substrings of this string slice, separated by
801 /// characters matched by a pattern.
803 /// The pattern can be a `&str`, [`char`], or a closure that determines the
806 /// # Iterator behavior
808 /// The returned iterator will be a [`DoubleEndedIterator`] if the pattern
809 /// allows a reverse search and forward/reverse search yields the same
810 /// elements. This is true for, eg, [`char`] but not for `&str`.
812 /// [`DoubleEndedIterator`]: iter/trait.DoubleEndedIterator.html
814 /// If the pattern allows a reverse search but its results might differ
815 /// from a forward search, the [`rsplit()`] method can be used.
817 /// [`char`]: primitive.char.html
818 /// [`rsplit()`]: #method.rsplit
825 /// let v: Vec<&str> = "Mary had a little lamb".split(' ').collect();
826 /// assert_eq!(v, ["Mary", "had", "a", "little", "lamb"]);
828 /// let v: Vec<&str> = "".split('X').collect();
829 /// assert_eq!(v, [""]);
831 /// let v: Vec<&str> = "lionXXtigerXleopard".split('X').collect();
832 /// assert_eq!(v, ["lion", "", "tiger", "leopard"]);
834 /// let v: Vec<&str> = "lion::tiger::leopard".split("::").collect();
835 /// assert_eq!(v, ["lion", "tiger", "leopard"]);
837 /// let v: Vec<&str> = "abc1def2ghi".split(char::is_numeric).collect();
838 /// assert_eq!(v, ["abc", "def", "ghi"]);
840 /// let v: Vec<&str> = "lionXtigerXleopard".split(char::is_uppercase).collect();
841 /// assert_eq!(v, ["lion", "tiger", "leopard"]);
844 /// A more complex pattern, using a closure:
847 /// let v: Vec<&str> = "abc1defXghi".split(|c| c == '1' || c == 'X').collect();
848 /// assert_eq!(v, ["abc", "def", "ghi"]);
851 /// If a string contains multiple contiguous separators, you will end up
852 /// with empty strings in the output:
855 /// let x = "||||a||b|c".to_string();
856 /// let d: Vec<_> = x.split('|').collect();
858 /// assert_eq!(d, &["", "", "", "", "a", "", "b", "c"]);
861 /// Contiguous separators are separated by the empty string.
864 /// let x = "(///)".to_string();
865 /// let d: Vec<_> = x.split('/').collect();;
867 /// assert_eq!(d, &["(", "", "", ")"]);
870 /// Separators at the start or end of a string are neighbored
871 /// by empty strings.
874 /// let d: Vec<_> = "010".split("0").collect();
875 /// assert_eq!(d, &["", "1", ""]);
878 /// When the empty string is used as a separator, it separates
879 /// every character in the string, along with the beginning
880 /// and end of the string.
883 /// let f: Vec<_> = "rust".split("").collect();
884 /// assert_eq!(f, &["", "r", "u", "s", "t", ""]);
887 /// Contiguous separators can lead to possibly surprising behavior
888 /// when whitespace is used as the separator. This code is correct:
891 /// let x = " a b c".to_string();
892 /// let d: Vec<_> = x.split(' ').collect();
894 /// assert_eq!(d, &["", "", "", "", "a", "", "b", "c"]);
897 /// It does _not_ give you:
900 /// assert_eq!(d, &["a", "b", "c"]);
903 /// Use [`split_whitespace()`] for this behavior.
905 /// [`split_whitespace()`]: #method.split_whitespace
906 #[stable(feature = "rust1", since = "1.0.0")]
907 pub fn split
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> Split
<'a
, P
> {
908 core_str
::StrExt
::split(self, pat
)
911 /// An iterator over substrings of the given string slice, separated by
912 /// characters matched by a pattern and yielded in reverse order.
914 /// The pattern can be a `&str`, [`char`], or a closure that determines the
917 /// [`char`]: primitive.char.html
919 /// # Iterator behavior
921 /// The returned iterator requires that the pattern supports a reverse
922 /// search, and it will be a [`DoubleEndedIterator`] if a forward/reverse
923 /// search yields the same elements.
925 /// [`DoubleEndedIterator`]: iter/trait.DoubleEndedIterator.html
927 /// For iterating from the front, the [`split()`] method can be used.
929 /// [`split()`]: #method.split
936 /// let v: Vec<&str> = "Mary had a little lamb".rsplit(' ').collect();
937 /// assert_eq!(v, ["lamb", "little", "a", "had", "Mary"]);
939 /// let v: Vec<&str> = "".rsplit('X').collect();
940 /// assert_eq!(v, [""]);
942 /// let v: Vec<&str> = "lionXXtigerXleopard".rsplit('X').collect();
943 /// assert_eq!(v, ["leopard", "tiger", "", "lion"]);
945 /// let v: Vec<&str> = "lion::tiger::leopard".rsplit("::").collect();
946 /// assert_eq!(v, ["leopard", "tiger", "lion"]);
949 /// A more complex pattern, using a closure:
952 /// let v: Vec<&str> = "abc1defXghi".rsplit(|c| c == '1' || c == 'X').collect();
953 /// assert_eq!(v, ["ghi", "def", "abc"]);
955 #[stable(feature = "rust1", since = "1.0.0")]
956 pub fn rsplit
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> RSplit
<'a
, P
>
957 where P
::Searcher
: ReverseSearcher
<'a
>
959 core_str
::StrExt
::rsplit(self, pat
)
962 /// An iterator over substrings of the given string slice, separated by
963 /// characters matched by a pattern.
965 /// The pattern can be a `&str`, [`char`], or a closure that determines the
968 /// Equivalent to [`split()`], except that the trailing substring
969 /// is skipped if empty.
971 /// [`split()`]: #method.split
973 /// This method can be used for string data that is _terminated_,
974 /// rather than _separated_ by a pattern.
976 /// # Iterator behavior
978 /// The returned iterator will be a [`DoubleEndedIterator`] if the pattern
979 /// allows a reverse search and forward/reverse search yields the same
980 /// elements. This is true for, eg, [`char`] but not for `&str`.
982 /// [`DoubleEndedIterator`]: iter/trait.DoubleEndedIterator.html
983 /// [`char`]: primitive.char.html
985 /// If the pattern allows a reverse search but its results might differ
986 /// from a forward search, the [`rsplit_terminator()`] method can be used.
988 /// [`rsplit_terminator()`]: #method.rsplit_terminator
995 /// let v: Vec<&str> = "A.B.".split_terminator('.').collect();
996 /// assert_eq!(v, ["A", "B"]);
998 /// let v: Vec<&str> = "A..B..".split_terminator(".").collect();
999 /// assert_eq!(v, ["A", "", "B", ""]);
1001 #[stable(feature = "rust1", since = "1.0.0")]
1002 pub fn split_terminator
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> SplitTerminator
<'a
, P
> {
1003 core_str
::StrExt
::split_terminator(self, pat
)
1006 /// An iterator over substrings of `self`, separated by characters
1007 /// matched by a pattern and yielded in reverse order.
1009 /// The pattern can be a simple `&str`, [`char`], or a closure that
1010 /// determines the split.
1011 /// Additional libraries might provide more complex patterns like
1012 /// regular expressions.
1014 /// [`char`]: primitive.char.html
1016 /// Equivalent to [`split()`], except that the trailing substring is
1017 /// skipped if empty.
1019 /// [`split()`]: #method.split
1021 /// This method can be used for string data that is _terminated_,
1022 /// rather than _separated_ by a pattern.
1024 /// # Iterator behavior
1026 /// The returned iterator requires that the pattern supports a
1027 /// reverse search, and it will be double ended if a forward/reverse
1028 /// search yields the same elements.
1030 /// For iterating from the front, the [`split_terminator()`] method can be
1033 /// [`split_terminator()`]: #method.split_terminator
1038 /// let v: Vec<&str> = "A.B.".rsplit_terminator('.').collect();
1039 /// assert_eq!(v, ["B", "A"]);
1041 /// let v: Vec<&str> = "A..B..".rsplit_terminator(".").collect();
1042 /// assert_eq!(v, ["", "B", "", "A"]);
1044 #[stable(feature = "rust1", since = "1.0.0")]
1045 pub fn rsplit_terminator
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> RSplitTerminator
<'a
, P
>
1046 where P
::Searcher
: ReverseSearcher
<'a
>
1048 core_str
::StrExt
::rsplit_terminator(self, pat
)
1051 /// An iterator over substrings of the given string slice, separated by a
1052 /// pattern, restricted to returning at most `count` items.
1054 /// The last element returned, if any, will contain the remainder of the
1057 /// The pattern can be a `&str`, [`char`], or a closure that determines the
1060 /// [`char`]: primitive.char.html
1062 /// # Iterator behavior
1064 /// The returned iterator will not be double ended, because it is
1065 /// not efficient to support.
1067 /// If the pattern allows a reverse search, the [`rsplitn()`] method can be
1070 /// [`rsplitn()`]: #method.rsplitn
1074 /// Simple patterns:
1077 /// let v: Vec<&str> = "Mary had a little lambda".splitn(3, ' ').collect();
1078 /// assert_eq!(v, ["Mary", "had", "a little lambda"]);
1080 /// let v: Vec<&str> = "lionXXtigerXleopard".splitn(3, "X").collect();
1081 /// assert_eq!(v, ["lion", "", "tigerXleopard"]);
1083 /// let v: Vec<&str> = "abcXdef".splitn(1, 'X').collect();
1084 /// assert_eq!(v, ["abcXdef"]);
1086 /// let v: Vec<&str> = "".splitn(1, 'X').collect();
1087 /// assert_eq!(v, [""]);
1090 /// A more complex pattern, using a closure:
1093 /// let v: Vec<&str> = "abc1defXghi".splitn(2, |c| c == '1' || c == 'X').collect();
1094 /// assert_eq!(v, ["abc", "defXghi"]);
1096 #[stable(feature = "rust1", since = "1.0.0")]
1097 pub fn splitn
<'a
, P
: Pattern
<'a
>>(&'a
self, count
: usize, pat
: P
) -> SplitN
<'a
, P
> {
1098 core_str
::StrExt
::splitn(self, count
, pat
)
1101 /// An iterator over substrings of this string slice, separated by a
1102 /// pattern, starting from the end of the string, restricted to returning
1103 /// at most `count` items.
1105 /// The last element returned, if any, will contain the remainder of the
1108 /// The pattern can be a `&str`, [`char`], or a closure that
1109 /// determines the split.
1111 /// [`char`]: primitive.char.html
1113 /// # Iterator behavior
1115 /// The returned iterator will not be double ended, because it is not
1116 /// efficient to support.
1118 /// For splitting from the front, the [`splitn()`] method can be used.
1120 /// [`splitn()`]: #method.splitn
1124 /// Simple patterns:
1127 /// let v: Vec<&str> = "Mary had a little lamb".rsplitn(3, ' ').collect();
1128 /// assert_eq!(v, ["lamb", "little", "Mary had a"]);
1130 /// let v: Vec<&str> = "lionXXtigerXleopard".rsplitn(3, 'X').collect();
1131 /// assert_eq!(v, ["leopard", "tiger", "lionX"]);
1133 /// let v: Vec<&str> = "lion::tiger::leopard".rsplitn(2, "::").collect();
1134 /// assert_eq!(v, ["leopard", "lion::tiger"]);
1137 /// A more complex pattern, using a closure:
1140 /// let v: Vec<&str> = "abc1defXghi".rsplitn(2, |c| c == '1' || c == 'X').collect();
1141 /// assert_eq!(v, ["ghi", "abc1def"]);
1143 #[stable(feature = "rust1", since = "1.0.0")]
1144 pub fn rsplitn
<'a
, P
: Pattern
<'a
>>(&'a
self, count
: usize, pat
: P
) -> RSplitN
<'a
, P
>
1145 where P
::Searcher
: ReverseSearcher
<'a
>
1147 core_str
::StrExt
::rsplitn(self, count
, pat
)
1150 /// An iterator over the matches of a pattern within the given string
1153 /// The pattern can be a `&str`, [`char`], or a closure that
1154 /// determines if a character matches.
1156 /// [`char`]: primitive.char.html
1158 /// # Iterator behavior
1160 /// The returned iterator will be a [`DoubleEndedIterator`] if the pattern
1161 /// allows a reverse search and forward/reverse search yields the same
1162 /// elements. This is true for, eg, [`char`] but not for `&str`.
1164 /// [`DoubleEndedIterator`]: iter/trait.DoubleEndedIterator.html
1165 /// [`char`]: primitive.char.html
1167 /// If the pattern allows a reverse search but its results might differ
1168 /// from a forward search, the [`rmatches()`] method can be used.
1170 /// [`rmatches()`]: #method.rmatches
1177 /// let v: Vec<&str> = "abcXXXabcYYYabc".matches("abc").collect();
1178 /// assert_eq!(v, ["abc", "abc", "abc"]);
1180 /// let v: Vec<&str> = "1abc2abc3".matches(char::is_numeric).collect();
1181 /// assert_eq!(v, ["1", "2", "3"]);
1183 #[stable(feature = "str_matches", since = "1.2.0")]
1184 pub fn matches
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> Matches
<'a
, P
> {
1185 core_str
::StrExt
::matches(self, pat
)
1188 /// An iterator over the matches of a pattern within this string slice,
1189 /// yielded in reverse order.
1191 /// The pattern can be a `&str`, [`char`], or a closure that determines if
1192 /// a character matches.
1194 /// [`char`]: primitive.char.html
1196 /// # Iterator behavior
1198 /// The returned iterator requires that the pattern supports a reverse
1199 /// search, and it will be a [`DoubleEndedIterator`] if a forward/reverse
1200 /// search yields the same elements.
1202 /// [`DoubleEndedIterator`]: iter/trait.DoubleEndedIterator.html
1204 /// For iterating from the front, the [`matches()`] method can be used.
1206 /// [`matches()`]: #method.matches
1213 /// let v: Vec<&str> = "abcXXXabcYYYabc".rmatches("abc").collect();
1214 /// assert_eq!(v, ["abc", "abc", "abc"]);
1216 /// let v: Vec<&str> = "1abc2abc3".rmatches(char::is_numeric).collect();
1217 /// assert_eq!(v, ["3", "2", "1"]);
1219 #[stable(feature = "str_matches", since = "1.2.0")]
1220 pub fn rmatches
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> RMatches
<'a
, P
>
1221 where P
::Searcher
: ReverseSearcher
<'a
>
1223 core_str
::StrExt
::rmatches(self, pat
)
1226 /// An iterator over the disjoint matches of a pattern within this string
1227 /// slice as well as the index that the match starts at.
1229 /// For matches of `pat` within `self` that overlap, only the indices
1230 /// corresponding to the first match are returned.
1232 /// The pattern can be a `&str`, [`char`], or a closure that determines
1233 /// if a character matches.
1235 /// [`char`]: primitive.char.html
1237 /// # Iterator behavior
1239 /// The returned iterator will be a [`DoubleEndedIterator`] if the pattern
1240 /// allows a reverse search and forward/reverse search yields the same
1241 /// elements. This is true for, eg, [`char`] but not for `&str`.
1243 /// [`DoubleEndedIterator`]: iter/trait.DoubleEndedIterator.html
1245 /// If the pattern allows a reverse search but its results might differ
1246 /// from a forward search, the [`rmatch_indices()`] method can be used.
1248 /// [`rmatch_indices()`]: #method.rmatch_indices
1255 /// let v: Vec<_> = "abcXXXabcYYYabc".match_indices("abc").collect();
1256 /// assert_eq!(v, [(0, "abc"), (6, "abc"), (12, "abc")]);
1258 /// let v: Vec<_> = "1abcabc2".match_indices("abc").collect();
1259 /// assert_eq!(v, [(1, "abc"), (4, "abc")]);
1261 /// let v: Vec<_> = "ababa".match_indices("aba").collect();
1262 /// assert_eq!(v, [(0, "aba")]); // only the first `aba`
1264 #[stable(feature = "str_match_indices", since = "1.5.0")]
1265 pub fn match_indices
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> MatchIndices
<'a
, P
> {
1266 core_str
::StrExt
::match_indices(self, pat
)
1269 /// An iterator over the disjoint matches of a pattern within `self`,
1270 /// yielded in reverse order along with the index of the match.
1272 /// For matches of `pat` within `self` that overlap, only the indices
1273 /// corresponding to the last match are returned.
1275 /// The pattern can be a `&str`, [`char`], or a closure that determines if a
1276 /// character matches.
1278 /// [`char`]: primitive.char.html
1280 /// # Iterator behavior
1282 /// The returned iterator requires that the pattern supports a reverse
1283 /// search, and it will be a [`DoubleEndedIterator`] if a forward/reverse
1284 /// search yields the same elements.
1286 /// [`DoubleEndedIterator`]: iter/trait.DoubleEndedIterator.html
1288 /// For iterating from the front, the [`match_indices()`] method can be used.
1290 /// [`match_indices()`]: #method.match_indices
1297 /// let v: Vec<_> = "abcXXXabcYYYabc".rmatch_indices("abc").collect();
1298 /// assert_eq!(v, [(12, "abc"), (6, "abc"), (0, "abc")]);
1300 /// let v: Vec<_> = "1abcabc2".rmatch_indices("abc").collect();
1301 /// assert_eq!(v, [(4, "abc"), (1, "abc")]);
1303 /// let v: Vec<_> = "ababa".rmatch_indices("aba").collect();
1304 /// assert_eq!(v, [(2, "aba")]); // only the last `aba`
1306 #[stable(feature = "str_match_indices", since = "1.5.0")]
1307 pub fn rmatch_indices
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> RMatchIndices
<'a
, P
>
1308 where P
::Searcher
: ReverseSearcher
<'a
>
1310 core_str
::StrExt
::rmatch_indices(self, pat
)
1313 /// Returns a string slice with leading and trailing whitespace removed.
1315 /// 'Whitespace' is defined according to the terms of the Unicode Derived
1316 /// Core Property `White_Space`.
1323 /// let s = " Hello\tworld\t";
1325 /// assert_eq!("Hello\tworld", s.trim());
1327 #[stable(feature = "rust1", since = "1.0.0")]
1328 pub fn trim(&self) -> &str {
1329 UnicodeStr
::trim(self)
1332 /// Returns a string slice with leading whitespace removed.
1334 /// 'Whitespace' is defined according to the terms of the Unicode Derived
1335 /// Core Property `White_Space`.
1337 /// # Text directionality
1339 /// A string is a sequence of bytes. 'Left' in this context means the first
1340 /// position of that byte string; for a language like Arabic or Hebrew
1341 /// which are 'right to left' rather than 'left to right', this will be
1342 /// the _right_ side, not the left.
1349 /// let s = " Hello\tworld\t";
1351 /// assert_eq!("Hello\tworld\t", s.trim_left());
1357 /// let s = " English";
1358 /// assert!(Some('E') == s.trim_left().chars().next());
1360 /// let s = " עברית";
1361 /// assert!(Some('ע') == s.trim_left().chars().next());
1363 #[stable(feature = "rust1", since = "1.0.0")]
1364 pub fn trim_left(&self) -> &str {
1365 UnicodeStr
::trim_left(self)
1368 /// Returns a string slice with trailing whitespace removed.
1370 /// 'Whitespace' is defined according to the terms of the Unicode Derived
1371 /// Core Property `White_Space`.
1373 /// # Text directionality
1375 /// A string is a sequence of bytes. 'Right' in this context means the last
1376 /// position of that byte string; for a language like Arabic or Hebrew
1377 /// which are 'right to left' rather than 'left to right', this will be
1378 /// the _left_ side, not the right.
1385 /// let s = " Hello\tworld\t";
1387 /// assert_eq!(" Hello\tworld", s.trim_right());
1393 /// let s = "English ";
1394 /// assert!(Some('h') == s.trim_right().chars().rev().next());
1396 /// let s = "עברית ";
1397 /// assert!(Some('ת') == s.trim_right().chars().rev().next());
1399 #[stable(feature = "rust1", since = "1.0.0")]
1400 pub fn trim_right(&self) -> &str {
1401 UnicodeStr
::trim_right(self)
1404 /// Returns a string slice with all prefixes and suffixes that match a
1405 /// pattern repeatedly removed.
1407 /// The pattern can be a [`char`] or a closure that determines if a
1408 /// character matches.
1410 /// [`char`]: primitive.char.html
1414 /// Simple patterns:
1417 /// assert_eq!("11foo1bar11".trim_matches('1'), "foo1bar");
1418 /// assert_eq!("123foo1bar123".trim_matches(char::is_numeric), "foo1bar");
1420 /// let x: &[_] = &['1', '2'];
1421 /// assert_eq!("12foo1bar12".trim_matches(x), "foo1bar");
1424 /// A more complex pattern, using a closure:
1427 /// assert_eq!("1foo1barXX".trim_matches(|c| c == '1' || c == 'X'), "foo1bar");
1429 #[stable(feature = "rust1", since = "1.0.0")]
1430 pub fn trim_matches
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> &'a
str
1431 where P
::Searcher
: DoubleEndedSearcher
<'a
>
1433 core_str
::StrExt
::trim_matches(self, pat
)
1436 /// Returns a string slice with all prefixes that match a pattern
1437 /// repeatedly removed.
1439 /// The pattern can be a `&str`, [`char`], or a closure that determines if
1440 /// a character matches.
1442 /// [`char`]: primitive.char.html
1444 /// # Text directionality
1446 /// A string is a sequence of bytes. 'Left' in this context means the first
1447 /// position of that byte string; for a language like Arabic or Hebrew
1448 /// which are 'right to left' rather than 'left to right', this will be
1449 /// the _right_ side, not the left.
1456 /// assert_eq!("11foo1bar11".trim_left_matches('1'), "foo1bar11");
1457 /// assert_eq!("123foo1bar123".trim_left_matches(char::is_numeric), "foo1bar123");
1459 /// let x: &[_] = &['1', '2'];
1460 /// assert_eq!("12foo1bar12".trim_left_matches(x), "foo1bar12");
1462 #[stable(feature = "rust1", since = "1.0.0")]
1463 pub fn trim_left_matches
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> &'a
str {
1464 core_str
::StrExt
::trim_left_matches(self, pat
)
1467 /// Returns a string slice with all suffixes that match a pattern
1468 /// repeatedly removed.
1470 /// The pattern can be a `&str`, [`char`], or a closure that
1471 /// determines if a character matches.
1473 /// [`char`]: primitive.char.html
1475 /// # Text directionality
1477 /// A string is a sequence of bytes. 'Right' in this context means the last
1478 /// position of that byte string; for a language like Arabic or Hebrew
1479 /// which are 'right to left' rather than 'left to right', this will be
1480 /// the _left_ side, not the right.
1484 /// Simple patterns:
1487 /// assert_eq!("11foo1bar11".trim_right_matches('1'), "11foo1bar");
1488 /// assert_eq!("123foo1bar123".trim_right_matches(char::is_numeric), "123foo1bar");
1490 /// let x: &[_] = &['1', '2'];
1491 /// assert_eq!("12foo1bar12".trim_right_matches(x), "12foo1bar");
1494 /// A more complex pattern, using a closure:
1497 /// assert_eq!("1fooX".trim_left_matches(|c| c == '1' || c == 'X'), "fooX");
1499 #[stable(feature = "rust1", since = "1.0.0")]
1500 pub fn trim_right_matches
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> &'a
str
1501 where P
::Searcher
: ReverseSearcher
<'a
>
1503 core_str
::StrExt
::trim_right_matches(self, pat
)
1506 /// Parses this string slice into another type.
1508 /// Because `parse()` is so general, it can cause problems with type
1509 /// inference. As such, `parse()` is one of the few times you'll see
1510 /// the syntax affectionately known as the 'turbofish': `::<>`. This
1511 /// helps the inference algorithm understand specifically which type
1512 /// you're trying to parse into.
1514 /// `parse()` can parse any type that implements the [`FromStr`] trait.
1516 /// [`FromStr`]: str/trait.FromStr.html
1520 /// Will return [`Err`] if it's not possible to parse this string slice into
1521 /// the desired type.
1523 /// [`Err`]: str/trait.FromStr.html#associatedtype.Err
1530 /// let four: u32 = "4".parse().unwrap();
1532 /// assert_eq!(4, four);
1535 /// Using the 'turbofish' instead of annotating `four`:
1538 /// let four = "4".parse::<u32>();
1540 /// assert_eq!(Ok(4), four);
1543 /// Failing to parse:
1546 /// let nope = "j".parse::<u32>();
1548 /// assert!(nope.is_err());
1551 #[stable(feature = "rust1", since = "1.0.0")]
1552 pub fn parse
<F
: FromStr
>(&self) -> Result
<F
, F
::Err
> {
1553 core_str
::StrExt
::parse(self)
1556 /// Replaces all matches of a pattern with another string.
1558 /// `replace` creates a new [`String`], and copies the data from this string slice into it.
1559 /// While doing so, it attempts to find matches of a pattern. If it finds any, it
1560 /// replaces them with the replacement string slice.
1562 /// [`String`]: string/struct.String.html
1569 /// let s = "this is old";
1571 /// assert_eq!("this is new", s.replace("old", "new"));
1574 /// When the pattern doesn't match:
1577 /// let s = "this is old";
1578 /// assert_eq!(s, s.replace("cookie monster", "little lamb"));
1580 #[stable(feature = "rust1", since = "1.0.0")]
1581 pub fn replace
<'a
, P
: Pattern
<'a
>>(&'a
self, from
: P
, to
: &str) -> String
{
1582 let mut result
= String
::new();
1583 let mut last_end
= 0;
1584 for (start
, part
) in self.match_indices(from
) {
1585 result
.push_str(unsafe { self.slice_unchecked(last_end, start) }
);
1586 result
.push_str(to
);
1587 last_end
= start
+ part
.len();
1589 result
.push_str(unsafe { self.slice_unchecked(last_end, self.len()) }
);
1593 /// Returns the lowercase equivalent of this string slice, as a new [`String`].
1595 /// 'Lowercase' is defined according to the terms of the Unicode Derived Core Property
1598 /// [`String`]: string/struct.String.html
1605 /// let s = "HELLO";
1607 /// assert_eq!("hello", s.to_lowercase());
1610 /// A tricky example, with sigma:
1613 /// let sigma = "Σ";
1615 /// assert_eq!("σ", sigma.to_lowercase());
1617 /// // but at the end of a word, it's ς, not σ:
1618 /// let odysseus = "ὈΔΥΣΣΕΎΣ";
1620 /// assert_eq!("ὀδυσσεύς", odysseus.to_lowercase());
1623 /// Languages without case are not changed:
1626 /// let new_year = "农历新年";
1628 /// assert_eq!(new_year, new_year.to_lowercase());
1630 #[stable(feature = "unicode_case_mapping", since = "1.2.0")]
1631 pub fn to_lowercase(&self) -> String
{
1632 let mut s
= String
::with_capacity(self.len());
1633 for (i
, c
) in self[..].char_indices() {
1635 // Σ maps to σ, except at the end of a word where it maps to ς.
1636 // This is the only conditional (contextual) but language-independent mapping
1637 // in `SpecialCasing.txt`,
1638 // so hard-code it rather than have a generic "condition" mechanism.
1639 // See https://github.com/rust-lang/rust/issues/26035
1640 map_uppercase_sigma(self, i
, &mut s
)
1642 s
.extend(c
.to_lowercase());
1647 fn map_uppercase_sigma(from
: &str, i
: usize, to
: &mut String
) {
1648 // See http://www.unicode.org/versions/Unicode7.0.0/ch03.pdf#G33992
1649 // for the definition of `Final_Sigma`.
1650 debug_assert
!('Σ'
.len_utf8() == 2);
1651 let is_word_final
= case_ignoreable_then_cased(from
[..i
].chars().rev()) &&
1652 !case_ignoreable_then_cased(from
[i
+ 2..].chars());
1653 to
.push_str(if is_word_final
{
1660 fn case_ignoreable_then_cased
<I
: Iterator
<Item
= char>>(iter
: I
) -> bool
{
1661 use rustc_unicode
::derived_property
::{Cased, Case_Ignorable}
;
1662 match iter
.skip_while(|&c
| Case_Ignorable(c
)).next() {
1663 Some(c
) => Cased(c
),
1669 /// Returns the uppercase equivalent of this string slice, as a new [`String`].
1671 /// 'Uppercase' is defined according to the terms of the Unicode Derived Core Property
1674 /// [`String`]: string/struct.String.html
1681 /// let s = "hello";
1683 /// assert_eq!("HELLO", s.to_uppercase());
1686 /// Scripts without case are not changed:
1689 /// let new_year = "农历新年";
1691 /// assert_eq!(new_year, new_year.to_uppercase());
1693 #[stable(feature = "unicode_case_mapping", since = "1.2.0")]
1694 pub fn to_uppercase(&self) -> String
{
1695 let mut s
= String
::with_capacity(self.len());
1696 s
.extend(self.chars().flat_map(|c
| c
.to_uppercase()));
1700 /// Escapes each char in `s` with `char::escape_debug`.
1701 #[unstable(feature = "str_escape",
1702 reason
= "return type may change to be an iterator",
1704 pub fn escape_debug(&self) -> String
{
1705 self.chars().flat_map(|c
| c
.escape_debug()).collect()
1708 /// Escapes each char in `s` with `char::escape_default`.
1709 #[unstable(feature = "str_escape",
1710 reason
= "return type may change to be an iterator",
1712 pub fn escape_default(&self) -> String
{
1713 self.chars().flat_map(|c
| c
.escape_default()).collect()
1716 /// Escapes each char in `s` with `char::escape_unicode`.
1717 #[unstable(feature = "str_escape",
1718 reason
= "return type may change to be an iterator",
1720 pub fn escape_unicode(&self) -> String
{
1721 self.chars().flat_map(|c
| c
.escape_unicode()).collect()
1724 /// Converts a `Box<str>` into a [`String`] without copying or allocating.
1726 /// [`String`]: string/struct.String.html
1733 /// let string = String::from("birthday gift");
1734 /// let boxed_str = string.clone().into_boxed_str();
1736 /// assert_eq!(boxed_str.into_string(), string);
1738 #[stable(feature = "box_str", since = "1.4.0")]
1739 pub fn into_string(self: Box
<str>) -> String
{
1741 let slice
= mem
::transmute
::<Box
<str>, Box
<[u8]>>(self);
1742 String
::from_utf8_unchecked(slice
.into_vec())