1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! String manipulation
13 //! For more details, see std::str
15 #![stable(feature = "rust1", since = "1.0.0")]
17 use self::pattern
::Pattern
;
18 use self::pattern
::{Searcher, ReverseSearcher, DoubleEndedSearcher}
;
22 use iter
::{Map, Cloned, FusedIterator}
;
28 /// A trait to abstract the idea of creating a new instance of a type from a
31 /// `FromStr`'s [`from_str()`] method is often used implicitly, through
32 /// [`str`]'s [`parse()`] method. See [`parse()`]'s documentation for examples.
34 /// [`from_str()`]: #tymethod.from_str
35 /// [`str`]: ../../std/primitive.str.html
36 /// [`parse()`]: ../../std/primitive.str.html#method.parse
37 #[stable(feature = "rust1", since = "1.0.0")]
38 pub trait FromStr
: Sized
{
39 /// The associated error which can be returned from parsing.
40 #[stable(feature = "rust1", since = "1.0.0")]
43 /// Parses a string `s` to return a value of this type.
45 /// If parsing succeeds, return the value inside `Ok`, otherwise
46 /// when the string is ill-formatted return an error specific to the
47 /// inside `Err`. The error type is specific to implementation of the trait.
51 /// Basic usage with [`i32`][ithirtytwo], a type that implements `FromStr`:
53 /// [ithirtytwo]: ../../std/primitive.i32.html
56 /// use std::str::FromStr;
59 /// let x = i32::from_str(s).unwrap();
63 #[stable(feature = "rust1", since = "1.0.0")]
64 fn from_str(s
: &str) -> Result
<Self, Self::Err
>;
67 #[stable(feature = "rust1", since = "1.0.0")]
68 impl FromStr
for bool
{
69 type Err
= ParseBoolError
;
71 /// Parse a `bool` from a string.
73 /// Yields a `Result<bool, ParseBoolError>`, because `s` may or may not
74 /// actually be parseable.
79 /// use std::str::FromStr;
81 /// assert_eq!(FromStr::from_str("true"), Ok(true));
82 /// assert_eq!(FromStr::from_str("false"), Ok(false));
83 /// assert!(<bool as FromStr>::from_str("not even a boolean").is_err());
86 /// Note, in many cases, the `.parse()` method on `str` is more proper.
89 /// assert_eq!("true".parse(), Ok(true));
90 /// assert_eq!("false".parse(), Ok(false));
91 /// assert!("not even a boolean".parse::<bool>().is_err());
94 fn from_str(s
: &str) -> Result
<bool
, ParseBoolError
> {
98 _
=> Err(ParseBoolError { _priv: () }
),
103 /// An error returned when parsing a `bool` from a string fails.
104 #[derive(Debug, Clone, PartialEq, Eq)]
105 #[stable(feature = "rust1", since = "1.0.0")]
106 pub struct ParseBoolError { _priv: () }
108 #[stable(feature = "rust1", since = "1.0.0")]
109 impl fmt
::Display
for ParseBoolError
{
110 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
111 "provided string was not `true` or `false`".fmt(f
)
116 Section: Creating a string
119 /// Errors which can occur when attempting to interpret a sequence of `u8`
122 /// As such, the `from_utf8` family of functions and methods for both `String`s
123 /// and `&str`s make use of this error, for example.
124 #[derive(Copy, Eq, PartialEq, Clone, Debug)]
125 #[stable(feature = "rust1", since = "1.0.0")]
126 pub struct Utf8Error
{
131 /// Returns the index in the given string up to which valid UTF-8 was
134 /// It is the maximum index such that `from_utf8(input[..index])`
135 /// would return `Some(_)`.
144 /// // some invalid bytes, in a vector
145 /// let sparkle_heart = vec![0, 159, 146, 150];
147 /// // std::str::from_utf8 returns a Utf8Error
148 /// let error = str::from_utf8(&sparkle_heart).unwrap_err();
150 /// // the second byte is invalid here
151 /// assert_eq!(1, error.valid_up_to());
153 #[stable(feature = "utf8_error", since = "1.5.0")]
154 pub fn valid_up_to(&self) -> usize { self.valid_up_to }
157 /// Converts a slice of bytes to a string slice.
159 /// A string slice (`&str`) is made of bytes (`u8`), and a byte slice (`&[u8]`)
160 /// is made of bytes, so this function converts between the two. Not all byte
161 /// slices are valid string slices, however: `&str` requires that it is valid
162 /// UTF-8. `from_utf8()` checks to ensure that the bytes are valid UTF-8, and
163 /// then does the conversion.
165 /// If you are sure that the byte slice is valid UTF-8, and you don't want to
166 /// incur the overhead of the validity check, there is an unsafe version of
167 /// this function, [`from_utf8_unchecked()`][fromutf8u], which has the same
168 /// behavior but skips the check.
170 /// [fromutf8u]: fn.from_utf8_unchecked.html
172 /// If you need a `String` instead of a `&str`, consider
173 /// [`String::from_utf8()`][string].
175 /// [string]: ../../std/string/struct.String.html#method.from_utf8
177 /// Because you can stack-allocate a `[u8; N]`, and you can take a `&[u8]` of
178 /// it, this function is one way to have a stack-allocated string. There is
179 /// an example of this in the examples section below.
183 /// Returns `Err` if the slice is not UTF-8 with a description as to why the
184 /// provided slice is not UTF-8.
193 /// // some bytes, in a vector
194 /// let sparkle_heart = vec![240, 159, 146, 150];
196 /// // We know these bytes are valid, so just use `unwrap()`.
197 /// let sparkle_heart = str::from_utf8(&sparkle_heart).unwrap();
199 /// assert_eq!("💖", sparkle_heart);
207 /// // some invalid bytes, in a vector
208 /// let sparkle_heart = vec![0, 159, 146, 150];
210 /// assert!(str::from_utf8(&sparkle_heart).is_err());
213 /// See the docs for [`Utf8Error`][error] for more details on the kinds of
214 /// errors that can be returned.
216 /// [error]: struct.Utf8Error.html
218 /// A "stack allocated string":
223 /// // some bytes, in a stack-allocated array
224 /// let sparkle_heart = [240, 159, 146, 150];
226 /// // We know these bytes are valid, so just use `unwrap()`.
227 /// let sparkle_heart = str::from_utf8(&sparkle_heart).unwrap();
229 /// assert_eq!("💖", sparkle_heart);
231 #[stable(feature = "rust1", since = "1.0.0")]
232 pub fn from_utf8(v
: &[u8]) -> Result
<&str, Utf8Error
> {
233 run_utf8_validation(v
)?
;
234 Ok(unsafe { from_utf8_unchecked(v) }
)
237 /// Forms a str from a pointer and a length.
239 /// The `len` argument is the number of bytes in the string.
243 /// This function is unsafe as there is no guarantee that the given pointer is
244 /// valid for `len` bytes, nor whether the lifetime inferred is a suitable
245 /// lifetime for the returned str.
247 /// The data must be valid UTF-8
249 /// `p` must be non-null, even for zero-length str.
253 /// The lifetime for the returned str is inferred from its usage. To
254 /// prevent accidental misuse, it's suggested to tie the lifetime to whichever
255 /// source lifetime is safe in the context, such as by providing a helper
256 /// function taking the lifetime of a host value for the str, or by explicit
258 /// Performs the same functionality as `from_raw_parts`, except that a mutable
261 unsafe fn from_raw_parts_mut
<'a
>(p
: *mut u8, len
: usize) -> &'a
mut str {
262 mem
::transmute
::<&mut [u8], &mut str>(slice
::from_raw_parts_mut(p
, len
))
265 /// Converts a slice of bytes to a string slice without checking
266 /// that the string contains valid UTF-8.
268 /// See the safe version, [`from_utf8()`][fromutf8], for more information.
270 /// [fromutf8]: fn.from_utf8.html
274 /// This function is unsafe because it does not check that the bytes passed to
275 /// it are valid UTF-8. If this constraint is violated, undefined behavior
276 /// results, as the rest of Rust assumes that `&str`s are valid UTF-8.
285 /// // some bytes, in a vector
286 /// let sparkle_heart = vec![240, 159, 146, 150];
288 /// let sparkle_heart = unsafe {
289 /// str::from_utf8_unchecked(&sparkle_heart)
292 /// assert_eq!("💖", sparkle_heart);
295 #[stable(feature = "rust1", since = "1.0.0")]
296 pub unsafe fn from_utf8_unchecked(v
: &[u8]) -> &str {
300 #[stable(feature = "rust1", since = "1.0.0")]
301 impl fmt
::Display
for Utf8Error
{
302 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
303 write
!(f
, "invalid utf-8: invalid byte near index {}", self.valid_up_to
)
311 /// Iterator for the char (representing *Unicode Scalar Values*) of a string
313 /// Created with the method [`chars()`].
315 /// [`chars()`]: ../../std/primitive.str.html#method.chars
316 #[derive(Clone, Debug)]
317 #[stable(feature = "rust1", since = "1.0.0")]
318 pub struct Chars
<'a
> {
319 iter
: slice
::Iter
<'a
, u8>
322 /// Return the initial codepoint accumulator for the first byte.
323 /// The first byte is special, only want bottom 5 bits for width 2, 4 bits
324 /// for width 3, and 3 bits for width 4.
326 fn utf8_first_byte(byte
: u8, width
: u32) -> u32 { (byte & (0x7F >> width)) as u32 }
328 /// Return the value of `ch` updated with continuation byte `byte`.
330 fn utf8_acc_cont_byte(ch
: u32, byte
: u8) -> u32 { (ch << 6) | (byte & CONT_MASK) as u32 }
332 /// Checks whether the byte is a UTF-8 continuation byte (i.e. starts with the
335 fn utf8_is_cont_byte(byte
: u8) -> bool { (byte & !CONT_MASK) == TAG_CONT_U8 }
338 fn unwrap_or_0(opt
: Option
<&u8>) -> u8 {
345 /// Reads the next code point out of a byte iterator (assuming a
346 /// UTF-8-like encoding).
347 #[unstable(feature = "str_internals", issue = "0")]
349 pub fn next_code_point
<'a
, I
: Iterator
<Item
= &'a
u8>>(bytes
: &mut I
) -> Option
<u32> {
351 let x
= match bytes
.next() {
353 Some(&next_byte
) if next_byte
< 128 => return Some(next_byte
as u32),
354 Some(&next_byte
) => next_byte
,
357 // Multibyte case follows
358 // Decode from a byte combination out of: [[[x y] z] w]
359 // NOTE: Performance is sensitive to the exact formulation here
360 let init
= utf8_first_byte(x
, 2);
361 let y
= unwrap_or_0(bytes
.next());
362 let mut ch
= utf8_acc_cont_byte(init
, y
);
365 // 5th bit in 0xE0 .. 0xEF is always clear, so `init` is still valid
366 let z
= unwrap_or_0(bytes
.next());
367 let y_z
= utf8_acc_cont_byte((y
& CONT_MASK
) as u32, z
);
368 ch
= init
<< 12 | y_z
;
371 // use only the lower 3 bits of `init`
372 let w
= unwrap_or_0(bytes
.next());
373 ch
= (init
& 7) << 18 | utf8_acc_cont_byte(y_z
, w
);
380 /// Reads the last code point out of a byte iterator (assuming a
381 /// UTF-8-like encoding).
383 fn next_code_point_reverse
<'a
, I
>(bytes
: &mut I
) -> Option
<u32>
384 where I
: DoubleEndedIterator
<Item
= &'a
u8>,
387 let w
= match bytes
.next_back() {
389 Some(&next_byte
) if next_byte
< 128 => return Some(next_byte
as u32),
390 Some(&back_byte
) => back_byte
,
393 // Multibyte case follows
394 // Decode from a byte combination out of: [x [y [z w]]]
396 let z
= unwrap_or_0(bytes
.next_back());
397 ch
= utf8_first_byte(z
, 2);
398 if utf8_is_cont_byte(z
) {
399 let y
= unwrap_or_0(bytes
.next_back());
400 ch
= utf8_first_byte(y
, 3);
401 if utf8_is_cont_byte(y
) {
402 let x
= unwrap_or_0(bytes
.next_back());
403 ch
= utf8_first_byte(x
, 4);
404 ch
= utf8_acc_cont_byte(ch
, y
);
406 ch
= utf8_acc_cont_byte(ch
, z
);
408 ch
= utf8_acc_cont_byte(ch
, w
);
413 #[stable(feature = "rust1", since = "1.0.0")]
414 impl<'a
> Iterator
for Chars
<'a
> {
418 fn next(&mut self) -> Option
<char> {
419 next_code_point(&mut self.iter
).map(|ch
| {
420 // str invariant says `ch` is a valid Unicode Scalar Value
422 char::from_u32_unchecked(ch
)
428 fn size_hint(&self) -> (usize, Option
<usize>) {
429 let len
= self.iter
.len();
430 // `(len + 3)` can't overflow, because we know that the `slice::Iter`
431 // belongs to a slice in memory which has a maximum length of
432 // `isize::MAX` (that's well below `usize::MAX`).
433 ((len
+ 3) / 4, Some(len
))
437 #[stable(feature = "rust1", since = "1.0.0")]
438 impl<'a
> DoubleEndedIterator
for Chars
<'a
> {
440 fn next_back(&mut self) -> Option
<char> {
441 next_code_point_reverse(&mut self.iter
).map(|ch
| {
442 // str invariant says `ch` is a valid Unicode Scalar Value
444 char::from_u32_unchecked(ch
)
450 #[unstable(feature = "fused", issue = "35602")]
451 impl<'a
> FusedIterator
for Chars
<'a
> {}
454 /// View the underlying data as a subslice of the original data.
456 /// This has the same lifetime as the original slice, and so the
457 /// iterator can continue to be used while this exists.
462 /// let mut chars = "abc".chars();
464 /// assert_eq!(chars.as_str(), "abc");
466 /// assert_eq!(chars.as_str(), "bc");
469 /// assert_eq!(chars.as_str(), "");
471 #[stable(feature = "iter_to_slice", since = "1.4.0")]
473 pub fn as_str(&self) -> &'a
str {
474 unsafe { from_utf8_unchecked(self.iter.as_slice()) }
478 /// Iterator for a string's characters and their byte offsets.
479 #[derive(Clone, Debug)]
480 #[stable(feature = "rust1", since = "1.0.0")]
481 pub struct CharIndices
<'a
> {
486 #[stable(feature = "rust1", since = "1.0.0")]
487 impl<'a
> Iterator
for CharIndices
<'a
> {
488 type Item
= (usize, char);
491 fn next(&mut self) -> Option
<(usize, char)> {
492 let pre_len
= self.iter
.iter
.len();
493 match self.iter
.next() {
496 let index
= self.front_offset
;
497 let len
= self.iter
.iter
.len();
498 self.front_offset
+= pre_len
- len
;
505 fn size_hint(&self) -> (usize, Option
<usize>) {
506 self.iter
.size_hint()
510 #[stable(feature = "rust1", since = "1.0.0")]
511 impl<'a
> DoubleEndedIterator
for CharIndices
<'a
> {
513 fn next_back(&mut self) -> Option
<(usize, char)> {
514 match self.iter
.next_back() {
517 let index
= self.front_offset
+ self.iter
.iter
.len();
524 #[unstable(feature = "fused", issue = "35602")]
525 impl<'a
> FusedIterator
for CharIndices
<'a
> {}
527 impl<'a
> CharIndices
<'a
> {
528 /// View the underlying data as a subslice of the original data.
530 /// This has the same lifetime as the original slice, and so the
531 /// iterator can continue to be used while this exists.
532 #[stable(feature = "iter_to_slice", since = "1.4.0")]
534 pub fn as_str(&self) -> &'a
str {
539 /// External iterator for a string's bytes.
540 /// Use with the `std::iter` module.
542 /// Created with the method [`bytes()`].
544 /// [`bytes()`]: ../../std/primitive.str.html#method.bytes
545 #[stable(feature = "rust1", since = "1.0.0")]
546 #[derive(Clone, Debug)]
547 pub struct Bytes
<'a
>(Cloned
<slice
::Iter
<'a
, u8>>);
549 #[stable(feature = "rust1", since = "1.0.0")]
550 impl<'a
> Iterator
for Bytes
<'a
> {
554 fn next(&mut self) -> Option
<u8> {
559 fn size_hint(&self) -> (usize, Option
<usize>) {
564 fn count(self) -> usize {
569 fn last(self) -> Option
<Self::Item
> {
574 fn nth(&mut self, n
: usize) -> Option
<Self::Item
> {
579 #[stable(feature = "rust1", since = "1.0.0")]
580 impl<'a
> DoubleEndedIterator
for Bytes
<'a
> {
582 fn next_back(&mut self) -> Option
<u8> {
587 #[stable(feature = "rust1", since = "1.0.0")]
588 impl<'a
> ExactSizeIterator
for Bytes
<'a
> {
590 fn len(&self) -> usize {
595 #[unstable(feature = "fused", issue = "35602")]
596 impl<'a
> FusedIterator
for Bytes
<'a
> {}
598 /// This macro generates a Clone impl for string pattern API
599 /// wrapper types of the form X<'a, P>
600 macro_rules
! derive_pattern_clone
{
601 (clone $t
:ident with
|$s
:ident
| $e
:expr
) => {
602 impl<'a
, P
: Pattern
<'a
>> Clone
for $t
<'a
, P
>
603 where P
::Searcher
: Clone
605 fn clone(&self) -> Self {
613 /// This macro generates two public iterator structs
614 /// wrapping a private internal one that makes use of the `Pattern` API.
616 /// For all patterns `P: Pattern<'a>` the following items will be
617 /// generated (generics omitted):
619 /// struct $forward_iterator($internal_iterator);
620 /// struct $reverse_iterator($internal_iterator);
622 /// impl Iterator for $forward_iterator
623 /// { /* internal ends up calling Searcher::next_match() */ }
625 /// impl DoubleEndedIterator for $forward_iterator
626 /// where P::Searcher: DoubleEndedSearcher
627 /// { /* internal ends up calling Searcher::next_match_back() */ }
629 /// impl Iterator for $reverse_iterator
630 /// where P::Searcher: ReverseSearcher
631 /// { /* internal ends up calling Searcher::next_match_back() */ }
633 /// impl DoubleEndedIterator for $reverse_iterator
634 /// where P::Searcher: DoubleEndedSearcher
635 /// { /* internal ends up calling Searcher::next_match() */ }
637 /// The internal one is defined outside the macro, and has almost the same
638 /// semantic as a DoubleEndedIterator by delegating to `pattern::Searcher` and
639 /// `pattern::ReverseSearcher` for both forward and reverse iteration.
641 /// "Almost", because a `Searcher` and a `ReverseSearcher` for a given
642 /// `Pattern` might not return the same elements, so actually implementing
643 /// `DoubleEndedIterator` for it would be incorrect.
644 /// (See the docs in `str::pattern` for more details)
646 /// However, the internal struct still represents a single ended iterator from
647 /// either end, and depending on pattern is also a valid double ended iterator,
648 /// so the two wrapper structs implement `Iterator`
649 /// and `DoubleEndedIterator` depending on the concrete pattern type, leading
650 /// to the complex impls seen above.
651 macro_rules
! generate_pattern_iterators
{
655 $
(#[$forward_iterator_attribute:meta])*
656 struct $forward_iterator
:ident
;
660 $
(#[$reverse_iterator_attribute:meta])*
661 struct $reverse_iterator
:ident
;
663 // Stability of all generated items
665 $
(#[$common_stability_attribute:meta])*
667 // Internal almost-iterator that is being delegated to
669 $internal_iterator
:ident
yielding ($iterty
:ty
);
671 // Kind of delgation - either single ended or double ended
674 $
(#[$forward_iterator_attribute])*
675 $
(#[$common_stability_attribute])*
676 pub struct $forward_iterator
<'a
, P
: Pattern
<'a
>>($internal_iterator
<'a
, P
>);
678 $
(#[$common_stability_attribute])*
679 impl<'a
, P
: Pattern
<'a
>> fmt
::Debug
for $forward_iterator
<'a
, P
>
680 where P
::Searcher
: fmt
::Debug
682 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
683 f
.debug_tuple(stringify
!($forward_iterator
))
689 $
(#[$common_stability_attribute])*
690 impl<'a
, P
: Pattern
<'a
>> Iterator
for $forward_iterator
<'a
, P
> {
694 fn next(&mut self) -> Option
<$iterty
> {
699 $
(#[$common_stability_attribute])*
700 impl<'a
, P
: Pattern
<'a
>> Clone
for $forward_iterator
<'a
, P
>
701 where P
::Searcher
: Clone
703 fn clone(&self) -> Self {
704 $
forward_iterator(self.0.clone())
708 $
(#[$reverse_iterator_attribute])*
709 $
(#[$common_stability_attribute])*
710 pub struct $reverse_iterator
<'a
, P
: Pattern
<'a
>>($internal_iterator
<'a
, P
>);
712 $
(#[$common_stability_attribute])*
713 impl<'a
, P
: Pattern
<'a
>> fmt
::Debug
for $reverse_iterator
<'a
, P
>
714 where P
::Searcher
: fmt
::Debug
716 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
717 f
.debug_tuple(stringify
!($reverse_iterator
))
723 $
(#[$common_stability_attribute])*
724 impl<'a
, P
: Pattern
<'a
>> Iterator
for $reverse_iterator
<'a
, P
>
725 where P
::Searcher
: ReverseSearcher
<'a
>
730 fn next(&mut self) -> Option
<$iterty
> {
735 $
(#[$common_stability_attribute])*
736 impl<'a
, P
: Pattern
<'a
>> Clone
for $reverse_iterator
<'a
, P
>
737 where P
::Searcher
: Clone
739 fn clone(&self) -> Self {
740 $
reverse_iterator(self.0.clone())
744 #[unstable(feature = "fused", issue = "35602")]
745 impl<'a
, P
: Pattern
<'a
>> FusedIterator
for $forward_iterator
<'a
, P
> {}
747 #[unstable(feature = "fused", issue = "35602")]
748 impl<'a
, P
: Pattern
<'a
>> FusedIterator
for $reverse_iterator
<'a
, P
>
749 where P
::Searcher
: ReverseSearcher
<'a
> {}
751 generate_pattern_iterators
!($
($t
)* with $
(#[$common_stability_attribute])*,
753 $reverse_iterator
, $iterty
);
756 double ended
; with $
(#[$common_stability_attribute:meta])*,
757 $forward_iterator
:ident
,
758 $reverse_iterator
:ident
, $iterty
:ty
760 $
(#[$common_stability_attribute])*
761 impl<'a
, P
: Pattern
<'a
>> DoubleEndedIterator
for $forward_iterator
<'a
, P
>
762 where P
::Searcher
: DoubleEndedSearcher
<'a
>
765 fn next_back(&mut self) -> Option
<$iterty
> {
770 $
(#[$common_stability_attribute])*
771 impl<'a
, P
: Pattern
<'a
>> DoubleEndedIterator
for $reverse_iterator
<'a
, P
>
772 where P
::Searcher
: DoubleEndedSearcher
<'a
>
775 fn next_back(&mut self) -> Option
<$iterty
> {
781 single ended
; with $
(#[$common_stability_attribute:meta])*,
782 $forward_iterator
:ident
,
783 $reverse_iterator
:ident
, $iterty
:ty
787 derive_pattern_clone
!{
789 with
|s
| SplitInternal { matcher: s.matcher.clone(), ..*s }
792 struct SplitInternal
<'a
, P
: Pattern
<'a
>> {
795 matcher
: P
::Searcher
,
796 allow_trailing_empty
: bool
,
800 impl<'a
, P
: Pattern
<'a
>> fmt
::Debug
for SplitInternal
<'a
, P
> where P
::Searcher
: fmt
::Debug
{
801 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
802 f
.debug_struct("SplitInternal")
803 .field("start", &self.start
)
804 .field("end", &self.end
)
805 .field("matcher", &self.matcher
)
806 .field("allow_trailing_empty", &self.allow_trailing_empty
)
807 .field("finished", &self.finished
)
812 impl<'a
, P
: Pattern
<'a
>> SplitInternal
<'a
, P
> {
814 fn get_end(&mut self) -> Option
<&'a
str> {
815 if !self.finished
&& (self.allow_trailing_empty
|| self.end
- self.start
> 0) {
816 self.finished
= true;
818 let string
= self.matcher
.haystack().slice_unchecked(self.start
, self.end
);
827 fn next(&mut self) -> Option
<&'a
str> {
828 if self.finished { return None }
830 let haystack
= self.matcher
.haystack();
831 match self.matcher
.next_match() {
832 Some((a
, b
)) => unsafe {
833 let elt
= haystack
.slice_unchecked(self.start
, a
);
837 None
=> self.get_end(),
842 fn next_back(&mut self) -> Option
<&'a
str>
843 where P
::Searcher
: ReverseSearcher
<'a
>
845 if self.finished { return None }
847 if !self.allow_trailing_empty
{
848 self.allow_trailing_empty
= true;
849 match self.next_back() {
850 Some(elt
) if !elt
.is_empty() => return Some(elt
),
851 _
=> if self.finished { return None }
855 let haystack
= self.matcher
.haystack();
856 match self.matcher
.next_match_back() {
857 Some((a
, b
)) => unsafe {
858 let elt
= haystack
.slice_unchecked(b
, self.end
);
863 self.finished
= true;
864 Some(haystack
.slice_unchecked(self.start
, self.end
))
870 generate_pattern_iterators
! {
872 /// Created with the method [`split()`].
874 /// [`split()`]: ../../std/primitive.str.html#method.split
877 /// Created with the method [`rsplit()`].
879 /// [`rsplit()`]: ../../std/primitive.str.html#method.rsplit
882 #[stable(feature = "rust1", since = "1.0.0")]
884 SplitInternal
yielding (&'a
str);
885 delegate double ended
;
888 generate_pattern_iterators
! {
890 /// Created with the method [`split_terminator()`].
892 /// [`split_terminator()`]: ../../std/primitive.str.html#method.split_terminator
893 struct SplitTerminator
;
895 /// Created with the method [`rsplit_terminator()`].
897 /// [`rsplit_terminator()`]: ../../std/primitive.str.html#method.rsplit_terminator
898 struct RSplitTerminator
;
900 #[stable(feature = "rust1", since = "1.0.0")]
902 SplitInternal
yielding (&'a
str);
903 delegate double ended
;
906 derive_pattern_clone
!{
908 with
|s
| SplitNInternal { iter: s.iter.clone(), ..*s }
911 struct SplitNInternal
<'a
, P
: Pattern
<'a
>> {
912 iter
: SplitInternal
<'a
, P
>,
913 /// The number of splits remaining
917 impl<'a
, P
: Pattern
<'a
>> fmt
::Debug
for SplitNInternal
<'a
, P
> where P
::Searcher
: fmt
::Debug
{
918 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
919 f
.debug_struct("SplitNInternal")
920 .field("iter", &self.iter
)
921 .field("count", &self.count
)
926 impl<'a
, P
: Pattern
<'a
>> SplitNInternal
<'a
, P
> {
928 fn next(&mut self) -> Option
<&'a
str> {
931 1 => { self.count = 0; self.iter.get_end() }
932 _
=> { self.count -= 1; self.iter.next() }
937 fn next_back(&mut self) -> Option
<&'a
str>
938 where P
::Searcher
: ReverseSearcher
<'a
>
942 1 => { self.count = 0; self.iter.get_end() }
943 _
=> { self.count -= 1; self.iter.next_back() }
948 generate_pattern_iterators
! {
950 /// Created with the method [`splitn()`].
952 /// [`splitn()`]: ../../std/primitive.str.html#method.splitn
955 /// Created with the method [`rsplitn()`].
957 /// [`rsplitn()`]: ../../std/primitive.str.html#method.rsplitn
960 #[stable(feature = "rust1", since = "1.0.0")]
962 SplitNInternal
yielding (&'a
str);
963 delegate single ended
;
966 derive_pattern_clone
!{
967 clone MatchIndicesInternal
968 with
|s
| MatchIndicesInternal(s
.0.clone())
971 struct MatchIndicesInternal
<'a
, P
: Pattern
<'a
>>(P
::Searcher
);
973 impl<'a
, P
: Pattern
<'a
>> fmt
::Debug
for MatchIndicesInternal
<'a
, P
> where P
::Searcher
: fmt
::Debug
{
974 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
975 f
.debug_tuple("MatchIndicesInternal")
981 impl<'a
, P
: Pattern
<'a
>> MatchIndicesInternal
<'a
, P
> {
983 fn next(&mut self) -> Option
<(usize, &'a
str)> {
984 self.0.next_match().map(|(start
, end
)| unsafe {
985 (start
, self.0.haystack().slice_unchecked(start
, end
))
990 fn next_back(&mut self) -> Option
<(usize, &'a
str)>
991 where P
::Searcher
: ReverseSearcher
<'a
>
993 self.0.next_match_back().map(|(start
, end
)| unsafe {
994 (start
, self.0.haystack().slice_unchecked(start
, end
))
999 generate_pattern_iterators
! {
1001 /// Created with the method [`match_indices()`].
1003 /// [`match_indices()`]: ../../std/primitive.str.html#method.match_indices
1004 struct MatchIndices
;
1006 /// Created with the method [`rmatch_indices()`].
1008 /// [`rmatch_indices()`]: ../../std/primitive.str.html#method.rmatch_indices
1009 struct RMatchIndices
;
1011 #[stable(feature = "str_match_indices", since = "1.5.0")]
1013 MatchIndicesInternal
yielding ((usize, &'a
str));
1014 delegate double ended
;
1017 derive_pattern_clone
!{
1018 clone MatchesInternal
1019 with
|s
| MatchesInternal(s
.0.clone())
1022 struct MatchesInternal
<'a
, P
: Pattern
<'a
>>(P
::Searcher
);
1024 impl<'a
, P
: Pattern
<'a
>> fmt
::Debug
for MatchesInternal
<'a
, P
> where P
::Searcher
: fmt
::Debug
{
1025 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
1026 f
.debug_tuple("MatchesInternal")
1032 impl<'a
, P
: Pattern
<'a
>> MatchesInternal
<'a
, P
> {
1034 fn next(&mut self) -> Option
<&'a
str> {
1035 self.0.next_match().map(|(a
, b
)| unsafe {
1036 // Indices are known to be on utf8 boundaries
1037 self.0.haystack().slice_unchecked(a
, b
)
1042 fn next_back(&mut self) -> Option
<&'a
str>
1043 where P
::Searcher
: ReverseSearcher
<'a
>
1045 self.0.next_match_back().map(|(a
, b
)| unsafe {
1046 // Indices are known to be on utf8 boundaries
1047 self.0.haystack().slice_unchecked(a
, b
)
1052 generate_pattern_iterators
! {
1054 /// Created with the method [`matches()`].
1056 /// [`matches()`]: ../../std/primitive.str.html#method.matches
1059 /// Created with the method [`rmatches()`].
1061 /// [`rmatches()`]: ../../std/primitive.str.html#method.rmatches
1064 #[stable(feature = "str_matches", since = "1.2.0")]
1066 MatchesInternal
yielding (&'a
str);
1067 delegate double ended
;
1070 /// Created with the method [`lines()`].
1072 /// [`lines()`]: ../../std/primitive.str.html#method.lines
1073 #[stable(feature = "rust1", since = "1.0.0")]
1074 #[derive(Clone, Debug)]
1075 pub struct Lines
<'a
>(Map
<SplitTerminator
<'a
, char>, LinesAnyMap
>);
1077 #[stable(feature = "rust1", since = "1.0.0")]
1078 impl<'a
> Iterator
for Lines
<'a
> {
1079 type Item
= &'a
str;
1082 fn next(&mut self) -> Option
<&'a
str> {
1087 fn size_hint(&self) -> (usize, Option
<usize>) {
1092 #[stable(feature = "rust1", since = "1.0.0")]
1093 impl<'a
> DoubleEndedIterator
for Lines
<'a
> {
1095 fn next_back(&mut self) -> Option
<&'a
str> {
1100 #[unstable(feature = "fused", issue = "35602")]
1101 impl<'a
> FusedIterator
for Lines
<'a
> {}
1103 /// Created with the method [`lines_any()`].
1105 /// [`lines_any()`]: ../../std/primitive.str.html#method.lines_any
1106 #[stable(feature = "rust1", since = "1.0.0")]
1107 #[rustc_deprecated(since = "1.4.0", reason = "use lines()/Lines instead now")]
1108 #[derive(Clone, Debug)]
1109 #[allow(deprecated)]
1110 pub struct LinesAny
<'a
>(Lines
<'a
>);
1112 /// A nameable, cloneable fn type
1116 impl<'a
> Fn
<(&'a
str,)> for LinesAnyMap
{
1118 extern "rust-call" fn call(&self, (line
,): (&'a
str,)) -> &'a
str {
1120 if l
> 0 && line
.as_bytes()[l
- 1] == b'
\r' { &line[0 .. l - 1] }
1125 impl<'a
> FnMut
<(&'a
str,)> for LinesAnyMap
{
1127 extern "rust-call" fn call_mut(&mut self, (line
,): (&'a
str,)) -> &'a
str {
1128 Fn
::call(&*self, (line
,))
1132 impl<'a
> FnOnce
<(&'a
str,)> for LinesAnyMap
{
1133 type Output
= &'a
str;
1136 extern "rust-call" fn call_once(self, (line
,): (&'a
str,)) -> &'a
str {
1137 Fn
::call(&self, (line
,))
1141 #[stable(feature = "rust1", since = "1.0.0")]
1142 #[allow(deprecated)]
1143 impl<'a
> Iterator
for LinesAny
<'a
> {
1144 type Item
= &'a
str;
1147 fn next(&mut self) -> Option
<&'a
str> {
1152 fn size_hint(&self) -> (usize, Option
<usize>) {
1157 #[stable(feature = "rust1", since = "1.0.0")]
1158 #[allow(deprecated)]
1159 impl<'a
> DoubleEndedIterator
for LinesAny
<'a
> {
1161 fn next_back(&mut self) -> Option
<&'a
str> {
1166 #[unstable(feature = "fused", issue = "35602")]
1167 #[allow(deprecated)]
1168 impl<'a
> FusedIterator
for LinesAny
<'a
> {}
1171 Section: Comparing strings
1174 /// Bytewise slice equality
1175 /// NOTE: This function is (ab)used in rustc::middle::trans::_match
1176 /// to compare &[u8] byte slices that are not necessarily valid UTF-8.
1179 fn eq_slice(a
: &str, b
: &str) -> bool
{
1180 a
.as_bytes() == b
.as_bytes()
1184 Section: UTF-8 validation
1187 // use truncation to fit u64 into usize
1188 const NONASCII_MASK
: usize = 0x80808080_80808080u64 as usize;
1190 /// Return `true` if any byte in the word `x` is nonascii (>= 128).
1192 fn contains_nonascii(x
: usize) -> bool
{
1193 (x
& NONASCII_MASK
) != 0
1196 /// Walk through `iter` checking that it's a valid UTF-8 sequence,
1197 /// returning `true` in that case, or, if it is invalid, `false` with
1198 /// `iter` reset such that it is pointing at the first byte in the
1199 /// invalid sequence.
1201 fn run_utf8_validation(v
: &[u8]) -> Result
<(), Utf8Error
> {
1204 while offset
< len
{
1205 let old_offset
= offset
;
1206 macro_rules
! err
{ () => {{
1207 return Err(Utf8Error
{
1208 valid_up_to
: old_offset
1212 macro_rules
! next
{ () => {{
1214 // we needed data, but there was none: error!
1221 let first
= v
[offset
];
1223 let w
= UTF8_CHAR_WIDTH
[first
as usize];
1224 let second
= next
!();
1225 // 2-byte encoding is for codepoints \u{0080} to \u{07ff}
1226 // first C2 80 last DF BF
1227 // 3-byte encoding is for codepoints \u{0800} to \u{ffff}
1228 // first E0 A0 80 last EF BF BF
1229 // excluding surrogates codepoints \u{d800} to \u{dfff}
1230 // ED A0 80 to ED BF BF
1231 // 4-byte encoding is for codepoints \u{1000}0 to \u{10ff}ff
1232 // first F0 90 80 80 last F4 8F BF BF
1234 // Use the UTF-8 syntax from the RFC
1236 // https://tools.ietf.org/html/rfc3629
1238 // UTF8-2 = %xC2-DF UTF8-tail
1239 // UTF8-3 = %xE0 %xA0-BF UTF8-tail / %xE1-EC 2( UTF8-tail ) /
1240 // %xED %x80-9F UTF8-tail / %xEE-EF 2( UTF8-tail )
1241 // UTF8-4 = %xF0 %x90-BF 2( UTF8-tail ) / %xF1-F3 3( UTF8-tail ) /
1242 // %xF4 %x80-8F 2( UTF8-tail )
1244 2 => if second
& !CONT_MASK
!= TAG_CONT_U8 {err!()}
,
1246 match (first
, second
, next
!() & !CONT_MASK
) {
1247 (0xE0 , 0xA0 ... 0xBF, TAG_CONT_U8
) |
1248 (0xE1 ... 0xEC, 0x80 ... 0xBF, TAG_CONT_U8
) |
1249 (0xED , 0x80 ... 0x9F, TAG_CONT_U8
) |
1250 (0xEE ... 0xEF, 0x80 ... 0xBF, TAG_CONT_U8
) => {}
1255 match (first
, second
, next
!() & !CONT_MASK
, next
!() & !CONT_MASK
) {
1256 (0xF0 , 0x90 ... 0xBF, TAG_CONT_U8
, TAG_CONT_U8
) |
1257 (0xF1 ... 0xF3, 0x80 ... 0xBF, TAG_CONT_U8
, TAG_CONT_U8
) |
1258 (0xF4 , 0x80 ... 0x8F, TAG_CONT_U8
, TAG_CONT_U8
) => {}
1266 // Ascii case, try to skip forward quickly.
1267 // When the pointer is aligned, read 2 words of data per iteration
1268 // until we find a word containing a non-ascii byte.
1269 let usize_bytes
= mem
::size_of
::<usize>();
1270 let bytes_per_iteration
= 2 * usize_bytes
;
1271 let ptr
= v
.as_ptr();
1272 let align
= (ptr
as usize + offset
) & (usize_bytes
- 1);
1274 if len
>= bytes_per_iteration
{
1275 while offset
<= len
- bytes_per_iteration
{
1277 let u
= *(ptr
.offset(offset
as isize) as *const usize);
1278 let v
= *(ptr
.offset((offset
+ usize_bytes
) as isize) as *const usize);
1280 // break if there is a nonascii byte
1281 let zu
= contains_nonascii(u
);
1282 let zv
= contains_nonascii(v
);
1287 offset
+= bytes_per_iteration
;
1290 // step from the point where the wordwise loop stopped
1291 while offset
< len
&& v
[offset
] < 128 {
1303 // https://tools.ietf.org/html/rfc3629
1304 static UTF8_CHAR_WIDTH
: [u8; 256] = [
1305 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1306 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 0x1F
1307 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1308 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 0x3F
1309 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1310 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 0x5F
1311 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1312 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 0x7F
1313 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1314 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 0x9F
1315 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1316 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 0xBF
1317 0,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
1318 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, // 0xDF
1319 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3, // 0xEF
1320 4,4,4,4,4,0,0,0,0,0,0,0,0,0,0,0, // 0xFF
1323 /// Mask of the value bits of a continuation byte
1324 const CONT_MASK
: u8 = 0b0011_1111;
1325 /// Value of the tag bits (tag mask is !CONT_MASK) of a continuation byte
1326 const TAG_CONT_U8
: u8 = 0b1000_0000;
1329 Section: Trait implementations
1337 #[stable(feature = "rust1", since = "1.0.0")]
1340 fn cmp(&self, other
: &str) -> Ordering
{
1341 self.as_bytes().cmp(other
.as_bytes())
1345 #[stable(feature = "rust1", since = "1.0.0")]
1346 impl PartialEq
for str {
1348 fn eq(&self, other
: &str) -> bool
{
1349 eq_slice(self, other
)
1352 fn ne(&self, other
: &str) -> bool { !(*self).eq(other) }
1355 #[stable(feature = "rust1", since = "1.0.0")]
1358 #[stable(feature = "rust1", since = "1.0.0")]
1359 impl PartialOrd
for str {
1361 fn partial_cmp(&self, other
: &str) -> Option
<Ordering
> {
1362 Some(self.cmp(other
))
1366 /// Implements substring slicing with syntax `&self[begin .. end]`.
1368 /// Returns a slice of the given string from the byte range
1369 /// [`begin`..`end`).
1371 /// This operation is `O(1)`.
1375 /// Panics if `begin` or `end` does not point to the starting
1376 /// byte offset of a character (as defined by `is_char_boundary`).
1377 /// Requires that `begin <= end` and `end <= len` where `len` is the
1378 /// length of the string.
1383 /// let s = "Löwe 老虎 Léopard";
1384 /// assert_eq!(&s[0 .. 1], "L");
1386 /// assert_eq!(&s[1 .. 9], "öwe 老");
1388 /// // these will panic:
1389 /// // byte 2 lies within `ö`:
1392 /// // byte 8 lies within `老`
1395 /// // byte 100 is outside the string
1396 /// // &s[3 .. 100];
1398 #[stable(feature = "rust1", since = "1.0.0")]
1399 impl ops
::Index
<ops
::Range
<usize>> for str {
1402 fn index(&self, index
: ops
::Range
<usize>) -> &str {
1403 // is_char_boundary checks that the index is in [0, .len()]
1404 if index
.start
<= index
.end
&&
1405 self.is_char_boundary(index
.start
) &&
1406 self.is_char_boundary(index
.end
) {
1407 unsafe { self.slice_unchecked(index.start, index.end) }
1409 super::slice_error_fail(self, index
.start
, index
.end
)
1414 /// Implements mutable substring slicing with syntax
1415 /// `&mut self[begin .. end]`.
1417 /// Returns a mutable slice of the given string from the byte range
1418 /// [`begin`..`end`).
1420 /// This operation is `O(1)`.
1424 /// Panics if `begin` or `end` does not point to the starting
1425 /// byte offset of a character (as defined by `is_char_boundary`).
1426 /// Requires that `begin <= end` and `end <= len` where `len` is the
1427 /// length of the string.
1428 #[stable(feature = "derefmut_for_string", since = "1.2.0")]
1429 impl ops
::IndexMut
<ops
::Range
<usize>> for str {
1431 fn index_mut(&mut self, index
: ops
::Range
<usize>) -> &mut str {
1432 // is_char_boundary checks that the index is in [0, .len()]
1433 if index
.start
<= index
.end
&&
1434 self.is_char_boundary(index
.start
) &&
1435 self.is_char_boundary(index
.end
) {
1436 unsafe { self.slice_mut_unchecked(index.start, index.end) }
1438 super::slice_error_fail(self, index
.start
, index
.end
)
1443 /// Implements substring slicing with syntax `&self[.. end]`.
1445 /// Returns a slice of the string from the beginning to byte offset
1448 /// Equivalent to `&self[0 .. end]`.
1449 #[stable(feature = "rust1", since = "1.0.0")]
1450 impl ops
::Index
<ops
::RangeTo
<usize>> for str {
1454 fn index(&self, index
: ops
::RangeTo
<usize>) -> &str {
1455 // is_char_boundary checks that the index is in [0, .len()]
1456 if self.is_char_boundary(index
.end
) {
1457 unsafe { self.slice_unchecked(0, index.end) }
1459 super::slice_error_fail(self, 0, index
.end
)
1464 /// Implements mutable substring slicing with syntax `&mut self[.. end]`.
1466 /// Returns a mutable slice of the string from the beginning to byte offset
1469 /// Equivalent to `&mut self[0 .. end]`.
1470 #[stable(feature = "derefmut_for_string", since = "1.2.0")]
1471 impl ops
::IndexMut
<ops
::RangeTo
<usize>> for str {
1473 fn index_mut(&mut self, index
: ops
::RangeTo
<usize>) -> &mut str {
1474 // is_char_boundary checks that the index is in [0, .len()]
1475 if self.is_char_boundary(index
.end
) {
1476 unsafe { self.slice_mut_unchecked(0, index.end) }
1478 super::slice_error_fail(self, 0, index
.end
)
1483 /// Implements substring slicing with syntax `&self[begin ..]`.
1485 /// Returns a slice of the string from byte offset `begin`
1486 /// to the end of the string.
1488 /// Equivalent to `&self[begin .. len]`.
1489 #[stable(feature = "rust1", since = "1.0.0")]
1490 impl ops
::Index
<ops
::RangeFrom
<usize>> for str {
1494 fn index(&self, index
: ops
::RangeFrom
<usize>) -> &str {
1495 // is_char_boundary checks that the index is in [0, .len()]
1496 if self.is_char_boundary(index
.start
) {
1497 unsafe { self.slice_unchecked(index.start, self.len()) }
1499 super::slice_error_fail(self, index
.start
, self.len())
1504 /// Implements mutable substring slicing with syntax `&mut self[begin ..]`.
1506 /// Returns a mutable slice of the string from byte offset `begin`
1507 /// to the end of the string.
1509 /// Equivalent to `&mut self[begin .. len]`.
1510 #[stable(feature = "derefmut_for_string", since = "1.2.0")]
1511 impl ops
::IndexMut
<ops
::RangeFrom
<usize>> for str {
1513 fn index_mut(&mut self, index
: ops
::RangeFrom
<usize>) -> &mut str {
1514 // is_char_boundary checks that the index is in [0, .len()]
1515 if self.is_char_boundary(index
.start
) {
1516 let len
= self.len();
1517 unsafe { self.slice_mut_unchecked(index.start, len) }
1519 super::slice_error_fail(self, index
.start
, self.len())
1524 /// Implements substring slicing with syntax `&self[..]`.
1526 /// Returns a slice of the whole string. This operation can
1529 /// Equivalent to `&self[0 .. len]`.
1530 #[stable(feature = "rust1", since = "1.0.0")]
1531 impl ops
::Index
<ops
::RangeFull
> for str {
1535 fn index(&self, _index
: ops
::RangeFull
) -> &str {
1540 /// Implements mutable substring slicing with syntax `&mut self[..]`.
1542 /// Returns a mutable slice of the whole string. This operation can
1545 /// Equivalent to `&mut self[0 .. len]`.
1546 #[stable(feature = "derefmut_for_string", since = "1.2.0")]
1547 impl ops
::IndexMut
<ops
::RangeFull
> for str {
1549 fn index_mut(&mut self, _index
: ops
::RangeFull
) -> &mut str {
1554 #[unstable(feature = "inclusive_range",
1555 reason
= "recently added, follows RFC",
1557 impl ops
::Index
<ops
::RangeInclusive
<usize>> for str {
1561 fn index(&self, index
: ops
::RangeInclusive
<usize>) -> &str {
1563 ops
::RangeInclusive
::Empty { .. }
=> "",
1564 ops
::RangeInclusive
::NonEmpty { end, .. }
if end
== usize::max_value() =>
1565 panic
!("attempted to index slice up to maximum usize"),
1566 ops
::RangeInclusive
::NonEmpty { start, end }
=>
1567 self.index(start
.. end
+1)
1571 #[unstable(feature = "inclusive_range",
1572 reason
= "recently added, follows RFC",
1574 impl ops
::Index
<ops
::RangeToInclusive
<usize>> for str {
1578 fn index(&self, index
: ops
::RangeToInclusive
<usize>) -> &str {
1579 self.index(0...index
.end
)
1583 #[unstable(feature = "inclusive_range",
1584 reason
= "recently added, follows RFC",
1586 impl ops
::IndexMut
<ops
::RangeInclusive
<usize>> for str {
1588 fn index_mut(&mut self, index
: ops
::RangeInclusive
<usize>) -> &mut str {
1590 ops
::RangeInclusive
::Empty { .. }
=> &mut self[0..0], // `&mut ""` doesn't work
1591 ops
::RangeInclusive
::NonEmpty { end, .. }
if end
== usize::max_value() =>
1592 panic
!("attempted to index str up to maximum usize"),
1593 ops
::RangeInclusive
::NonEmpty { start, end }
=>
1594 self.index_mut(start
.. end
+1)
1598 #[unstable(feature = "inclusive_range",
1599 reason
= "recently added, follows RFC",
1601 impl ops
::IndexMut
<ops
::RangeToInclusive
<usize>> for str {
1603 fn index_mut(&mut self, index
: ops
::RangeToInclusive
<usize>) -> &mut str {
1604 self.index_mut(0...index
.end
)
1609 /// Methods for string slices
1610 #[allow(missing_docs)]
1612 #[unstable(feature = "core_str_ext",
1613 reason
= "stable interface provided by `impl str` in later crates",
1616 // NB there are no docs here are they're all located on the StrExt trait in
1617 // libcollections, not here.
1619 #[stable(feature = "core", since = "1.6.0")]
1620 fn contains
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> bool
;
1621 #[stable(feature = "core", since = "1.6.0")]
1622 fn chars(&self) -> Chars
;
1623 #[stable(feature = "core", since = "1.6.0")]
1624 fn bytes(&self) -> Bytes
;
1625 #[stable(feature = "core", since = "1.6.0")]
1626 fn char_indices(&self) -> CharIndices
;
1627 #[stable(feature = "core", since = "1.6.0")]
1628 fn split
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> Split
<'a
, P
>;
1629 #[stable(feature = "core", since = "1.6.0")]
1630 fn rsplit
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> RSplit
<'a
, P
>
1631 where P
::Searcher
: ReverseSearcher
<'a
>;
1632 #[stable(feature = "core", since = "1.6.0")]
1633 fn splitn
<'a
, P
: Pattern
<'a
>>(&'a
self, count
: usize, pat
: P
) -> SplitN
<'a
, P
>;
1634 #[stable(feature = "core", since = "1.6.0")]
1635 fn rsplitn
<'a
, P
: Pattern
<'a
>>(&'a
self, count
: usize, pat
: P
) -> RSplitN
<'a
, P
>
1636 where P
::Searcher
: ReverseSearcher
<'a
>;
1637 #[stable(feature = "core", since = "1.6.0")]
1638 fn split_terminator
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> SplitTerminator
<'a
, P
>;
1639 #[stable(feature = "core", since = "1.6.0")]
1640 fn rsplit_terminator
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> RSplitTerminator
<'a
, P
>
1641 where P
::Searcher
: ReverseSearcher
<'a
>;
1642 #[stable(feature = "core", since = "1.6.0")]
1643 fn matches
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> Matches
<'a
, P
>;
1644 #[stable(feature = "core", since = "1.6.0")]
1645 fn rmatches
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> RMatches
<'a
, P
>
1646 where P
::Searcher
: ReverseSearcher
<'a
>;
1647 #[stable(feature = "core", since = "1.6.0")]
1648 fn match_indices
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> MatchIndices
<'a
, P
>;
1649 #[stable(feature = "core", since = "1.6.0")]
1650 fn rmatch_indices
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> RMatchIndices
<'a
, P
>
1651 where P
::Searcher
: ReverseSearcher
<'a
>;
1652 #[stable(feature = "core", since = "1.6.0")]
1653 fn lines(&self) -> Lines
;
1654 #[stable(feature = "core", since = "1.6.0")]
1655 #[rustc_deprecated(since = "1.6.0", reason = "use lines() instead now")]
1656 #[allow(deprecated)]
1657 fn lines_any(&self) -> LinesAny
;
1658 #[stable(feature = "core", since = "1.6.0")]
1659 unsafe fn slice_unchecked(&self, begin
: usize, end
: usize) -> &str;
1660 #[stable(feature = "core", since = "1.6.0")]
1661 unsafe fn slice_mut_unchecked(&mut self, begin
: usize, end
: usize) -> &mut str;
1662 #[stable(feature = "core", since = "1.6.0")]
1663 fn starts_with
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> bool
;
1664 #[stable(feature = "core", since = "1.6.0")]
1665 fn ends_with
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> bool
1666 where P
::Searcher
: ReverseSearcher
<'a
>;
1667 #[stable(feature = "core", since = "1.6.0")]
1668 fn trim_matches
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> &'a
str
1669 where P
::Searcher
: DoubleEndedSearcher
<'a
>;
1670 #[stable(feature = "core", since = "1.6.0")]
1671 fn trim_left_matches
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> &'a
str;
1672 #[stable(feature = "core", since = "1.6.0")]
1673 fn trim_right_matches
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> &'a
str
1674 where P
::Searcher
: ReverseSearcher
<'a
>;
1675 #[stable(feature = "is_char_boundary", since = "1.9.0")]
1676 fn is_char_boundary(&self, index
: usize) -> bool
;
1677 #[stable(feature = "core", since = "1.6.0")]
1678 fn as_bytes(&self) -> &[u8];
1679 #[stable(feature = "core", since = "1.6.0")]
1680 fn find
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> Option
<usize>;
1681 #[stable(feature = "core", since = "1.6.0")]
1682 fn rfind
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> Option
<usize>
1683 where P
::Searcher
: ReverseSearcher
<'a
>;
1684 fn find_str
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> Option
<usize>;
1685 #[stable(feature = "core", since = "1.6.0")]
1686 fn split_at(&self, mid
: usize) -> (&str, &str);
1687 #[stable(feature = "core", since = "1.6.0")]
1688 fn split_at_mut(&mut self, mid
: usize) -> (&mut str, &mut str);
1689 #[stable(feature = "core", since = "1.6.0")]
1690 fn as_ptr(&self) -> *const u8;
1691 #[stable(feature = "core", since = "1.6.0")]
1692 fn len(&self) -> usize;
1693 #[stable(feature = "core", since = "1.6.0")]
1694 fn is_empty(&self) -> bool
;
1695 #[stable(feature = "core", since = "1.6.0")]
1696 fn parse
<T
: FromStr
>(&self) -> Result
<T
, T
::Err
>;
1699 // truncate `&str` to length at most equal to `max`
1700 // return `true` if it were truncated, and the new str.
1701 fn truncate_to_char_boundary(s
: &str, mut max
: usize) -> (bool
, &str) {
1705 while !s
.is_char_boundary(max
) {
1714 fn slice_error_fail(s
: &str, begin
: usize, end
: usize) -> ! {
1715 const MAX_DISPLAY_LENGTH
: usize = 256;
1716 let (truncated
, s
) = truncate_to_char_boundary(s
, MAX_DISPLAY_LENGTH
);
1717 let ellipsis
= if truncated { "[...]" }
else { "" }
;
1719 assert
!(begin
<= end
, "begin <= end ({} <= {}) when slicing `{}`{}",
1720 begin
, end
, s
, ellipsis
);
1721 panic
!("index {} and/or {} in `{}`{} do not lie on character boundary",
1722 begin
, end
, s
, ellipsis
);
1725 #[stable(feature = "core", since = "1.6.0")]
1726 impl StrExt
for str {
1728 fn contains
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> bool
{
1729 pat
.is_contained_in(self)
1733 fn chars(&self) -> Chars
{
1734 Chars{iter: self.as_bytes().iter()}
1738 fn bytes(&self) -> Bytes
{
1739 Bytes(self.as_bytes().iter().cloned())
1743 fn char_indices(&self) -> CharIndices
{
1744 CharIndices { front_offset: 0, iter: self.chars() }
1748 fn split
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> Split
<'a
, P
> {
1749 Split(SplitInternal
{
1752 matcher
: pat
.into_searcher(self),
1753 allow_trailing_empty
: true,
1759 fn rsplit
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> RSplit
<'a
, P
>
1760 where P
::Searcher
: ReverseSearcher
<'a
>
1762 RSplit(self.split(pat
).0)
1766 fn splitn
<'a
, P
: Pattern
<'a
>>(&'a
self, count
: usize, pat
: P
) -> SplitN
<'a
, P
> {
1767 SplitN(SplitNInternal
{
1768 iter
: self.split(pat
).0,
1774 fn rsplitn
<'a
, P
: Pattern
<'a
>>(&'a
self, count
: usize, pat
: P
) -> RSplitN
<'a
, P
>
1775 where P
::Searcher
: ReverseSearcher
<'a
>
1777 RSplitN(self.splitn(count
, pat
).0)
1781 fn split_terminator
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> SplitTerminator
<'a
, P
> {
1782 SplitTerminator(SplitInternal
{
1783 allow_trailing_empty
: false,
1789 fn rsplit_terminator
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> RSplitTerminator
<'a
, P
>
1790 where P
::Searcher
: ReverseSearcher
<'a
>
1792 RSplitTerminator(self.split_terminator(pat
).0)
1796 fn matches
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> Matches
<'a
, P
> {
1797 Matches(MatchesInternal(pat
.into_searcher(self)))
1801 fn rmatches
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> RMatches
<'a
, P
>
1802 where P
::Searcher
: ReverseSearcher
<'a
>
1804 RMatches(self.matches(pat
).0)
1808 fn match_indices
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> MatchIndices
<'a
, P
> {
1809 MatchIndices(MatchIndicesInternal(pat
.into_searcher(self)))
1813 fn rmatch_indices
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> RMatchIndices
<'a
, P
>
1814 where P
::Searcher
: ReverseSearcher
<'a
>
1816 RMatchIndices(self.match_indices(pat
).0)
1819 fn lines(&self) -> Lines
{
1820 Lines(self.split_terminator('
\n'
).map(LinesAnyMap
))
1824 #[allow(deprecated)]
1825 fn lines_any(&self) -> LinesAny
{
1826 LinesAny(self.lines())
1830 unsafe fn slice_unchecked(&self, begin
: usize, end
: usize) -> &str {
1831 let ptr
= self.as_ptr().offset(begin
as isize);
1832 let len
= end
- begin
;
1833 from_utf8_unchecked(slice
::from_raw_parts(ptr
, len
))
1837 unsafe fn slice_mut_unchecked(&mut self, begin
: usize, end
: usize) -> &mut str {
1838 let ptr
= self.as_ptr().offset(begin
as isize);
1839 let len
= end
- begin
;
1840 mem
::transmute(slice
::from_raw_parts_mut(ptr
as *mut u8, len
))
1844 fn starts_with
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> bool
{
1845 pat
.is_prefix_of(self)
1849 fn ends_with
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> bool
1850 where P
::Searcher
: ReverseSearcher
<'a
>
1852 pat
.is_suffix_of(self)
1856 fn trim_matches
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> &'a
str
1857 where P
::Searcher
: DoubleEndedSearcher
<'a
>
1861 let mut matcher
= pat
.into_searcher(self);
1862 if let Some((a
, b
)) = matcher
.next_reject() {
1864 j
= b
; // Remember earliest known match, correct it below if
1865 // last match is different
1867 if let Some((_
, b
)) = matcher
.next_reject_back() {
1871 // Searcher is known to return valid indices
1872 self.slice_unchecked(i
, j
)
1877 fn trim_left_matches
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> &'a
str {
1878 let mut i
= self.len();
1879 let mut matcher
= pat
.into_searcher(self);
1880 if let Some((a
, _
)) = matcher
.next_reject() {
1884 // Searcher is known to return valid indices
1885 self.slice_unchecked(i
, self.len())
1890 fn trim_right_matches
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> &'a
str
1891 where P
::Searcher
: ReverseSearcher
<'a
>
1894 let mut matcher
= pat
.into_searcher(self);
1895 if let Some((_
, b
)) = matcher
.next_reject_back() {
1899 // Searcher is known to return valid indices
1900 self.slice_unchecked(0, j
)
1905 fn is_char_boundary(&self, index
: usize) -> bool
{
1906 // 0 and len are always ok.
1907 // Test for 0 explicitly so that it can optimize out the check
1908 // easily and skip reading string data for that case.
1909 if index
== 0 || index
== self.len() { return true; }
1910 match self.as_bytes().get(index
) {
1912 // This is bit magic equivalent to: b < 128 || b >= 192
1913 Some(&b
) => (b
as i8) >= -0x40,
1918 fn as_bytes(&self) -> &[u8] {
1919 unsafe { mem::transmute(self) }
1922 fn find
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> Option
<usize> {
1923 pat
.into_searcher(self).next_match().map(|(i
, _
)| i
)
1926 fn rfind
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> Option
<usize>
1927 where P
::Searcher
: ReverseSearcher
<'a
>
1929 pat
.into_searcher(self).next_match_back().map(|(i
, _
)| i
)
1932 fn find_str
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> Option
<usize> {
1937 fn split_at(&self, mid
: usize) -> (&str, &str) {
1938 // is_char_boundary checks that the index is in [0, .len()]
1939 if self.is_char_boundary(mid
) {
1941 (self.slice_unchecked(0, mid
),
1942 self.slice_unchecked(mid
, self.len()))
1945 slice_error_fail(self, 0, mid
)
1949 fn split_at_mut(&mut self, mid
: usize) -> (&mut str, &mut str) {
1950 // is_char_boundary checks that the index is in [0, .len()]
1951 if self.is_char_boundary(mid
) {
1952 let len
= self.len();
1953 let ptr
= self.as_ptr() as *mut u8;
1955 (from_raw_parts_mut(ptr
, mid
),
1956 from_raw_parts_mut(ptr
.offset(mid
as isize), len
- mid
))
1959 slice_error_fail(self, 0, mid
)
1964 fn as_ptr(&self) -> *const u8 {
1965 self as *const str as *const u8
1969 fn len(&self) -> usize {
1970 self.as_bytes().len()
1974 fn is_empty(&self) -> bool { self.len() == 0 }
1977 fn parse
<T
: FromStr
>(&self) -> Result
<T
, T
::Err
> { FromStr::from_str(self) }
1980 #[stable(feature = "rust1", since = "1.0.0")]
1981 impl AsRef
<[u8]> for str {
1983 fn as_ref(&self) -> &[u8] {
1988 #[stable(feature = "rust1", since = "1.0.0")]
1989 impl<'a
> Default
for &'a
str {
1990 /// Creates an empty str
1991 fn default() -> &'a
str { "" }