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}
;
20 use char::{self, CharExt}
;
26 use iter
::ExactSizeIterator
;
27 use iter
::{Map, Cloned, Iterator, DoubleEndedIterator}
;
30 use ops
::{Fn, FnMut, FnOnce}
;
31 use option
::Option
::{self, None, Some}
;
32 use result
::Result
::{self, Ok, Err}
;
33 use slice
::{self, SliceExt}
;
37 /// A trait to abstract the idea of creating a new instance of a type from a
40 /// `FromStr`'s [`from_str()`] method is often used implicitly, through
41 /// [`str`]'s [`parse()`] method. See [`parse()`]'s documentation for examples.
43 /// [`from_str()`]: #tymethod.from_str
44 /// [`str`]: ../../std/primitive.str.html
45 /// [`parse()`]: ../../std/primitive.str.html#method.parse
46 #[stable(feature = "rust1", since = "1.0.0")]
47 pub trait FromStr
: Sized
{
48 /// The associated error which can be returned from parsing.
49 #[stable(feature = "rust1", since = "1.0.0")]
52 /// Parses a string `s` to return a value of this type.
54 /// If parsing succeeds, return the value inside `Ok`, otherwise
55 /// when the string is ill-formatted return an error specific to the
56 /// inside `Err`. The error type is specific to implementation of the trait.
60 /// Basic usage with [`i32`][ithirtytwo], a type that implements `FromStr`:
62 /// [ithirtytwo]: ../../std/primitive.i32.html
65 /// use std::str::FromStr;
68 /// let x = i32::from_str(s).unwrap();
72 #[stable(feature = "rust1", since = "1.0.0")]
73 fn from_str(s
: &str) -> Result
<Self, Self::Err
>;
76 #[stable(feature = "rust1", since = "1.0.0")]
77 impl FromStr
for bool
{
78 type Err
= ParseBoolError
;
80 /// Parse a `bool` from a string.
82 /// Yields a `Result<bool, ParseBoolError>`, because `s` may or may not
83 /// actually be parseable.
88 /// use std::str::FromStr;
90 /// assert_eq!(FromStr::from_str("true"), Ok(true));
91 /// assert_eq!(FromStr::from_str("false"), Ok(false));
92 /// assert!(<bool as FromStr>::from_str("not even a boolean").is_err());
95 /// Note, in many cases, the `.parse()` method on `str` is more proper.
98 /// assert_eq!("true".parse(), Ok(true));
99 /// assert_eq!("false".parse(), Ok(false));
100 /// assert!("not even a boolean".parse::<bool>().is_err());
103 fn from_str(s
: &str) -> Result
<bool
, ParseBoolError
> {
106 "false" => Ok(false),
107 _
=> Err(ParseBoolError { _priv: () }
),
112 /// An error returned when parsing a `bool` from a string fails.
113 #[derive(Debug, Clone, PartialEq)]
114 #[stable(feature = "rust1", since = "1.0.0")]
115 pub struct ParseBoolError { _priv: () }
117 #[stable(feature = "rust1", since = "1.0.0")]
118 impl fmt
::Display
for ParseBoolError
{
119 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
120 "provided string was not `true` or `false`".fmt(f
)
125 Section: Creating a string
128 /// Errors which can occur when attempting to interpret a sequence of `u8`
131 /// As such, the `from_utf8` family of functions and methods for both `String`s
132 /// and `&str`s make use of this error, for example.
133 #[derive(Copy, Eq, PartialEq, Clone, Debug)]
134 #[stable(feature = "rust1", since = "1.0.0")]
135 pub struct Utf8Error
{
140 /// Returns the index in the given string up to which valid UTF-8 was
143 /// It is the maximum index such that `from_utf8(input[..index])`
144 /// would return `Some(_)`.
153 /// // some invalid bytes, in a vector
154 /// let sparkle_heart = vec![0, 159, 146, 150];
156 /// // std::str::from_utf8 returns a Utf8Error
157 /// let error = str::from_utf8(&sparkle_heart).unwrap_err();
159 /// // the second byte is invalid here
160 /// assert_eq!(1, error.valid_up_to());
162 #[stable(feature = "utf8_error", since = "1.5.0")]
163 pub fn valid_up_to(&self) -> usize { self.valid_up_to }
166 /// Converts a slice of bytes to a string slice.
168 /// A string slice (`&str`) is made of bytes (`u8`), and a byte slice (`&[u8]`)
169 /// is made of bytes, so this function converts between the two. Not all byte
170 /// slices are valid string slices, however: `&str` requires that it is valid
171 /// UTF-8. `from_utf8()` checks to ensure that the bytes are valid UTF-8, and
172 /// then does the conversion.
174 /// If you are sure that the byte slice is valid UTF-8, and you don't want to
175 /// incur the overhead of the validity check, there is an unsafe version of
176 /// this function, [`from_utf8_unchecked()`][fromutf8u], which has the same
177 /// behavior but skips the check.
179 /// [fromutf8u]: fn.from_utf8_unchecked.html
181 /// If you need a `String` instead of a `&str`, consider
182 /// [`String::from_utf8()`][string].
184 /// [string]: ../../std/string/struct.String.html#method.from_utf8
186 /// Because you can stack-allocate a `[u8; N]`, and you can take a `&[u8]` of
187 /// it, this function is one way to have a stack-allocated string. There is
188 /// an example of this in the examples section below.
192 /// Returns `Err` if the slice is not UTF-8 with a description as to why the
193 /// provided slice is not UTF-8.
202 /// // some bytes, in a vector
203 /// let sparkle_heart = vec![240, 159, 146, 150];
205 /// // We know these bytes are valid, so just use `unwrap()`.
206 /// let sparkle_heart = str::from_utf8(&sparkle_heart).unwrap();
208 /// assert_eq!("💖", sparkle_heart);
216 /// // some invalid bytes, in a vector
217 /// let sparkle_heart = vec![0, 159, 146, 150];
219 /// assert!(str::from_utf8(&sparkle_heart).is_err());
222 /// See the docs for [`Utf8Error`][error] for more details on the kinds of
223 /// errors that can be returned.
225 /// [error]: struct.Utf8Error.html
227 /// A "stack allocated string":
232 /// // some bytes, in a stack-allocated array
233 /// let sparkle_heart = [240, 159, 146, 150];
235 /// // We know these bytes are valid, so just use `unwrap()`.
236 /// let sparkle_heart = str::from_utf8(&sparkle_heart).unwrap();
238 /// assert_eq!("💖", sparkle_heart);
240 #[stable(feature = "rust1", since = "1.0.0")]
241 pub fn from_utf8(v
: &[u8]) -> Result
<&str, Utf8Error
> {
242 run_utf8_validation(v
)?
;
243 Ok(unsafe { from_utf8_unchecked(v) }
)
246 /// Forms a str from a pointer and a length.
248 /// The `len` argument is the number of bytes in the string.
252 /// This function is unsafe as there is no guarantee that the given pointer is
253 /// valid for `len` bytes, nor whether the lifetime inferred is a suitable
254 /// lifetime for the returned str.
256 /// The data must be valid UTF-8
258 /// `p` must be non-null, even for zero-length str.
262 /// The lifetime for the returned str is inferred from its usage. To
263 /// prevent accidental misuse, it's suggested to tie the lifetime to whichever
264 /// source lifetime is safe in the context, such as by providing a helper
265 /// function taking the lifetime of a host value for the str, or by explicit
267 /// Performs the same functionality as `from_raw_parts`, except that a mutable
270 unsafe fn from_raw_parts_mut
<'a
>(p
: *mut u8, len
: usize) -> &'a
mut str {
271 mem
::transmute
::<&mut [u8], &mut str>(slice
::from_raw_parts_mut(p
, len
))
274 /// Converts a slice of bytes to a string slice without checking
275 /// that the string contains valid UTF-8.
277 /// See the safe version, [`from_utf8()`][fromutf8], for more information.
279 /// [fromutf8]: fn.from_utf8.html
283 /// This function is unsafe because it does not check that the bytes passed to
284 /// it are valid UTF-8. If this constraint is violated, undefined behavior
285 /// results, as the rest of Rust assumes that `&str`s are valid UTF-8.
294 /// // some bytes, in a vector
295 /// let sparkle_heart = vec![240, 159, 146, 150];
297 /// let sparkle_heart = unsafe {
298 /// str::from_utf8_unchecked(&sparkle_heart)
301 /// assert_eq!("💖", sparkle_heart);
304 #[stable(feature = "rust1", since = "1.0.0")]
305 pub unsafe fn from_utf8_unchecked(v
: &[u8]) -> &str {
309 #[stable(feature = "rust1", since = "1.0.0")]
310 impl fmt
::Display
for Utf8Error
{
311 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
312 write
!(f
, "invalid utf-8: invalid byte near index {}", self.valid_up_to
)
320 /// Iterator for the char (representing *Unicode Scalar Values*) of a string
322 /// Created with the method [`chars()`].
324 /// [`chars()`]: ../../std/primitive.str.html#method.chars
325 #[derive(Clone, Debug)]
326 #[stable(feature = "rust1", since = "1.0.0")]
327 pub struct Chars
<'a
> {
328 iter
: slice
::Iter
<'a
, u8>
331 /// Return the initial codepoint accumulator for the first byte.
332 /// The first byte is special, only want bottom 5 bits for width 2, 4 bits
333 /// for width 3, and 3 bits for width 4.
335 fn utf8_first_byte(byte
: u8, width
: u32) -> u32 { (byte & (0x7F >> width)) as u32 }
337 /// Return the value of `ch` updated with continuation byte `byte`.
339 fn utf8_acc_cont_byte(ch
: u32, byte
: u8) -> u32 { (ch << 6) | (byte & CONT_MASK) as u32 }
341 /// Checks whether the byte is a UTF-8 continuation byte (i.e. starts with the
344 fn utf8_is_cont_byte(byte
: u8) -> bool { (byte & !CONT_MASK) == TAG_CONT_U8 }
347 fn unwrap_or_0(opt
: Option
<&u8>) -> u8 {
354 /// Reads the next code point out of a byte iterator (assuming a
355 /// UTF-8-like encoding).
356 #[unstable(feature = "str_internals", issue = "0")]
358 pub fn next_code_point(bytes
: &mut slice
::Iter
<u8>) -> Option
<u32> {
360 let x
= match bytes
.next() {
362 Some(&next_byte
) if next_byte
< 128 => return Some(next_byte
as u32),
363 Some(&next_byte
) => next_byte
,
366 // Multibyte case follows
367 // Decode from a byte combination out of: [[[x y] z] w]
368 // NOTE: Performance is sensitive to the exact formulation here
369 let init
= utf8_first_byte(x
, 2);
370 let y
= unwrap_or_0(bytes
.next());
371 let mut ch
= utf8_acc_cont_byte(init
, y
);
374 // 5th bit in 0xE0 .. 0xEF is always clear, so `init` is still valid
375 let z
= unwrap_or_0(bytes
.next());
376 let y_z
= utf8_acc_cont_byte((y
& CONT_MASK
) as u32, z
);
377 ch
= init
<< 12 | y_z
;
380 // use only the lower 3 bits of `init`
381 let w
= unwrap_or_0(bytes
.next());
382 ch
= (init
& 7) << 18 | utf8_acc_cont_byte(y_z
, w
);
389 /// Reads the last code point out of a byte iterator (assuming a
390 /// UTF-8-like encoding).
392 fn next_code_point_reverse(bytes
: &mut slice
::Iter
<u8>) -> Option
<u32> {
394 let w
= match bytes
.next_back() {
396 Some(&next_byte
) if next_byte
< 128 => return Some(next_byte
as u32),
397 Some(&back_byte
) => back_byte
,
400 // Multibyte case follows
401 // Decode from a byte combination out of: [x [y [z w]]]
403 let z
= unwrap_or_0(bytes
.next_back());
404 ch
= utf8_first_byte(z
, 2);
405 if utf8_is_cont_byte(z
) {
406 let y
= unwrap_or_0(bytes
.next_back());
407 ch
= utf8_first_byte(y
, 3);
408 if utf8_is_cont_byte(y
) {
409 let x
= unwrap_or_0(bytes
.next_back());
410 ch
= utf8_first_byte(x
, 4);
411 ch
= utf8_acc_cont_byte(ch
, y
);
413 ch
= utf8_acc_cont_byte(ch
, z
);
415 ch
= utf8_acc_cont_byte(ch
, w
);
420 #[stable(feature = "rust1", since = "1.0.0")]
421 impl<'a
> Iterator
for Chars
<'a
> {
425 fn next(&mut self) -> Option
<char> {
426 next_code_point(&mut self.iter
).map(|ch
| {
427 // str invariant says `ch` is a valid Unicode Scalar Value
429 char::from_u32_unchecked(ch
)
435 fn size_hint(&self) -> (usize, Option
<usize>) {
436 let (len
, _
) = self.iter
.size_hint();
437 // `(len + 3)` can't overflow, because we know that the `slice::Iter`
438 // belongs to a slice in memory which has a maximum length of
439 // `isize::MAX` (that's well below `usize::MAX`).
440 ((len
+ 3) / 4, Some(len
))
444 #[stable(feature = "rust1", since = "1.0.0")]
445 impl<'a
> DoubleEndedIterator
for Chars
<'a
> {
447 fn next_back(&mut self) -> Option
<char> {
448 next_code_point_reverse(&mut self.iter
).map(|ch
| {
449 // str invariant says `ch` is a valid Unicode Scalar Value
451 char::from_u32_unchecked(ch
)
458 /// View the underlying data as a subslice of the original data.
460 /// This has the same lifetime as the original slice, and so the
461 /// iterator can continue to be used while this exists.
462 #[stable(feature = "iter_to_slice", since = "1.4.0")]
464 pub fn as_str(&self) -> &'a
str {
465 unsafe { from_utf8_unchecked(self.iter.as_slice()) }
469 /// Iterator for a string's characters and their byte offsets.
470 #[derive(Clone, Debug)]
471 #[stable(feature = "rust1", since = "1.0.0")]
472 pub struct CharIndices
<'a
> {
477 #[stable(feature = "rust1", since = "1.0.0")]
478 impl<'a
> Iterator
for CharIndices
<'a
> {
479 type Item
= (usize, char);
482 fn next(&mut self) -> Option
<(usize, char)> {
483 let (pre_len
, _
) = self.iter
.iter
.size_hint();
484 match self.iter
.next() {
487 let index
= self.front_offset
;
488 let (len
, _
) = self.iter
.iter
.size_hint();
489 self.front_offset
+= pre_len
- len
;
496 fn size_hint(&self) -> (usize, Option
<usize>) {
497 self.iter
.size_hint()
501 #[stable(feature = "rust1", since = "1.0.0")]
502 impl<'a
> DoubleEndedIterator
for CharIndices
<'a
> {
504 fn next_back(&mut self) -> Option
<(usize, char)> {
505 match self.iter
.next_back() {
508 let (len
, _
) = self.iter
.iter
.size_hint();
509 let index
= self.front_offset
+ len
;
516 impl<'a
> CharIndices
<'a
> {
517 /// View the underlying data as a subslice of the original data.
519 /// This has the same lifetime as the original slice, and so the
520 /// iterator can continue to be used while this exists.
521 #[stable(feature = "iter_to_slice", since = "1.4.0")]
523 pub fn as_str(&self) -> &'a
str {
528 /// External iterator for a string's bytes.
529 /// Use with the `std::iter` module.
531 /// Created with the method [`bytes()`].
533 /// [`bytes()`]: ../../std/primitive.str.html#method.bytes
534 #[stable(feature = "rust1", since = "1.0.0")]
535 #[derive(Clone, Debug)]
536 pub struct Bytes
<'a
>(Cloned
<slice
::Iter
<'a
, u8>>);
538 #[stable(feature = "rust1", since = "1.0.0")]
539 impl<'a
> Iterator
for Bytes
<'a
> {
543 fn next(&mut self) -> Option
<u8> {
548 fn size_hint(&self) -> (usize, Option
<usize>) {
553 fn count(self) -> usize {
558 fn last(self) -> Option
<Self::Item
> {
563 fn nth(&mut self, n
: usize) -> Option
<Self::Item
> {
568 #[stable(feature = "rust1", since = "1.0.0")]
569 impl<'a
> DoubleEndedIterator
for Bytes
<'a
> {
571 fn next_back(&mut self) -> Option
<u8> {
576 #[stable(feature = "rust1", since = "1.0.0")]
577 impl<'a
> ExactSizeIterator
for Bytes
<'a
> {
579 fn len(&self) -> usize {
584 /// This macro generates a Clone impl for string pattern API
585 /// wrapper types of the form X<'a, P>
586 macro_rules
! derive_pattern_clone
{
587 (clone $t
:ident with
|$s
:ident
| $e
:expr
) => {
588 impl<'a
, P
: Pattern
<'a
>> Clone
for $t
<'a
, P
>
589 where P
::Searcher
: Clone
591 fn clone(&self) -> Self {
599 /// This macro generates two public iterator structs
600 /// wrapping a private internal one that makes use of the `Pattern` API.
602 /// For all patterns `P: Pattern<'a>` the following items will be
603 /// generated (generics omitted):
605 /// struct $forward_iterator($internal_iterator);
606 /// struct $reverse_iterator($internal_iterator);
608 /// impl Iterator for $forward_iterator
609 /// { /* internal ends up calling Searcher::next_match() */ }
611 /// impl DoubleEndedIterator for $forward_iterator
612 /// where P::Searcher: DoubleEndedSearcher
613 /// { /* internal ends up calling Searcher::next_match_back() */ }
615 /// impl Iterator for $reverse_iterator
616 /// where P::Searcher: ReverseSearcher
617 /// { /* internal ends up calling Searcher::next_match_back() */ }
619 /// impl DoubleEndedIterator for $reverse_iterator
620 /// where P::Searcher: DoubleEndedSearcher
621 /// { /* internal ends up calling Searcher::next_match() */ }
623 /// The internal one is defined outside the macro, and has almost the same
624 /// semantic as a DoubleEndedIterator by delegating to `pattern::Searcher` and
625 /// `pattern::ReverseSearcher` for both forward and reverse iteration.
627 /// "Almost", because a `Searcher` and a `ReverseSearcher` for a given
628 /// `Pattern` might not return the same elements, so actually implementing
629 /// `DoubleEndedIterator` for it would be incorrect.
630 /// (See the docs in `str::pattern` for more details)
632 /// However, the internal struct still represents a single ended iterator from
633 /// either end, and depending on pattern is also a valid double ended iterator,
634 /// so the two wrapper structs implement `Iterator`
635 /// and `DoubleEndedIterator` depending on the concrete pattern type, leading
636 /// to the complex impls seen above.
637 macro_rules
! generate_pattern_iterators
{
641 $
(#[$forward_iterator_attribute:meta])*
642 struct $forward_iterator
:ident
;
646 $
(#[$reverse_iterator_attribute:meta])*
647 struct $reverse_iterator
:ident
;
649 // Stability of all generated items
651 $
(#[$common_stability_attribute:meta])*
653 // Internal almost-iterator that is being delegated to
655 $internal_iterator
:ident
yielding ($iterty
:ty
);
657 // Kind of delgation - either single ended or double ended
660 $
(#[$forward_iterator_attribute])*
661 $
(#[$common_stability_attribute])*
662 pub struct $forward_iterator
<'a
, P
: Pattern
<'a
>>($internal_iterator
<'a
, P
>);
664 $
(#[$common_stability_attribute])*
665 impl<'a
, P
: Pattern
<'a
>> fmt
::Debug
for $forward_iterator
<'a
, P
>
666 where P
::Searcher
: fmt
::Debug
668 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
669 f
.debug_tuple(stringify
!($forward_iterator
))
675 $
(#[$common_stability_attribute])*
676 impl<'a
, P
: Pattern
<'a
>> Iterator
for $forward_iterator
<'a
, P
> {
680 fn next(&mut self) -> Option
<$iterty
> {
685 $
(#[$common_stability_attribute])*
686 impl<'a
, P
: Pattern
<'a
>> Clone
for $forward_iterator
<'a
, P
>
687 where P
::Searcher
: Clone
689 fn clone(&self) -> Self {
690 $
forward_iterator(self.0.clone())
694 $
(#[$reverse_iterator_attribute])*
695 $
(#[$common_stability_attribute])*
696 pub struct $reverse_iterator
<'a
, P
: Pattern
<'a
>>($internal_iterator
<'a
, P
>);
698 $
(#[$common_stability_attribute])*
699 impl<'a
, P
: Pattern
<'a
>> fmt
::Debug
for $reverse_iterator
<'a
, P
>
700 where P
::Searcher
: fmt
::Debug
702 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
703 f
.debug_tuple(stringify
!($reverse_iterator
))
709 $
(#[$common_stability_attribute])*
710 impl<'a
, P
: Pattern
<'a
>> Iterator
for $reverse_iterator
<'a
, P
>
711 where P
::Searcher
: ReverseSearcher
<'a
>
716 fn next(&mut self) -> Option
<$iterty
> {
721 $
(#[$common_stability_attribute])*
722 impl<'a
, P
: Pattern
<'a
>> Clone
for $reverse_iterator
<'a
, P
>
723 where P
::Searcher
: Clone
725 fn clone(&self) -> Self {
726 $
reverse_iterator(self.0.clone())
730 generate_pattern_iterators
!($
($t
)* with $
(#[$common_stability_attribute])*,
732 $reverse_iterator
, $iterty
);
735 double ended
; with $
(#[$common_stability_attribute:meta])*,
736 $forward_iterator
:ident
,
737 $reverse_iterator
:ident
, $iterty
:ty
739 $
(#[$common_stability_attribute])*
740 impl<'a
, P
: Pattern
<'a
>> DoubleEndedIterator
for $forward_iterator
<'a
, P
>
741 where P
::Searcher
: DoubleEndedSearcher
<'a
>
744 fn next_back(&mut self) -> Option
<$iterty
> {
749 $
(#[$common_stability_attribute])*
750 impl<'a
, P
: Pattern
<'a
>> DoubleEndedIterator
for $reverse_iterator
<'a
, P
>
751 where P
::Searcher
: DoubleEndedSearcher
<'a
>
754 fn next_back(&mut self) -> Option
<$iterty
> {
760 single ended
; with $
(#[$common_stability_attribute:meta])*,
761 $forward_iterator
:ident
,
762 $reverse_iterator
:ident
, $iterty
:ty
766 derive_pattern_clone
!{
768 with
|s
| SplitInternal { matcher: s.matcher.clone(), ..*s }
771 struct SplitInternal
<'a
, P
: Pattern
<'a
>> {
774 matcher
: P
::Searcher
,
775 allow_trailing_empty
: bool
,
779 impl<'a
, P
: Pattern
<'a
>> fmt
::Debug
for SplitInternal
<'a
, P
> where P
::Searcher
: fmt
::Debug
{
780 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
781 f
.debug_struct("SplitInternal")
782 .field("start", &self.start
)
783 .field("end", &self.end
)
784 .field("matcher", &self.matcher
)
785 .field("allow_trailing_empty", &self.allow_trailing_empty
)
786 .field("finished", &self.finished
)
791 impl<'a
, P
: Pattern
<'a
>> SplitInternal
<'a
, P
> {
793 fn get_end(&mut self) -> Option
<&'a
str> {
794 if !self.finished
&& (self.allow_trailing_empty
|| self.end
- self.start
> 0) {
795 self.finished
= true;
797 let string
= self.matcher
.haystack().slice_unchecked(self.start
, self.end
);
806 fn next(&mut self) -> Option
<&'a
str> {
807 if self.finished { return None }
809 let haystack
= self.matcher
.haystack();
810 match self.matcher
.next_match() {
811 Some((a
, b
)) => unsafe {
812 let elt
= haystack
.slice_unchecked(self.start
, a
);
816 None
=> self.get_end(),
821 fn next_back(&mut self) -> Option
<&'a
str>
822 where P
::Searcher
: ReverseSearcher
<'a
>
824 if self.finished { return None }
826 if !self.allow_trailing_empty
{
827 self.allow_trailing_empty
= true;
828 match self.next_back() {
829 Some(elt
) if !elt
.is_empty() => return Some(elt
),
830 _
=> if self.finished { return None }
834 let haystack
= self.matcher
.haystack();
835 match self.matcher
.next_match_back() {
836 Some((a
, b
)) => unsafe {
837 let elt
= haystack
.slice_unchecked(b
, self.end
);
842 self.finished
= true;
843 Some(haystack
.slice_unchecked(self.start
, self.end
))
849 generate_pattern_iterators
! {
851 /// Created with the method [`split()`].
853 /// [`split()`]: ../../std/primitive.str.html#method.split
856 /// Created with the method [`rsplit()`].
858 /// [`rsplit()`]: ../../std/primitive.str.html#method.rsplit
861 #[stable(feature = "rust1", since = "1.0.0")]
863 SplitInternal
yielding (&'a
str);
864 delegate double ended
;
867 generate_pattern_iterators
! {
869 /// Created with the method [`split_terminator()`].
871 /// [`split_terminator()`]: ../../std/primitive.str.html#method.split_terminator
872 struct SplitTerminator
;
874 /// Created with the method [`rsplit_terminator()`].
876 /// [`rsplit_terminator()`]: ../../std/primitive.str.html#method.rsplit_terminator
877 struct RSplitTerminator
;
879 #[stable(feature = "rust1", since = "1.0.0")]
881 SplitInternal
yielding (&'a
str);
882 delegate double ended
;
885 derive_pattern_clone
!{
887 with
|s
| SplitNInternal { iter: s.iter.clone(), ..*s }
890 struct SplitNInternal
<'a
, P
: Pattern
<'a
>> {
891 iter
: SplitInternal
<'a
, P
>,
892 /// The number of splits remaining
896 impl<'a
, P
: Pattern
<'a
>> fmt
::Debug
for SplitNInternal
<'a
, P
> where P
::Searcher
: fmt
::Debug
{
897 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
898 f
.debug_struct("SplitNInternal")
899 .field("iter", &self.iter
)
900 .field("count", &self.count
)
905 impl<'a
, P
: Pattern
<'a
>> SplitNInternal
<'a
, P
> {
907 fn next(&mut self) -> Option
<&'a
str> {
910 1 => { self.count = 0; self.iter.get_end() }
911 _
=> { self.count -= 1; self.iter.next() }
916 fn next_back(&mut self) -> Option
<&'a
str>
917 where P
::Searcher
: ReverseSearcher
<'a
>
921 1 => { self.count = 0; self.iter.get_end() }
922 _
=> { self.count -= 1; self.iter.next_back() }
927 generate_pattern_iterators
! {
929 /// Created with the method [`splitn()`].
931 /// [`splitn()`]: ../../std/primitive.str.html#method.splitn
934 /// Created with the method [`rsplitn()`].
936 /// [`rsplitn()`]: ../../std/primitive.str.html#method.rsplitn
939 #[stable(feature = "rust1", since = "1.0.0")]
941 SplitNInternal
yielding (&'a
str);
942 delegate single ended
;
945 derive_pattern_clone
!{
946 clone MatchIndicesInternal
947 with
|s
| MatchIndicesInternal(s
.0.clone())
950 struct MatchIndicesInternal
<'a
, P
: Pattern
<'a
>>(P
::Searcher
);
952 impl<'a
, P
: Pattern
<'a
>> fmt
::Debug
for MatchIndicesInternal
<'a
, P
> where P
::Searcher
: fmt
::Debug
{
953 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
954 f
.debug_tuple("MatchIndicesInternal")
960 impl<'a
, P
: Pattern
<'a
>> MatchIndicesInternal
<'a
, P
> {
962 fn next(&mut self) -> Option
<(usize, &'a
str)> {
963 self.0.next_match().map(|(start
, end
)| unsafe {
964 (start
, self.0.haystack().slice_unchecked(start
, end
))
969 fn next_back(&mut self) -> Option
<(usize, &'a
str)>
970 where P
::Searcher
: ReverseSearcher
<'a
>
972 self.0.next_match_back().map(|(start
, end
)| unsafe {
973 (start
, self.0.haystack().slice_unchecked(start
, end
))
978 generate_pattern_iterators
! {
980 /// Created with the method [`match_indices()`].
982 /// [`match_indices()`]: ../../std/primitive.str.html#method.match_indices
985 /// Created with the method [`rmatch_indices()`].
987 /// [`rmatch_indices()`]: ../../std/primitive.str.html#method.rmatch_indices
988 struct RMatchIndices
;
990 #[stable(feature = "str_match_indices", since = "1.5.0")]
992 MatchIndicesInternal
yielding ((usize, &'a
str));
993 delegate double ended
;
996 derive_pattern_clone
!{
997 clone MatchesInternal
998 with
|s
| MatchesInternal(s
.0.clone())
1001 struct MatchesInternal
<'a
, P
: Pattern
<'a
>>(P
::Searcher
);
1003 impl<'a
, P
: Pattern
<'a
>> fmt
::Debug
for MatchesInternal
<'a
, P
> where P
::Searcher
: fmt
::Debug
{
1004 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
1005 f
.debug_tuple("MatchesInternal")
1011 impl<'a
, P
: Pattern
<'a
>> MatchesInternal
<'a
, P
> {
1013 fn next(&mut self) -> Option
<&'a
str> {
1014 self.0.next_match().map(|(a
, b
)| unsafe {
1015 // Indices are known to be on utf8 boundaries
1016 self.0.haystack().slice_unchecked(a
, b
)
1021 fn next_back(&mut self) -> Option
<&'a
str>
1022 where P
::Searcher
: ReverseSearcher
<'a
>
1024 self.0.next_match_back().map(|(a
, b
)| unsafe {
1025 // Indices are known to be on utf8 boundaries
1026 self.0.haystack().slice_unchecked(a
, b
)
1031 generate_pattern_iterators
! {
1033 /// Created with the method [`matches()`].
1035 /// [`matches()`]: ../../std/primitive.str.html#method.matches
1038 /// Created with the method [`rmatches()`].
1040 /// [`rmatches()`]: ../../std/primitive.str.html#method.rmatches
1043 #[stable(feature = "str_matches", since = "1.2.0")]
1045 MatchesInternal
yielding (&'a
str);
1046 delegate double ended
;
1049 /// Created with the method [`lines()`].
1051 /// [`lines()`]: ../../std/primitive.str.html#method.lines
1052 #[stable(feature = "rust1", since = "1.0.0")]
1053 #[derive(Clone, Debug)]
1054 pub struct Lines
<'a
>(Map
<SplitTerminator
<'a
, char>, LinesAnyMap
>);
1056 #[stable(feature = "rust1", since = "1.0.0")]
1057 impl<'a
> Iterator
for Lines
<'a
> {
1058 type Item
= &'a
str;
1061 fn next(&mut self) -> Option
<&'a
str> {
1066 fn size_hint(&self) -> (usize, Option
<usize>) {
1071 #[stable(feature = "rust1", since = "1.0.0")]
1072 impl<'a
> DoubleEndedIterator
for Lines
<'a
> {
1074 fn next_back(&mut self) -> Option
<&'a
str> {
1079 /// Created with the method [`lines_any()`].
1081 /// [`lines_any()`]: ../../std/primitive.str.html#method.lines_any
1082 #[stable(feature = "rust1", since = "1.0.0")]
1083 #[rustc_deprecated(since = "1.4.0", reason = "use lines()/Lines instead now")]
1084 #[derive(Clone, Debug)]
1085 #[allow(deprecated)]
1086 pub struct LinesAny
<'a
>(Lines
<'a
>);
1088 /// A nameable, cloneable fn type
1092 impl<'a
> Fn
<(&'a
str,)> for LinesAnyMap
{
1094 extern "rust-call" fn call(&self, (line
,): (&'a
str,)) -> &'a
str {
1096 if l
> 0 && line
.as_bytes()[l
- 1] == b'
\r' { &line[0 .. l - 1] }
1101 impl<'a
> FnMut
<(&'a
str,)> for LinesAnyMap
{
1103 extern "rust-call" fn call_mut(&mut self, (line
,): (&'a
str,)) -> &'a
str {
1104 Fn
::call(&*self, (line
,))
1108 impl<'a
> FnOnce
<(&'a
str,)> for LinesAnyMap
{
1109 type Output
= &'a
str;
1112 extern "rust-call" fn call_once(self, (line
,): (&'a
str,)) -> &'a
str {
1113 Fn
::call(&self, (line
,))
1117 #[stable(feature = "rust1", since = "1.0.0")]
1118 #[allow(deprecated)]
1119 impl<'a
> Iterator
for LinesAny
<'a
> {
1120 type Item
= &'a
str;
1123 fn next(&mut self) -> Option
<&'a
str> {
1128 fn size_hint(&self) -> (usize, Option
<usize>) {
1133 #[stable(feature = "rust1", since = "1.0.0")]
1134 #[allow(deprecated)]
1135 impl<'a
> DoubleEndedIterator
for LinesAny
<'a
> {
1137 fn next_back(&mut self) -> Option
<&'a
str> {
1143 Section: Comparing strings
1146 /// Bytewise slice equality
1147 /// NOTE: This function is (ab)used in rustc::middle::trans::_match
1148 /// to compare &[u8] byte slices that are not necessarily valid UTF-8.
1151 fn eq_slice(a
: &str, b
: &str) -> bool
{
1152 a
.as_bytes() == b
.as_bytes()
1156 Section: UTF-8 validation
1159 // use truncation to fit u64 into usize
1160 const NONASCII_MASK
: usize = 0x80808080_80808080u64 as usize;
1162 /// Return `true` if any byte in the word `x` is nonascii (>= 128).
1164 fn contains_nonascii(x
: usize) -> bool
{
1165 (x
& NONASCII_MASK
) != 0
1168 /// Walk through `iter` checking that it's a valid UTF-8 sequence,
1169 /// returning `true` in that case, or, if it is invalid, `false` with
1170 /// `iter` reset such that it is pointing at the first byte in the
1171 /// invalid sequence.
1173 fn run_utf8_validation(v
: &[u8]) -> Result
<(), Utf8Error
> {
1176 while offset
< len
{
1177 let old_offset
= offset
;
1178 macro_rules
! err
{ () => {{
1179 return Err(Utf8Error
{
1180 valid_up_to
: old_offset
1184 macro_rules
! next
{ () => {{
1186 // we needed data, but there was none: error!
1193 let first
= v
[offset
];
1195 let w
= UTF8_CHAR_WIDTH
[first
as usize];
1196 let second
= next
!();
1197 // 2-byte encoding is for codepoints \u{0080} to \u{07ff}
1198 // first C2 80 last DF BF
1199 // 3-byte encoding is for codepoints \u{0800} to \u{ffff}
1200 // first E0 A0 80 last EF BF BF
1201 // excluding surrogates codepoints \u{d800} to \u{dfff}
1202 // ED A0 80 to ED BF BF
1203 // 4-byte encoding is for codepoints \u{1000}0 to \u{10ff}ff
1204 // first F0 90 80 80 last F4 8F BF BF
1206 // Use the UTF-8 syntax from the RFC
1208 // https://tools.ietf.org/html/rfc3629
1210 // UTF8-2 = %xC2-DF UTF8-tail
1211 // UTF8-3 = %xE0 %xA0-BF UTF8-tail / %xE1-EC 2( UTF8-tail ) /
1212 // %xED %x80-9F UTF8-tail / %xEE-EF 2( UTF8-tail )
1213 // UTF8-4 = %xF0 %x90-BF 2( UTF8-tail ) / %xF1-F3 3( UTF8-tail ) /
1214 // %xF4 %x80-8F 2( UTF8-tail )
1216 2 => if second
& !CONT_MASK
!= TAG_CONT_U8 {err!()}
,
1218 match (first
, second
, next
!() & !CONT_MASK
) {
1219 (0xE0 , 0xA0 ... 0xBF, TAG_CONT_U8
) |
1220 (0xE1 ... 0xEC, 0x80 ... 0xBF, TAG_CONT_U8
) |
1221 (0xED , 0x80 ... 0x9F, TAG_CONT_U8
) |
1222 (0xEE ... 0xEF, 0x80 ... 0xBF, TAG_CONT_U8
) => {}
1227 match (first
, second
, next
!() & !CONT_MASK
, next
!() & !CONT_MASK
) {
1228 (0xF0 , 0x90 ... 0xBF, TAG_CONT_U8
, TAG_CONT_U8
) |
1229 (0xF1 ... 0xF3, 0x80 ... 0xBF, TAG_CONT_U8
, TAG_CONT_U8
) |
1230 (0xF4 , 0x80 ... 0x8F, TAG_CONT_U8
, TAG_CONT_U8
) => {}
1238 // Ascii case, try to skip forward quickly.
1239 // When the pointer is aligned, read 2 words of data per iteration
1240 // until we find a word containing a non-ascii byte.
1241 let usize_bytes
= mem
::size_of
::<usize>();
1242 let bytes_per_iteration
= 2 * usize_bytes
;
1243 let ptr
= v
.as_ptr();
1244 let align
= (ptr
as usize + offset
) & (usize_bytes
- 1);
1246 if len
>= bytes_per_iteration
{
1247 while offset
<= len
- bytes_per_iteration
{
1249 let u
= *(ptr
.offset(offset
as isize) as *const usize);
1250 let v
= *(ptr
.offset((offset
+ usize_bytes
) as isize) as *const usize);
1252 // break if there is a nonascii byte
1253 let zu
= contains_nonascii(u
);
1254 let zv
= contains_nonascii(v
);
1259 offset
+= bytes_per_iteration
;
1262 // step from the point where the wordwise loop stopped
1263 while offset
< len
&& v
[offset
] < 128 {
1275 // https://tools.ietf.org/html/rfc3629
1276 static UTF8_CHAR_WIDTH
: [u8; 256] = [
1277 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1278 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 0x1F
1279 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1280 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 0x3F
1281 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1282 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 0x5F
1283 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1284 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 0x7F
1285 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1286 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 0x9F
1287 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1288 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 0xBF
1289 0,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
1290 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, // 0xDF
1291 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3, // 0xEF
1292 4,4,4,4,4,0,0,0,0,0,0,0,0,0,0,0, // 0xFF
1295 /// Struct that contains a `char` and the index of the first byte of
1296 /// the next `char` in a string. This can be used as a data structure
1297 /// for iterating over the UTF-8 bytes of a string.
1298 #[derive(Copy, Clone, Debug)]
1299 #[unstable(feature = "str_char",
1300 reason
= "existence of this struct is uncertain as it is frequently \
1301 able to be replaced with char.len_utf8() and/or \
1302 char/char_indices iterators",
1304 pub struct CharRange
{
1307 /// Index of the first byte of the next `char`
1311 /// Mask of the value bits of a continuation byte
1312 const CONT_MASK
: u8 = 0b0011_1111;
1313 /// Value of the tag bits (tag mask is !CONT_MASK) of a continuation byte
1314 const TAG_CONT_U8
: u8 = 0b1000_0000;
1317 Section: Trait implementations
1321 use cmp
::{Ord, Ordering, PartialEq, PartialOrd, Eq}
;
1324 use option
::Option
::Some
;
1326 use str::{StrExt, eq_slice}
;
1328 #[stable(feature = "rust1", since = "1.0.0")]
1331 fn cmp(&self, other
: &str) -> Ordering
{
1332 self.as_bytes().cmp(other
.as_bytes())
1336 #[stable(feature = "rust1", since = "1.0.0")]
1337 impl PartialEq
for str {
1339 fn eq(&self, other
: &str) -> bool
{
1340 eq_slice(self, other
)
1343 fn ne(&self, other
: &str) -> bool { !(*self).eq(other) }
1346 #[stable(feature = "rust1", since = "1.0.0")]
1349 #[stable(feature = "rust1", since = "1.0.0")]
1350 impl PartialOrd
for str {
1352 fn partial_cmp(&self, other
: &str) -> Option
<Ordering
> {
1353 Some(self.cmp(other
))
1357 /// Implements substring slicing with syntax `&self[begin .. end]`.
1359 /// Returns a slice of the given string from the byte range
1360 /// [`begin`..`end`).
1362 /// This operation is `O(1)`.
1366 /// Panics if `begin` or `end` does not point to the starting
1367 /// byte offset of a character (as defined by `is_char_boundary`).
1368 /// Requires that `begin <= end` and `end <= len` where `len` is the
1369 /// length of the string.
1374 /// let s = "Löwe 老虎 Léopard";
1375 /// assert_eq!(&s[0 .. 1], "L");
1377 /// assert_eq!(&s[1 .. 9], "öwe 老");
1379 /// // these will panic:
1380 /// // byte 2 lies within `ö`:
1383 /// // byte 8 lies within `老`
1386 /// // byte 100 is outside the string
1387 /// // &s[3 .. 100];
1389 #[stable(feature = "rust1", since = "1.0.0")]
1390 impl ops
::Index
<ops
::Range
<usize>> for str {
1393 fn index(&self, index
: ops
::Range
<usize>) -> &str {
1394 // is_char_boundary checks that the index is in [0, .len()]
1395 if index
.start
<= index
.end
&&
1396 self.is_char_boundary(index
.start
) &&
1397 self.is_char_boundary(index
.end
) {
1398 unsafe { self.slice_unchecked(index.start, index.end) }
1400 super::slice_error_fail(self, index
.start
, index
.end
)
1405 /// Implements mutable substring slicing with syntax
1406 /// `&mut self[begin .. end]`.
1408 /// Returns a mutable slice of the given string from the byte range
1409 /// [`begin`..`end`).
1411 /// This operation is `O(1)`.
1415 /// Panics if `begin` or `end` does not point to the starting
1416 /// byte offset of a character (as defined by `is_char_boundary`).
1417 /// Requires that `begin <= end` and `end <= len` where `len` is the
1418 /// length of the string.
1419 #[stable(feature = "derefmut_for_string", since = "1.2.0")]
1420 impl ops
::IndexMut
<ops
::Range
<usize>> for str {
1422 fn index_mut(&mut self, index
: ops
::Range
<usize>) -> &mut str {
1423 // is_char_boundary checks that the index is in [0, .len()]
1424 if index
.start
<= index
.end
&&
1425 self.is_char_boundary(index
.start
) &&
1426 self.is_char_boundary(index
.end
) {
1427 unsafe { self.slice_mut_unchecked(index.start, index.end) }
1429 super::slice_error_fail(self, index
.start
, index
.end
)
1434 /// Implements substring slicing with syntax `&self[.. end]`.
1436 /// Returns a slice of the string from the beginning to byte offset
1439 /// Equivalent to `&self[0 .. end]`.
1440 #[stable(feature = "rust1", since = "1.0.0")]
1441 impl ops
::Index
<ops
::RangeTo
<usize>> for str {
1445 fn index(&self, index
: ops
::RangeTo
<usize>) -> &str {
1446 // is_char_boundary checks that the index is in [0, .len()]
1447 if self.is_char_boundary(index
.end
) {
1448 unsafe { self.slice_unchecked(0, index.end) }
1450 super::slice_error_fail(self, 0, index
.end
)
1455 /// Implements mutable substring slicing with syntax `&mut self[.. end]`.
1457 /// Returns a mutable slice of the string from the beginning to byte offset
1460 /// Equivalent to `&mut self[0 .. end]`.
1461 #[stable(feature = "derefmut_for_string", since = "1.2.0")]
1462 impl ops
::IndexMut
<ops
::RangeTo
<usize>> for str {
1464 fn index_mut(&mut self, index
: ops
::RangeTo
<usize>) -> &mut str {
1465 // is_char_boundary checks that the index is in [0, .len()]
1466 if self.is_char_boundary(index
.end
) {
1467 unsafe { self.slice_mut_unchecked(0, index.end) }
1469 super::slice_error_fail(self, 0, index
.end
)
1474 /// Implements substring slicing with syntax `&self[begin ..]`.
1476 /// Returns a slice of the string from byte offset `begin`
1477 /// to the end of the string.
1479 /// Equivalent to `&self[begin .. len]`.
1480 #[stable(feature = "rust1", since = "1.0.0")]
1481 impl ops
::Index
<ops
::RangeFrom
<usize>> for str {
1485 fn index(&self, index
: ops
::RangeFrom
<usize>) -> &str {
1486 // is_char_boundary checks that the index is in [0, .len()]
1487 if self.is_char_boundary(index
.start
) {
1488 unsafe { self.slice_unchecked(index.start, self.len()) }
1490 super::slice_error_fail(self, index
.start
, self.len())
1495 /// Implements mutable substring slicing with syntax `&mut self[begin ..]`.
1497 /// Returns a mutable slice of the string from byte offset `begin`
1498 /// to the end of the string.
1500 /// Equivalent to `&mut self[begin .. len]`.
1501 #[stable(feature = "derefmut_for_string", since = "1.2.0")]
1502 impl ops
::IndexMut
<ops
::RangeFrom
<usize>> for str {
1504 fn index_mut(&mut self, index
: ops
::RangeFrom
<usize>) -> &mut str {
1505 // is_char_boundary checks that the index is in [0, .len()]
1506 if self.is_char_boundary(index
.start
) {
1507 let len
= self.len();
1508 unsafe { self.slice_mut_unchecked(index.start, len) }
1510 super::slice_error_fail(self, index
.start
, self.len())
1515 /// Implements substring slicing with syntax `&self[..]`.
1517 /// Returns a slice of the whole string. This operation can
1520 /// Equivalent to `&self[0 .. len]`.
1521 #[stable(feature = "rust1", since = "1.0.0")]
1522 impl ops
::Index
<ops
::RangeFull
> for str {
1526 fn index(&self, _index
: ops
::RangeFull
) -> &str {
1531 /// Implements mutable substring slicing with syntax `&mut self[..]`.
1533 /// Returns a mutable slice of the whole string. This operation can
1536 /// Equivalent to `&mut self[0 .. len]`.
1537 #[stable(feature = "derefmut_for_string", since = "1.2.0")]
1538 impl ops
::IndexMut
<ops
::RangeFull
> for str {
1540 fn index_mut(&mut self, _index
: ops
::RangeFull
) -> &mut str {
1545 #[unstable(feature = "inclusive_range",
1546 reason
= "recently added, follows RFC",
1548 impl ops
::Index
<ops
::RangeInclusive
<usize>> for str {
1552 fn index(&self, index
: ops
::RangeInclusive
<usize>) -> &str {
1554 ops
::RangeInclusive
::Empty { .. }
=> "",
1555 ops
::RangeInclusive
::NonEmpty { end, .. }
if end
== usize::max_value() =>
1556 panic
!("attempted to index slice up to maximum usize"),
1557 ops
::RangeInclusive
::NonEmpty { start, end }
=>
1558 self.index(start
.. end
+1)
1562 #[unstable(feature = "inclusive_range",
1563 reason
= "recently added, follows RFC",
1565 impl ops
::Index
<ops
::RangeToInclusive
<usize>> for str {
1569 fn index(&self, index
: ops
::RangeToInclusive
<usize>) -> &str {
1570 self.index(0...index
.end
)
1574 #[unstable(feature = "inclusive_range",
1575 reason
= "recently added, follows RFC",
1577 impl ops
::IndexMut
<ops
::RangeInclusive
<usize>> for str {
1579 fn index_mut(&mut self, index
: ops
::RangeInclusive
<usize>) -> &mut str {
1581 ops
::RangeInclusive
::Empty { .. }
=> &mut self[0..0], // `&mut ""` doesn't work
1582 ops
::RangeInclusive
::NonEmpty { end, .. }
if end
== usize::max_value() =>
1583 panic
!("attempted to index str up to maximum usize"),
1584 ops
::RangeInclusive
::NonEmpty { start, end }
=>
1585 self.index_mut(start
.. end
+1)
1589 #[unstable(feature = "inclusive_range",
1590 reason
= "recently added, follows RFC",
1592 impl ops
::IndexMut
<ops
::RangeToInclusive
<usize>> for str {
1594 fn index_mut(&mut self, index
: ops
::RangeToInclusive
<usize>) -> &mut str {
1595 self.index_mut(0...index
.end
)
1600 /// Methods for string slices
1601 #[allow(missing_docs)]
1603 #[unstable(feature = "core_str_ext",
1604 reason
= "stable interface provided by `impl str` in later crates",
1607 // NB there are no docs here are they're all located on the StrExt trait in
1608 // libcollections, not here.
1610 #[stable(feature = "core", since = "1.6.0")]
1611 fn contains
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> bool
;
1612 #[stable(feature = "core", since = "1.6.0")]
1613 fn chars(&self) -> Chars
;
1614 #[stable(feature = "core", since = "1.6.0")]
1615 fn bytes(&self) -> Bytes
;
1616 #[stable(feature = "core", since = "1.6.0")]
1617 fn char_indices(&self) -> CharIndices
;
1618 #[stable(feature = "core", since = "1.6.0")]
1619 fn split
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> Split
<'a
, P
>;
1620 #[stable(feature = "core", since = "1.6.0")]
1621 fn rsplit
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> RSplit
<'a
, P
>
1622 where P
::Searcher
: ReverseSearcher
<'a
>;
1623 #[stable(feature = "core", since = "1.6.0")]
1624 fn splitn
<'a
, P
: Pattern
<'a
>>(&'a
self, count
: usize, pat
: P
) -> SplitN
<'a
, P
>;
1625 #[stable(feature = "core", since = "1.6.0")]
1626 fn rsplitn
<'a
, P
: Pattern
<'a
>>(&'a
self, count
: usize, pat
: P
) -> RSplitN
<'a
, P
>
1627 where P
::Searcher
: ReverseSearcher
<'a
>;
1628 #[stable(feature = "core", since = "1.6.0")]
1629 fn split_terminator
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> SplitTerminator
<'a
, P
>;
1630 #[stable(feature = "core", since = "1.6.0")]
1631 fn rsplit_terminator
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> RSplitTerminator
<'a
, P
>
1632 where P
::Searcher
: ReverseSearcher
<'a
>;
1633 #[stable(feature = "core", since = "1.6.0")]
1634 fn matches
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> Matches
<'a
, P
>;
1635 #[stable(feature = "core", since = "1.6.0")]
1636 fn rmatches
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> RMatches
<'a
, P
>
1637 where P
::Searcher
: ReverseSearcher
<'a
>;
1638 #[stable(feature = "core", since = "1.6.0")]
1639 fn match_indices
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> MatchIndices
<'a
, P
>;
1640 #[stable(feature = "core", since = "1.6.0")]
1641 fn rmatch_indices
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> RMatchIndices
<'a
, P
>
1642 where P
::Searcher
: ReverseSearcher
<'a
>;
1643 #[stable(feature = "core", since = "1.6.0")]
1644 fn lines(&self) -> Lines
;
1645 #[stable(feature = "core", since = "1.6.0")]
1646 #[rustc_deprecated(since = "1.6.0", reason = "use lines() instead now")]
1647 #[allow(deprecated)]
1648 fn lines_any(&self) -> LinesAny
;
1649 #[stable(feature = "core", since = "1.6.0")]
1650 unsafe fn slice_unchecked(&self, begin
: usize, end
: usize) -> &str;
1651 #[stable(feature = "core", since = "1.6.0")]
1652 unsafe fn slice_mut_unchecked(&mut self, begin
: usize, end
: usize) -> &mut str;
1653 #[stable(feature = "core", since = "1.6.0")]
1654 fn starts_with
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> bool
;
1655 #[stable(feature = "core", since = "1.6.0")]
1656 fn ends_with
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> bool
1657 where P
::Searcher
: ReverseSearcher
<'a
>;
1658 #[stable(feature = "core", since = "1.6.0")]
1659 fn trim_matches
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> &'a
str
1660 where P
::Searcher
: DoubleEndedSearcher
<'a
>;
1661 #[stable(feature = "core", since = "1.6.0")]
1662 fn trim_left_matches
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> &'a
str;
1663 #[stable(feature = "core", since = "1.6.0")]
1664 fn trim_right_matches
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> &'a
str
1665 where P
::Searcher
: ReverseSearcher
<'a
>;
1666 #[stable(feature = "is_char_boundary", since = "1.9.0")]
1667 fn is_char_boundary(&self, index
: usize) -> bool
;
1668 #[unstable(feature = "str_char",
1669 reason
= "often replaced by char_indices, this method may \
1670 be removed in favor of just char_at() or eventually \
1671 removed altogether",
1673 #[rustc_deprecated(reason = "use slicing plus chars() plus len_utf8",
1675 fn char_range_at(&self, start
: usize) -> CharRange
;
1676 #[unstable(feature = "str_char",
1677 reason
= "often replaced by char_indices, this method may \
1678 be removed in favor of just char_at_reverse() or \
1679 eventually removed altogether",
1681 #[rustc_deprecated(reason = "use slicing plus chars().rev() plus len_utf8",
1683 fn char_range_at_reverse(&self, start
: usize) -> CharRange
;
1684 #[unstable(feature = "str_char",
1685 reason
= "frequently replaced by the chars() iterator, this \
1686 method may be removed or possibly renamed in the \
1687 future; it is normally replaced by chars/char_indices \
1688 iterators or by getting the first char from a \
1691 #[rustc_deprecated(reason = "use slicing plus chars()",
1693 fn char_at(&self, i
: usize) -> char;
1694 #[unstable(feature = "str_char",
1695 reason
= "see char_at for more details, but reverse semantics \
1696 are also somewhat unclear, especially with which \
1697 cases generate panics",
1699 #[rustc_deprecated(reason = "use slicing plus chars().rev()",
1701 fn char_at_reverse(&self, i
: usize) -> char;
1702 #[stable(feature = "core", since = "1.6.0")]
1703 fn as_bytes(&self) -> &[u8];
1704 #[stable(feature = "core", since = "1.6.0")]
1705 fn find
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> Option
<usize>;
1706 #[stable(feature = "core", since = "1.6.0")]
1707 fn rfind
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> Option
<usize>
1708 where P
::Searcher
: ReverseSearcher
<'a
>;
1709 fn find_str
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> Option
<usize>;
1710 #[stable(feature = "core", since = "1.6.0")]
1711 fn split_at(&self, mid
: usize) -> (&str, &str);
1712 #[stable(feature = "core", since = "1.6.0")]
1713 fn split_at_mut(&mut self, mid
: usize) -> (&mut str, &mut str);
1714 #[unstable(feature = "str_char",
1715 reason
= "awaiting conventions about shifting and slices and \
1716 may not be warranted with the existence of the chars \
1717 and/or char_indices iterators",
1719 #[rustc_deprecated(reason = "use chars() plus Chars::as_str",
1721 fn slice_shift_char(&self) -> Option
<(char, &str)>;
1722 #[stable(feature = "core", since = "1.6.0")]
1723 fn as_ptr(&self) -> *const u8;
1724 #[stable(feature = "core", since = "1.6.0")]
1725 fn len(&self) -> usize;
1726 #[stable(feature = "core", since = "1.6.0")]
1727 fn is_empty(&self) -> bool
;
1728 #[stable(feature = "core", since = "1.6.0")]
1729 fn parse
<T
: FromStr
>(&self) -> Result
<T
, T
::Err
>;
1732 // truncate `&str` to length at most equal to `max`
1733 // return `true` if it were truncated, and the new str.
1734 fn truncate_to_char_boundary(s
: &str, mut max
: usize) -> (bool
, &str) {
1738 while !s
.is_char_boundary(max
) {
1747 fn slice_error_fail(s
: &str, begin
: usize, end
: usize) -> ! {
1748 const MAX_DISPLAY_LENGTH
: usize = 256;
1749 let (truncated
, s
) = truncate_to_char_boundary(s
, MAX_DISPLAY_LENGTH
);
1750 let ellipsis
= if truncated { "[...]" }
else { "" }
;
1752 assert
!(begin
<= end
, "begin <= end ({} <= {}) when slicing `{}`{}",
1753 begin
, end
, s
, ellipsis
);
1754 panic
!("index {} and/or {} in `{}`{} do not lie on character boundary",
1755 begin
, end
, s
, ellipsis
);
1758 #[stable(feature = "core", since = "1.6.0")]
1759 impl StrExt
for str {
1761 fn contains
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> bool
{
1762 pat
.is_contained_in(self)
1766 fn chars(&self) -> Chars
{
1767 Chars{iter: self.as_bytes().iter()}
1771 fn bytes(&self) -> Bytes
{
1772 Bytes(self.as_bytes().iter().cloned())
1776 fn char_indices(&self) -> CharIndices
{
1777 CharIndices { front_offset: 0, iter: self.chars() }
1781 fn split
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> Split
<'a
, P
> {
1782 Split(SplitInternal
{
1785 matcher
: pat
.into_searcher(self),
1786 allow_trailing_empty
: true,
1792 fn rsplit
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> RSplit
<'a
, P
>
1793 where P
::Searcher
: ReverseSearcher
<'a
>
1795 RSplit(self.split(pat
).0)
1799 fn splitn
<'a
, P
: Pattern
<'a
>>(&'a
self, count
: usize, pat
: P
) -> SplitN
<'a
, P
> {
1800 SplitN(SplitNInternal
{
1801 iter
: self.split(pat
).0,
1807 fn rsplitn
<'a
, P
: Pattern
<'a
>>(&'a
self, count
: usize, pat
: P
) -> RSplitN
<'a
, P
>
1808 where P
::Searcher
: ReverseSearcher
<'a
>
1810 RSplitN(self.splitn(count
, pat
).0)
1814 fn split_terminator
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> SplitTerminator
<'a
, P
> {
1815 SplitTerminator(SplitInternal
{
1816 allow_trailing_empty
: false,
1822 fn rsplit_terminator
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> RSplitTerminator
<'a
, P
>
1823 where P
::Searcher
: ReverseSearcher
<'a
>
1825 RSplitTerminator(self.split_terminator(pat
).0)
1829 fn matches
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> Matches
<'a
, P
> {
1830 Matches(MatchesInternal(pat
.into_searcher(self)))
1834 fn rmatches
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> RMatches
<'a
, P
>
1835 where P
::Searcher
: ReverseSearcher
<'a
>
1837 RMatches(self.matches(pat
).0)
1841 fn match_indices
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> MatchIndices
<'a
, P
> {
1842 MatchIndices(MatchIndicesInternal(pat
.into_searcher(self)))
1846 fn rmatch_indices
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> RMatchIndices
<'a
, P
>
1847 where P
::Searcher
: ReverseSearcher
<'a
>
1849 RMatchIndices(self.match_indices(pat
).0)
1852 fn lines(&self) -> Lines
{
1853 Lines(self.split_terminator('
\n'
).map(LinesAnyMap
))
1857 #[allow(deprecated)]
1858 fn lines_any(&self) -> LinesAny
{
1859 LinesAny(self.lines())
1863 unsafe fn slice_unchecked(&self, begin
: usize, end
: usize) -> &str {
1864 let ptr
= self.as_ptr().offset(begin
as isize);
1865 let len
= end
- begin
;
1866 from_utf8_unchecked(slice
::from_raw_parts(ptr
, len
))
1870 unsafe fn slice_mut_unchecked(&mut self, begin
: usize, end
: usize) -> &mut str {
1871 let ptr
= self.as_ptr().offset(begin
as isize);
1872 let len
= end
- begin
;
1873 mem
::transmute(slice
::from_raw_parts_mut(ptr
as *mut u8, len
))
1877 fn starts_with
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> bool
{
1878 pat
.is_prefix_of(self)
1882 fn ends_with
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> bool
1883 where P
::Searcher
: ReverseSearcher
<'a
>
1885 pat
.is_suffix_of(self)
1889 fn trim_matches
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> &'a
str
1890 where P
::Searcher
: DoubleEndedSearcher
<'a
>
1894 let mut matcher
= pat
.into_searcher(self);
1895 if let Some((a
, b
)) = matcher
.next_reject() {
1897 j
= b
; // Remember earliest known match, correct it below if
1898 // last match is different
1900 if let Some((_
, b
)) = matcher
.next_reject_back() {
1904 // Searcher is known to return valid indices
1905 self.slice_unchecked(i
, j
)
1910 fn trim_left_matches
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> &'a
str {
1911 let mut i
= self.len();
1912 let mut matcher
= pat
.into_searcher(self);
1913 if let Some((a
, _
)) = matcher
.next_reject() {
1917 // Searcher is known to return valid indices
1918 self.slice_unchecked(i
, self.len())
1923 fn trim_right_matches
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> &'a
str
1924 where P
::Searcher
: ReverseSearcher
<'a
>
1927 let mut matcher
= pat
.into_searcher(self);
1928 if let Some((_
, b
)) = matcher
.next_reject_back() {
1932 // Searcher is known to return valid indices
1933 self.slice_unchecked(0, j
)
1938 fn is_char_boundary(&self, index
: usize) -> bool
{
1939 // 0 and len are always ok.
1940 // Test for 0 explicitly so that it can optimize out the check
1941 // easily and skip reading string data for that case.
1942 if index
== 0 || index
== self.len() { return true; }
1943 match self.as_bytes().get(index
) {
1945 Some(&b
) => b
< 128 || b
>= 192,
1950 fn char_range_at(&self, i
: usize) -> CharRange
{
1951 let (c
, n
) = char_range_at_raw(self.as_bytes(), i
);
1952 CharRange { ch: unsafe { char::from_u32_unchecked(c) }
, next
: n
}
1956 fn char_range_at_reverse(&self, start
: usize) -> CharRange
{
1957 let mut prev
= start
;
1959 prev
= prev
.saturating_sub(1);
1960 if self.as_bytes()[prev
] < 128 {
1961 return CharRange{ch: self.as_bytes()[prev] as char, next: prev}
1964 // Multibyte case is a fn to allow char_range_at_reverse to inline cleanly
1965 fn multibyte_char_range_at_reverse(s
: &str, mut i
: usize) -> CharRange
{
1966 // while there is a previous byte == 10......
1967 while i
> 0 && s
.as_bytes()[i
] & !CONT_MASK
== TAG_CONT_U8
{
1971 let first
= s
.as_bytes()[i
];
1972 let w
= UTF8_CHAR_WIDTH
[first
as usize];
1975 let mut val
= utf8_first_byte(first
, w
as u32);
1976 val
= utf8_acc_cont_byte(val
, s
.as_bytes()[i
+ 1]);
1977 if w
> 2 { val = utf8_acc_cont_byte(val, s.as_bytes()[i + 2]); }
1978 if w
> 3 { val = utf8_acc_cont_byte(val, s.as_bytes()[i + 3]); }
1980 CharRange {ch: unsafe { char::from_u32_unchecked(val) }
, next
: i
}
1983 multibyte_char_range_at_reverse(self, prev
)
1987 #[allow(deprecated)]
1988 fn char_at(&self, i
: usize) -> char {
1989 self.char_range_at(i
).ch
1993 #[allow(deprecated)]
1994 fn char_at_reverse(&self, i
: usize) -> char {
1995 self.char_range_at_reverse(i
).ch
1999 fn as_bytes(&self) -> &[u8] {
2000 unsafe { mem::transmute(self) }
2003 fn find
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> Option
<usize> {
2004 pat
.into_searcher(self).next_match().map(|(i
, _
)| i
)
2007 fn rfind
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> Option
<usize>
2008 where P
::Searcher
: ReverseSearcher
<'a
>
2010 pat
.into_searcher(self).next_match_back().map(|(i
, _
)| i
)
2013 fn find_str
<'a
, P
: Pattern
<'a
>>(&'a
self, pat
: P
) -> Option
<usize> {
2018 fn split_at(&self, mid
: usize) -> (&str, &str) {
2019 // is_char_boundary checks that the index is in [0, .len()]
2020 if self.is_char_boundary(mid
) {
2022 (self.slice_unchecked(0, mid
),
2023 self.slice_unchecked(mid
, self.len()))
2026 slice_error_fail(self, 0, mid
)
2030 fn split_at_mut(&mut self, mid
: usize) -> (&mut str, &mut str) {
2031 // is_char_boundary checks that the index is in [0, .len()]
2032 if self.is_char_boundary(mid
) {
2033 let len
= self.len();
2034 let ptr
= self.as_ptr() as *mut u8;
2036 (from_raw_parts_mut(ptr
, mid
),
2037 from_raw_parts_mut(ptr
.offset(mid
as isize), len
- mid
))
2040 slice_error_fail(self, 0, mid
)
2045 #[allow(deprecated)]
2046 fn slice_shift_char(&self) -> Option
<(char, &str)> {
2047 if self.is_empty() {
2050 let ch
= self.char_at(0);
2051 let next_s
= unsafe { self.slice_unchecked(ch.len_utf8(), self.len()) }
;
2057 fn as_ptr(&self) -> *const u8 {
2058 self as *const str as *const u8
2062 fn len(&self) -> usize {
2063 self.as_bytes().len()
2067 fn is_empty(&self) -> bool { self.len() == 0 }
2070 fn parse
<T
: FromStr
>(&self) -> Result
<T
, T
::Err
> { FromStr::from_str(self) }
2073 #[stable(feature = "rust1", since = "1.0.0")]
2074 impl AsRef
<[u8]> for str {
2076 fn as_ref(&self) -> &[u8] {
2081 /// Pluck a code point out of a UTF-8-like byte slice and return the
2082 /// index of the next code point.
2084 fn char_range_at_raw(bytes
: &[u8], i
: usize) -> (u32, usize) {
2086 return (bytes
[i
] as u32, i
+ 1);
2089 // Multibyte case is a fn to allow char_range_at to inline cleanly
2090 fn multibyte_char_range_at(bytes
: &[u8], i
: usize) -> (u32, usize) {
2091 let first
= bytes
[i
];
2092 let w
= UTF8_CHAR_WIDTH
[first
as usize];
2095 let mut val
= utf8_first_byte(first
, w
as u32);
2096 val
= utf8_acc_cont_byte(val
, bytes
[i
+ 1]);
2097 if w
> 2 { val = utf8_acc_cont_byte(val, bytes[i + 2]); }
2098 if w
> 3 { val = utf8_acc_cont_byte(val, bytes[i + 3]); }
2100 (val
, i
+ w
as usize)
2103 multibyte_char_range_at(bytes
, i
)
2106 #[stable(feature = "rust1", since = "1.0.0")]
2107 impl<'a
> Default
for &'a
str {
2108 fn default() -> &'a
str { "" }