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 #![doc(primitive = "str")]
16 #![stable(feature = "rust1", since = "1.0.0")]
18 use self::pattern::Pattern;
19 use self::pattern::{Searcher, ReverseSearcher, DoubleEndedSearcher};
27 use iter::ExactSizeIterator;
28 use iter::{Map, Iterator, DoubleEndedIterator};
30 use ops::{Fn, FnMut, FnOnce};
31 use option::Option::{self, None, Some};
32 use raw::{Repr, Slice};
33 use result::Result::{self, Ok, Err};
34 use slice::{self, SliceExt};
38 /// A trait to abstract the idea of creating a new instance of a type from a
40 #[stable(feature = "rust1", since = "1.0.0")]
42 /// The associated error which can be returned from parsing.
43 #[stable(feature = "rust1", since = "1.0.0")]
46 /// Parses a string `s` to return a value of this type.
48 /// If parsing succeeds, return the value inside `Ok`, otherwise
49 /// when the string is ill-formatted return an error specific to the
50 /// inside `Err`. The error type is specific to implementation of the trait.
51 #[stable(feature = "rust1", since = "1.0.0")]
52 fn from_str(s: &str) -> Result<Self, Self::Err>;
55 #[stable(feature = "rust1", since = "1.0.0")]
56 impl FromStr for bool {
57 type Err = ParseBoolError;
59 /// Parse a `bool` from a string.
61 /// Yields a `Result<bool, ParseBoolError>`, because `s` may or may not
62 /// actually be parseable.
67 /// use std::str::FromStr;
69 /// assert_eq!(FromStr::from_str("true"), Ok(true));
70 /// assert_eq!(FromStr::from_str("false"), Ok(false));
71 /// assert!(<bool as FromStr>::from_str("not even a boolean").is_err());
74 /// Note, in many cases, the `.parse()` method on `str` is more proper.
77 /// assert_eq!("true".parse(), Ok(true));
78 /// assert_eq!("false".parse(), Ok(false));
79 /// assert!("not even a boolean".parse::<bool>().is_err());
82 fn from_str(s: &str) -> Result<bool, ParseBoolError> {
86 _ => Err(ParseBoolError { _priv: () }),
91 /// An error returned when parsing a `bool` from a string fails.
92 #[derive(Debug, Clone, PartialEq)]
93 #[stable(feature = "rust1", since = "1.0.0")]
94 pub struct ParseBoolError { _priv: () }
96 #[stable(feature = "rust1", since = "1.0.0")]
97 impl fmt::Display for ParseBoolError {
98 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
99 "provided string was not `true` or `false`".fmt(f)
104 Section: Creating a string
107 /// Errors which can occur when attempting to interpret a byte slice as a `str`.
108 #[derive(Copy, Eq, PartialEq, Clone, Debug)]
109 #[stable(feature = "rust1", since = "1.0.0")]
110 pub struct Utf8Error {
115 /// Returns the index in the given string up to which valid UTF-8 was
118 /// Starting at the index provided, but not necessarily at it precisely, an
119 /// invalid UTF-8 encoding sequence was found.
120 #[unstable(feature = "utf8_error", reason = "method just added")]
121 pub fn valid_up_to(&self) -> usize { self.valid_up_to }
124 /// Converts a slice of bytes to a string slice without performing any
127 /// Once the slice has been validated as utf-8, it is transmuted in-place and
128 /// returned as a '&str' instead of a '&[u8]'
132 /// Returns `Err` if the slice is not utf-8 with a description as to why the
133 /// provided slice is not utf-8.
134 #[stable(feature = "rust1", since = "1.0.0")]
135 pub fn from_utf8(v: &[u8]) -> Result<&str, Utf8Error> {
136 try!(run_utf8_validation_iterator(&mut v.iter()));
137 Ok(unsafe { from_utf8_unchecked(v) })
140 /// Converts a slice of bytes to a string slice without checking
141 /// that the string contains valid UTF-8.
143 #[stable(feature = "rust1", since = "1.0.0")]
144 pub unsafe fn from_utf8_unchecked<'a>(v: &'a [u8]) -> &'a str {
148 #[stable(feature = "rust1", since = "1.0.0")]
149 impl fmt::Display for Utf8Error {
150 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
151 write!(f, "invalid utf-8: invalid byte near index {}", self.valid_up_to)
159 /// Iterator for the char (representing *Unicode Scalar Values*) of a string
161 /// Created with the method `.chars()`.
163 #[stable(feature = "rust1", since = "1.0.0")]
164 pub struct Chars<'a> {
165 iter: slice::Iter<'a, u8>
168 /// Return the initial codepoint accumulator for the first byte.
169 /// The first byte is special, only want bottom 5 bits for width 2, 4 bits
170 /// for width 3, and 3 bits for width 4.
172 fn utf8_first_byte(byte: u8, width: u32) -> u32 { (byte & (0x7F >> width)) as u32 }
174 /// Return the value of `ch` updated with continuation byte `byte`.
176 fn utf8_acc_cont_byte(ch: u32, byte: u8) -> u32 { (ch << 6) | (byte & CONT_MASK) as u32 }
178 /// Checks whether the byte is a UTF-8 continuation byte (i.e. starts with the
181 fn utf8_is_cont_byte(byte: u8) -> bool { (byte & !CONT_MASK) == TAG_CONT_U8 }
184 fn unwrap_or_0(opt: Option<&u8>) -> u8 {
191 /// Reads the next code point out of a byte iterator (assuming a
192 /// UTF-8-like encoding).
193 #[unstable(feature = "str_internals")]
195 pub fn next_code_point(bytes: &mut slice::Iter<u8>) -> Option<u32> {
197 let x = match bytes.next() {
199 Some(&next_byte) if next_byte < 128 => return Some(next_byte as u32),
200 Some(&next_byte) => next_byte,
203 // Multibyte case follows
204 // Decode from a byte combination out of: [[[x y] z] w]
205 // NOTE: Performance is sensitive to the exact formulation here
206 let init = utf8_first_byte(x, 2);
207 let y = unwrap_or_0(bytes.next());
208 let mut ch = utf8_acc_cont_byte(init, y);
211 // 5th bit in 0xE0 .. 0xEF is always clear, so `init` is still valid
212 let z = unwrap_or_0(bytes.next());
213 let y_z = utf8_acc_cont_byte((y & CONT_MASK) as u32, z);
214 ch = init << 12 | y_z;
217 // use only the lower 3 bits of `init`
218 let w = unwrap_or_0(bytes.next());
219 ch = (init & 7) << 18 | utf8_acc_cont_byte(y_z, w);
226 /// Reads the last code point out of a byte iterator (assuming a
227 /// UTF-8-like encoding).
229 fn next_code_point_reverse(bytes: &mut slice::Iter<u8>) -> Option<u32> {
231 let w = match bytes.next_back() {
233 Some(&next_byte) if next_byte < 128 => return Some(next_byte as u32),
234 Some(&back_byte) => back_byte,
237 // Multibyte case follows
238 // Decode from a byte combination out of: [x [y [z w]]]
240 let z = unwrap_or_0(bytes.next_back());
241 ch = utf8_first_byte(z, 2);
242 if utf8_is_cont_byte(z) {
243 let y = unwrap_or_0(bytes.next_back());
244 ch = utf8_first_byte(y, 3);
245 if utf8_is_cont_byte(y) {
246 let x = unwrap_or_0(bytes.next_back());
247 ch = utf8_first_byte(x, 4);
248 ch = utf8_acc_cont_byte(ch, y);
250 ch = utf8_acc_cont_byte(ch, z);
252 ch = utf8_acc_cont_byte(ch, w);
257 #[stable(feature = "rust1", since = "1.0.0")]
258 impl<'a> Iterator for Chars<'a> {
262 fn next(&mut self) -> Option<char> {
263 next_code_point(&mut self.iter).map(|ch| {
264 // str invariant says `ch` is a valid Unicode Scalar Value
272 fn size_hint(&self) -> (usize, Option<usize>) {
273 let (len, _) = self.iter.size_hint();
274 // `(len + 3)` can't overflow, because we know that the `slice::Iter`
275 // belongs to a slice in memory which has a maximum length of
276 // `isize::MAX` (that's well below `usize::MAX`).
277 ((len + 3) / 4, Some(len))
281 #[stable(feature = "rust1", since = "1.0.0")]
282 impl<'a> DoubleEndedIterator for Chars<'a> {
284 fn next_back(&mut self) -> Option<char> {
285 next_code_point_reverse(&mut self.iter).map(|ch| {
286 // str invariant says `ch` is a valid Unicode Scalar Value
294 /// Iterator for a string's characters and their byte offsets.
296 #[stable(feature = "rust1", since = "1.0.0")]
297 pub struct CharIndices<'a> {
302 #[stable(feature = "rust1", since = "1.0.0")]
303 impl<'a> Iterator for CharIndices<'a> {
304 type Item = (usize, char);
307 fn next(&mut self) -> Option<(usize, char)> {
308 let (pre_len, _) = self.iter.iter.size_hint();
309 match self.iter.next() {
312 let index = self.front_offset;
313 let (len, _) = self.iter.iter.size_hint();
314 self.front_offset += pre_len - len;
321 fn size_hint(&self) -> (usize, Option<usize>) {
322 self.iter.size_hint()
326 #[stable(feature = "rust1", since = "1.0.0")]
327 impl<'a> DoubleEndedIterator for CharIndices<'a> {
329 fn next_back(&mut self) -> Option<(usize, char)> {
330 match self.iter.next_back() {
333 let (len, _) = self.iter.iter.size_hint();
334 let index = self.front_offset + len;
341 /// External iterator for a string's bytes.
342 /// Use with the `std::iter` module.
344 /// Created with the method `.bytes()`.
345 #[stable(feature = "rust1", since = "1.0.0")]
347 pub struct Bytes<'a>(Map<slice::Iter<'a, u8>, BytesDeref>);
349 /// A nameable, clonable fn type
353 impl<'a> Fn<(&'a u8,)> for BytesDeref {
355 extern "rust-call" fn call(&self, (ptr,): (&'a u8,)) -> u8 {
360 impl<'a> FnMut<(&'a u8,)> for BytesDeref {
362 extern "rust-call" fn call_mut(&mut self, (ptr,): (&'a u8,)) -> u8 {
363 Fn::call(&*self, (ptr,))
367 impl<'a> FnOnce<(&'a u8,)> for BytesDeref {
371 extern "rust-call" fn call_once(self, (ptr,): (&'a u8,)) -> u8 {
372 Fn::call(&self, (ptr,))
376 #[stable(feature = "rust1", since = "1.0.0")]
377 impl<'a> Iterator for Bytes<'a> {
381 fn next(&mut self) -> Option<u8> {
386 fn size_hint(&self) -> (usize, Option<usize>) {
391 #[stable(feature = "rust1", since = "1.0.0")]
392 impl<'a> DoubleEndedIterator for Bytes<'a> {
394 fn next_back(&mut self) -> Option<u8> {
399 #[stable(feature = "rust1", since = "1.0.0")]
400 impl<'a> ExactSizeIterator for Bytes<'a> {
402 fn len(&self) -> usize {
407 /// This macro generates a Clone impl for string pattern API
408 /// wrapper types of the form X<'a, P>
409 macro_rules! derive_pattern_clone {
410 (clone $t:ident with |$s:ident| $e:expr) => {
411 impl<'a, P: Pattern<'a>> Clone for $t<'a, P>
412 where P::Searcher: Clone
414 fn clone(&self) -> Self {
422 /// This macro generates two public iterator structs
423 /// wrapping an private internal one that makes use of the `Pattern` API.
425 /// For all patterns `P: Pattern<'a>` the following items will be
426 /// generated (generics omitted):
428 /// struct $forward_iterator($internal_iterator);
429 /// struct $reverse_iterator($internal_iterator);
431 /// impl Iterator for $forward_iterator
432 /// { /* internal ends up calling Searcher::next_match() */ }
434 /// impl DoubleEndedIterator for $forward_iterator
435 /// where P::Searcher: DoubleEndedSearcher
436 /// { /* internal ends up calling Searcher::next_match_back() */ }
438 /// impl Iterator for $reverse_iterator
439 /// where P::Searcher: ReverseSearcher
440 /// { /* internal ends up calling Searcher::next_match_back() */ }
442 /// impl DoubleEndedIterator for $reverse_iterator
443 /// where P::Searcher: DoubleEndedSearcher
444 /// { /* internal ends up calling Searcher::next_match() */ }
446 /// The internal one is defined outside the macro, and has almost the same
447 /// semantic as a DoubleEndedIterator by delegating to `pattern::Searcher` and
448 /// `pattern::ReverseSearcher` for both forward and reverse iteration.
450 /// "Almost", because a `Searcher` and a `ReverseSearcher` for a given
451 /// `Pattern` might not return the same elements, so actually implementing
452 /// `DoubleEndedIterator` for it would be incorrect.
453 /// (See the docs in `str::pattern` for more details)
455 /// However, the internal struct still represents a single ended iterator from
456 /// either end, and depending on pattern is also a valid double ended iterator,
457 /// so the two wrapper structs implement `Iterator`
458 /// and `DoubleEndedIterator` depending on the concrete pattern type, leading
459 /// to the complex impls seen above.
460 macro_rules! generate_pattern_iterators {
464 $(#[$forward_iterator_attribute:meta])*
465 struct $forward_iterator:ident;
469 $(#[$reverse_iterator_attribute:meta])*
470 struct $reverse_iterator:ident;
472 // Stability of all generated items
474 $(#[$common_stability_attribute:meta])*
476 // Internal almost-iterator that is being delegated to
478 $internal_iterator:ident yielding ($iterty:ty);
480 // Kind of delgation - either single ended or double ended
483 $(#[$forward_iterator_attribute])*
484 $(#[$common_stability_attribute])*
485 pub struct $forward_iterator<'a, P: Pattern<'a>>($internal_iterator<'a, P>);
487 $(#[$common_stability_attribute])*
488 impl<'a, P: Pattern<'a>> Iterator for $forward_iterator<'a, P> {
492 fn next(&mut self) -> Option<$iterty> {
497 $(#[$common_stability_attribute])*
498 impl<'a, P: Pattern<'a>> Clone for $forward_iterator<'a, P>
499 where P::Searcher: Clone
501 fn clone(&self) -> Self {
502 $forward_iterator(self.0.clone())
506 $(#[$reverse_iterator_attribute])*
507 $(#[$common_stability_attribute])*
508 pub struct $reverse_iterator<'a, P: Pattern<'a>>($internal_iterator<'a, P>);
510 $(#[$common_stability_attribute])*
511 impl<'a, P: Pattern<'a>> Iterator for $reverse_iterator<'a, P>
512 where P::Searcher: ReverseSearcher<'a>
517 fn next(&mut self) -> Option<$iterty> {
522 $(#[$common_stability_attribute])*
523 impl<'a, P: Pattern<'a>> Clone for $reverse_iterator<'a, P>
524 where P::Searcher: Clone
526 fn clone(&self) -> Self {
527 $reverse_iterator(self.0.clone())
531 generate_pattern_iterators!($($t)* with $(#[$common_stability_attribute])*,
533 $reverse_iterator, $iterty);
536 double ended; with $(#[$common_stability_attribute:meta])*,
537 $forward_iterator:ident,
538 $reverse_iterator:ident, $iterty:ty
540 $(#[$common_stability_attribute])*
541 impl<'a, P: Pattern<'a>> DoubleEndedIterator for $forward_iterator<'a, P>
542 where P::Searcher: DoubleEndedSearcher<'a>
545 fn next_back(&mut self) -> Option<$iterty> {
550 $(#[$common_stability_attribute])*
551 impl<'a, P: Pattern<'a>> DoubleEndedIterator for $reverse_iterator<'a, P>
552 where P::Searcher: DoubleEndedSearcher<'a>
555 fn next_back(&mut self) -> Option<$iterty> {
561 single ended; with $(#[$common_stability_attribute:meta])*,
562 $forward_iterator:ident,
563 $reverse_iterator:ident, $iterty:ty
567 derive_pattern_clone!{
569 with |s| SplitInternal { matcher: s.matcher.clone(), ..*s }
571 struct SplitInternal<'a, P: Pattern<'a>> {
574 matcher: P::Searcher,
575 allow_trailing_empty: bool,
579 impl<'a, P: Pattern<'a>> SplitInternal<'a, P> {
581 fn get_end(&mut self) -> Option<&'a str> {
582 if !self.finished && (self.allow_trailing_empty || self.end - self.start > 0) {
583 self.finished = true;
585 let string = self.matcher.haystack().slice_unchecked(self.start, self.end);
594 fn next(&mut self) -> Option<&'a str> {
595 if self.finished { return None }
597 let haystack = self.matcher.haystack();
598 match self.matcher.next_match() {
599 Some((a, b)) => unsafe {
600 let elt = haystack.slice_unchecked(self.start, a);
604 None => self.get_end(),
609 fn next_back(&mut self) -> Option<&'a str>
610 where P::Searcher: ReverseSearcher<'a>
612 if self.finished { return None }
614 if !self.allow_trailing_empty {
615 self.allow_trailing_empty = true;
616 match self.next_back() {
617 Some(elt) if !elt.is_empty() => return Some(elt),
618 _ => if self.finished { return None }
622 let haystack = self.matcher.haystack();
623 match self.matcher.next_match_back() {
624 Some((a, b)) => unsafe {
625 let elt = haystack.slice_unchecked(b, self.end);
630 self.finished = true;
631 Some(haystack.slice_unchecked(self.start, self.end))
637 generate_pattern_iterators! {
639 #[doc="Created with the method `.split()`."]
642 #[doc="Created with the method `.rsplit()`."]
645 #[stable(feature = "rust1", since = "1.0.0")]
647 SplitInternal yielding (&'a str);
648 delegate double ended;
651 generate_pattern_iterators! {
653 #[doc="Created with the method `.split_terminator()`."]
654 struct SplitTerminator;
656 #[doc="Created with the method `.rsplit_terminator()`."]
657 struct RSplitTerminator;
659 #[stable(feature = "rust1", since = "1.0.0")]
661 SplitInternal yielding (&'a str);
662 delegate double ended;
665 derive_pattern_clone!{
667 with |s| SplitNInternal { iter: s.iter.clone(), ..*s }
669 struct SplitNInternal<'a, P: Pattern<'a>> {
670 iter: SplitInternal<'a, P>,
671 /// The number of splits remaining
675 impl<'a, P: Pattern<'a>> SplitNInternal<'a, P> {
677 fn next(&mut self) -> Option<&'a str> {
680 1 => { self.count = 0; self.iter.get_end() }
681 _ => { self.count -= 1; self.iter.next() }
686 fn next_back(&mut self) -> Option<&'a str>
687 where P::Searcher: ReverseSearcher<'a>
691 1 => { self.count = 0; self.iter.get_end() }
692 _ => { self.count -= 1; self.iter.next_back() }
697 generate_pattern_iterators! {
699 #[doc="Created with the method `.splitn()`."]
702 #[doc="Created with the method `.rsplitn()`."]
705 #[stable(feature = "rust1", since = "1.0.0")]
707 SplitNInternal yielding (&'a str);
708 delegate single ended;
711 derive_pattern_clone!{
712 clone MatchIndicesInternal
713 with |s| MatchIndicesInternal(s.0.clone())
715 struct MatchIndicesInternal<'a, P: Pattern<'a>>(P::Searcher);
717 impl<'a, P: Pattern<'a>> MatchIndicesInternal<'a, P> {
719 fn next(&mut self) -> Option<(usize, usize)> {
724 fn next_back(&mut self) -> Option<(usize, usize)>
725 where P::Searcher: ReverseSearcher<'a>
727 self.0.next_match_back()
731 generate_pattern_iterators! {
733 #[doc="Created with the method `.match_indices()`."]
736 #[doc="Created with the method `.rmatch_indices()`."]
737 struct RMatchIndices;
739 #[unstable(feature = "str_match_indices",
740 reason = "type may be removed or have its iterator impl changed")]
742 MatchIndicesInternal yielding ((usize, usize));
743 delegate double ended;
746 derive_pattern_clone!{
747 clone MatchesInternal
748 with |s| MatchesInternal(s.0.clone())
750 struct MatchesInternal<'a, P: Pattern<'a>>(P::Searcher);
752 impl<'a, P: Pattern<'a>> MatchesInternal<'a, P> {
754 fn next(&mut self) -> Option<&'a str> {
755 self.0.next_match().map(|(a, b)| unsafe {
756 // Indices are known to be on utf8 boundaries
757 self.0.haystack().slice_unchecked(a, b)
762 fn next_back(&mut self) -> Option<&'a str>
763 where P::Searcher: ReverseSearcher<'a>
765 self.0.next_match_back().map(|(a, b)| unsafe {
766 // Indices are known to be on utf8 boundaries
767 self.0.haystack().slice_unchecked(a, b)
772 generate_pattern_iterators! {
774 #[doc="Created with the method `.matches()`."]
777 #[doc="Created with the method `.rmatches()`."]
780 #[stable(feature = "str_matches", since = "1.2.0")]
782 MatchesInternal yielding (&'a str);
783 delegate double ended;
786 /// Created with the method `.lines()`.
787 #[stable(feature = "rust1", since = "1.0.0")]
789 pub struct Lines<'a>(SplitTerminator<'a, char>);
791 #[stable(feature = "rust1", since = "1.0.0")]
792 impl<'a> Iterator for Lines<'a> {
796 fn next(&mut self) -> Option<&'a str> {
801 fn size_hint(&self) -> (usize, Option<usize>) {
806 #[stable(feature = "rust1", since = "1.0.0")]
807 impl<'a> DoubleEndedIterator for Lines<'a> {
809 fn next_back(&mut self) -> Option<&'a str> {
814 /// Created with the method `.lines_any()`.
815 #[stable(feature = "rust1", since = "1.0.0")]
817 pub struct LinesAny<'a>(Map<Lines<'a>, LinesAnyMap>);
819 /// A nameable, clonable fn type
823 impl<'a> Fn<(&'a str,)> for LinesAnyMap {
825 extern "rust-call" fn call(&self, (line,): (&'a str,)) -> &'a str {
827 if l > 0 && line.as_bytes()[l - 1] == b'\r' { &line[0 .. l - 1] }
832 impl<'a> FnMut<(&'a str,)> for LinesAnyMap {
834 extern "rust-call" fn call_mut(&mut self, (line,): (&'a str,)) -> &'a str {
835 Fn::call(&*self, (line,))
839 impl<'a> FnOnce<(&'a str,)> for LinesAnyMap {
840 type Output = &'a str;
843 extern "rust-call" fn call_once(self, (line,): (&'a str,)) -> &'a str {
844 Fn::call(&self, (line,))
848 #[stable(feature = "rust1", since = "1.0.0")]
849 impl<'a> Iterator for LinesAny<'a> {
853 fn next(&mut self) -> Option<&'a str> {
858 fn size_hint(&self) -> (usize, Option<usize>) {
863 #[stable(feature = "rust1", since = "1.0.0")]
864 impl<'a> DoubleEndedIterator for LinesAny<'a> {
866 fn next_back(&mut self) -> Option<&'a str> {
872 Section: Comparing strings
875 // share the implementation of the lang-item vs. non-lang-item
877 /// NOTE: This function is (ab)used in rustc::middle::trans::_match
878 /// to compare &[u8] byte slices that are not necessarily valid UTF-8.
880 fn eq_slice_(a: &str, b: &str) -> bool {
881 // NOTE: In theory n should be libc::size_t and not usize, but libc is not available here
882 #[allow(improper_ctypes)]
883 extern { fn memcmp(s1: *const i8, s2: *const i8, n: usize) -> i32; }
884 a.len() == b.len() && unsafe {
885 memcmp(a.as_ptr() as *const i8,
886 b.as_ptr() as *const i8,
891 /// Bytewise slice equality
892 /// NOTE: This function is (ab)used in rustc::middle::trans::_match
893 /// to compare &[u8] byte slices that are not necessarily valid UTF-8.
896 fn eq_slice(a: &str, b: &str) -> bool {
904 /// Walk through `iter` checking that it's a valid UTF-8 sequence,
905 /// returning `true` in that case, or, if it is invalid, `false` with
906 /// `iter` reset such that it is pointing at the first byte in the
907 /// invalid sequence.
909 fn run_utf8_validation_iterator(iter: &mut slice::Iter<u8>)
910 -> Result<(), Utf8Error> {
911 let whole = iter.as_slice();
913 // save the current thing we're pointing at.
914 let old = iter.clone();
916 // restore the iterator we had at the start of this codepoint.
917 macro_rules! err { () => {{
919 return Err(Utf8Error {
920 valid_up_to: whole.len() - iter.as_slice().len()
924 macro_rules! next { () => {
927 // we needed data, but there was none: error!
932 let first = match iter.next() {
934 // we're at the end of the iterator and a codepoint
935 // boundary at the same time, so this string is valid.
936 None => return Ok(())
939 // ASCII characters are always valid, so only large
940 // bytes need more examination.
942 let w = UTF8_CHAR_WIDTH[first as usize];
943 let second = next!();
944 // 2-byte encoding is for codepoints \u{0080} to \u{07ff}
945 // first C2 80 last DF BF
946 // 3-byte encoding is for codepoints \u{0800} to \u{ffff}
947 // first E0 A0 80 last EF BF BF
948 // excluding surrogates codepoints \u{d800} to \u{dfff}
949 // ED A0 80 to ED BF BF
950 // 4-byte encoding is for codepoints \u{1000}0 to \u{10ff}ff
951 // first F0 90 80 80 last F4 8F BF BF
953 // Use the UTF-8 syntax from the RFC
955 // https://tools.ietf.org/html/rfc3629
957 // UTF8-2 = %xC2-DF UTF8-tail
958 // UTF8-3 = %xE0 %xA0-BF UTF8-tail / %xE1-EC 2( UTF8-tail ) /
959 // %xED %x80-9F UTF8-tail / %xEE-EF 2( UTF8-tail )
960 // UTF8-4 = %xF0 %x90-BF 2( UTF8-tail ) / %xF1-F3 3( UTF8-tail ) /
961 // %xF4 %x80-8F 2( UTF8-tail )
963 2 => if second & !CONT_MASK != TAG_CONT_U8 {err!()},
965 match (first, second, next!() & !CONT_MASK) {
966 (0xE0 , 0xA0 ... 0xBF, TAG_CONT_U8) |
967 (0xE1 ... 0xEC, 0x80 ... 0xBF, TAG_CONT_U8) |
968 (0xED , 0x80 ... 0x9F, TAG_CONT_U8) |
969 (0xEE ... 0xEF, 0x80 ... 0xBF, TAG_CONT_U8) => {}
974 match (first, second, next!() & !CONT_MASK, next!() & !CONT_MASK) {
975 (0xF0 , 0x90 ... 0xBF, TAG_CONT_U8, TAG_CONT_U8) |
976 (0xF1 ... 0xF3, 0x80 ... 0xBF, TAG_CONT_U8, TAG_CONT_U8) |
977 (0xF4 , 0x80 ... 0x8F, TAG_CONT_U8, TAG_CONT_U8) => {}
987 // https://tools.ietf.org/html/rfc3629
988 static UTF8_CHAR_WIDTH: [u8; 256] = [
989 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
990 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 0x1F
991 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
992 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 0x3F
993 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
994 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 0x5F
995 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
996 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 0x7F
997 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
998 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 0x9F
999 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1000 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 0xBF
1001 0,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
1002 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, // 0xDF
1003 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3, // 0xEF
1004 4,4,4,4,4,0,0,0,0,0,0,0,0,0,0,0, // 0xFF
1007 /// Struct that contains a `char` and the index of the first byte of
1008 /// the next `char` in a string. This can be used as a data structure
1009 /// for iterating over the UTF-8 bytes of a string.
1010 #[derive(Copy, Clone)]
1011 #[unstable(feature = "str_char",
1012 reason = "existence of this struct is uncertain as it is frequently \
1013 able to be replaced with char.len_utf8() and/or \
1014 char/char_indices iterators")]
1015 pub struct CharRange {
1018 /// Index of the first byte of the next `char`
1022 /// Mask of the value bits of a continuation byte
1023 const CONT_MASK: u8 = 0b0011_1111;
1024 /// Value of the tag bits (tag mask is !CONT_MASK) of a continuation byte
1025 const TAG_CONT_U8: u8 = 0b1000_0000;
1028 Section: Trait implementations
1032 use cmp::{Ordering, Ord, PartialEq, PartialOrd, Eq};
1033 use cmp::Ordering::{Less, Equal, Greater};
1036 use option::Option::Some;
1038 use str::{StrExt, eq_slice};
1040 #[stable(feature = "rust1", since = "1.0.0")]
1043 fn cmp(&self, other: &str) -> Ordering {
1044 for (s_b, o_b) in self.bytes().zip(other.bytes()) {
1045 match s_b.cmp(&o_b) {
1046 Greater => return Greater,
1047 Less => return Less,
1052 self.len().cmp(&other.len())
1056 #[stable(feature = "rust1", since = "1.0.0")]
1057 impl PartialEq for str {
1059 fn eq(&self, other: &str) -> bool {
1060 eq_slice(self, other)
1063 fn ne(&self, other: &str) -> bool { !(*self).eq(other) }
1066 #[stable(feature = "rust1", since = "1.0.0")]
1069 #[stable(feature = "rust1", since = "1.0.0")]
1070 impl PartialOrd for str {
1072 fn partial_cmp(&self, other: &str) -> Option<Ordering> {
1073 Some(self.cmp(other))
1077 /// Returns a slice of the given string from the byte range
1078 /// [`begin`..`end`).
1080 /// This operation is `O(1)`.
1082 /// Panics when `begin` and `end` do not point to valid characters
1083 /// or point beyond the last character of the string.
1088 /// let s = "Löwe 老虎 Léopard";
1089 /// assert_eq!(&s[0 .. 1], "L");
1091 /// assert_eq!(&s[1 .. 9], "öwe 老");
1093 /// // these will panic:
1094 /// // byte 2 lies within `ö`:
1097 /// // byte 8 lies within `老`
1100 /// // byte 100 is outside the string
1101 /// // &s[3 .. 100];
1103 #[stable(feature = "rust1", since = "1.0.0")]
1104 impl ops::Index<ops::Range<usize>> for str {
1107 fn index(&self, index: ops::Range<usize>) -> &str {
1108 // is_char_boundary checks that the index is in [0, .len()]
1109 if index.start <= index.end &&
1110 self.is_char_boundary(index.start) &&
1111 self.is_char_boundary(index.end) {
1112 unsafe { self.slice_unchecked(index.start, index.end) }
1114 super::slice_error_fail(self, index.start, index.end)
1119 /// Returns a mutable slice of the given string from the byte range
1120 /// [`begin`..`end`).
1121 #[stable(feature = "derefmut_for_string", since = "1.2.0")]
1122 impl ops::IndexMut<ops::Range<usize>> for str {
1124 fn index_mut(&mut self, index: ops::Range<usize>) -> &mut str {
1125 // is_char_boundary checks that the index is in [0, .len()]
1126 if index.start <= index.end &&
1127 self.is_char_boundary(index.start) &&
1128 self.is_char_boundary(index.end) {
1129 unsafe { self.slice_mut_unchecked(index.start, index.end) }
1131 super::slice_error_fail(self, index.start, index.end)
1136 /// Returns a slice of the string from the beginning to byte
1139 /// Equivalent to `self[0 .. end]`.
1141 /// Panics when `end` does not point to a valid character, or is
1143 #[stable(feature = "rust1", since = "1.0.0")]
1144 impl ops::Index<ops::RangeTo<usize>> for str {
1148 fn index(&self, index: ops::RangeTo<usize>) -> &str {
1149 // is_char_boundary checks that the index is in [0, .len()]
1150 if self.is_char_boundary(index.end) {
1151 unsafe { self.slice_unchecked(0, index.end) }
1153 super::slice_error_fail(self, 0, index.end)
1158 /// Returns a mutable slice of the string from the beginning to byte
1160 #[stable(feature = "derefmut_for_string", since = "1.2.0")]
1161 impl ops::IndexMut<ops::RangeTo<usize>> for str {
1163 fn index_mut(&mut self, index: ops::RangeTo<usize>) -> &mut str {
1164 // is_char_boundary checks that the index is in [0, .len()]
1165 if self.is_char_boundary(index.end) {
1166 unsafe { self.slice_mut_unchecked(0, index.end) }
1168 super::slice_error_fail(self, 0, index.end)
1173 /// Returns a slice of the string from `begin` to its end.
1175 /// Equivalent to `self[begin .. self.len()]`.
1177 /// Panics when `begin` does not point to a valid character, or is
1179 #[stable(feature = "rust1", since = "1.0.0")]
1180 impl ops::Index<ops::RangeFrom<usize>> for str {
1184 fn index(&self, index: ops::RangeFrom<usize>) -> &str {
1185 // is_char_boundary checks that the index is in [0, .len()]
1186 if self.is_char_boundary(index.start) {
1187 unsafe { self.slice_unchecked(index.start, self.len()) }
1189 super::slice_error_fail(self, index.start, self.len())
1194 /// Returns a slice of the string from `begin` to its end.
1195 #[stable(feature = "derefmut_for_string", since = "1.2.0")]
1196 impl ops::IndexMut<ops::RangeFrom<usize>> for str {
1198 fn index_mut(&mut self, index: ops::RangeFrom<usize>) -> &mut str {
1199 // is_char_boundary checks that the index is in [0, .len()]
1200 if self.is_char_boundary(index.start) {
1201 let len = self.len();
1202 unsafe { self.slice_mut_unchecked(index.start, len) }
1204 super::slice_error_fail(self, index.start, self.len())
1209 #[stable(feature = "rust1", since = "1.0.0")]
1210 impl ops::Index<ops::RangeFull> for str {
1214 fn index(&self, _index: ops::RangeFull) -> &str {
1219 #[stable(feature = "derefmut_for_string", since = "1.2.0")]
1220 impl ops::IndexMut<ops::RangeFull> for str {
1222 fn index_mut(&mut self, _index: ops::RangeFull) -> &mut str {
1228 /// Methods for string slices
1229 #[allow(missing_docs)]
1231 #[unstable(feature = "core_str_ext",
1232 reason = "stable interface provided by `impl str` in later crates")]
1234 // NB there are no docs here are they're all located on the StrExt trait in
1235 // libcollections, not here.
1237 fn contains<'a, P: Pattern<'a>>(&'a self, pat: P) -> bool;
1238 fn contains_char<'a, P: Pattern<'a>>(&'a self, pat: P) -> bool;
1239 fn chars<'a>(&'a self) -> Chars<'a>;
1240 fn bytes<'a>(&'a self) -> Bytes<'a>;
1241 fn char_indices<'a>(&'a self) -> CharIndices<'a>;
1242 fn split<'a, P: Pattern<'a>>(&'a self, pat: P) -> Split<'a, P>;
1243 fn rsplit<'a, P: Pattern<'a>>(&'a self, pat: P) -> RSplit<'a, P>
1244 where P::Searcher: ReverseSearcher<'a>;
1245 fn splitn<'a, P: Pattern<'a>>(&'a self, count: usize, pat: P) -> SplitN<'a, P>;
1246 fn rsplitn<'a, P: Pattern<'a>>(&'a self, count: usize, pat: P) -> RSplitN<'a, P>
1247 where P::Searcher: ReverseSearcher<'a>;
1248 fn split_terminator<'a, P: Pattern<'a>>(&'a self, pat: P) -> SplitTerminator<'a, P>;
1249 fn rsplit_terminator<'a, P: Pattern<'a>>(&'a self, pat: P) -> RSplitTerminator<'a, P>
1250 where P::Searcher: ReverseSearcher<'a>;
1251 fn matches<'a, P: Pattern<'a>>(&'a self, pat: P) -> Matches<'a, P>;
1252 fn rmatches<'a, P: Pattern<'a>>(&'a self, pat: P) -> RMatches<'a, P>
1253 where P::Searcher: ReverseSearcher<'a>;
1254 fn match_indices<'a, P: Pattern<'a>>(&'a self, pat: P) -> MatchIndices<'a, P>;
1255 fn rmatch_indices<'a, P: Pattern<'a>>(&'a self, pat: P) -> RMatchIndices<'a, P>
1256 where P::Searcher: ReverseSearcher<'a>;
1257 fn lines<'a>(&'a self) -> Lines<'a>;
1258 fn lines_any<'a>(&'a self) -> LinesAny<'a>;
1259 fn char_len(&self) -> usize;
1260 fn slice_chars<'a>(&'a self, begin: usize, end: usize) -> &'a str;
1261 unsafe fn slice_unchecked<'a>(&'a self, begin: usize, end: usize) -> &'a str;
1262 unsafe fn slice_mut_unchecked<'a>(&'a mut self, begin: usize, end: usize) -> &'a mut str;
1263 fn starts_with<'a, P: Pattern<'a>>(&'a self, pat: P) -> bool;
1264 fn ends_with<'a, P: Pattern<'a>>(&'a self, pat: P) -> bool
1265 where P::Searcher: ReverseSearcher<'a>;
1266 fn trim_matches<'a, P: Pattern<'a>>(&'a self, pat: P) -> &'a str
1267 where P::Searcher: DoubleEndedSearcher<'a>;
1268 fn trim_left_matches<'a, P: Pattern<'a>>(&'a self, pat: P) -> &'a str;
1269 fn trim_right_matches<'a, P: Pattern<'a>>(&'a self, pat: P) -> &'a str
1270 where P::Searcher: ReverseSearcher<'a>;
1271 fn is_char_boundary(&self, index: usize) -> bool;
1272 fn char_range_at(&self, start: usize) -> CharRange;
1273 fn char_range_at_reverse(&self, start: usize) -> CharRange;
1274 fn char_at(&self, i: usize) -> char;
1275 fn char_at_reverse(&self, i: usize) -> char;
1276 fn as_bytes<'a>(&'a self) -> &'a [u8];
1277 fn find<'a, P: Pattern<'a>>(&'a self, pat: P) -> Option<usize>;
1278 fn rfind<'a, P: Pattern<'a>>(&'a self, pat: P) -> Option<usize>
1279 where P::Searcher: ReverseSearcher<'a>;
1280 fn find_str<'a, P: Pattern<'a>>(&'a self, pat: P) -> Option<usize>;
1281 fn split_at(&self, mid: usize) -> (&str, &str);
1282 fn split_at_mut(&mut self, mid: usize) -> (&mut str, &mut str);
1283 fn slice_shift_char<'a>(&'a self) -> Option<(char, &'a str)>;
1284 fn subslice_offset(&self, inner: &str) -> usize;
1285 fn as_ptr(&self) -> *const u8;
1286 fn len(&self) -> usize;
1287 fn is_empty(&self) -> bool;
1288 fn parse<T: FromStr>(&self) -> Result<T, T::Err>;
1292 fn slice_error_fail(s: &str, begin: usize, end: usize) -> ! {
1293 assert!(begin <= end);
1294 panic!("index {} and/or {} in `{}` do not lie on character boundary",
1298 impl StrExt for str {
1300 fn contains<'a, P: Pattern<'a>>(&'a self, pat: P) -> bool {
1301 pat.is_contained_in(self)
1305 fn contains_char<'a, P: Pattern<'a>>(&'a self, pat: P) -> bool {
1306 pat.is_contained_in(self)
1310 fn chars(&self) -> Chars {
1311 Chars{iter: self.as_bytes().iter()}
1315 fn bytes(&self) -> Bytes {
1316 Bytes(self.as_bytes().iter().map(BytesDeref))
1320 fn char_indices(&self) -> CharIndices {
1321 CharIndices { front_offset: 0, iter: self.chars() }
1325 fn split<'a, P: Pattern<'a>>(&'a self, pat: P) -> Split<'a, P> {
1326 Split(SplitInternal {
1329 matcher: pat.into_searcher(self),
1330 allow_trailing_empty: true,
1336 fn rsplit<'a, P: Pattern<'a>>(&'a self, pat: P) -> RSplit<'a, P>
1337 where P::Searcher: ReverseSearcher<'a>
1339 RSplit(self.split(pat).0)
1343 fn splitn<'a, P: Pattern<'a>>(&'a self, count: usize, pat: P) -> SplitN<'a, P> {
1344 SplitN(SplitNInternal {
1345 iter: self.split(pat).0,
1351 fn rsplitn<'a, P: Pattern<'a>>(&'a self, count: usize, pat: P) -> RSplitN<'a, P>
1352 where P::Searcher: ReverseSearcher<'a>
1354 RSplitN(self.splitn(count, pat).0)
1358 fn split_terminator<'a, P: Pattern<'a>>(&'a self, pat: P) -> SplitTerminator<'a, P> {
1359 SplitTerminator(SplitInternal {
1360 allow_trailing_empty: false,
1366 fn rsplit_terminator<'a, P: Pattern<'a>>(&'a self, pat: P) -> RSplitTerminator<'a, P>
1367 where P::Searcher: ReverseSearcher<'a>
1369 RSplitTerminator(self.split_terminator(pat).0)
1373 fn matches<'a, P: Pattern<'a>>(&'a self, pat: P) -> Matches<'a, P> {
1374 Matches(MatchesInternal(pat.into_searcher(self)))
1378 fn rmatches<'a, P: Pattern<'a>>(&'a self, pat: P) -> RMatches<'a, P>
1379 where P::Searcher: ReverseSearcher<'a>
1381 RMatches(self.matches(pat).0)
1385 fn match_indices<'a, P: Pattern<'a>>(&'a self, pat: P) -> MatchIndices<'a, P> {
1386 MatchIndices(MatchIndicesInternal(pat.into_searcher(self)))
1390 fn rmatch_indices<'a, P: Pattern<'a>>(&'a self, pat: P) -> RMatchIndices<'a, P>
1391 where P::Searcher: ReverseSearcher<'a>
1393 RMatchIndices(self.match_indices(pat).0)
1396 fn lines(&self) -> Lines {
1397 Lines(self.split_terminator('\n'))
1401 fn lines_any(&self) -> LinesAny {
1402 LinesAny(self.lines().map(LinesAnyMap))
1406 fn char_len(&self) -> usize { self.chars().count() }
1408 fn slice_chars(&self, begin: usize, end: usize) -> &str {
1409 assert!(begin <= end);
1411 let mut begin_byte = None;
1412 let mut end_byte = None;
1414 // This could be even more efficient by not decoding,
1415 // only finding the char boundaries
1416 for (idx, _) in self.char_indices() {
1417 if count == begin { begin_byte = Some(idx); }
1418 if count == end { end_byte = Some(idx); break; }
1421 if begin_byte.is_none() && count == begin { begin_byte = Some(self.len()) }
1422 if end_byte.is_none() && count == end { end_byte = Some(self.len()) }
1424 match (begin_byte, end_byte) {
1425 (None, _) => panic!("slice_chars: `begin` is beyond end of string"),
1426 (_, None) => panic!("slice_chars: `end` is beyond end of string"),
1427 (Some(a), Some(b)) => unsafe { self.slice_unchecked(a, b) }
1432 unsafe fn slice_unchecked(&self, begin: usize, end: usize) -> &str {
1433 mem::transmute(Slice {
1434 data: self.as_ptr().offset(begin as isize),
1440 unsafe fn slice_mut_unchecked(&mut self, begin: usize, end: usize) -> &mut str {
1441 mem::transmute(Slice {
1442 data: self.as_ptr().offset(begin as isize),
1448 fn starts_with<'a, P: Pattern<'a>>(&'a self, pat: P) -> bool {
1449 pat.is_prefix_of(self)
1453 fn ends_with<'a, P: Pattern<'a>>(&'a self, pat: P) -> bool
1454 where P::Searcher: ReverseSearcher<'a>
1456 pat.is_suffix_of(self)
1460 fn trim_matches<'a, P: Pattern<'a>>(&'a self, pat: P) -> &'a str
1461 where P::Searcher: DoubleEndedSearcher<'a>
1465 let mut matcher = pat.into_searcher(self);
1466 if let Some((a, b)) = matcher.next_reject() {
1468 j = b; // Rember earliest known match, correct it below if
1469 // last match is different
1471 if let Some((_, b)) = matcher.next_reject_back() {
1475 // Searcher is known to return valid indices
1476 self.slice_unchecked(i, j)
1481 fn trim_left_matches<'a, P: Pattern<'a>>(&'a self, pat: P) -> &'a str {
1482 let mut i = self.len();
1483 let mut matcher = pat.into_searcher(self);
1484 if let Some((a, _)) = matcher.next_reject() {
1488 // Searcher is known to return valid indices
1489 self.slice_unchecked(i, self.len())
1494 fn trim_right_matches<'a, P: Pattern<'a>>(&'a self, pat: P) -> &'a str
1495 where P::Searcher: ReverseSearcher<'a>
1498 let mut matcher = pat.into_searcher(self);
1499 if let Some((_, b)) = matcher.next_reject_back() {
1503 // Searcher is known to return valid indices
1504 self.slice_unchecked(0, j)
1509 fn is_char_boundary(&self, index: usize) -> bool {
1510 if index == self.len() { return true; }
1511 match self.as_bytes().get(index) {
1513 Some(&b) => b < 128 || b >= 192,
1518 fn char_range_at(&self, i: usize) -> CharRange {
1519 let (c, n) = char_range_at_raw(self.as_bytes(), i);
1520 CharRange { ch: unsafe { mem::transmute(c) }, next: n }
1524 fn char_range_at_reverse(&self, start: usize) -> CharRange {
1525 let mut prev = start;
1527 prev = prev.saturating_sub(1);
1528 if self.as_bytes()[prev] < 128 {
1529 return CharRange{ch: self.as_bytes()[prev] as char, next: prev}
1532 // Multibyte case is a fn to allow char_range_at_reverse to inline cleanly
1533 fn multibyte_char_range_at_reverse(s: &str, mut i: usize) -> CharRange {
1534 // while there is a previous byte == 10......
1535 while i > 0 && s.as_bytes()[i] & !CONT_MASK == TAG_CONT_U8 {
1539 let first= s.as_bytes()[i];
1540 let w = UTF8_CHAR_WIDTH[first as usize];
1543 let mut val = utf8_first_byte(first, w as u32);
1544 val = utf8_acc_cont_byte(val, s.as_bytes()[i + 1]);
1545 if w > 2 { val = utf8_acc_cont_byte(val, s.as_bytes()[i + 2]); }
1546 if w > 3 { val = utf8_acc_cont_byte(val, s.as_bytes()[i + 3]); }
1548 return CharRange {ch: unsafe { mem::transmute(val) }, next: i};
1551 return multibyte_char_range_at_reverse(self, prev);
1555 fn char_at(&self, i: usize) -> char {
1556 self.char_range_at(i).ch
1560 fn char_at_reverse(&self, i: usize) -> char {
1561 self.char_range_at_reverse(i).ch
1565 fn as_bytes(&self) -> &[u8] {
1566 unsafe { mem::transmute(self) }
1569 fn find<'a, P: Pattern<'a>>(&'a self, pat: P) -> Option<usize> {
1570 pat.into_searcher(self).next_match().map(|(i, _)| i)
1573 fn rfind<'a, P: Pattern<'a>>(&'a self, pat: P) -> Option<usize>
1574 where P::Searcher: ReverseSearcher<'a>
1576 pat.into_searcher(self).next_match_back().map(|(i, _)| i)
1579 fn find_str<'a, P: Pattern<'a>>(&'a self, pat: P) -> Option<usize> {
1583 fn split_at(&self, mid: usize) -> (&str, &str) {
1584 // is_char_boundary checks that the index is in [0, .len()]
1585 if self.is_char_boundary(mid) {
1587 (self.slice_unchecked(0, mid),
1588 self.slice_unchecked(mid, self.len()))
1591 slice_error_fail(self, 0, mid)
1595 fn split_at_mut(&mut self, mid: usize) -> (&mut str, &mut str) {
1596 // is_char_boundary checks that the index is in [0, .len()]
1597 if self.is_char_boundary(mid) {
1598 let len = self.len();
1600 let self2: &mut str = mem::transmute_copy(&self);
1601 (self.slice_mut_unchecked(0, mid),
1602 self2.slice_mut_unchecked(mid, len))
1605 slice_error_fail(self, 0, mid)
1610 fn slice_shift_char(&self) -> Option<(char, &str)> {
1611 if self.is_empty() {
1614 let ch = self.char_at(0);
1615 let next_s = unsafe { self.slice_unchecked(ch.len_utf8(), self.len()) };
1620 fn subslice_offset(&self, inner: &str) -> usize {
1621 let a_start = self.as_ptr() as usize;
1622 let a_end = a_start + self.len();
1623 let b_start = inner.as_ptr() as usize;
1624 let b_end = b_start + inner.len();
1626 assert!(a_start <= b_start);
1627 assert!(b_end <= a_end);
1632 fn as_ptr(&self) -> *const u8 {
1637 fn len(&self) -> usize { self.repr().len }
1640 fn is_empty(&self) -> bool { self.len() == 0 }
1643 fn parse<T: FromStr>(&self) -> Result<T, T::Err> { FromStr::from_str(self) }
1646 #[stable(feature = "rust1", since = "1.0.0")]
1647 impl AsRef<[u8]> for str {
1649 fn as_ref(&self) -> &[u8] {
1654 /// Pluck a code point out of a UTF-8-like byte slice and return the
1655 /// index of the next code point.
1657 fn char_range_at_raw(bytes: &[u8], i: usize) -> (u32, usize) {
1659 return (bytes[i] as u32, i + 1);
1662 // Multibyte case is a fn to allow char_range_at to inline cleanly
1663 fn multibyte_char_range_at(bytes: &[u8], i: usize) -> (u32, usize) {
1664 let first = bytes[i];
1665 let w = UTF8_CHAR_WIDTH[first as usize];
1668 let mut val = utf8_first_byte(first, w as u32);
1669 val = utf8_acc_cont_byte(val, bytes[i + 1]);
1670 if w > 2 { val = utf8_acc_cont_byte(val, bytes[i + 2]); }
1671 if w > 3 { val = utf8_acc_cont_byte(val, bytes[i + 3]); }
1673 return (val, i + w as usize);
1676 multibyte_char_range_at(bytes, i)
1679 #[stable(feature = "rust1", since = "1.0.0")]
1680 impl<'a> Default for &'a str {
1681 #[stable(feature = "rust1", since = "1.0.0")]
1682 fn default() -> &'a str { "" }