2014-04-30 23:06:36 -07:00
|
|
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
//! String manipulation
|
|
|
|
//!
|
|
|
|
//! For more details, see std::str
|
|
|
|
|
2015-06-09 11:18:03 -07:00
|
|
|
#![stable(feature = "rust1", since = "1.0.0")]
|
2014-05-28 19:53:37 -07:00
|
|
|
|
2015-03-23 14:21:42 +01:00
|
|
|
use self::pattern::Pattern;
|
|
|
|
use self::pattern::{Searcher, ReverseSearcher, DoubleEndedSearcher};
|
2014-11-06 00:05:53 -08:00
|
|
|
|
2015-07-24 03:04:55 +02:00
|
|
|
use char::{self, CharExt};
|
2015-01-23 10:54:32 -05:00
|
|
|
use clone::Clone;
|
2015-06-14 23:17:17 +02:00
|
|
|
use cmp::Eq;
|
2015-05-06 15:53:34 -07:00
|
|
|
use convert::AsRef;
|
2014-04-30 23:06:36 -07:00
|
|
|
use default::Default;
|
2015-01-20 15:45:07 -08:00
|
|
|
use fmt;
|
2015-01-03 00:28:23 -05:00
|
|
|
use iter::ExactSizeIterator;
|
2015-03-11 22:41:24 -07:00
|
|
|
use iter::{Map, Iterator, DoubleEndedIterator};
|
2015-08-07 09:27:27 -04:00
|
|
|
use marker::Sized;
|
2014-11-20 10:11:15 -08:00
|
|
|
use mem;
|
2015-02-15 15:09:26 -05:00
|
|
|
use ops::{Fn, FnMut, FnOnce};
|
2015-01-03 22:42:21 -05:00
|
|
|
use option::Option::{self, None, Some};
|
2014-11-20 10:11:15 -08:00
|
|
|
use raw::{Repr, Slice};
|
2015-01-03 22:42:21 -05:00
|
|
|
use result::Result::{self, Ok, Err};
|
|
|
|
use slice::{self, SliceExt};
|
2014-04-30 23:06:36 -07:00
|
|
|
|
2015-03-23 14:21:42 +01:00
|
|
|
pub mod pattern;
|
2014-12-18 02:12:53 +01:00
|
|
|
|
2014-11-15 15:52:00 +11:00
|
|
|
/// A trait to abstract the idea of creating a new instance of a type from a
|
|
|
|
/// string.
|
2015-01-27 22:52:32 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-08-07 09:27:27 -04:00
|
|
|
pub trait FromStr: Sized {
|
2015-01-27 22:52:32 -08:00
|
|
|
/// The associated error which can be returned from parsing.
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
type Err;
|
|
|
|
|
2015-05-11 00:22:04 +02:00
|
|
|
/// Parses a string `s` to return a value of this type.
|
|
|
|
///
|
|
|
|
/// If parsing succeeds, return the value inside `Ok`, otherwise
|
|
|
|
/// when the string is ill-formatted return an error specific to the
|
|
|
|
/// inside `Err`. The error type is specific to implementation of the trait.
|
2015-01-27 22:52:32 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err>;
|
2014-11-15 15:52:00 +11:00
|
|
|
}
|
|
|
|
|
2015-01-27 22:52:32 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-11-15 15:52:00 +11:00
|
|
|
impl FromStr for bool {
|
2015-01-27 22:52:32 -08:00
|
|
|
type Err = ParseBoolError;
|
|
|
|
|
2014-11-15 15:52:00 +11:00
|
|
|
/// Parse a `bool` from a string.
|
|
|
|
///
|
2015-03-02 18:01:01 +08:00
|
|
|
/// Yields a `Result<bool, ParseBoolError>`, because `s` may or may not
|
|
|
|
/// actually be parseable.
|
2014-11-15 15:52:00 +11:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
2015-03-12 22:42:38 -04:00
|
|
|
/// ```
|
2015-03-02 18:01:01 +08:00
|
|
|
/// use std::str::FromStr;
|
|
|
|
///
|
|
|
|
/// assert_eq!(FromStr::from_str("true"), Ok(true));
|
|
|
|
/// assert_eq!(FromStr::from_str("false"), Ok(false));
|
|
|
|
/// assert!(<bool as FromStr>::from_str("not even a boolean").is_err());
|
|
|
|
/// ```
|
|
|
|
///
|
2015-03-24 19:57:49 +01:00
|
|
|
/// Note, in many cases, the `.parse()` method on `str` is more proper.
|
2015-03-02 18:01:01 +08:00
|
|
|
///
|
2015-03-12 22:42:38 -04:00
|
|
|
/// ```
|
2015-01-27 22:52:32 -08:00
|
|
|
/// assert_eq!("true".parse(), Ok(true));
|
|
|
|
/// assert_eq!("false".parse(), Ok(false));
|
|
|
|
/// assert!("not even a boolean".parse::<bool>().is_err());
|
2014-11-15 15:52:00 +11:00
|
|
|
/// ```
|
|
|
|
#[inline]
|
2015-01-27 22:52:32 -08:00
|
|
|
fn from_str(s: &str) -> Result<bool, ParseBoolError> {
|
2014-11-15 15:52:00 +11:00
|
|
|
match s {
|
2015-01-27 22:52:32 -08:00
|
|
|
"true" => Ok(true),
|
|
|
|
"false" => Ok(false),
|
|
|
|
_ => Err(ParseBoolError { _priv: () }),
|
2014-11-15 15:52:00 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-27 22:52:32 -08:00
|
|
|
/// An error returned when parsing a `bool` from a string fails.
|
2015-01-30 12:26:44 -08:00
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
2015-01-27 22:52:32 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
pub struct ParseBoolError { _priv: () }
|
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
impl fmt::Display for ParseBoolError {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
"provided string was not `true` or `false`".fmt(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-30 23:06:36 -07:00
|
|
|
/*
|
|
|
|
Section: Creating a string
|
|
|
|
*/
|
|
|
|
|
std: Stabilize the std::str module
This commit starts out by consolidating all `str` extension traits into one
`StrExt` trait to be included in the prelude. This means that
`UnicodeStrPrelude`, `StrPrelude`, and `StrAllocating` have all been merged into
one `StrExt` exported by the standard library. Some functionality is currently
duplicated with the `StrExt` present in libcore.
This commit also currently avoids any methods which require any form of pattern
to operate. These functions will be stabilized via a separate RFC.
Next, stability of methods and structures are as follows:
Stable
* from_utf8_unchecked
* CowString - after moving to std::string
* StrExt::as_bytes
* StrExt::as_ptr
* StrExt::bytes/Bytes - also made a struct instead of a typedef
* StrExt::char_indices/CharIndices - CharOffsets was renamed
* StrExt::chars/Chars
* StrExt::is_empty
* StrExt::len
* StrExt::lines/Lines
* StrExt::lines_any/LinesAny
* StrExt::slice_unchecked
* StrExt::trim
* StrExt::trim_left
* StrExt::trim_right
* StrExt::words/Words - also made a struct instead of a typedef
Unstable
* from_utf8 - the error type was changed to a `Result`, but the error type has
yet to prove itself
* from_c_str - this function will be handled by the c_str RFC
* FromStr - this trait will have an associated error type eventually
* StrExt::escape_default - needs iterators at least, unsure if it should make
the cut
* StrExt::escape_unicode - needs iterators at least, unsure if it should make
the cut
* StrExt::slice_chars - this function has yet to prove itself
* StrExt::slice_shift_char - awaiting conventions about slicing and shifting
* StrExt::graphemes/Graphemes - this functionality may only be in libunicode
* StrExt::grapheme_indices/GraphemeIndices - this functionality may only be in
libunicode
* StrExt::width - this functionality may only be in libunicode
* StrExt::utf16_units - this functionality may only be in libunicode
* StrExt::nfd_chars - this functionality may only be in libunicode
* StrExt::nfkd_chars - this functionality may only be in libunicode
* StrExt::nfc_chars - this functionality may only be in libunicode
* StrExt::nfkc_chars - this functionality may only be in libunicode
* StrExt::is_char_boundary - naming is uncertain with container conventions
* StrExt::char_range_at - naming is uncertain with container conventions
* StrExt::char_range_at_reverse - naming is uncertain with container conventions
* StrExt::char_at - naming is uncertain with container conventions
* StrExt::char_at_reverse - naming is uncertain with container conventions
* StrVector::concat - this functionality may be replaced with iterators, but
it's not certain at this time
* StrVector::connect - as with concat, may be deprecated in favor of iterators
Deprecated
* StrAllocating and UnicodeStrPrelude have been merged into StrExit
* eq_slice - compiler implementation detail
* from_str - use the inherent parse() method
* is_utf8 - call from_utf8 instead
* replace - call the method instead
* truncate_utf16_at_nul - this is an implementation detail of windows and does
not need to be exposed.
* utf8_char_width - moved to libunicode
* utf16_items - moved to libunicode
* is_utf16 - moved to libunicode
* Utf16Items - moved to libunicode
* Utf16Item - moved to libunicode
* Utf16Encoder - moved to libunicode
* AnyLines - renamed to LinesAny and made a struct
* SendStr - use CowString<'static> instead
* str::raw - all functionality is deprecated
* StrExt::into_string - call to_string() instead
* StrExt::repeat - use iterators instead
* StrExt::char_len - use .chars().count() instead
* StrExt::is_alphanumeric - use .chars().all(..)
* StrExt::is_whitespace - use .chars().all(..)
Pending deprecation -- while slicing syntax is being worked out, these methods
are all #[unstable]
* Str - while currently used for generic programming, this trait will be
replaced with one of [], deref coercions, or a generic conversion trait.
* StrExt::slice - use slicing syntax instead
* StrExt::slice_to - use slicing syntax instead
* StrExt::slice_from - use slicing syntax instead
* StrExt::lev_distance - deprecated with no replacement
Awaiting stabilization due to patterns and/or matching
* StrExt::contains
* StrExt::contains_char
* StrExt::split
* StrExt::splitn
* StrExt::split_terminator
* StrExt::rsplitn
* StrExt::match_indices
* StrExt::split_str
* StrExt::starts_with
* StrExt::ends_with
* StrExt::trim_chars
* StrExt::trim_left_chars
* StrExt::trim_right_chars
* StrExt::find
* StrExt::rfind
* StrExt::find_str
* StrExt::subslice_offset
2014-12-10 09:02:31 -08:00
|
|
|
/// Errors which can occur when attempting to interpret a byte slice as a `str`.
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Copy, Eq, PartialEq, Clone, Debug)]
|
2015-04-10 16:05:09 -07:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
pub struct Utf8Error {
|
|
|
|
valid_up_to: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Utf8Error {
|
|
|
|
/// Returns the index in the given string up to which valid UTF-8 was
|
|
|
|
/// verified.
|
std: Stabilize the std::str module
This commit starts out by consolidating all `str` extension traits into one
`StrExt` trait to be included in the prelude. This means that
`UnicodeStrPrelude`, `StrPrelude`, and `StrAllocating` have all been merged into
one `StrExt` exported by the standard library. Some functionality is currently
duplicated with the `StrExt` present in libcore.
This commit also currently avoids any methods which require any form of pattern
to operate. These functions will be stabilized via a separate RFC.
Next, stability of methods and structures are as follows:
Stable
* from_utf8_unchecked
* CowString - after moving to std::string
* StrExt::as_bytes
* StrExt::as_ptr
* StrExt::bytes/Bytes - also made a struct instead of a typedef
* StrExt::char_indices/CharIndices - CharOffsets was renamed
* StrExt::chars/Chars
* StrExt::is_empty
* StrExt::len
* StrExt::lines/Lines
* StrExt::lines_any/LinesAny
* StrExt::slice_unchecked
* StrExt::trim
* StrExt::trim_left
* StrExt::trim_right
* StrExt::words/Words - also made a struct instead of a typedef
Unstable
* from_utf8 - the error type was changed to a `Result`, but the error type has
yet to prove itself
* from_c_str - this function will be handled by the c_str RFC
* FromStr - this trait will have an associated error type eventually
* StrExt::escape_default - needs iterators at least, unsure if it should make
the cut
* StrExt::escape_unicode - needs iterators at least, unsure if it should make
the cut
* StrExt::slice_chars - this function has yet to prove itself
* StrExt::slice_shift_char - awaiting conventions about slicing and shifting
* StrExt::graphemes/Graphemes - this functionality may only be in libunicode
* StrExt::grapheme_indices/GraphemeIndices - this functionality may only be in
libunicode
* StrExt::width - this functionality may only be in libunicode
* StrExt::utf16_units - this functionality may only be in libunicode
* StrExt::nfd_chars - this functionality may only be in libunicode
* StrExt::nfkd_chars - this functionality may only be in libunicode
* StrExt::nfc_chars - this functionality may only be in libunicode
* StrExt::nfkc_chars - this functionality may only be in libunicode
* StrExt::is_char_boundary - naming is uncertain with container conventions
* StrExt::char_range_at - naming is uncertain with container conventions
* StrExt::char_range_at_reverse - naming is uncertain with container conventions
* StrExt::char_at - naming is uncertain with container conventions
* StrExt::char_at_reverse - naming is uncertain with container conventions
* StrVector::concat - this functionality may be replaced with iterators, but
it's not certain at this time
* StrVector::connect - as with concat, may be deprecated in favor of iterators
Deprecated
* StrAllocating and UnicodeStrPrelude have been merged into StrExit
* eq_slice - compiler implementation detail
* from_str - use the inherent parse() method
* is_utf8 - call from_utf8 instead
* replace - call the method instead
* truncate_utf16_at_nul - this is an implementation detail of windows and does
not need to be exposed.
* utf8_char_width - moved to libunicode
* utf16_items - moved to libunicode
* is_utf16 - moved to libunicode
* Utf16Items - moved to libunicode
* Utf16Item - moved to libunicode
* Utf16Encoder - moved to libunicode
* AnyLines - renamed to LinesAny and made a struct
* SendStr - use CowString<'static> instead
* str::raw - all functionality is deprecated
* StrExt::into_string - call to_string() instead
* StrExt::repeat - use iterators instead
* StrExt::char_len - use .chars().count() instead
* StrExt::is_alphanumeric - use .chars().all(..)
* StrExt::is_whitespace - use .chars().all(..)
Pending deprecation -- while slicing syntax is being worked out, these methods
are all #[unstable]
* Str - while currently used for generic programming, this trait will be
replaced with one of [], deref coercions, or a generic conversion trait.
* StrExt::slice - use slicing syntax instead
* StrExt::slice_to - use slicing syntax instead
* StrExt::slice_from - use slicing syntax instead
* StrExt::lev_distance - deprecated with no replacement
Awaiting stabilization due to patterns and/or matching
* StrExt::contains
* StrExt::contains_char
* StrExt::split
* StrExt::splitn
* StrExt::split_terminator
* StrExt::rsplitn
* StrExt::match_indices
* StrExt::split_str
* StrExt::starts_with
* StrExt::ends_with
* StrExt::trim_chars
* StrExt::trim_left_chars
* StrExt::trim_right_chars
* StrExt::find
* StrExt::rfind
* StrExt::find_str
* StrExt::subslice_offset
2014-12-10 09:02:31 -08:00
|
|
|
///
|
2015-04-10 16:05:09 -07:00
|
|
|
/// Starting at the index provided, but not necessarily at it precisely, an
|
|
|
|
/// invalid UTF-8 encoding sequence was found.
|
2015-08-12 17:23:48 -07:00
|
|
|
#[unstable(feature = "utf8_error", reason = "method just added",
|
|
|
|
issue = "27734")]
|
2015-04-10 16:05:09 -07:00
|
|
|
pub fn valid_up_to(&self) -> usize { self.valid_up_to }
|
std: Stabilize the std::str module
This commit starts out by consolidating all `str` extension traits into one
`StrExt` trait to be included in the prelude. This means that
`UnicodeStrPrelude`, `StrPrelude`, and `StrAllocating` have all been merged into
one `StrExt` exported by the standard library. Some functionality is currently
duplicated with the `StrExt` present in libcore.
This commit also currently avoids any methods which require any form of pattern
to operate. These functions will be stabilized via a separate RFC.
Next, stability of methods and structures are as follows:
Stable
* from_utf8_unchecked
* CowString - after moving to std::string
* StrExt::as_bytes
* StrExt::as_ptr
* StrExt::bytes/Bytes - also made a struct instead of a typedef
* StrExt::char_indices/CharIndices - CharOffsets was renamed
* StrExt::chars/Chars
* StrExt::is_empty
* StrExt::len
* StrExt::lines/Lines
* StrExt::lines_any/LinesAny
* StrExt::slice_unchecked
* StrExt::trim
* StrExt::trim_left
* StrExt::trim_right
* StrExt::words/Words - also made a struct instead of a typedef
Unstable
* from_utf8 - the error type was changed to a `Result`, but the error type has
yet to prove itself
* from_c_str - this function will be handled by the c_str RFC
* FromStr - this trait will have an associated error type eventually
* StrExt::escape_default - needs iterators at least, unsure if it should make
the cut
* StrExt::escape_unicode - needs iterators at least, unsure if it should make
the cut
* StrExt::slice_chars - this function has yet to prove itself
* StrExt::slice_shift_char - awaiting conventions about slicing and shifting
* StrExt::graphemes/Graphemes - this functionality may only be in libunicode
* StrExt::grapheme_indices/GraphemeIndices - this functionality may only be in
libunicode
* StrExt::width - this functionality may only be in libunicode
* StrExt::utf16_units - this functionality may only be in libunicode
* StrExt::nfd_chars - this functionality may only be in libunicode
* StrExt::nfkd_chars - this functionality may only be in libunicode
* StrExt::nfc_chars - this functionality may only be in libunicode
* StrExt::nfkc_chars - this functionality may only be in libunicode
* StrExt::is_char_boundary - naming is uncertain with container conventions
* StrExt::char_range_at - naming is uncertain with container conventions
* StrExt::char_range_at_reverse - naming is uncertain with container conventions
* StrExt::char_at - naming is uncertain with container conventions
* StrExt::char_at_reverse - naming is uncertain with container conventions
* StrVector::concat - this functionality may be replaced with iterators, but
it's not certain at this time
* StrVector::connect - as with concat, may be deprecated in favor of iterators
Deprecated
* StrAllocating and UnicodeStrPrelude have been merged into StrExit
* eq_slice - compiler implementation detail
* from_str - use the inherent parse() method
* is_utf8 - call from_utf8 instead
* replace - call the method instead
* truncate_utf16_at_nul - this is an implementation detail of windows and does
not need to be exposed.
* utf8_char_width - moved to libunicode
* utf16_items - moved to libunicode
* is_utf16 - moved to libunicode
* Utf16Items - moved to libunicode
* Utf16Item - moved to libunicode
* Utf16Encoder - moved to libunicode
* AnyLines - renamed to LinesAny and made a struct
* SendStr - use CowString<'static> instead
* str::raw - all functionality is deprecated
* StrExt::into_string - call to_string() instead
* StrExt::repeat - use iterators instead
* StrExt::char_len - use .chars().count() instead
* StrExt::is_alphanumeric - use .chars().all(..)
* StrExt::is_whitespace - use .chars().all(..)
Pending deprecation -- while slicing syntax is being worked out, these methods
are all #[unstable]
* Str - while currently used for generic programming, this trait will be
replaced with one of [], deref coercions, or a generic conversion trait.
* StrExt::slice - use slicing syntax instead
* StrExt::slice_to - use slicing syntax instead
* StrExt::slice_from - use slicing syntax instead
* StrExt::lev_distance - deprecated with no replacement
Awaiting stabilization due to patterns and/or matching
* StrExt::contains
* StrExt::contains_char
* StrExt::split
* StrExt::splitn
* StrExt::split_terminator
* StrExt::rsplitn
* StrExt::match_indices
* StrExt::split_str
* StrExt::starts_with
* StrExt::ends_with
* StrExt::trim_chars
* StrExt::trim_left_chars
* StrExt::trim_right_chars
* StrExt::find
* StrExt::rfind
* StrExt::find_str
* StrExt::subslice_offset
2014-12-10 09:02:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Converts a slice of bytes to a string slice without performing any
|
|
|
|
/// allocations.
|
2014-04-30 23:06:36 -07:00
|
|
|
///
|
2015-07-24 03:04:55 +02:00
|
|
|
/// Once the slice has been validated as UTF-8, it is transmuted in-place and
|
2014-04-30 23:06:36 -07:00
|
|
|
/// returned as a '&str' instead of a '&[u8]'
|
|
|
|
///
|
std: Stabilize the std::str module
This commit starts out by consolidating all `str` extension traits into one
`StrExt` trait to be included in the prelude. This means that
`UnicodeStrPrelude`, `StrPrelude`, and `StrAllocating` have all been merged into
one `StrExt` exported by the standard library. Some functionality is currently
duplicated with the `StrExt` present in libcore.
This commit also currently avoids any methods which require any form of pattern
to operate. These functions will be stabilized via a separate RFC.
Next, stability of methods and structures are as follows:
Stable
* from_utf8_unchecked
* CowString - after moving to std::string
* StrExt::as_bytes
* StrExt::as_ptr
* StrExt::bytes/Bytes - also made a struct instead of a typedef
* StrExt::char_indices/CharIndices - CharOffsets was renamed
* StrExt::chars/Chars
* StrExt::is_empty
* StrExt::len
* StrExt::lines/Lines
* StrExt::lines_any/LinesAny
* StrExt::slice_unchecked
* StrExt::trim
* StrExt::trim_left
* StrExt::trim_right
* StrExt::words/Words - also made a struct instead of a typedef
Unstable
* from_utf8 - the error type was changed to a `Result`, but the error type has
yet to prove itself
* from_c_str - this function will be handled by the c_str RFC
* FromStr - this trait will have an associated error type eventually
* StrExt::escape_default - needs iterators at least, unsure if it should make
the cut
* StrExt::escape_unicode - needs iterators at least, unsure if it should make
the cut
* StrExt::slice_chars - this function has yet to prove itself
* StrExt::slice_shift_char - awaiting conventions about slicing and shifting
* StrExt::graphemes/Graphemes - this functionality may only be in libunicode
* StrExt::grapheme_indices/GraphemeIndices - this functionality may only be in
libunicode
* StrExt::width - this functionality may only be in libunicode
* StrExt::utf16_units - this functionality may only be in libunicode
* StrExt::nfd_chars - this functionality may only be in libunicode
* StrExt::nfkd_chars - this functionality may only be in libunicode
* StrExt::nfc_chars - this functionality may only be in libunicode
* StrExt::nfkc_chars - this functionality may only be in libunicode
* StrExt::is_char_boundary - naming is uncertain with container conventions
* StrExt::char_range_at - naming is uncertain with container conventions
* StrExt::char_range_at_reverse - naming is uncertain with container conventions
* StrExt::char_at - naming is uncertain with container conventions
* StrExt::char_at_reverse - naming is uncertain with container conventions
* StrVector::concat - this functionality may be replaced with iterators, but
it's not certain at this time
* StrVector::connect - as with concat, may be deprecated in favor of iterators
Deprecated
* StrAllocating and UnicodeStrPrelude have been merged into StrExit
* eq_slice - compiler implementation detail
* from_str - use the inherent parse() method
* is_utf8 - call from_utf8 instead
* replace - call the method instead
* truncate_utf16_at_nul - this is an implementation detail of windows and does
not need to be exposed.
* utf8_char_width - moved to libunicode
* utf16_items - moved to libunicode
* is_utf16 - moved to libunicode
* Utf16Items - moved to libunicode
* Utf16Item - moved to libunicode
* Utf16Encoder - moved to libunicode
* AnyLines - renamed to LinesAny and made a struct
* SendStr - use CowString<'static> instead
* str::raw - all functionality is deprecated
* StrExt::into_string - call to_string() instead
* StrExt::repeat - use iterators instead
* StrExt::char_len - use .chars().count() instead
* StrExt::is_alphanumeric - use .chars().all(..)
* StrExt::is_whitespace - use .chars().all(..)
Pending deprecation -- while slicing syntax is being worked out, these methods
are all #[unstable]
* Str - while currently used for generic programming, this trait will be
replaced with one of [], deref coercions, or a generic conversion trait.
* StrExt::slice - use slicing syntax instead
* StrExt::slice_to - use slicing syntax instead
* StrExt::slice_from - use slicing syntax instead
* StrExt::lev_distance - deprecated with no replacement
Awaiting stabilization due to patterns and/or matching
* StrExt::contains
* StrExt::contains_char
* StrExt::split
* StrExt::splitn
* StrExt::split_terminator
* StrExt::rsplitn
* StrExt::match_indices
* StrExt::split_str
* StrExt::starts_with
* StrExt::ends_with
* StrExt::trim_chars
* StrExt::trim_left_chars
* StrExt::trim_right_chars
* StrExt::find
* StrExt::rfind
* StrExt::find_str
* StrExt::subslice_offset
2014-12-10 09:02:31 -08:00
|
|
|
/// # Failure
|
|
|
|
///
|
2015-07-24 03:04:55 +02:00
|
|
|
/// Returns `Err` if the slice is not UTF-8 with a description as to why the
|
|
|
|
/// provided slice is not UTF-8.
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
std: Stabilize the std::str module
This commit starts out by consolidating all `str` extension traits into one
`StrExt` trait to be included in the prelude. This means that
`UnicodeStrPrelude`, `StrPrelude`, and `StrAllocating` have all been merged into
one `StrExt` exported by the standard library. Some functionality is currently
duplicated with the `StrExt` present in libcore.
This commit also currently avoids any methods which require any form of pattern
to operate. These functions will be stabilized via a separate RFC.
Next, stability of methods and structures are as follows:
Stable
* from_utf8_unchecked
* CowString - after moving to std::string
* StrExt::as_bytes
* StrExt::as_ptr
* StrExt::bytes/Bytes - also made a struct instead of a typedef
* StrExt::char_indices/CharIndices - CharOffsets was renamed
* StrExt::chars/Chars
* StrExt::is_empty
* StrExt::len
* StrExt::lines/Lines
* StrExt::lines_any/LinesAny
* StrExt::slice_unchecked
* StrExt::trim
* StrExt::trim_left
* StrExt::trim_right
* StrExt::words/Words - also made a struct instead of a typedef
Unstable
* from_utf8 - the error type was changed to a `Result`, but the error type has
yet to prove itself
* from_c_str - this function will be handled by the c_str RFC
* FromStr - this trait will have an associated error type eventually
* StrExt::escape_default - needs iterators at least, unsure if it should make
the cut
* StrExt::escape_unicode - needs iterators at least, unsure if it should make
the cut
* StrExt::slice_chars - this function has yet to prove itself
* StrExt::slice_shift_char - awaiting conventions about slicing and shifting
* StrExt::graphemes/Graphemes - this functionality may only be in libunicode
* StrExt::grapheme_indices/GraphemeIndices - this functionality may only be in
libunicode
* StrExt::width - this functionality may only be in libunicode
* StrExt::utf16_units - this functionality may only be in libunicode
* StrExt::nfd_chars - this functionality may only be in libunicode
* StrExt::nfkd_chars - this functionality may only be in libunicode
* StrExt::nfc_chars - this functionality may only be in libunicode
* StrExt::nfkc_chars - this functionality may only be in libunicode
* StrExt::is_char_boundary - naming is uncertain with container conventions
* StrExt::char_range_at - naming is uncertain with container conventions
* StrExt::char_range_at_reverse - naming is uncertain with container conventions
* StrExt::char_at - naming is uncertain with container conventions
* StrExt::char_at_reverse - naming is uncertain with container conventions
* StrVector::concat - this functionality may be replaced with iterators, but
it's not certain at this time
* StrVector::connect - as with concat, may be deprecated in favor of iterators
Deprecated
* StrAllocating and UnicodeStrPrelude have been merged into StrExit
* eq_slice - compiler implementation detail
* from_str - use the inherent parse() method
* is_utf8 - call from_utf8 instead
* replace - call the method instead
* truncate_utf16_at_nul - this is an implementation detail of windows and does
not need to be exposed.
* utf8_char_width - moved to libunicode
* utf16_items - moved to libunicode
* is_utf16 - moved to libunicode
* Utf16Items - moved to libunicode
* Utf16Item - moved to libunicode
* Utf16Encoder - moved to libunicode
* AnyLines - renamed to LinesAny and made a struct
* SendStr - use CowString<'static> instead
* str::raw - all functionality is deprecated
* StrExt::into_string - call to_string() instead
* StrExt::repeat - use iterators instead
* StrExt::char_len - use .chars().count() instead
* StrExt::is_alphanumeric - use .chars().all(..)
* StrExt::is_whitespace - use .chars().all(..)
Pending deprecation -- while slicing syntax is being worked out, these methods
are all #[unstable]
* Str - while currently used for generic programming, this trait will be
replaced with one of [], deref coercions, or a generic conversion trait.
* StrExt::slice - use slicing syntax instead
* StrExt::slice_to - use slicing syntax instead
* StrExt::slice_from - use slicing syntax instead
* StrExt::lev_distance - deprecated with no replacement
Awaiting stabilization due to patterns and/or matching
* StrExt::contains
* StrExt::contains_char
* StrExt::split
* StrExt::splitn
* StrExt::split_terminator
* StrExt::rsplitn
* StrExt::match_indices
* StrExt::split_str
* StrExt::starts_with
* StrExt::ends_with
* StrExt::trim_chars
* StrExt::trim_left_chars
* StrExt::trim_right_chars
* StrExt::find
* StrExt::rfind
* StrExt::find_str
* StrExt::subslice_offset
2014-12-10 09:02:31 -08:00
|
|
|
pub fn from_utf8(v: &[u8]) -> Result<&str, Utf8Error> {
|
|
|
|
try!(run_utf8_validation_iterator(&mut v.iter()));
|
|
|
|
Ok(unsafe { from_utf8_unchecked(v) })
|
2014-11-20 10:11:15 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Converts a slice of bytes to a string slice without checking
|
|
|
|
/// that the string contains valid UTF-8.
|
2015-05-03 12:09:40 +02:00
|
|
|
#[inline(always)]
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-11-20 10:11:15 -08:00
|
|
|
pub unsafe fn from_utf8_unchecked<'a>(v: &'a [u8]) -> &'a str {
|
|
|
|
mem::transmute(v)
|
|
|
|
}
|
|
|
|
|
2015-01-24 09:15:42 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-20 15:45:07 -08:00
|
|
|
impl fmt::Display for Utf8Error {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2015-04-10 16:05:09 -07:00
|
|
|
write!(f, "invalid utf-8: invalid byte near index {}", self.valid_up_to)
|
2015-01-20 15:45:07 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-30 23:06:36 -07:00
|
|
|
/*
|
|
|
|
Section: Iterators
|
|
|
|
*/
|
|
|
|
|
2014-07-17 19:34:07 +02:00
|
|
|
/// Iterator for the char (representing *Unicode Scalar Values*) of a string
|
|
|
|
///
|
|
|
|
/// Created with the method `.chars()`.
|
2015-01-23 10:54:32 -05:00
|
|
|
#[derive(Clone)]
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-04-30 23:06:36 -07:00
|
|
|
pub struct Chars<'a> {
|
2014-12-19 21:52:10 +01:00
|
|
|
iter: slice::Iter<'a, u8>
|
2014-07-17 19:34:07 +02:00
|
|
|
}
|
|
|
|
|
2015-03-10 12:06:44 +01:00
|
|
|
/// Return the initial codepoint accumulator for the first byte.
|
|
|
|
/// The first byte is special, only want bottom 5 bits for width 2, 4 bits
|
|
|
|
/// for width 3, and 3 bits for width 4.
|
|
|
|
#[inline]
|
|
|
|
fn utf8_first_byte(byte: u8, width: u32) -> u32 { (byte & (0x7F >> width)) as u32 }
|
2014-07-17 19:34:07 +02:00
|
|
|
|
2015-03-10 12:06:44 +01:00
|
|
|
/// Return the value of `ch` updated with continuation byte `byte`.
|
|
|
|
#[inline]
|
|
|
|
fn utf8_acc_cont_byte(ch: u32, byte: u8) -> u32 { (ch << 6) | (byte & CONT_MASK) as u32 }
|
2014-07-17 19:34:07 +02:00
|
|
|
|
2015-03-10 12:06:44 +01:00
|
|
|
/// Checks whether the byte is a UTF-8 continuation byte (i.e. starts with the
|
|
|
|
/// bits `10`).
|
|
|
|
#[inline]
|
|
|
|
fn utf8_is_cont_byte(byte: u8) -> bool { (byte & !CONT_MASK) == TAG_CONT_U8 }
|
2014-07-17 19:34:07 +02:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn unwrap_or_0(opt: Option<&u8>) -> u8 {
|
|
|
|
match opt {
|
|
|
|
Some(&byte) => byte,
|
|
|
|
None => 0,
|
|
|
|
}
|
2014-04-30 23:06:36 -07:00
|
|
|
}
|
|
|
|
|
2015-01-21 15:55:31 -08:00
|
|
|
/// Reads the next code point out of a byte iterator (assuming a
|
|
|
|
/// UTF-8-like encoding).
|
2015-08-12 17:23:48 -07:00
|
|
|
#[unstable(feature = "str_internals", issue = "0")]
|
2015-01-27 14:09:18 +01:00
|
|
|
#[inline]
|
2015-01-21 15:55:31 -08:00
|
|
|
pub fn next_code_point(bytes: &mut slice::Iter<u8>) -> Option<u32> {
|
|
|
|
// Decode UTF-8
|
|
|
|
let x = match bytes.next() {
|
|
|
|
None => return None,
|
|
|
|
Some(&next_byte) if next_byte < 128 => return Some(next_byte as u32),
|
|
|
|
Some(&next_byte) => next_byte,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Multibyte case follows
|
|
|
|
// Decode from a byte combination out of: [[[x y] z] w]
|
|
|
|
// NOTE: Performance is sensitive to the exact formulation here
|
2015-03-10 12:06:44 +01:00
|
|
|
let init = utf8_first_byte(x, 2);
|
2015-01-21 15:55:31 -08:00
|
|
|
let y = unwrap_or_0(bytes.next());
|
2015-03-10 12:06:44 +01:00
|
|
|
let mut ch = utf8_acc_cont_byte(init, y);
|
2015-01-21 15:55:31 -08:00
|
|
|
if x >= 0xE0 {
|
|
|
|
// [[x y z] w] case
|
|
|
|
// 5th bit in 0xE0 .. 0xEF is always clear, so `init` is still valid
|
|
|
|
let z = unwrap_or_0(bytes.next());
|
2015-03-10 12:06:44 +01:00
|
|
|
let y_z = utf8_acc_cont_byte((y & CONT_MASK) as u32, z);
|
2015-01-21 15:55:31 -08:00
|
|
|
ch = init << 12 | y_z;
|
|
|
|
if x >= 0xF0 {
|
|
|
|
// [x y z w] case
|
|
|
|
// use only the lower 3 bits of `init`
|
|
|
|
let w = unwrap_or_0(bytes.next());
|
2015-03-10 12:06:44 +01:00
|
|
|
ch = (init & 7) << 18 | utf8_acc_cont_byte(y_z, w);
|
2015-01-21 15:55:31 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(ch)
|
|
|
|
}
|
|
|
|
|
2015-01-27 14:09:18 +01:00
|
|
|
/// Reads the last code point out of a byte iterator (assuming a
|
|
|
|
/// UTF-8-like encoding).
|
|
|
|
#[inline]
|
2015-06-10 18:10:12 -07:00
|
|
|
fn next_code_point_reverse(bytes: &mut slice::Iter<u8>) -> Option<u32> {
|
2015-01-27 14:09:18 +01:00
|
|
|
// Decode UTF-8
|
|
|
|
let w = match bytes.next_back() {
|
|
|
|
None => return None,
|
|
|
|
Some(&next_byte) if next_byte < 128 => return Some(next_byte as u32),
|
|
|
|
Some(&back_byte) => back_byte,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Multibyte case follows
|
|
|
|
// Decode from a byte combination out of: [x [y [z w]]]
|
|
|
|
let mut ch;
|
|
|
|
let z = unwrap_or_0(bytes.next_back());
|
2015-03-10 12:06:44 +01:00
|
|
|
ch = utf8_first_byte(z, 2);
|
|
|
|
if utf8_is_cont_byte(z) {
|
2015-01-27 14:09:18 +01:00
|
|
|
let y = unwrap_or_0(bytes.next_back());
|
2015-03-10 12:06:44 +01:00
|
|
|
ch = utf8_first_byte(y, 3);
|
|
|
|
if utf8_is_cont_byte(y) {
|
2015-01-27 14:09:18 +01:00
|
|
|
let x = unwrap_or_0(bytes.next_back());
|
2015-03-10 12:06:44 +01:00
|
|
|
ch = utf8_first_byte(x, 4);
|
|
|
|
ch = utf8_acc_cont_byte(ch, y);
|
2015-01-27 14:09:18 +01:00
|
|
|
}
|
2015-03-10 12:06:44 +01:00
|
|
|
ch = utf8_acc_cont_byte(ch, z);
|
2015-01-27 14:09:18 +01:00
|
|
|
}
|
2015-03-10 12:06:44 +01:00
|
|
|
ch = utf8_acc_cont_byte(ch, w);
|
2015-01-27 14:09:18 +01:00
|
|
|
|
|
|
|
Some(ch)
|
|
|
|
}
|
|
|
|
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-29 16:18:41 -05:00
|
|
|
impl<'a> Iterator for Chars<'a> {
|
|
|
|
type Item = char;
|
|
|
|
|
2014-04-30 23:06:36 -07:00
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<char> {
|
2015-01-21 15:55:31 -08:00
|
|
|
next_code_point(&mut self.iter).map(|ch| {
|
|
|
|
// str invariant says `ch` is a valid Unicode Scalar Value
|
|
|
|
unsafe {
|
2015-07-24 03:04:55 +02:00
|
|
|
char::from_u32_unchecked(ch)
|
2014-07-17 19:34:07 +02:00
|
|
|
}
|
2015-01-21 15:55:31 -08:00
|
|
|
})
|
2014-04-30 23:06:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2015-01-27 14:09:18 +01:00
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
2014-07-17 19:34:07 +02:00
|
|
|
let (len, _) = self.iter.size_hint();
|
2015-03-10 14:18:24 +01:00
|
|
|
// `(len + 3)` can't overflow, because we know that the `slice::Iter`
|
|
|
|
// belongs to a slice in memory which has a maximum length of
|
|
|
|
// `isize::MAX` (that's well below `usize::MAX`).
|
|
|
|
((len + 3) / 4, Some(len))
|
2014-04-30 23:06:36 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-29 16:18:41 -05:00
|
|
|
impl<'a> DoubleEndedIterator for Chars<'a> {
|
2014-04-30 23:06:36 -07:00
|
|
|
#[inline]
|
|
|
|
fn next_back(&mut self) -> Option<char> {
|
2015-01-27 14:09:18 +01:00
|
|
|
next_code_point_reverse(&mut self.iter).map(|ch| {
|
|
|
|
// str invariant says `ch` is a valid Unicode Scalar Value
|
|
|
|
unsafe {
|
2015-07-24 03:04:55 +02:00
|
|
|
char::from_u32_unchecked(ch)
|
2014-07-17 19:34:07 +02:00
|
|
|
}
|
2015-01-27 14:09:18 +01:00
|
|
|
})
|
2014-04-30 23:06:36 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-05 18:54:07 +02:00
|
|
|
/// Iterator for a string's characters and their byte offsets.
|
2015-01-03 22:54:18 -05:00
|
|
|
#[derive(Clone)]
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
std: Stabilize the std::str module
This commit starts out by consolidating all `str` extension traits into one
`StrExt` trait to be included in the prelude. This means that
`UnicodeStrPrelude`, `StrPrelude`, and `StrAllocating` have all been merged into
one `StrExt` exported by the standard library. Some functionality is currently
duplicated with the `StrExt` present in libcore.
This commit also currently avoids any methods which require any form of pattern
to operate. These functions will be stabilized via a separate RFC.
Next, stability of methods and structures are as follows:
Stable
* from_utf8_unchecked
* CowString - after moving to std::string
* StrExt::as_bytes
* StrExt::as_ptr
* StrExt::bytes/Bytes - also made a struct instead of a typedef
* StrExt::char_indices/CharIndices - CharOffsets was renamed
* StrExt::chars/Chars
* StrExt::is_empty
* StrExt::len
* StrExt::lines/Lines
* StrExt::lines_any/LinesAny
* StrExt::slice_unchecked
* StrExt::trim
* StrExt::trim_left
* StrExt::trim_right
* StrExt::words/Words - also made a struct instead of a typedef
Unstable
* from_utf8 - the error type was changed to a `Result`, but the error type has
yet to prove itself
* from_c_str - this function will be handled by the c_str RFC
* FromStr - this trait will have an associated error type eventually
* StrExt::escape_default - needs iterators at least, unsure if it should make
the cut
* StrExt::escape_unicode - needs iterators at least, unsure if it should make
the cut
* StrExt::slice_chars - this function has yet to prove itself
* StrExt::slice_shift_char - awaiting conventions about slicing and shifting
* StrExt::graphemes/Graphemes - this functionality may only be in libunicode
* StrExt::grapheme_indices/GraphemeIndices - this functionality may only be in
libunicode
* StrExt::width - this functionality may only be in libunicode
* StrExt::utf16_units - this functionality may only be in libunicode
* StrExt::nfd_chars - this functionality may only be in libunicode
* StrExt::nfkd_chars - this functionality may only be in libunicode
* StrExt::nfc_chars - this functionality may only be in libunicode
* StrExt::nfkc_chars - this functionality may only be in libunicode
* StrExt::is_char_boundary - naming is uncertain with container conventions
* StrExt::char_range_at - naming is uncertain with container conventions
* StrExt::char_range_at_reverse - naming is uncertain with container conventions
* StrExt::char_at - naming is uncertain with container conventions
* StrExt::char_at_reverse - naming is uncertain with container conventions
* StrVector::concat - this functionality may be replaced with iterators, but
it's not certain at this time
* StrVector::connect - as with concat, may be deprecated in favor of iterators
Deprecated
* StrAllocating and UnicodeStrPrelude have been merged into StrExit
* eq_slice - compiler implementation detail
* from_str - use the inherent parse() method
* is_utf8 - call from_utf8 instead
* replace - call the method instead
* truncate_utf16_at_nul - this is an implementation detail of windows and does
not need to be exposed.
* utf8_char_width - moved to libunicode
* utf16_items - moved to libunicode
* is_utf16 - moved to libunicode
* Utf16Items - moved to libunicode
* Utf16Item - moved to libunicode
* Utf16Encoder - moved to libunicode
* AnyLines - renamed to LinesAny and made a struct
* SendStr - use CowString<'static> instead
* str::raw - all functionality is deprecated
* StrExt::into_string - call to_string() instead
* StrExt::repeat - use iterators instead
* StrExt::char_len - use .chars().count() instead
* StrExt::is_alphanumeric - use .chars().all(..)
* StrExt::is_whitespace - use .chars().all(..)
Pending deprecation -- while slicing syntax is being worked out, these methods
are all #[unstable]
* Str - while currently used for generic programming, this trait will be
replaced with one of [], deref coercions, or a generic conversion trait.
* StrExt::slice - use slicing syntax instead
* StrExt::slice_to - use slicing syntax instead
* StrExt::slice_from - use slicing syntax instead
* StrExt::lev_distance - deprecated with no replacement
Awaiting stabilization due to patterns and/or matching
* StrExt::contains
* StrExt::contains_char
* StrExt::split
* StrExt::splitn
* StrExt::split_terminator
* StrExt::rsplitn
* StrExt::match_indices
* StrExt::split_str
* StrExt::starts_with
* StrExt::ends_with
* StrExt::trim_chars
* StrExt::trim_left_chars
* StrExt::trim_right_chars
* StrExt::find
* StrExt::rfind
* StrExt::find_str
* StrExt::subslice_offset
2014-12-10 09:02:31 -08:00
|
|
|
pub struct CharIndices<'a> {
|
2015-01-27 14:09:18 +01:00
|
|
|
front_offset: usize,
|
2014-04-30 23:06:36 -07:00
|
|
|
iter: Chars<'a>,
|
|
|
|
}
|
|
|
|
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-29 16:18:41 -05:00
|
|
|
impl<'a> Iterator for CharIndices<'a> {
|
2015-01-27 14:09:18 +01:00
|
|
|
type Item = (usize, char);
|
2014-12-29 16:18:41 -05:00
|
|
|
|
2014-04-30 23:06:36 -07:00
|
|
|
#[inline]
|
2015-01-27 14:09:18 +01:00
|
|
|
fn next(&mut self) -> Option<(usize, char)> {
|
2014-07-19 15:39:02 +02:00
|
|
|
let (pre_len, _) = self.iter.iter.size_hint();
|
2014-07-17 19:34:07 +02:00
|
|
|
match self.iter.next() {
|
|
|
|
None => None,
|
|
|
|
Some(ch) => {
|
2014-07-19 15:39:02 +02:00
|
|
|
let index = self.front_offset;
|
2014-07-17 19:34:07 +02:00
|
|
|
let (len, _) = self.iter.iter.size_hint();
|
2014-07-19 15:39:02 +02:00
|
|
|
self.front_offset += pre_len - len;
|
2014-07-17 19:34:07 +02:00
|
|
|
Some((index, ch))
|
|
|
|
}
|
|
|
|
}
|
2014-04-30 23:06:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2015-01-27 14:09:18 +01:00
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
2014-04-30 23:06:36 -07:00
|
|
|
self.iter.size_hint()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-29 16:18:41 -05:00
|
|
|
impl<'a> DoubleEndedIterator for CharIndices<'a> {
|
2014-04-30 23:06:36 -07:00
|
|
|
#[inline]
|
2015-01-27 14:09:18 +01:00
|
|
|
fn next_back(&mut self) -> Option<(usize, char)> {
|
2014-07-17 19:34:07 +02:00
|
|
|
match self.iter.next_back() {
|
|
|
|
None => None,
|
|
|
|
Some(ch) => {
|
|
|
|
let (len, _) = self.iter.iter.size_hint();
|
2014-07-19 15:39:02 +02:00
|
|
|
let index = self.front_offset + len;
|
|
|
|
Some((index, ch))
|
2014-07-17 19:34:07 +02:00
|
|
|
}
|
|
|
|
}
|
2014-04-30 23:06:36 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// External iterator for a string's bytes.
|
|
|
|
/// Use with the `std::iter` module.
|
2014-12-18 02:12:53 +01:00
|
|
|
///
|
2015-04-20 09:55:07 -04:00
|
|
|
/// Created with the method `.bytes()`.
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-03 22:54:18 -05:00
|
|
|
#[derive(Clone)]
|
2015-02-01 12:15:36 -08:00
|
|
|
pub struct Bytes<'a>(Map<slice::Iter<'a, u8>, BytesDeref>);
|
2014-12-18 22:16:48 -05:00
|
|
|
|
2015-03-15 01:48:34 +01:00
|
|
|
/// A nameable, clonable fn type
|
|
|
|
#[derive(Clone)]
|
2014-12-18 02:12:53 +01:00
|
|
|
struct BytesDeref;
|
2014-12-18 22:16:48 -05:00
|
|
|
|
2015-02-15 15:09:26 -05:00
|
|
|
impl<'a> Fn<(&'a u8,)> for BytesDeref {
|
|
|
|
#[inline]
|
|
|
|
extern "rust-call" fn call(&self, (ptr,): (&'a u8,)) -> u8 {
|
|
|
|
*ptr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> FnMut<(&'a u8,)> for BytesDeref {
|
|
|
|
#[inline]
|
|
|
|
extern "rust-call" fn call_mut(&mut self, (ptr,): (&'a u8,)) -> u8 {
|
|
|
|
Fn::call(&*self, (ptr,))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> FnOnce<(&'a u8,)> for BytesDeref {
|
|
|
|
type Output = u8;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
extern "rust-call" fn call_once(self, (ptr,): (&'a u8,)) -> u8 {
|
|
|
|
Fn::call(&self, (ptr,))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-15 01:48:34 +01:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
impl<'a> Iterator for Bytes<'a> {
|
|
|
|
type Item = u8;
|
2014-04-30 23:06:36 -07:00
|
|
|
|
2015-03-15 01:48:34 +01:00
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<u8> {
|
|
|
|
self.0.next()
|
|
|
|
}
|
2014-04-30 23:06:36 -07:00
|
|
|
|
2015-03-15 01:48:34 +01:00
|
|
|
#[inline]
|
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
|
|
|
self.0.size_hint()
|
|
|
|
}
|
2014-04-30 23:06:36 -07:00
|
|
|
}
|
|
|
|
|
2015-03-15 01:48:34 +01:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
impl<'a> DoubleEndedIterator for Bytes<'a> {
|
|
|
|
#[inline]
|
|
|
|
fn next_back(&mut self) -> Option<u8> {
|
|
|
|
self.0.next_back()
|
|
|
|
}
|
2014-04-30 23:06:36 -07:00
|
|
|
}
|
|
|
|
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-03-15 01:48:34 +01:00
|
|
|
impl<'a> ExactSizeIterator for Bytes<'a> {
|
|
|
|
#[inline]
|
|
|
|
fn len(&self) -> usize {
|
|
|
|
self.0.len()
|
|
|
|
}
|
2015-03-14 19:34:21 -04:00
|
|
|
}
|
|
|
|
|
2015-04-05 18:52:14 +02:00
|
|
|
/// This macro generates a Clone impl for string pattern API
|
|
|
|
/// wrapper types of the form X<'a, P>
|
|
|
|
macro_rules! derive_pattern_clone {
|
|
|
|
(clone $t:ident with |$s:ident| $e:expr) => {
|
|
|
|
impl<'a, P: Pattern<'a>> Clone for $t<'a, P>
|
|
|
|
where P::Searcher: Clone
|
|
|
|
{
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
let $s = self;
|
|
|
|
$e
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-03-14 20:07:13 -04:00
|
|
|
}
|
2015-03-14 19:34:21 -04:00
|
|
|
|
2015-03-15 01:48:34 +01:00
|
|
|
/// This macro generates two public iterator structs
|
|
|
|
/// wrapping an private internal one that makes use of the `Pattern` API.
|
|
|
|
///
|
|
|
|
/// For all patterns `P: Pattern<'a>` the following items will be
|
2015-05-04 13:21:27 -04:00
|
|
|
/// generated (generics omitted):
|
2015-03-15 01:48:34 +01:00
|
|
|
///
|
|
|
|
/// struct $forward_iterator($internal_iterator);
|
|
|
|
/// struct $reverse_iterator($internal_iterator);
|
|
|
|
///
|
|
|
|
/// impl Iterator for $forward_iterator
|
|
|
|
/// { /* internal ends up calling Searcher::next_match() */ }
|
|
|
|
///
|
|
|
|
/// impl DoubleEndedIterator for $forward_iterator
|
|
|
|
/// where P::Searcher: DoubleEndedSearcher
|
|
|
|
/// { /* internal ends up calling Searcher::next_match_back() */ }
|
|
|
|
///
|
|
|
|
/// impl Iterator for $reverse_iterator
|
|
|
|
/// where P::Searcher: ReverseSearcher
|
|
|
|
/// { /* internal ends up calling Searcher::next_match_back() */ }
|
|
|
|
///
|
|
|
|
/// impl DoubleEndedIterator for $reverse_iterator
|
|
|
|
/// where P::Searcher: DoubleEndedSearcher
|
|
|
|
/// { /* internal ends up calling Searcher::next_match() */ }
|
|
|
|
///
|
|
|
|
/// The internal one is defined outside the macro, and has almost the same
|
|
|
|
/// semantic as a DoubleEndedIterator by delegating to `pattern::Searcher` and
|
|
|
|
/// `pattern::ReverseSearcher` for both forward and reverse iteration.
|
|
|
|
///
|
|
|
|
/// "Almost", because a `Searcher` and a `ReverseSearcher` for a given
|
|
|
|
/// `Pattern` might not return the same elements, so actually implementing
|
|
|
|
/// `DoubleEndedIterator` for it would be incorrect.
|
|
|
|
/// (See the docs in `str::pattern` for more details)
|
|
|
|
///
|
|
|
|
/// However, the internal struct still represents a single ended iterator from
|
|
|
|
/// either end, and depending on pattern is also a valid double ended iterator,
|
|
|
|
/// so the two wrapper structs implement `Iterator`
|
|
|
|
/// and `DoubleEndedIterator` depending on the concrete pattern type, leading
|
|
|
|
/// to the complex impls seen above.
|
|
|
|
macro_rules! generate_pattern_iterators {
|
|
|
|
{
|
|
|
|
// Forward iterator
|
|
|
|
forward:
|
|
|
|
$(#[$forward_iterator_attribute:meta])*
|
|
|
|
struct $forward_iterator:ident;
|
|
|
|
|
|
|
|
// Reverse iterator
|
|
|
|
reverse:
|
|
|
|
$(#[$reverse_iterator_attribute:meta])*
|
|
|
|
struct $reverse_iterator:ident;
|
|
|
|
|
|
|
|
// Stability of all generated items
|
|
|
|
stability:
|
|
|
|
$(#[$common_stability_attribute:meta])*
|
|
|
|
|
|
|
|
// Internal almost-iterator that is being delegated to
|
|
|
|
internal:
|
|
|
|
$internal_iterator:ident yielding ($iterty:ty);
|
|
|
|
|
|
|
|
// Kind of delgation - either single ended or double ended
|
|
|
|
delegate $($t:tt)*
|
|
|
|
} => {
|
|
|
|
$(#[$forward_iterator_attribute])*
|
|
|
|
$(#[$common_stability_attribute])*
|
|
|
|
pub struct $forward_iterator<'a, P: Pattern<'a>>($internal_iterator<'a, P>);
|
|
|
|
|
|
|
|
$(#[$common_stability_attribute])*
|
|
|
|
impl<'a, P: Pattern<'a>> Iterator for $forward_iterator<'a, P> {
|
|
|
|
type Item = $iterty;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<$iterty> {
|
|
|
|
self.0.next()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-05 18:52:14 +02:00
|
|
|
$(#[$common_stability_attribute])*
|
|
|
|
impl<'a, P: Pattern<'a>> Clone for $forward_iterator<'a, P>
|
|
|
|
where P::Searcher: Clone
|
|
|
|
{
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
$forward_iterator(self.0.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-15 01:48:34 +01:00
|
|
|
$(#[$reverse_iterator_attribute])*
|
|
|
|
$(#[$common_stability_attribute])*
|
|
|
|
pub struct $reverse_iterator<'a, P: Pattern<'a>>($internal_iterator<'a, P>);
|
|
|
|
|
|
|
|
$(#[$common_stability_attribute])*
|
|
|
|
impl<'a, P: Pattern<'a>> Iterator for $reverse_iterator<'a, P>
|
|
|
|
where P::Searcher: ReverseSearcher<'a>
|
|
|
|
{
|
|
|
|
type Item = $iterty;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<$iterty> {
|
|
|
|
self.0.next_back()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-05 18:52:14 +02:00
|
|
|
$(#[$common_stability_attribute])*
|
|
|
|
impl<'a, P: Pattern<'a>> Clone for $reverse_iterator<'a, P>
|
|
|
|
where P::Searcher: Clone
|
|
|
|
{
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
$reverse_iterator(self.0.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-15 01:48:34 +01:00
|
|
|
generate_pattern_iterators!($($t)* with $(#[$common_stability_attribute])*,
|
|
|
|
$forward_iterator,
|
|
|
|
$reverse_iterator, $iterty);
|
|
|
|
};
|
|
|
|
{
|
|
|
|
double ended; with $(#[$common_stability_attribute:meta])*,
|
|
|
|
$forward_iterator:ident,
|
|
|
|
$reverse_iterator:ident, $iterty:ty
|
|
|
|
} => {
|
|
|
|
$(#[$common_stability_attribute])*
|
|
|
|
impl<'a, P: Pattern<'a>> DoubleEndedIterator for $forward_iterator<'a, P>
|
|
|
|
where P::Searcher: DoubleEndedSearcher<'a>
|
|
|
|
{
|
|
|
|
#[inline]
|
|
|
|
fn next_back(&mut self) -> Option<$iterty> {
|
|
|
|
self.0.next_back()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$(#[$common_stability_attribute])*
|
|
|
|
impl<'a, P: Pattern<'a>> DoubleEndedIterator for $reverse_iterator<'a, P>
|
|
|
|
where P::Searcher: DoubleEndedSearcher<'a>
|
|
|
|
{
|
|
|
|
#[inline]
|
|
|
|
fn next_back(&mut self) -> Option<$iterty> {
|
|
|
|
self.0.next()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
{
|
|
|
|
single ended; with $(#[$common_stability_attribute:meta])*,
|
|
|
|
$forward_iterator:ident,
|
|
|
|
$reverse_iterator:ident, $iterty:ty
|
|
|
|
} => {}
|
std: Stabilize the std::str module
This commit starts out by consolidating all `str` extension traits into one
`StrExt` trait to be included in the prelude. This means that
`UnicodeStrPrelude`, `StrPrelude`, and `StrAllocating` have all been merged into
one `StrExt` exported by the standard library. Some functionality is currently
duplicated with the `StrExt` present in libcore.
This commit also currently avoids any methods which require any form of pattern
to operate. These functions will be stabilized via a separate RFC.
Next, stability of methods and structures are as follows:
Stable
* from_utf8_unchecked
* CowString - after moving to std::string
* StrExt::as_bytes
* StrExt::as_ptr
* StrExt::bytes/Bytes - also made a struct instead of a typedef
* StrExt::char_indices/CharIndices - CharOffsets was renamed
* StrExt::chars/Chars
* StrExt::is_empty
* StrExt::len
* StrExt::lines/Lines
* StrExt::lines_any/LinesAny
* StrExt::slice_unchecked
* StrExt::trim
* StrExt::trim_left
* StrExt::trim_right
* StrExt::words/Words - also made a struct instead of a typedef
Unstable
* from_utf8 - the error type was changed to a `Result`, but the error type has
yet to prove itself
* from_c_str - this function will be handled by the c_str RFC
* FromStr - this trait will have an associated error type eventually
* StrExt::escape_default - needs iterators at least, unsure if it should make
the cut
* StrExt::escape_unicode - needs iterators at least, unsure if it should make
the cut
* StrExt::slice_chars - this function has yet to prove itself
* StrExt::slice_shift_char - awaiting conventions about slicing and shifting
* StrExt::graphemes/Graphemes - this functionality may only be in libunicode
* StrExt::grapheme_indices/GraphemeIndices - this functionality may only be in
libunicode
* StrExt::width - this functionality may only be in libunicode
* StrExt::utf16_units - this functionality may only be in libunicode
* StrExt::nfd_chars - this functionality may only be in libunicode
* StrExt::nfkd_chars - this functionality may only be in libunicode
* StrExt::nfc_chars - this functionality may only be in libunicode
* StrExt::nfkc_chars - this functionality may only be in libunicode
* StrExt::is_char_boundary - naming is uncertain with container conventions
* StrExt::char_range_at - naming is uncertain with container conventions
* StrExt::char_range_at_reverse - naming is uncertain with container conventions
* StrExt::char_at - naming is uncertain with container conventions
* StrExt::char_at_reverse - naming is uncertain with container conventions
* StrVector::concat - this functionality may be replaced with iterators, but
it's not certain at this time
* StrVector::connect - as with concat, may be deprecated in favor of iterators
Deprecated
* StrAllocating and UnicodeStrPrelude have been merged into StrExit
* eq_slice - compiler implementation detail
* from_str - use the inherent parse() method
* is_utf8 - call from_utf8 instead
* replace - call the method instead
* truncate_utf16_at_nul - this is an implementation detail of windows and does
not need to be exposed.
* utf8_char_width - moved to libunicode
* utf16_items - moved to libunicode
* is_utf16 - moved to libunicode
* Utf16Items - moved to libunicode
* Utf16Item - moved to libunicode
* Utf16Encoder - moved to libunicode
* AnyLines - renamed to LinesAny and made a struct
* SendStr - use CowString<'static> instead
* str::raw - all functionality is deprecated
* StrExt::into_string - call to_string() instead
* StrExt::repeat - use iterators instead
* StrExt::char_len - use .chars().count() instead
* StrExt::is_alphanumeric - use .chars().all(..)
* StrExt::is_whitespace - use .chars().all(..)
Pending deprecation -- while slicing syntax is being worked out, these methods
are all #[unstable]
* Str - while currently used for generic programming, this trait will be
replaced with one of [], deref coercions, or a generic conversion trait.
* StrExt::slice - use slicing syntax instead
* StrExt::slice_to - use slicing syntax instead
* StrExt::slice_from - use slicing syntax instead
* StrExt::lev_distance - deprecated with no replacement
Awaiting stabilization due to patterns and/or matching
* StrExt::contains
* StrExt::contains_char
* StrExt::split
* StrExt::splitn
* StrExt::split_terminator
* StrExt::rsplitn
* StrExt::match_indices
* StrExt::split_str
* StrExt::starts_with
* StrExt::ends_with
* StrExt::trim_chars
* StrExt::trim_left_chars
* StrExt::trim_right_chars
* StrExt::find
* StrExt::rfind
* StrExt::find_str
* StrExt::subslice_offset
2014-12-10 09:02:31 -08:00
|
|
|
}
|
|
|
|
|
2015-04-05 18:52:14 +02:00
|
|
|
derive_pattern_clone!{
|
|
|
|
clone SplitInternal
|
|
|
|
with |s| SplitInternal { matcher: s.matcher.clone(), ..*s }
|
|
|
|
}
|
2015-03-15 01:48:34 +01:00
|
|
|
struct SplitInternal<'a, P: Pattern<'a>> {
|
|
|
|
start: usize,
|
|
|
|
end: usize,
|
|
|
|
matcher: P::Searcher,
|
|
|
|
allow_trailing_empty: bool,
|
|
|
|
finished: bool,
|
std: Stabilize the std::str module
This commit starts out by consolidating all `str` extension traits into one
`StrExt` trait to be included in the prelude. This means that
`UnicodeStrPrelude`, `StrPrelude`, and `StrAllocating` have all been merged into
one `StrExt` exported by the standard library. Some functionality is currently
duplicated with the `StrExt` present in libcore.
This commit also currently avoids any methods which require any form of pattern
to operate. These functions will be stabilized via a separate RFC.
Next, stability of methods and structures are as follows:
Stable
* from_utf8_unchecked
* CowString - after moving to std::string
* StrExt::as_bytes
* StrExt::as_ptr
* StrExt::bytes/Bytes - also made a struct instead of a typedef
* StrExt::char_indices/CharIndices - CharOffsets was renamed
* StrExt::chars/Chars
* StrExt::is_empty
* StrExt::len
* StrExt::lines/Lines
* StrExt::lines_any/LinesAny
* StrExt::slice_unchecked
* StrExt::trim
* StrExt::trim_left
* StrExt::trim_right
* StrExt::words/Words - also made a struct instead of a typedef
Unstable
* from_utf8 - the error type was changed to a `Result`, but the error type has
yet to prove itself
* from_c_str - this function will be handled by the c_str RFC
* FromStr - this trait will have an associated error type eventually
* StrExt::escape_default - needs iterators at least, unsure if it should make
the cut
* StrExt::escape_unicode - needs iterators at least, unsure if it should make
the cut
* StrExt::slice_chars - this function has yet to prove itself
* StrExt::slice_shift_char - awaiting conventions about slicing and shifting
* StrExt::graphemes/Graphemes - this functionality may only be in libunicode
* StrExt::grapheme_indices/GraphemeIndices - this functionality may only be in
libunicode
* StrExt::width - this functionality may only be in libunicode
* StrExt::utf16_units - this functionality may only be in libunicode
* StrExt::nfd_chars - this functionality may only be in libunicode
* StrExt::nfkd_chars - this functionality may only be in libunicode
* StrExt::nfc_chars - this functionality may only be in libunicode
* StrExt::nfkc_chars - this functionality may only be in libunicode
* StrExt::is_char_boundary - naming is uncertain with container conventions
* StrExt::char_range_at - naming is uncertain with container conventions
* StrExt::char_range_at_reverse - naming is uncertain with container conventions
* StrExt::char_at - naming is uncertain with container conventions
* StrExt::char_at_reverse - naming is uncertain with container conventions
* StrVector::concat - this functionality may be replaced with iterators, but
it's not certain at this time
* StrVector::connect - as with concat, may be deprecated in favor of iterators
Deprecated
* StrAllocating and UnicodeStrPrelude have been merged into StrExit
* eq_slice - compiler implementation detail
* from_str - use the inherent parse() method
* is_utf8 - call from_utf8 instead
* replace - call the method instead
* truncate_utf16_at_nul - this is an implementation detail of windows and does
not need to be exposed.
* utf8_char_width - moved to libunicode
* utf16_items - moved to libunicode
* is_utf16 - moved to libunicode
* Utf16Items - moved to libunicode
* Utf16Item - moved to libunicode
* Utf16Encoder - moved to libunicode
* AnyLines - renamed to LinesAny and made a struct
* SendStr - use CowString<'static> instead
* str::raw - all functionality is deprecated
* StrExt::into_string - call to_string() instead
* StrExt::repeat - use iterators instead
* StrExt::char_len - use .chars().count() instead
* StrExt::is_alphanumeric - use .chars().all(..)
* StrExt::is_whitespace - use .chars().all(..)
Pending deprecation -- while slicing syntax is being worked out, these methods
are all #[unstable]
* Str - while currently used for generic programming, this trait will be
replaced with one of [], deref coercions, or a generic conversion trait.
* StrExt::slice - use slicing syntax instead
* StrExt::slice_to - use slicing syntax instead
* StrExt::slice_from - use slicing syntax instead
* StrExt::lev_distance - deprecated with no replacement
Awaiting stabilization due to patterns and/or matching
* StrExt::contains
* StrExt::contains_char
* StrExt::split
* StrExt::splitn
* StrExt::split_terminator
* StrExt::rsplitn
* StrExt::match_indices
* StrExt::split_str
* StrExt::starts_with
* StrExt::ends_with
* StrExt::trim_chars
* StrExt::trim_left_chars
* StrExt::trim_right_chars
* StrExt::find
* StrExt::rfind
* StrExt::find_str
* StrExt::subslice_offset
2014-12-10 09:02:31 -08:00
|
|
|
}
|
2014-04-30 23:06:36 -07:00
|
|
|
|
2015-03-15 01:48:34 +01:00
|
|
|
impl<'a, P: Pattern<'a>> SplitInternal<'a, P> {
|
2014-04-30 23:06:36 -07:00
|
|
|
#[inline]
|
|
|
|
fn get_end(&mut self) -> Option<&'a str> {
|
2015-01-27 14:09:18 +01:00
|
|
|
if !self.finished && (self.allow_trailing_empty || self.end - self.start > 0) {
|
2014-04-30 23:06:36 -07:00
|
|
|
self.finished = true;
|
2015-01-27 14:09:18 +01:00
|
|
|
unsafe {
|
|
|
|
let string = self.matcher.haystack().slice_unchecked(self.start, self.end);
|
|
|
|
Some(string)
|
|
|
|
}
|
2014-04-30 23:06:36 -07:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2014-12-29 16:18:41 -05:00
|
|
|
|
2014-04-30 23:06:36 -07:00
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<&'a str> {
|
|
|
|
if self.finished { return None }
|
|
|
|
|
2015-01-27 14:09:18 +01:00
|
|
|
let haystack = self.matcher.haystack();
|
|
|
|
match self.matcher.next_match() {
|
2014-04-30 23:06:36 -07:00
|
|
|
Some((a, b)) => unsafe {
|
2015-01-27 14:09:18 +01:00
|
|
|
let elt = haystack.slice_unchecked(self.start, a);
|
|
|
|
self.start = b;
|
2014-04-30 23:06:36 -07:00
|
|
|
Some(elt)
|
|
|
|
},
|
|
|
|
None => self.get_end(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2015-03-15 01:48:34 +01:00
|
|
|
fn next_back(&mut self) -> Option<&'a str>
|
|
|
|
where P::Searcher: ReverseSearcher<'a>
|
|
|
|
{
|
2014-04-30 23:06:36 -07:00
|
|
|
if self.finished { return None }
|
|
|
|
|
|
|
|
if !self.allow_trailing_empty {
|
|
|
|
self.allow_trailing_empty = true;
|
|
|
|
match self.next_back() {
|
|
|
|
Some(elt) if !elt.is_empty() => return Some(elt),
|
|
|
|
_ => if self.finished { return None }
|
|
|
|
}
|
|
|
|
}
|
2015-01-27 14:09:18 +01:00
|
|
|
|
|
|
|
let haystack = self.matcher.haystack();
|
|
|
|
match self.matcher.next_match_back() {
|
2014-04-30 23:06:36 -07:00
|
|
|
Some((a, b)) => unsafe {
|
2015-01-27 14:09:18 +01:00
|
|
|
let elt = haystack.slice_unchecked(b, self.end);
|
|
|
|
self.end = a;
|
2014-04-30 23:06:36 -07:00
|
|
|
Some(elt)
|
|
|
|
},
|
2015-01-27 14:09:18 +01:00
|
|
|
None => unsafe {
|
|
|
|
self.finished = true;
|
|
|
|
Some(haystack.slice_unchecked(self.start, self.end))
|
|
|
|
},
|
2014-04-30 23:06:36 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-15 01:48:34 +01:00
|
|
|
generate_pattern_iterators! {
|
|
|
|
forward:
|
2015-07-15 19:57:47 +01:00
|
|
|
/// Created with the method `.split()`.
|
2015-03-15 01:48:34 +01:00
|
|
|
struct Split;
|
|
|
|
reverse:
|
2015-07-15 19:57:47 +01:00
|
|
|
/// Created with the method `.rsplit()`.
|
2015-03-15 01:48:34 +01:00
|
|
|
struct RSplit;
|
|
|
|
stability:
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
internal:
|
|
|
|
SplitInternal yielding (&'a str);
|
|
|
|
delegate double ended;
|
|
|
|
}
|
|
|
|
|
|
|
|
generate_pattern_iterators! {
|
|
|
|
forward:
|
2015-07-15 19:57:47 +01:00
|
|
|
/// Created with the method `.split_terminator()`.
|
2015-03-15 01:48:34 +01:00
|
|
|
struct SplitTerminator;
|
|
|
|
reverse:
|
2015-07-15 19:57:47 +01:00
|
|
|
/// Created with the method `.rsplit_terminator()`.
|
2015-03-15 01:48:34 +01:00
|
|
|
struct RSplitTerminator;
|
|
|
|
stability:
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
internal:
|
|
|
|
SplitInternal yielding (&'a str);
|
|
|
|
delegate double ended;
|
|
|
|
}
|
2014-12-29 16:18:41 -05:00
|
|
|
|
2015-04-05 18:52:14 +02:00
|
|
|
derive_pattern_clone!{
|
|
|
|
clone SplitNInternal
|
|
|
|
with |s| SplitNInternal { iter: s.iter.clone(), ..*s }
|
|
|
|
}
|
2015-03-15 01:48:34 +01:00
|
|
|
struct SplitNInternal<'a, P: Pattern<'a>> {
|
|
|
|
iter: SplitInternal<'a, P>,
|
|
|
|
/// The number of splits remaining
|
|
|
|
count: usize,
|
|
|
|
}
|
2014-12-29 16:18:41 -05:00
|
|
|
|
2015-03-15 01:48:34 +01:00
|
|
|
impl<'a, P: Pattern<'a>> SplitNInternal<'a, P> {
|
2014-04-30 23:06:36 -07:00
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<&'a str> {
|
2015-04-01 11:28:34 -07:00
|
|
|
match self.count {
|
|
|
|
0 => None,
|
|
|
|
1 => { self.count = 0; self.iter.get_end() }
|
|
|
|
_ => { self.count -= 1; self.iter.next() }
|
2014-04-30 23:06:36 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-14 19:34:21 -04:00
|
|
|
#[inline]
|
2015-03-15 01:48:34 +01:00
|
|
|
fn next_back(&mut self) -> Option<&'a str>
|
|
|
|
where P::Searcher: ReverseSearcher<'a>
|
|
|
|
{
|
|
|
|
match self.count {
|
|
|
|
0 => None,
|
|
|
|
1 => { self.count = 0; self.iter.get_end() }
|
|
|
|
_ => { self.count -= 1; self.iter.next_back() }
|
2015-03-14 19:34:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-15 01:48:34 +01:00
|
|
|
generate_pattern_iterators! {
|
|
|
|
forward:
|
2015-07-15 19:57:47 +01:00
|
|
|
/// Created with the method `.splitn()`.
|
2015-03-15 01:48:34 +01:00
|
|
|
struct SplitN;
|
|
|
|
reverse:
|
2015-07-15 19:57:47 +01:00
|
|
|
/// Created with the method `.rsplitn()`.
|
2015-03-15 01:48:34 +01:00
|
|
|
struct RSplitN;
|
|
|
|
stability:
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
internal:
|
|
|
|
SplitNInternal yielding (&'a str);
|
|
|
|
delegate single ended;
|
|
|
|
}
|
|
|
|
|
2015-04-05 18:52:14 +02:00
|
|
|
derive_pattern_clone!{
|
|
|
|
clone MatchIndicesInternal
|
|
|
|
with |s| MatchIndicesInternal(s.0.clone())
|
|
|
|
}
|
2015-03-15 01:48:34 +01:00
|
|
|
struct MatchIndicesInternal<'a, P: Pattern<'a>>(P::Searcher);
|
|
|
|
|
|
|
|
impl<'a, P: Pattern<'a>> MatchIndicesInternal<'a, P> {
|
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<(usize, usize)> {
|
|
|
|
self.0.next_match()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn next_back(&mut self) -> Option<(usize, usize)>
|
|
|
|
where P::Searcher: ReverseSearcher<'a>
|
|
|
|
{
|
|
|
|
self.0.next_match_back()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
generate_pattern_iterators! {
|
|
|
|
forward:
|
2015-07-15 19:57:47 +01:00
|
|
|
/// Created with the method `.match_indices()`.
|
2015-03-15 01:48:34 +01:00
|
|
|
struct MatchIndices;
|
|
|
|
reverse:
|
2015-07-15 19:57:47 +01:00
|
|
|
/// Created with the method `.rmatch_indices()`.
|
2015-03-15 01:48:34 +01:00
|
|
|
struct RMatchIndices;
|
|
|
|
stability:
|
2015-06-10 18:49:26 -07:00
|
|
|
#[unstable(feature = "str_match_indices",
|
2015-08-12 17:23:48 -07:00
|
|
|
reason = "type may be removed or have its iterator impl changed",
|
|
|
|
issue = "27743")]
|
2015-03-15 01:48:34 +01:00
|
|
|
internal:
|
|
|
|
MatchIndicesInternal yielding ((usize, usize));
|
|
|
|
delegate double ended;
|
|
|
|
}
|
|
|
|
|
2015-04-05 18:52:14 +02:00
|
|
|
derive_pattern_clone!{
|
|
|
|
clone MatchesInternal
|
|
|
|
with |s| MatchesInternal(s.0.clone())
|
|
|
|
}
|
2015-03-15 01:48:34 +01:00
|
|
|
struct MatchesInternal<'a, P: Pattern<'a>>(P::Searcher);
|
|
|
|
|
|
|
|
impl<'a, P: Pattern<'a>> MatchesInternal<'a, P> {
|
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<&'a str> {
|
|
|
|
self.0.next_match().map(|(a, b)| unsafe {
|
|
|
|
// Indices are known to be on utf8 boundaries
|
|
|
|
self.0.haystack().slice_unchecked(a, b)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn next_back(&mut self) -> Option<&'a str>
|
|
|
|
where P::Searcher: ReverseSearcher<'a>
|
|
|
|
{
|
|
|
|
self.0.next_match_back().map(|(a, b)| unsafe {
|
|
|
|
// Indices are known to be on utf8 boundaries
|
|
|
|
self.0.haystack().slice_unchecked(a, b)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
generate_pattern_iterators! {
|
|
|
|
forward:
|
2015-07-15 19:57:47 +01:00
|
|
|
/// Created with the method `.matches()`.
|
2015-03-15 01:48:34 +01:00
|
|
|
struct Matches;
|
|
|
|
reverse:
|
2015-07-15 19:57:47 +01:00
|
|
|
/// Created with the method `.rmatches()`.
|
2015-03-15 01:48:34 +01:00
|
|
|
struct RMatches;
|
|
|
|
stability:
|
2015-06-10 18:49:26 -07:00
|
|
|
#[stable(feature = "str_matches", since = "1.2.0")]
|
2015-03-15 01:48:34 +01:00
|
|
|
internal:
|
|
|
|
MatchesInternal yielding (&'a str);
|
|
|
|
delegate double ended;
|
|
|
|
}
|
|
|
|
|
2015-04-20 09:55:07 -04:00
|
|
|
/// Created with the method `.lines()`.
|
2015-03-15 01:48:34 +01:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-04-05 18:52:14 +02:00
|
|
|
#[derive(Clone)]
|
2015-03-15 01:48:34 +01:00
|
|
|
pub struct Lines<'a>(SplitTerminator<'a, char>);
|
|
|
|
|
2015-03-14 19:34:21 -04:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-03-15 01:48:34 +01:00
|
|
|
impl<'a> Iterator for Lines<'a> {
|
2015-03-14 19:34:21 -04:00
|
|
|
type Item = &'a str;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<&'a str> {
|
2015-03-15 01:48:34 +01:00
|
|
|
self.0.next()
|
|
|
|
}
|
2015-03-14 19:34:21 -04:00
|
|
|
|
2015-03-15 01:48:34 +01:00
|
|
|
#[inline]
|
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
|
|
|
self.0.size_hint()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
impl<'a> DoubleEndedIterator for Lines<'a> {
|
|
|
|
#[inline]
|
|
|
|
fn next_back(&mut self) -> Option<&'a str> {
|
|
|
|
self.0.next_back()
|
2015-03-14 19:34:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-20 09:55:07 -04:00
|
|
|
/// Created with the method `.lines_any()`.
|
2015-03-15 01:48:34 +01:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-04-05 18:52:14 +02:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct LinesAny<'a>(Map<Lines<'a>, LinesAnyMap>);
|
|
|
|
|
|
|
|
/// A nameable, clonable fn type
|
|
|
|
#[derive(Clone)]
|
|
|
|
struct LinesAnyMap;
|
|
|
|
|
|
|
|
impl<'a> Fn<(&'a str,)> for LinesAnyMap {
|
|
|
|
#[inline]
|
|
|
|
extern "rust-call" fn call(&self, (line,): (&'a str,)) -> &'a str {
|
|
|
|
let l = line.len();
|
|
|
|
if l > 0 && line.as_bytes()[l - 1] == b'\r' { &line[0 .. l - 1] }
|
|
|
|
else { line }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> FnMut<(&'a str,)> for LinesAnyMap {
|
|
|
|
#[inline]
|
|
|
|
extern "rust-call" fn call_mut(&mut self, (line,): (&'a str,)) -> &'a str {
|
|
|
|
Fn::call(&*self, (line,))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> FnOnce<(&'a str,)> for LinesAnyMap {
|
|
|
|
type Output = &'a str;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
extern "rust-call" fn call_once(self, (line,): (&'a str,)) -> &'a str {
|
|
|
|
Fn::call(&self, (line,))
|
2015-03-14 19:34:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-14 20:07:13 -04:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-03-15 01:48:34 +01:00
|
|
|
impl<'a> Iterator for LinesAny<'a> {
|
2015-03-14 20:07:13 -04:00
|
|
|
type Item = &'a str;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<&'a str> {
|
2015-03-15 01:48:34 +01:00
|
|
|
self.0.next()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
|
|
|
self.0.size_hint()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
impl<'a> DoubleEndedIterator for LinesAny<'a> {
|
|
|
|
#[inline]
|
|
|
|
fn next_back(&mut self) -> Option<&'a str> {
|
|
|
|
self.0.next_back()
|
2015-03-14 20:07:13 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-30 23:06:36 -07:00
|
|
|
/*
|
|
|
|
Section: Comparing strings
|
|
|
|
*/
|
|
|
|
|
2015-07-21 18:30:18 +02:00
|
|
|
/// Bytewise slice equality
|
2014-06-07 15:32:01 +01:00
|
|
|
/// NOTE: This function is (ab)used in rustc::middle::trans::_match
|
|
|
|
/// to compare &[u8] byte slices that are not necessarily valid UTF-8.
|
2015-07-21 18:30:18 +02:00
|
|
|
#[lang = "str_eq"]
|
2014-04-30 23:06:36 -07:00
|
|
|
#[inline]
|
2015-07-21 18:30:18 +02:00
|
|
|
fn eq_slice(a: &str, b: &str) -> bool {
|
2015-02-17 23:47:08 +01:00
|
|
|
// NOTE: In theory n should be libc::size_t and not usize, but libc is not available here
|
2014-10-27 15:37:07 -07:00
|
|
|
#[allow(improper_ctypes)]
|
2015-01-27 14:09:18 +01:00
|
|
|
extern { fn memcmp(s1: *const i8, s2: *const i8, n: usize) -> i32; }
|
2014-04-30 23:06:36 -07:00
|
|
|
a.len() == b.len() && unsafe {
|
2014-06-25 12:47:34 -07:00
|
|
|
memcmp(a.as_ptr() as *const i8,
|
|
|
|
b.as_ptr() as *const i8,
|
2014-04-30 23:06:36 -07:00
|
|
|
a.len()) == 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Section: Misc
|
|
|
|
*/
|
|
|
|
|
|
|
|
/// Walk through `iter` checking that it's a valid UTF-8 sequence,
|
|
|
|
/// returning `true` in that case, or, if it is invalid, `false` with
|
|
|
|
/// `iter` reset such that it is pointing at the first byte in the
|
|
|
|
/// invalid sequence.
|
|
|
|
#[inline(always)]
|
2014-12-22 12:49:57 -08:00
|
|
|
fn run_utf8_validation_iterator(iter: &mut slice::Iter<u8>)
|
std: Stabilize the std::str module
This commit starts out by consolidating all `str` extension traits into one
`StrExt` trait to be included in the prelude. This means that
`UnicodeStrPrelude`, `StrPrelude`, and `StrAllocating` have all been merged into
one `StrExt` exported by the standard library. Some functionality is currently
duplicated with the `StrExt` present in libcore.
This commit also currently avoids any methods which require any form of pattern
to operate. These functions will be stabilized via a separate RFC.
Next, stability of methods and structures are as follows:
Stable
* from_utf8_unchecked
* CowString - after moving to std::string
* StrExt::as_bytes
* StrExt::as_ptr
* StrExt::bytes/Bytes - also made a struct instead of a typedef
* StrExt::char_indices/CharIndices - CharOffsets was renamed
* StrExt::chars/Chars
* StrExt::is_empty
* StrExt::len
* StrExt::lines/Lines
* StrExt::lines_any/LinesAny
* StrExt::slice_unchecked
* StrExt::trim
* StrExt::trim_left
* StrExt::trim_right
* StrExt::words/Words - also made a struct instead of a typedef
Unstable
* from_utf8 - the error type was changed to a `Result`, but the error type has
yet to prove itself
* from_c_str - this function will be handled by the c_str RFC
* FromStr - this trait will have an associated error type eventually
* StrExt::escape_default - needs iterators at least, unsure if it should make
the cut
* StrExt::escape_unicode - needs iterators at least, unsure if it should make
the cut
* StrExt::slice_chars - this function has yet to prove itself
* StrExt::slice_shift_char - awaiting conventions about slicing and shifting
* StrExt::graphemes/Graphemes - this functionality may only be in libunicode
* StrExt::grapheme_indices/GraphemeIndices - this functionality may only be in
libunicode
* StrExt::width - this functionality may only be in libunicode
* StrExt::utf16_units - this functionality may only be in libunicode
* StrExt::nfd_chars - this functionality may only be in libunicode
* StrExt::nfkd_chars - this functionality may only be in libunicode
* StrExt::nfc_chars - this functionality may only be in libunicode
* StrExt::nfkc_chars - this functionality may only be in libunicode
* StrExt::is_char_boundary - naming is uncertain with container conventions
* StrExt::char_range_at - naming is uncertain with container conventions
* StrExt::char_range_at_reverse - naming is uncertain with container conventions
* StrExt::char_at - naming is uncertain with container conventions
* StrExt::char_at_reverse - naming is uncertain with container conventions
* StrVector::concat - this functionality may be replaced with iterators, but
it's not certain at this time
* StrVector::connect - as with concat, may be deprecated in favor of iterators
Deprecated
* StrAllocating and UnicodeStrPrelude have been merged into StrExit
* eq_slice - compiler implementation detail
* from_str - use the inherent parse() method
* is_utf8 - call from_utf8 instead
* replace - call the method instead
* truncate_utf16_at_nul - this is an implementation detail of windows and does
not need to be exposed.
* utf8_char_width - moved to libunicode
* utf16_items - moved to libunicode
* is_utf16 - moved to libunicode
* Utf16Items - moved to libunicode
* Utf16Item - moved to libunicode
* Utf16Encoder - moved to libunicode
* AnyLines - renamed to LinesAny and made a struct
* SendStr - use CowString<'static> instead
* str::raw - all functionality is deprecated
* StrExt::into_string - call to_string() instead
* StrExt::repeat - use iterators instead
* StrExt::char_len - use .chars().count() instead
* StrExt::is_alphanumeric - use .chars().all(..)
* StrExt::is_whitespace - use .chars().all(..)
Pending deprecation -- while slicing syntax is being worked out, these methods
are all #[unstable]
* Str - while currently used for generic programming, this trait will be
replaced with one of [], deref coercions, or a generic conversion trait.
* StrExt::slice - use slicing syntax instead
* StrExt::slice_to - use slicing syntax instead
* StrExt::slice_from - use slicing syntax instead
* StrExt::lev_distance - deprecated with no replacement
Awaiting stabilization due to patterns and/or matching
* StrExt::contains
* StrExt::contains_char
* StrExt::split
* StrExt::splitn
* StrExt::split_terminator
* StrExt::rsplitn
* StrExt::match_indices
* StrExt::split_str
* StrExt::starts_with
* StrExt::ends_with
* StrExt::trim_chars
* StrExt::trim_left_chars
* StrExt::trim_right_chars
* StrExt::find
* StrExt::rfind
* StrExt::find_str
* StrExt::subslice_offset
2014-12-10 09:02:31 -08:00
|
|
|
-> Result<(), Utf8Error> {
|
|
|
|
let whole = iter.as_slice();
|
2014-04-30 23:06:36 -07:00
|
|
|
loop {
|
|
|
|
// save the current thing we're pointing at.
|
2015-01-23 10:54:32 -05:00
|
|
|
let old = iter.clone();
|
2014-04-30 23:06:36 -07:00
|
|
|
|
|
|
|
// restore the iterator we had at the start of this codepoint.
|
2015-01-02 14:44:21 -08:00
|
|
|
macro_rules! err { () => {{
|
2015-01-23 10:54:32 -05:00
|
|
|
*iter = old.clone();
|
2015-04-10 16:05:09 -07:00
|
|
|
return Err(Utf8Error {
|
|
|
|
valid_up_to: whole.len() - iter.as_slice().len()
|
|
|
|
})
|
2015-01-02 14:44:21 -08:00
|
|
|
}}}
|
|
|
|
|
|
|
|
macro_rules! next { () => {
|
std: Stabilize the std::str module
This commit starts out by consolidating all `str` extension traits into one
`StrExt` trait to be included in the prelude. This means that
`UnicodeStrPrelude`, `StrPrelude`, and `StrAllocating` have all been merged into
one `StrExt` exported by the standard library. Some functionality is currently
duplicated with the `StrExt` present in libcore.
This commit also currently avoids any methods which require any form of pattern
to operate. These functions will be stabilized via a separate RFC.
Next, stability of methods and structures are as follows:
Stable
* from_utf8_unchecked
* CowString - after moving to std::string
* StrExt::as_bytes
* StrExt::as_ptr
* StrExt::bytes/Bytes - also made a struct instead of a typedef
* StrExt::char_indices/CharIndices - CharOffsets was renamed
* StrExt::chars/Chars
* StrExt::is_empty
* StrExt::len
* StrExt::lines/Lines
* StrExt::lines_any/LinesAny
* StrExt::slice_unchecked
* StrExt::trim
* StrExt::trim_left
* StrExt::trim_right
* StrExt::words/Words - also made a struct instead of a typedef
Unstable
* from_utf8 - the error type was changed to a `Result`, but the error type has
yet to prove itself
* from_c_str - this function will be handled by the c_str RFC
* FromStr - this trait will have an associated error type eventually
* StrExt::escape_default - needs iterators at least, unsure if it should make
the cut
* StrExt::escape_unicode - needs iterators at least, unsure if it should make
the cut
* StrExt::slice_chars - this function has yet to prove itself
* StrExt::slice_shift_char - awaiting conventions about slicing and shifting
* StrExt::graphemes/Graphemes - this functionality may only be in libunicode
* StrExt::grapheme_indices/GraphemeIndices - this functionality may only be in
libunicode
* StrExt::width - this functionality may only be in libunicode
* StrExt::utf16_units - this functionality may only be in libunicode
* StrExt::nfd_chars - this functionality may only be in libunicode
* StrExt::nfkd_chars - this functionality may only be in libunicode
* StrExt::nfc_chars - this functionality may only be in libunicode
* StrExt::nfkc_chars - this functionality may only be in libunicode
* StrExt::is_char_boundary - naming is uncertain with container conventions
* StrExt::char_range_at - naming is uncertain with container conventions
* StrExt::char_range_at_reverse - naming is uncertain with container conventions
* StrExt::char_at - naming is uncertain with container conventions
* StrExt::char_at_reverse - naming is uncertain with container conventions
* StrVector::concat - this functionality may be replaced with iterators, but
it's not certain at this time
* StrVector::connect - as with concat, may be deprecated in favor of iterators
Deprecated
* StrAllocating and UnicodeStrPrelude have been merged into StrExit
* eq_slice - compiler implementation detail
* from_str - use the inherent parse() method
* is_utf8 - call from_utf8 instead
* replace - call the method instead
* truncate_utf16_at_nul - this is an implementation detail of windows and does
not need to be exposed.
* utf8_char_width - moved to libunicode
* utf16_items - moved to libunicode
* is_utf16 - moved to libunicode
* Utf16Items - moved to libunicode
* Utf16Item - moved to libunicode
* Utf16Encoder - moved to libunicode
* AnyLines - renamed to LinesAny and made a struct
* SendStr - use CowString<'static> instead
* str::raw - all functionality is deprecated
* StrExt::into_string - call to_string() instead
* StrExt::repeat - use iterators instead
* StrExt::char_len - use .chars().count() instead
* StrExt::is_alphanumeric - use .chars().all(..)
* StrExt::is_whitespace - use .chars().all(..)
Pending deprecation -- while slicing syntax is being worked out, these methods
are all #[unstable]
* Str - while currently used for generic programming, this trait will be
replaced with one of [], deref coercions, or a generic conversion trait.
* StrExt::slice - use slicing syntax instead
* StrExt::slice_to - use slicing syntax instead
* StrExt::slice_from - use slicing syntax instead
* StrExt::lev_distance - deprecated with no replacement
Awaiting stabilization due to patterns and/or matching
* StrExt::contains
* StrExt::contains_char
* StrExt::split
* StrExt::splitn
* StrExt::split_terminator
* StrExt::rsplitn
* StrExt::match_indices
* StrExt::split_str
* StrExt::starts_with
* StrExt::ends_with
* StrExt::trim_chars
* StrExt::trim_left_chars
* StrExt::trim_right_chars
* StrExt::find
* StrExt::rfind
* StrExt::find_str
* StrExt::subslice_offset
2014-12-10 09:02:31 -08:00
|
|
|
match iter.next() {
|
|
|
|
Some(a) => *a,
|
|
|
|
// we needed data, but there was none: error!
|
2015-04-10 16:05:09 -07:00
|
|
|
None => err!(),
|
std: Stabilize the std::str module
This commit starts out by consolidating all `str` extension traits into one
`StrExt` trait to be included in the prelude. This means that
`UnicodeStrPrelude`, `StrPrelude`, and `StrAllocating` have all been merged into
one `StrExt` exported by the standard library. Some functionality is currently
duplicated with the `StrExt` present in libcore.
This commit also currently avoids any methods which require any form of pattern
to operate. These functions will be stabilized via a separate RFC.
Next, stability of methods and structures are as follows:
Stable
* from_utf8_unchecked
* CowString - after moving to std::string
* StrExt::as_bytes
* StrExt::as_ptr
* StrExt::bytes/Bytes - also made a struct instead of a typedef
* StrExt::char_indices/CharIndices - CharOffsets was renamed
* StrExt::chars/Chars
* StrExt::is_empty
* StrExt::len
* StrExt::lines/Lines
* StrExt::lines_any/LinesAny
* StrExt::slice_unchecked
* StrExt::trim
* StrExt::trim_left
* StrExt::trim_right
* StrExt::words/Words - also made a struct instead of a typedef
Unstable
* from_utf8 - the error type was changed to a `Result`, but the error type has
yet to prove itself
* from_c_str - this function will be handled by the c_str RFC
* FromStr - this trait will have an associated error type eventually
* StrExt::escape_default - needs iterators at least, unsure if it should make
the cut
* StrExt::escape_unicode - needs iterators at least, unsure if it should make
the cut
* StrExt::slice_chars - this function has yet to prove itself
* StrExt::slice_shift_char - awaiting conventions about slicing and shifting
* StrExt::graphemes/Graphemes - this functionality may only be in libunicode
* StrExt::grapheme_indices/GraphemeIndices - this functionality may only be in
libunicode
* StrExt::width - this functionality may only be in libunicode
* StrExt::utf16_units - this functionality may only be in libunicode
* StrExt::nfd_chars - this functionality may only be in libunicode
* StrExt::nfkd_chars - this functionality may only be in libunicode
* StrExt::nfc_chars - this functionality may only be in libunicode
* StrExt::nfkc_chars - this functionality may only be in libunicode
* StrExt::is_char_boundary - naming is uncertain with container conventions
* StrExt::char_range_at - naming is uncertain with container conventions
* StrExt::char_range_at_reverse - naming is uncertain with container conventions
* StrExt::char_at - naming is uncertain with container conventions
* StrExt::char_at_reverse - naming is uncertain with container conventions
* StrVector::concat - this functionality may be replaced with iterators, but
it's not certain at this time
* StrVector::connect - as with concat, may be deprecated in favor of iterators
Deprecated
* StrAllocating and UnicodeStrPrelude have been merged into StrExit
* eq_slice - compiler implementation detail
* from_str - use the inherent parse() method
* is_utf8 - call from_utf8 instead
* replace - call the method instead
* truncate_utf16_at_nul - this is an implementation detail of windows and does
not need to be exposed.
* utf8_char_width - moved to libunicode
* utf16_items - moved to libunicode
* is_utf16 - moved to libunicode
* Utf16Items - moved to libunicode
* Utf16Item - moved to libunicode
* Utf16Encoder - moved to libunicode
* AnyLines - renamed to LinesAny and made a struct
* SendStr - use CowString<'static> instead
* str::raw - all functionality is deprecated
* StrExt::into_string - call to_string() instead
* StrExt::repeat - use iterators instead
* StrExt::char_len - use .chars().count() instead
* StrExt::is_alphanumeric - use .chars().all(..)
* StrExt::is_whitespace - use .chars().all(..)
Pending deprecation -- while slicing syntax is being worked out, these methods
are all #[unstable]
* Str - while currently used for generic programming, this trait will be
replaced with one of [], deref coercions, or a generic conversion trait.
* StrExt::slice - use slicing syntax instead
* StrExt::slice_to - use slicing syntax instead
* StrExt::slice_from - use slicing syntax instead
* StrExt::lev_distance - deprecated with no replacement
Awaiting stabilization due to patterns and/or matching
* StrExt::contains
* StrExt::contains_char
* StrExt::split
* StrExt::splitn
* StrExt::split_terminator
* StrExt::rsplitn
* StrExt::match_indices
* StrExt::split_str
* StrExt::starts_with
* StrExt::ends_with
* StrExt::trim_chars
* StrExt::trim_left_chars
* StrExt::trim_right_chars
* StrExt::find
* StrExt::rfind
* StrExt::find_str
* StrExt::subslice_offset
2014-12-10 09:02:31 -08:00
|
|
|
}
|
2015-01-02 14:44:21 -08:00
|
|
|
}}
|
2014-04-30 23:06:36 -07:00
|
|
|
|
|
|
|
let first = match iter.next() {
|
|
|
|
Some(&b) => b,
|
|
|
|
// we're at the end of the iterator and a codepoint
|
|
|
|
// boundary at the same time, so this string is valid.
|
std: Stabilize the std::str module
This commit starts out by consolidating all `str` extension traits into one
`StrExt` trait to be included in the prelude. This means that
`UnicodeStrPrelude`, `StrPrelude`, and `StrAllocating` have all been merged into
one `StrExt` exported by the standard library. Some functionality is currently
duplicated with the `StrExt` present in libcore.
This commit also currently avoids any methods which require any form of pattern
to operate. These functions will be stabilized via a separate RFC.
Next, stability of methods and structures are as follows:
Stable
* from_utf8_unchecked
* CowString - after moving to std::string
* StrExt::as_bytes
* StrExt::as_ptr
* StrExt::bytes/Bytes - also made a struct instead of a typedef
* StrExt::char_indices/CharIndices - CharOffsets was renamed
* StrExt::chars/Chars
* StrExt::is_empty
* StrExt::len
* StrExt::lines/Lines
* StrExt::lines_any/LinesAny
* StrExt::slice_unchecked
* StrExt::trim
* StrExt::trim_left
* StrExt::trim_right
* StrExt::words/Words - also made a struct instead of a typedef
Unstable
* from_utf8 - the error type was changed to a `Result`, but the error type has
yet to prove itself
* from_c_str - this function will be handled by the c_str RFC
* FromStr - this trait will have an associated error type eventually
* StrExt::escape_default - needs iterators at least, unsure if it should make
the cut
* StrExt::escape_unicode - needs iterators at least, unsure if it should make
the cut
* StrExt::slice_chars - this function has yet to prove itself
* StrExt::slice_shift_char - awaiting conventions about slicing and shifting
* StrExt::graphemes/Graphemes - this functionality may only be in libunicode
* StrExt::grapheme_indices/GraphemeIndices - this functionality may only be in
libunicode
* StrExt::width - this functionality may only be in libunicode
* StrExt::utf16_units - this functionality may only be in libunicode
* StrExt::nfd_chars - this functionality may only be in libunicode
* StrExt::nfkd_chars - this functionality may only be in libunicode
* StrExt::nfc_chars - this functionality may only be in libunicode
* StrExt::nfkc_chars - this functionality may only be in libunicode
* StrExt::is_char_boundary - naming is uncertain with container conventions
* StrExt::char_range_at - naming is uncertain with container conventions
* StrExt::char_range_at_reverse - naming is uncertain with container conventions
* StrExt::char_at - naming is uncertain with container conventions
* StrExt::char_at_reverse - naming is uncertain with container conventions
* StrVector::concat - this functionality may be replaced with iterators, but
it's not certain at this time
* StrVector::connect - as with concat, may be deprecated in favor of iterators
Deprecated
* StrAllocating and UnicodeStrPrelude have been merged into StrExit
* eq_slice - compiler implementation detail
* from_str - use the inherent parse() method
* is_utf8 - call from_utf8 instead
* replace - call the method instead
* truncate_utf16_at_nul - this is an implementation detail of windows and does
not need to be exposed.
* utf8_char_width - moved to libunicode
* utf16_items - moved to libunicode
* is_utf16 - moved to libunicode
* Utf16Items - moved to libunicode
* Utf16Item - moved to libunicode
* Utf16Encoder - moved to libunicode
* AnyLines - renamed to LinesAny and made a struct
* SendStr - use CowString<'static> instead
* str::raw - all functionality is deprecated
* StrExt::into_string - call to_string() instead
* StrExt::repeat - use iterators instead
* StrExt::char_len - use .chars().count() instead
* StrExt::is_alphanumeric - use .chars().all(..)
* StrExt::is_whitespace - use .chars().all(..)
Pending deprecation -- while slicing syntax is being worked out, these methods
are all #[unstable]
* Str - while currently used for generic programming, this trait will be
replaced with one of [], deref coercions, or a generic conversion trait.
* StrExt::slice - use slicing syntax instead
* StrExt::slice_to - use slicing syntax instead
* StrExt::slice_from - use slicing syntax instead
* StrExt::lev_distance - deprecated with no replacement
Awaiting stabilization due to patterns and/or matching
* StrExt::contains
* StrExt::contains_char
* StrExt::split
* StrExt::splitn
* StrExt::split_terminator
* StrExt::rsplitn
* StrExt::match_indices
* StrExt::split_str
* StrExt::starts_with
* StrExt::ends_with
* StrExt::trim_chars
* StrExt::trim_left_chars
* StrExt::trim_right_chars
* StrExt::find
* StrExt::rfind
* StrExt::find_str
* StrExt::subslice_offset
2014-12-10 09:02:31 -08:00
|
|
|
None => return Ok(())
|
2014-04-30 23:06:36 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
// ASCII characters are always valid, so only large
|
|
|
|
// bytes need more examination.
|
|
|
|
if first >= 128 {
|
2015-03-10 12:06:44 +01:00
|
|
|
let w = UTF8_CHAR_WIDTH[first as usize];
|
2014-04-30 23:06:36 -07:00
|
|
|
let second = next!();
|
2014-12-09 14:08:10 -08:00
|
|
|
// 2-byte encoding is for codepoints \u{0080} to \u{07ff}
|
2014-04-30 23:06:36 -07:00
|
|
|
// first C2 80 last DF BF
|
2014-12-09 14:08:10 -08:00
|
|
|
// 3-byte encoding is for codepoints \u{0800} to \u{ffff}
|
2014-04-30 23:06:36 -07:00
|
|
|
// first E0 A0 80 last EF BF BF
|
2014-12-09 14:08:10 -08:00
|
|
|
// excluding surrogates codepoints \u{d800} to \u{dfff}
|
2014-04-30 23:06:36 -07:00
|
|
|
// ED A0 80 to ED BF BF
|
2014-12-09 14:08:10 -08:00
|
|
|
// 4-byte encoding is for codepoints \u{1000}0 to \u{10ff}ff
|
2014-04-30 23:06:36 -07:00
|
|
|
// first F0 90 80 80 last F4 8F BF BF
|
|
|
|
//
|
|
|
|
// Use the UTF-8 syntax from the RFC
|
|
|
|
//
|
|
|
|
// https://tools.ietf.org/html/rfc3629
|
|
|
|
// UTF8-1 = %x00-7F
|
|
|
|
// UTF8-2 = %xC2-DF UTF8-tail
|
|
|
|
// UTF8-3 = %xE0 %xA0-BF UTF8-tail / %xE1-EC 2( UTF8-tail ) /
|
|
|
|
// %xED %x80-9F UTF8-tail / %xEE-EF 2( UTF8-tail )
|
|
|
|
// UTF8-4 = %xF0 %x90-BF 2( UTF8-tail ) / %xF1-F3 3( UTF8-tail ) /
|
|
|
|
// %xF4 %x80-8F 2( UTF8-tail )
|
|
|
|
match w {
|
2014-07-18 00:59:49 +02:00
|
|
|
2 => if second & !CONT_MASK != TAG_CONT_U8 {err!()},
|
2014-04-30 23:06:36 -07:00
|
|
|
3 => {
|
2014-07-18 00:59:49 +02:00
|
|
|
match (first, second, next!() & !CONT_MASK) {
|
2014-09-26 21:13:20 -07:00
|
|
|
(0xE0 , 0xA0 ... 0xBF, TAG_CONT_U8) |
|
|
|
|
(0xE1 ... 0xEC, 0x80 ... 0xBF, TAG_CONT_U8) |
|
|
|
|
(0xED , 0x80 ... 0x9F, TAG_CONT_U8) |
|
|
|
|
(0xEE ... 0xEF, 0x80 ... 0xBF, TAG_CONT_U8) => {}
|
2014-04-30 23:06:36 -07:00
|
|
|
_ => err!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
4 => {
|
2014-07-18 00:59:49 +02:00
|
|
|
match (first, second, next!() & !CONT_MASK, next!() & !CONT_MASK) {
|
2014-09-26 21:13:20 -07:00
|
|
|
(0xF0 , 0x90 ... 0xBF, TAG_CONT_U8, TAG_CONT_U8) |
|
|
|
|
(0xF1 ... 0xF3, 0x80 ... 0xBF, TAG_CONT_U8, TAG_CONT_U8) |
|
|
|
|
(0xF4 , 0x80 ... 0x8F, TAG_CONT_U8, TAG_CONT_U8) => {}
|
2014-04-30 23:06:36 -07:00
|
|
|
_ => err!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => err!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://tools.ietf.org/html/rfc3629
|
2014-12-30 21:19:41 +13:00
|
|
|
static UTF8_CHAR_WIDTH: [u8; 256] = [
|
2014-04-30 23:06:36 -07:00
|
|
|
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
|
|
|
|
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 0x1F
|
|
|
|
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
|
|
|
|
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 0x3F
|
|
|
|
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
|
|
|
|
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 0x5F
|
|
|
|
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
|
|
|
|
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 0x7F
|
|
|
|
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
|
|
|
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 0x9F
|
|
|
|
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
|
|
|
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 0xBF
|
|
|
|
0,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
|
|
|
|
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, // 0xDF
|
|
|
|
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3, // 0xEF
|
|
|
|
4,4,4,4,4,0,0,0,0,0,0,0,0,0,0,0, // 0xFF
|
|
|
|
];
|
|
|
|
|
|
|
|
/// Struct that contains a `char` and the index of the first byte of
|
|
|
|
/// the next `char` in a string. This can be used as a data structure
|
|
|
|
/// for iterating over the UTF-8 bytes of a string.
|
2015-03-30 09:40:52 -04:00
|
|
|
#[derive(Copy, Clone)]
|
2015-03-10 16:29:02 -07:00
|
|
|
#[unstable(feature = "str_char",
|
|
|
|
reason = "existence of this struct is uncertain as it is frequently \
|
|
|
|
able to be replaced with char.len_utf8() and/or \
|
2015-08-12 17:23:48 -07:00
|
|
|
char/char_indices iterators",
|
|
|
|
issue = "27754")]
|
2014-04-30 23:06:36 -07:00
|
|
|
pub struct CharRange {
|
|
|
|
/// Current `char`
|
|
|
|
pub ch: char,
|
|
|
|
/// Index of the first byte of the next `char`
|
2015-01-27 14:09:18 +01:00
|
|
|
pub next: usize,
|
2014-04-30 23:06:36 -07:00
|
|
|
}
|
|
|
|
|
2014-07-18 00:59:49 +02:00
|
|
|
/// Mask of the value bits of a continuation byte
|
2015-03-03 10:42:26 +02:00
|
|
|
const CONT_MASK: u8 = 0b0011_1111;
|
2014-07-18 00:59:49 +02:00
|
|
|
/// Value of the tag bits (tag mask is !CONT_MASK) of a continuation byte
|
2015-03-03 10:42:26 +02:00
|
|
|
const TAG_CONT_U8: u8 = 0b1000_0000;
|
2014-04-30 23:06:36 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
Section: Trait implementations
|
|
|
|
*/
|
|
|
|
|
2015-01-04 23:32:20 -08:00
|
|
|
mod traits {
|
2015-01-01 23:53:35 -08:00
|
|
|
use cmp::{Ordering, Ord, PartialEq, PartialOrd, Eq};
|
2014-11-28 11:57:41 -05:00
|
|
|
use cmp::Ordering::{Less, Equal, Greater};
|
2015-03-11 22:41:24 -07:00
|
|
|
use iter::Iterator;
|
2014-11-28 11:57:41 -05:00
|
|
|
use option::Option;
|
|
|
|
use option::Option::Some;
|
2014-09-26 21:46:22 -07:00
|
|
|
use ops;
|
2015-01-01 23:53:35 -08:00
|
|
|
use str::{StrExt, eq_slice};
|
2014-04-30 23:06:36 -07:00
|
|
|
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-10-29 20:11:16 -05:00
|
|
|
impl Ord for str {
|
|
|
|
#[inline]
|
|
|
|
fn cmp(&self, other: &str) -> Ordering {
|
|
|
|
for (s_b, o_b) in self.bytes().zip(other.bytes()) {
|
|
|
|
match s_b.cmp(&o_b) {
|
|
|
|
Greater => return Greater,
|
|
|
|
Less => return Less,
|
|
|
|
Equal => ()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.len().cmp(&other.len())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-10-29 20:11:16 -05:00
|
|
|
impl PartialEq for str {
|
|
|
|
#[inline]
|
|
|
|
fn eq(&self, other: &str) -> bool {
|
|
|
|
eq_slice(self, other)
|
|
|
|
}
|
|
|
|
#[inline]
|
|
|
|
fn ne(&self, other: &str) -> bool { !(*self).eq(other) }
|
|
|
|
}
|
|
|
|
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-10-29 20:11:16 -05:00
|
|
|
impl Eq for str {}
|
|
|
|
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-10-29 20:11:16 -05:00
|
|
|
impl PartialOrd for str {
|
|
|
|
#[inline]
|
|
|
|
fn partial_cmp(&self, other: &str) -> Option<Ordering> {
|
|
|
|
Some(self.cmp(other))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-17 16:15:47 -08:00
|
|
|
/// Returns a slice of the given string from the byte range
|
|
|
|
/// [`begin`..`end`).
|
|
|
|
///
|
|
|
|
/// This operation is `O(1)`.
|
|
|
|
///
|
|
|
|
/// Panics when `begin` and `end` do not point to valid characters
|
|
|
|
/// or point beyond the last character of the string.
|
|
|
|
///
|
2015-03-11 21:11:40 -04:00
|
|
|
/// # Examples
|
2015-01-17 16:15:47 -08:00
|
|
|
///
|
2015-03-12 22:42:38 -04:00
|
|
|
/// ```
|
2015-01-17 16:15:47 -08:00
|
|
|
/// let s = "Löwe 老虎 Léopard";
|
|
|
|
/// assert_eq!(&s[0 .. 1], "L");
|
|
|
|
///
|
|
|
|
/// assert_eq!(&s[1 .. 9], "öwe 老");
|
|
|
|
///
|
|
|
|
/// // these will panic:
|
|
|
|
/// // byte 2 lies within `ö`:
|
|
|
|
/// // &s[2 ..3];
|
|
|
|
///
|
|
|
|
/// // byte 8 lies within `老`
|
|
|
|
/// // &s[1 .. 8];
|
|
|
|
///
|
|
|
|
/// // byte 100 is outside the string
|
|
|
|
/// // &s[3 .. 100];
|
|
|
|
/// ```
|
2015-03-21 19:33:27 -04:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
impl ops::Index<ops::Range<usize>> for str {
|
|
|
|
type Output = str;
|
|
|
|
#[inline]
|
|
|
|
fn index(&self, index: ops::Range<usize>) -> &str {
|
|
|
|
// is_char_boundary checks that the index is in [0, .len()]
|
|
|
|
if index.start <= index.end &&
|
|
|
|
self.is_char_boundary(index.start) &&
|
|
|
|
self.is_char_boundary(index.end) {
|
|
|
|
unsafe { self.slice_unchecked(index.start, index.end) }
|
|
|
|
} else {
|
|
|
|
super::slice_error_fail(self, index.start, index.end)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-15 18:33:21 +02:00
|
|
|
/// Returns a mutable slice of the given string from the byte range
|
|
|
|
/// [`begin`..`end`).
|
|
|
|
#[stable(feature = "derefmut_for_string", since = "1.2.0")]
|
|
|
|
impl ops::IndexMut<ops::Range<usize>> for str {
|
|
|
|
#[inline]
|
|
|
|
fn index_mut(&mut self, index: ops::Range<usize>) -> &mut str {
|
|
|
|
// is_char_boundary checks that the index is in [0, .len()]
|
|
|
|
if index.start <= index.end &&
|
|
|
|
self.is_char_boundary(index.start) &&
|
|
|
|
self.is_char_boundary(index.end) {
|
|
|
|
unsafe { self.slice_mut_unchecked(index.start, index.end) }
|
|
|
|
} else {
|
|
|
|
super::slice_error_fail(self, index.start, index.end)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-17 16:15:47 -08:00
|
|
|
/// Returns a slice of the string from the beginning to byte
|
|
|
|
/// `end`.
|
|
|
|
///
|
|
|
|
/// Equivalent to `self[0 .. end]`.
|
|
|
|
///
|
|
|
|
/// Panics when `end` does not point to a valid character, or is
|
|
|
|
/// out of bounds.
|
2015-01-24 09:15:42 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-27 14:09:18 +01:00
|
|
|
impl ops::Index<ops::RangeTo<usize>> for str {
|
2015-01-04 17:43:24 +13:00
|
|
|
type Output = str;
|
2015-03-21 19:33:27 -04:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn index(&self, index: ops::RangeTo<usize>) -> &str {
|
|
|
|
// is_char_boundary checks that the index is in [0, .len()]
|
|
|
|
if self.is_char_boundary(index.end) {
|
|
|
|
unsafe { self.slice_unchecked(0, index.end) }
|
|
|
|
} else {
|
|
|
|
super::slice_error_fail(self, 0, index.end)
|
|
|
|
}
|
|
|
|
}
|
2014-12-31 20:20:40 +13:00
|
|
|
}
|
2015-01-17 16:15:47 -08:00
|
|
|
|
2015-06-15 18:33:21 +02:00
|
|
|
/// Returns a mutable slice of the string from the beginning to byte
|
|
|
|
/// `end`.
|
|
|
|
#[stable(feature = "derefmut_for_string", since = "1.2.0")]
|
|
|
|
impl ops::IndexMut<ops::RangeTo<usize>> for str {
|
|
|
|
#[inline]
|
|
|
|
fn index_mut(&mut self, index: ops::RangeTo<usize>) -> &mut str {
|
|
|
|
// is_char_boundary checks that the index is in [0, .len()]
|
|
|
|
if self.is_char_boundary(index.end) {
|
|
|
|
unsafe { self.slice_mut_unchecked(0, index.end) }
|
|
|
|
} else {
|
|
|
|
super::slice_error_fail(self, 0, index.end)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-17 16:15:47 -08:00
|
|
|
/// Returns a slice of the string from `begin` to its end.
|
|
|
|
///
|
|
|
|
/// Equivalent to `self[begin .. self.len()]`.
|
|
|
|
///
|
|
|
|
/// Panics when `begin` does not point to a valid character, or is
|
|
|
|
/// out of bounds.
|
2015-01-24 09:15:42 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-27 14:09:18 +01:00
|
|
|
impl ops::Index<ops::RangeFrom<usize>> for str {
|
2015-01-04 17:43:24 +13:00
|
|
|
type Output = str;
|
2015-03-21 19:33:27 -04:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn index(&self, index: ops::RangeFrom<usize>) -> &str {
|
|
|
|
// is_char_boundary checks that the index is in [0, .len()]
|
|
|
|
if self.is_char_boundary(index.start) {
|
|
|
|
unsafe { self.slice_unchecked(index.start, self.len()) }
|
|
|
|
} else {
|
|
|
|
super::slice_error_fail(self, index.start, self.len())
|
|
|
|
}
|
|
|
|
}
|
2014-12-31 20:20:40 +13:00
|
|
|
}
|
2015-01-17 16:15:47 -08:00
|
|
|
|
2015-06-15 18:33:21 +02:00
|
|
|
/// Returns a slice of the string from `begin` to its end.
|
|
|
|
#[stable(feature = "derefmut_for_string", since = "1.2.0")]
|
|
|
|
impl ops::IndexMut<ops::RangeFrom<usize>> for str {
|
|
|
|
#[inline]
|
|
|
|
fn index_mut(&mut self, index: ops::RangeFrom<usize>) -> &mut str {
|
|
|
|
// is_char_boundary checks that the index is in [0, .len()]
|
|
|
|
if self.is_char_boundary(index.start) {
|
|
|
|
let len = self.len();
|
|
|
|
unsafe { self.slice_mut_unchecked(index.start, len) }
|
|
|
|
} else {
|
|
|
|
super::slice_error_fail(self, index.start, self.len())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-28 17:06:46 +13:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
impl ops::Index<ops::RangeFull> for str {
|
|
|
|
type Output = str;
|
2015-03-21 19:33:27 -04:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn index(&self, _index: ops::RangeFull) -> &str {
|
|
|
|
self
|
|
|
|
}
|
2015-01-28 17:06:46 +13:00
|
|
|
}
|
2015-06-15 18:33:21 +02:00
|
|
|
|
|
|
|
#[stable(feature = "derefmut_for_string", since = "1.2.0")]
|
|
|
|
impl ops::IndexMut<ops::RangeFull> for str {
|
|
|
|
#[inline]
|
|
|
|
fn index_mut(&mut self, _index: ops::RangeFull) -> &mut str {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
2014-04-30 23:06:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Methods for string slices
|
std: Stabilize the std::str module
This commit starts out by consolidating all `str` extension traits into one
`StrExt` trait to be included in the prelude. This means that
`UnicodeStrPrelude`, `StrPrelude`, and `StrAllocating` have all been merged into
one `StrExt` exported by the standard library. Some functionality is currently
duplicated with the `StrExt` present in libcore.
This commit also currently avoids any methods which require any form of pattern
to operate. These functions will be stabilized via a separate RFC.
Next, stability of methods and structures are as follows:
Stable
* from_utf8_unchecked
* CowString - after moving to std::string
* StrExt::as_bytes
* StrExt::as_ptr
* StrExt::bytes/Bytes - also made a struct instead of a typedef
* StrExt::char_indices/CharIndices - CharOffsets was renamed
* StrExt::chars/Chars
* StrExt::is_empty
* StrExt::len
* StrExt::lines/Lines
* StrExt::lines_any/LinesAny
* StrExt::slice_unchecked
* StrExt::trim
* StrExt::trim_left
* StrExt::trim_right
* StrExt::words/Words - also made a struct instead of a typedef
Unstable
* from_utf8 - the error type was changed to a `Result`, but the error type has
yet to prove itself
* from_c_str - this function will be handled by the c_str RFC
* FromStr - this trait will have an associated error type eventually
* StrExt::escape_default - needs iterators at least, unsure if it should make
the cut
* StrExt::escape_unicode - needs iterators at least, unsure if it should make
the cut
* StrExt::slice_chars - this function has yet to prove itself
* StrExt::slice_shift_char - awaiting conventions about slicing and shifting
* StrExt::graphemes/Graphemes - this functionality may only be in libunicode
* StrExt::grapheme_indices/GraphemeIndices - this functionality may only be in
libunicode
* StrExt::width - this functionality may only be in libunicode
* StrExt::utf16_units - this functionality may only be in libunicode
* StrExt::nfd_chars - this functionality may only be in libunicode
* StrExt::nfkd_chars - this functionality may only be in libunicode
* StrExt::nfc_chars - this functionality may only be in libunicode
* StrExt::nfkc_chars - this functionality may only be in libunicode
* StrExt::is_char_boundary - naming is uncertain with container conventions
* StrExt::char_range_at - naming is uncertain with container conventions
* StrExt::char_range_at_reverse - naming is uncertain with container conventions
* StrExt::char_at - naming is uncertain with container conventions
* StrExt::char_at_reverse - naming is uncertain with container conventions
* StrVector::concat - this functionality may be replaced with iterators, but
it's not certain at this time
* StrVector::connect - as with concat, may be deprecated in favor of iterators
Deprecated
* StrAllocating and UnicodeStrPrelude have been merged into StrExit
* eq_slice - compiler implementation detail
* from_str - use the inherent parse() method
* is_utf8 - call from_utf8 instead
* replace - call the method instead
* truncate_utf16_at_nul - this is an implementation detail of windows and does
not need to be exposed.
* utf8_char_width - moved to libunicode
* utf16_items - moved to libunicode
* is_utf16 - moved to libunicode
* Utf16Items - moved to libunicode
* Utf16Item - moved to libunicode
* Utf16Encoder - moved to libunicode
* AnyLines - renamed to LinesAny and made a struct
* SendStr - use CowString<'static> instead
* str::raw - all functionality is deprecated
* StrExt::into_string - call to_string() instead
* StrExt::repeat - use iterators instead
* StrExt::char_len - use .chars().count() instead
* StrExt::is_alphanumeric - use .chars().all(..)
* StrExt::is_whitespace - use .chars().all(..)
Pending deprecation -- while slicing syntax is being worked out, these methods
are all #[unstable]
* Str - while currently used for generic programming, this trait will be
replaced with one of [], deref coercions, or a generic conversion trait.
* StrExt::slice - use slicing syntax instead
* StrExt::slice_to - use slicing syntax instead
* StrExt::slice_from - use slicing syntax instead
* StrExt::lev_distance - deprecated with no replacement
Awaiting stabilization due to patterns and/or matching
* StrExt::contains
* StrExt::contains_char
* StrExt::split
* StrExt::splitn
* StrExt::split_terminator
* StrExt::rsplitn
* StrExt::match_indices
* StrExt::split_str
* StrExt::starts_with
* StrExt::ends_with
* StrExt::trim_chars
* StrExt::trim_left_chars
* StrExt::trim_right_chars
* StrExt::find
* StrExt::rfind
* StrExt::find_str
* StrExt::subslice_offset
2014-12-10 09:02:31 -08:00
|
|
|
#[allow(missing_docs)]
|
2015-04-07 17:15:58 -07:00
|
|
|
#[doc(hidden)]
|
2015-06-09 11:18:03 -07:00
|
|
|
#[unstable(feature = "core_str_ext",
|
2015-08-12 17:23:48 -07:00
|
|
|
reason = "stable interface provided by `impl str` in later crates",
|
|
|
|
issue = "27701")]
|
2015-01-04 21:39:02 -05:00
|
|
|
pub trait StrExt {
|
std: Stabilize the std::str module
This commit starts out by consolidating all `str` extension traits into one
`StrExt` trait to be included in the prelude. This means that
`UnicodeStrPrelude`, `StrPrelude`, and `StrAllocating` have all been merged into
one `StrExt` exported by the standard library. Some functionality is currently
duplicated with the `StrExt` present in libcore.
This commit also currently avoids any methods which require any form of pattern
to operate. These functions will be stabilized via a separate RFC.
Next, stability of methods and structures are as follows:
Stable
* from_utf8_unchecked
* CowString - after moving to std::string
* StrExt::as_bytes
* StrExt::as_ptr
* StrExt::bytes/Bytes - also made a struct instead of a typedef
* StrExt::char_indices/CharIndices - CharOffsets was renamed
* StrExt::chars/Chars
* StrExt::is_empty
* StrExt::len
* StrExt::lines/Lines
* StrExt::lines_any/LinesAny
* StrExt::slice_unchecked
* StrExt::trim
* StrExt::trim_left
* StrExt::trim_right
* StrExt::words/Words - also made a struct instead of a typedef
Unstable
* from_utf8 - the error type was changed to a `Result`, but the error type has
yet to prove itself
* from_c_str - this function will be handled by the c_str RFC
* FromStr - this trait will have an associated error type eventually
* StrExt::escape_default - needs iterators at least, unsure if it should make
the cut
* StrExt::escape_unicode - needs iterators at least, unsure if it should make
the cut
* StrExt::slice_chars - this function has yet to prove itself
* StrExt::slice_shift_char - awaiting conventions about slicing and shifting
* StrExt::graphemes/Graphemes - this functionality may only be in libunicode
* StrExt::grapheme_indices/GraphemeIndices - this functionality may only be in
libunicode
* StrExt::width - this functionality may only be in libunicode
* StrExt::utf16_units - this functionality may only be in libunicode
* StrExt::nfd_chars - this functionality may only be in libunicode
* StrExt::nfkd_chars - this functionality may only be in libunicode
* StrExt::nfc_chars - this functionality may only be in libunicode
* StrExt::nfkc_chars - this functionality may only be in libunicode
* StrExt::is_char_boundary - naming is uncertain with container conventions
* StrExt::char_range_at - naming is uncertain with container conventions
* StrExt::char_range_at_reverse - naming is uncertain with container conventions
* StrExt::char_at - naming is uncertain with container conventions
* StrExt::char_at_reverse - naming is uncertain with container conventions
* StrVector::concat - this functionality may be replaced with iterators, but
it's not certain at this time
* StrVector::connect - as with concat, may be deprecated in favor of iterators
Deprecated
* StrAllocating and UnicodeStrPrelude have been merged into StrExit
* eq_slice - compiler implementation detail
* from_str - use the inherent parse() method
* is_utf8 - call from_utf8 instead
* replace - call the method instead
* truncate_utf16_at_nul - this is an implementation detail of windows and does
not need to be exposed.
* utf8_char_width - moved to libunicode
* utf16_items - moved to libunicode
* is_utf16 - moved to libunicode
* Utf16Items - moved to libunicode
* Utf16Item - moved to libunicode
* Utf16Encoder - moved to libunicode
* AnyLines - renamed to LinesAny and made a struct
* SendStr - use CowString<'static> instead
* str::raw - all functionality is deprecated
* StrExt::into_string - call to_string() instead
* StrExt::repeat - use iterators instead
* StrExt::char_len - use .chars().count() instead
* StrExt::is_alphanumeric - use .chars().all(..)
* StrExt::is_whitespace - use .chars().all(..)
Pending deprecation -- while slicing syntax is being worked out, these methods
are all #[unstable]
* Str - while currently used for generic programming, this trait will be
replaced with one of [], deref coercions, or a generic conversion trait.
* StrExt::slice - use slicing syntax instead
* StrExt::slice_to - use slicing syntax instead
* StrExt::slice_from - use slicing syntax instead
* StrExt::lev_distance - deprecated with no replacement
Awaiting stabilization due to patterns and/or matching
* StrExt::contains
* StrExt::contains_char
* StrExt::split
* StrExt::splitn
* StrExt::split_terminator
* StrExt::rsplitn
* StrExt::match_indices
* StrExt::split_str
* StrExt::starts_with
* StrExt::ends_with
* StrExt::trim_chars
* StrExt::trim_left_chars
* StrExt::trim_right_chars
* StrExt::find
* StrExt::rfind
* StrExt::find_str
* StrExt::subslice_offset
2014-12-10 09:02:31 -08:00
|
|
|
// NB there are no docs here are they're all located on the StrExt trait in
|
|
|
|
// libcollections, not here.
|
2014-04-30 23:06:36 -07:00
|
|
|
|
2014-12-30 21:54:17 +01:00
|
|
|
fn contains<'a, P: Pattern<'a>>(&'a self, pat: P) -> bool;
|
|
|
|
fn contains_char<'a, P: Pattern<'a>>(&'a self, pat: P) -> bool;
|
2014-10-23 10:43:18 -05:00
|
|
|
fn chars<'a>(&'a self) -> Chars<'a>;
|
|
|
|
fn bytes<'a>(&'a self) -> Bytes<'a>;
|
std: Stabilize the std::str module
This commit starts out by consolidating all `str` extension traits into one
`StrExt` trait to be included in the prelude. This means that
`UnicodeStrPrelude`, `StrPrelude`, and `StrAllocating` have all been merged into
one `StrExt` exported by the standard library. Some functionality is currently
duplicated with the `StrExt` present in libcore.
This commit also currently avoids any methods which require any form of pattern
to operate. These functions will be stabilized via a separate RFC.
Next, stability of methods and structures are as follows:
Stable
* from_utf8_unchecked
* CowString - after moving to std::string
* StrExt::as_bytes
* StrExt::as_ptr
* StrExt::bytes/Bytes - also made a struct instead of a typedef
* StrExt::char_indices/CharIndices - CharOffsets was renamed
* StrExt::chars/Chars
* StrExt::is_empty
* StrExt::len
* StrExt::lines/Lines
* StrExt::lines_any/LinesAny
* StrExt::slice_unchecked
* StrExt::trim
* StrExt::trim_left
* StrExt::trim_right
* StrExt::words/Words - also made a struct instead of a typedef
Unstable
* from_utf8 - the error type was changed to a `Result`, but the error type has
yet to prove itself
* from_c_str - this function will be handled by the c_str RFC
* FromStr - this trait will have an associated error type eventually
* StrExt::escape_default - needs iterators at least, unsure if it should make
the cut
* StrExt::escape_unicode - needs iterators at least, unsure if it should make
the cut
* StrExt::slice_chars - this function has yet to prove itself
* StrExt::slice_shift_char - awaiting conventions about slicing and shifting
* StrExt::graphemes/Graphemes - this functionality may only be in libunicode
* StrExt::grapheme_indices/GraphemeIndices - this functionality may only be in
libunicode
* StrExt::width - this functionality may only be in libunicode
* StrExt::utf16_units - this functionality may only be in libunicode
* StrExt::nfd_chars - this functionality may only be in libunicode
* StrExt::nfkd_chars - this functionality may only be in libunicode
* StrExt::nfc_chars - this functionality may only be in libunicode
* StrExt::nfkc_chars - this functionality may only be in libunicode
* StrExt::is_char_boundary - naming is uncertain with container conventions
* StrExt::char_range_at - naming is uncertain with container conventions
* StrExt::char_range_at_reverse - naming is uncertain with container conventions
* StrExt::char_at - naming is uncertain with container conventions
* StrExt::char_at_reverse - naming is uncertain with container conventions
* StrVector::concat - this functionality may be replaced with iterators, but
it's not certain at this time
* StrVector::connect - as with concat, may be deprecated in favor of iterators
Deprecated
* StrAllocating and UnicodeStrPrelude have been merged into StrExit
* eq_slice - compiler implementation detail
* from_str - use the inherent parse() method
* is_utf8 - call from_utf8 instead
* replace - call the method instead
* truncate_utf16_at_nul - this is an implementation detail of windows and does
not need to be exposed.
* utf8_char_width - moved to libunicode
* utf16_items - moved to libunicode
* is_utf16 - moved to libunicode
* Utf16Items - moved to libunicode
* Utf16Item - moved to libunicode
* Utf16Encoder - moved to libunicode
* AnyLines - renamed to LinesAny and made a struct
* SendStr - use CowString<'static> instead
* str::raw - all functionality is deprecated
* StrExt::into_string - call to_string() instead
* StrExt::repeat - use iterators instead
* StrExt::char_len - use .chars().count() instead
* StrExt::is_alphanumeric - use .chars().all(..)
* StrExt::is_whitespace - use .chars().all(..)
Pending deprecation -- while slicing syntax is being worked out, these methods
are all #[unstable]
* Str - while currently used for generic programming, this trait will be
replaced with one of [], deref coercions, or a generic conversion trait.
* StrExt::slice - use slicing syntax instead
* StrExt::slice_to - use slicing syntax instead
* StrExt::slice_from - use slicing syntax instead
* StrExt::lev_distance - deprecated with no replacement
Awaiting stabilization due to patterns and/or matching
* StrExt::contains
* StrExt::contains_char
* StrExt::split
* StrExt::splitn
* StrExt::split_terminator
* StrExt::rsplitn
* StrExt::match_indices
* StrExt::split_str
* StrExt::starts_with
* StrExt::ends_with
* StrExt::trim_chars
* StrExt::trim_left_chars
* StrExt::trim_right_chars
* StrExt::find
* StrExt::rfind
* StrExt::find_str
* StrExt::subslice_offset
2014-12-10 09:02:31 -08:00
|
|
|
fn char_indices<'a>(&'a self) -> CharIndices<'a>;
|
2015-01-27 14:09:18 +01:00
|
|
|
fn split<'a, P: Pattern<'a>>(&'a self, pat: P) -> Split<'a, P>;
|
2015-03-14 19:34:21 -04:00
|
|
|
fn rsplit<'a, P: Pattern<'a>>(&'a self, pat: P) -> RSplit<'a, P>
|
|
|
|
where P::Searcher: ReverseSearcher<'a>;
|
2015-03-15 01:48:34 +01:00
|
|
|
fn splitn<'a, P: Pattern<'a>>(&'a self, count: usize, pat: P) -> SplitN<'a, P>;
|
2015-03-14 20:07:13 -04:00
|
|
|
fn rsplitn<'a, P: Pattern<'a>>(&'a self, count: usize, pat: P) -> RSplitN<'a, P>
|
|
|
|
where P::Searcher: ReverseSearcher<'a>;
|
2015-03-15 01:48:34 +01:00
|
|
|
fn split_terminator<'a, P: Pattern<'a>>(&'a self, pat: P) -> SplitTerminator<'a, P>;
|
|
|
|
fn rsplit_terminator<'a, P: Pattern<'a>>(&'a self, pat: P) -> RSplitTerminator<'a, P>
|
|
|
|
where P::Searcher: ReverseSearcher<'a>;
|
|
|
|
fn matches<'a, P: Pattern<'a>>(&'a self, pat: P) -> Matches<'a, P>;
|
|
|
|
fn rmatches<'a, P: Pattern<'a>>(&'a self, pat: P) -> RMatches<'a, P>
|
|
|
|
where P::Searcher: ReverseSearcher<'a>;
|
2015-01-14 20:45:51 +01:00
|
|
|
fn match_indices<'a, P: Pattern<'a>>(&'a self, pat: P) -> MatchIndices<'a, P>;
|
2015-03-15 01:48:34 +01:00
|
|
|
fn rmatch_indices<'a, P: Pattern<'a>>(&'a self, pat: P) -> RMatchIndices<'a, P>
|
|
|
|
where P::Searcher: ReverseSearcher<'a>;
|
std: Stabilize the std::str module
This commit starts out by consolidating all `str` extension traits into one
`StrExt` trait to be included in the prelude. This means that
`UnicodeStrPrelude`, `StrPrelude`, and `StrAllocating` have all been merged into
one `StrExt` exported by the standard library. Some functionality is currently
duplicated with the `StrExt` present in libcore.
This commit also currently avoids any methods which require any form of pattern
to operate. These functions will be stabilized via a separate RFC.
Next, stability of methods and structures are as follows:
Stable
* from_utf8_unchecked
* CowString - after moving to std::string
* StrExt::as_bytes
* StrExt::as_ptr
* StrExt::bytes/Bytes - also made a struct instead of a typedef
* StrExt::char_indices/CharIndices - CharOffsets was renamed
* StrExt::chars/Chars
* StrExt::is_empty
* StrExt::len
* StrExt::lines/Lines
* StrExt::lines_any/LinesAny
* StrExt::slice_unchecked
* StrExt::trim
* StrExt::trim_left
* StrExt::trim_right
* StrExt::words/Words - also made a struct instead of a typedef
Unstable
* from_utf8 - the error type was changed to a `Result`, but the error type has
yet to prove itself
* from_c_str - this function will be handled by the c_str RFC
* FromStr - this trait will have an associated error type eventually
* StrExt::escape_default - needs iterators at least, unsure if it should make
the cut
* StrExt::escape_unicode - needs iterators at least, unsure if it should make
the cut
* StrExt::slice_chars - this function has yet to prove itself
* StrExt::slice_shift_char - awaiting conventions about slicing and shifting
* StrExt::graphemes/Graphemes - this functionality may only be in libunicode
* StrExt::grapheme_indices/GraphemeIndices - this functionality may only be in
libunicode
* StrExt::width - this functionality may only be in libunicode
* StrExt::utf16_units - this functionality may only be in libunicode
* StrExt::nfd_chars - this functionality may only be in libunicode
* StrExt::nfkd_chars - this functionality may only be in libunicode
* StrExt::nfc_chars - this functionality may only be in libunicode
* StrExt::nfkc_chars - this functionality may only be in libunicode
* StrExt::is_char_boundary - naming is uncertain with container conventions
* StrExt::char_range_at - naming is uncertain with container conventions
* StrExt::char_range_at_reverse - naming is uncertain with container conventions
* StrExt::char_at - naming is uncertain with container conventions
* StrExt::char_at_reverse - naming is uncertain with container conventions
* StrVector::concat - this functionality may be replaced with iterators, but
it's not certain at this time
* StrVector::connect - as with concat, may be deprecated in favor of iterators
Deprecated
* StrAllocating and UnicodeStrPrelude have been merged into StrExit
* eq_slice - compiler implementation detail
* from_str - use the inherent parse() method
* is_utf8 - call from_utf8 instead
* replace - call the method instead
* truncate_utf16_at_nul - this is an implementation detail of windows and does
not need to be exposed.
* utf8_char_width - moved to libunicode
* utf16_items - moved to libunicode
* is_utf16 - moved to libunicode
* Utf16Items - moved to libunicode
* Utf16Item - moved to libunicode
* Utf16Encoder - moved to libunicode
* AnyLines - renamed to LinesAny and made a struct
* SendStr - use CowString<'static> instead
* str::raw - all functionality is deprecated
* StrExt::into_string - call to_string() instead
* StrExt::repeat - use iterators instead
* StrExt::char_len - use .chars().count() instead
* StrExt::is_alphanumeric - use .chars().all(..)
* StrExt::is_whitespace - use .chars().all(..)
Pending deprecation -- while slicing syntax is being worked out, these methods
are all #[unstable]
* Str - while currently used for generic programming, this trait will be
replaced with one of [], deref coercions, or a generic conversion trait.
* StrExt::slice - use slicing syntax instead
* StrExt::slice_to - use slicing syntax instead
* StrExt::slice_from - use slicing syntax instead
* StrExt::lev_distance - deprecated with no replacement
Awaiting stabilization due to patterns and/or matching
* StrExt::contains
* StrExt::contains_char
* StrExt::split
* StrExt::splitn
* StrExt::split_terminator
* StrExt::rsplitn
* StrExt::match_indices
* StrExt::split_str
* StrExt::starts_with
* StrExt::ends_with
* StrExt::trim_chars
* StrExt::trim_left_chars
* StrExt::trim_right_chars
* StrExt::find
* StrExt::rfind
* StrExt::find_str
* StrExt::subslice_offset
2014-12-10 09:02:31 -08:00
|
|
|
fn lines<'a>(&'a self) -> Lines<'a>;
|
|
|
|
fn lines_any<'a>(&'a self) -> LinesAny<'a>;
|
2015-01-27 14:09:18 +01:00
|
|
|
fn char_len(&self) -> usize;
|
|
|
|
fn slice_chars<'a>(&'a self, begin: usize, end: usize) -> &'a str;
|
|
|
|
unsafe fn slice_unchecked<'a>(&'a self, begin: usize, end: usize) -> &'a str;
|
2015-06-15 18:33:21 +02:00
|
|
|
unsafe fn slice_mut_unchecked<'a>(&'a mut self, begin: usize, end: usize) -> &'a mut str;
|
2015-01-27 14:09:18 +01:00
|
|
|
fn starts_with<'a, P: Pattern<'a>>(&'a self, pat: P) -> bool;
|
|
|
|
fn ends_with<'a, P: Pattern<'a>>(&'a self, pat: P) -> bool
|
|
|
|
where P::Searcher: ReverseSearcher<'a>;
|
2014-12-30 21:54:17 +01:00
|
|
|
fn trim_matches<'a, P: Pattern<'a>>(&'a self, pat: P) -> &'a str
|
2015-01-27 14:09:18 +01:00
|
|
|
where P::Searcher: DoubleEndedSearcher<'a>;
|
2014-12-30 21:54:17 +01:00
|
|
|
fn trim_left_matches<'a, P: Pattern<'a>>(&'a self, pat: P) -> &'a str;
|
|
|
|
fn trim_right_matches<'a, P: Pattern<'a>>(&'a self, pat: P) -> &'a str
|
2015-01-27 14:09:18 +01:00
|
|
|
where P::Searcher: ReverseSearcher<'a>;
|
|
|
|
fn is_char_boundary(&self, index: usize) -> bool;
|
|
|
|
fn char_range_at(&self, start: usize) -> CharRange;
|
|
|
|
fn char_range_at_reverse(&self, start: usize) -> CharRange;
|
|
|
|
fn char_at(&self, i: usize) -> char;
|
|
|
|
fn char_at_reverse(&self, i: usize) -> char;
|
2014-10-23 10:43:18 -05:00
|
|
|
fn as_bytes<'a>(&'a self) -> &'a [u8];
|
2015-01-27 14:09:18 +01:00
|
|
|
fn find<'a, P: Pattern<'a>>(&'a self, pat: P) -> Option<usize>;
|
|
|
|
fn rfind<'a, P: Pattern<'a>>(&'a self, pat: P) -> Option<usize>
|
|
|
|
where P::Searcher: ReverseSearcher<'a>;
|
|
|
|
fn find_str<'a, P: Pattern<'a>>(&'a self, pat: P) -> Option<usize>;
|
2015-06-09 11:23:22 +02:00
|
|
|
fn split_at(&self, mid: usize) -> (&str, &str);
|
2015-06-15 19:24:52 +02:00
|
|
|
fn split_at_mut(&mut self, mid: usize) -> (&mut str, &mut str);
|
change return type of slice_shift_char
`slice_shift_char` splits a `str` into it's leading `char` and the remainder
of the `str`. Currently, it returns a `(Option<char>, &str)` such that:
"bar".slice_shift_char() => (Some('b'), "ar")
"ar".slice_shift_char() => (Some('a'), "r")
"r".slice_shift_char() => (Some('r'), "")
"".slice_shift_char() => (None, "")
This is a little odd. Either a `str` can be split into both a head and a
tail or it cannot. So the return type should be `Option<(char, &str)>`.
With the current behaviour, in the case of the empty string, the `str`
returned is meaningless - it is always the empty string.
This commit changes slice_shift_char so that:
"bar".slice_shift_char() => Some(('b', "ar"))
"ar".slice_shift_char() => Some(('a', "r"))
"r".slice_shift_char() => Some(('r', ""))
"".slice_shift_char() => None
[breaking-change]
2014-11-17 17:35:18 +08:00
|
|
|
fn slice_shift_char<'a>(&'a self) -> Option<(char, &'a str)>;
|
2015-01-27 14:09:18 +01:00
|
|
|
fn subslice_offset(&self, inner: &str) -> usize;
|
2014-06-25 12:47:34 -07:00
|
|
|
fn as_ptr(&self) -> *const u8;
|
2015-01-27 14:09:18 +01:00
|
|
|
fn len(&self) -> usize;
|
std: Stabilize the std::str module
This commit starts out by consolidating all `str` extension traits into one
`StrExt` trait to be included in the prelude. This means that
`UnicodeStrPrelude`, `StrPrelude`, and `StrAllocating` have all been merged into
one `StrExt` exported by the standard library. Some functionality is currently
duplicated with the `StrExt` present in libcore.
This commit also currently avoids any methods which require any form of pattern
to operate. These functions will be stabilized via a separate RFC.
Next, stability of methods and structures are as follows:
Stable
* from_utf8_unchecked
* CowString - after moving to std::string
* StrExt::as_bytes
* StrExt::as_ptr
* StrExt::bytes/Bytes - also made a struct instead of a typedef
* StrExt::char_indices/CharIndices - CharOffsets was renamed
* StrExt::chars/Chars
* StrExt::is_empty
* StrExt::len
* StrExt::lines/Lines
* StrExt::lines_any/LinesAny
* StrExt::slice_unchecked
* StrExt::trim
* StrExt::trim_left
* StrExt::trim_right
* StrExt::words/Words - also made a struct instead of a typedef
Unstable
* from_utf8 - the error type was changed to a `Result`, but the error type has
yet to prove itself
* from_c_str - this function will be handled by the c_str RFC
* FromStr - this trait will have an associated error type eventually
* StrExt::escape_default - needs iterators at least, unsure if it should make
the cut
* StrExt::escape_unicode - needs iterators at least, unsure if it should make
the cut
* StrExt::slice_chars - this function has yet to prove itself
* StrExt::slice_shift_char - awaiting conventions about slicing and shifting
* StrExt::graphemes/Graphemes - this functionality may only be in libunicode
* StrExt::grapheme_indices/GraphemeIndices - this functionality may only be in
libunicode
* StrExt::width - this functionality may only be in libunicode
* StrExt::utf16_units - this functionality may only be in libunicode
* StrExt::nfd_chars - this functionality may only be in libunicode
* StrExt::nfkd_chars - this functionality may only be in libunicode
* StrExt::nfc_chars - this functionality may only be in libunicode
* StrExt::nfkc_chars - this functionality may only be in libunicode
* StrExt::is_char_boundary - naming is uncertain with container conventions
* StrExt::char_range_at - naming is uncertain with container conventions
* StrExt::char_range_at_reverse - naming is uncertain with container conventions
* StrExt::char_at - naming is uncertain with container conventions
* StrExt::char_at_reverse - naming is uncertain with container conventions
* StrVector::concat - this functionality may be replaced with iterators, but
it's not certain at this time
* StrVector::connect - as with concat, may be deprecated in favor of iterators
Deprecated
* StrAllocating and UnicodeStrPrelude have been merged into StrExit
* eq_slice - compiler implementation detail
* from_str - use the inherent parse() method
* is_utf8 - call from_utf8 instead
* replace - call the method instead
* truncate_utf16_at_nul - this is an implementation detail of windows and does
not need to be exposed.
* utf8_char_width - moved to libunicode
* utf16_items - moved to libunicode
* is_utf16 - moved to libunicode
* Utf16Items - moved to libunicode
* Utf16Item - moved to libunicode
* Utf16Encoder - moved to libunicode
* AnyLines - renamed to LinesAny and made a struct
* SendStr - use CowString<'static> instead
* str::raw - all functionality is deprecated
* StrExt::into_string - call to_string() instead
* StrExt::repeat - use iterators instead
* StrExt::char_len - use .chars().count() instead
* StrExt::is_alphanumeric - use .chars().all(..)
* StrExt::is_whitespace - use .chars().all(..)
Pending deprecation -- while slicing syntax is being worked out, these methods
are all #[unstable]
* Str - while currently used for generic programming, this trait will be
replaced with one of [], deref coercions, or a generic conversion trait.
* StrExt::slice - use slicing syntax instead
* StrExt::slice_to - use slicing syntax instead
* StrExt::slice_from - use slicing syntax instead
* StrExt::lev_distance - deprecated with no replacement
Awaiting stabilization due to patterns and/or matching
* StrExt::contains
* StrExt::contains_char
* StrExt::split
* StrExt::splitn
* StrExt::split_terminator
* StrExt::rsplitn
* StrExt::match_indices
* StrExt::split_str
* StrExt::starts_with
* StrExt::ends_with
* StrExt::trim_chars
* StrExt::trim_left_chars
* StrExt::trim_right_chars
* StrExt::find
* StrExt::rfind
* StrExt::find_str
* StrExt::subslice_offset
2014-12-10 09:02:31 -08:00
|
|
|
fn is_empty(&self) -> bool;
|
2015-01-27 22:52:32 -08:00
|
|
|
fn parse<T: FromStr>(&self) -> Result<T, T::Err>;
|
2014-04-30 23:06:36 -07:00
|
|
|
}
|
|
|
|
|
2014-08-23 12:30:08 +02:00
|
|
|
#[inline(never)]
|
2015-01-27 14:09:18 +01:00
|
|
|
fn slice_error_fail(s: &str, begin: usize, end: usize) -> ! {
|
2014-08-23 12:30:08 +02:00
|
|
|
assert!(begin <= end);
|
2014-10-09 15:17:22 -04:00
|
|
|
panic!("index {} and/or {} in `{}` do not lie on character boundary",
|
2014-08-23 12:30:08 +02:00
|
|
|
begin, end, s);
|
|
|
|
}
|
|
|
|
|
std: Stabilize the std::str module
This commit starts out by consolidating all `str` extension traits into one
`StrExt` trait to be included in the prelude. This means that
`UnicodeStrPrelude`, `StrPrelude`, and `StrAllocating` have all been merged into
one `StrExt` exported by the standard library. Some functionality is currently
duplicated with the `StrExt` present in libcore.
This commit also currently avoids any methods which require any form of pattern
to operate. These functions will be stabilized via a separate RFC.
Next, stability of methods and structures are as follows:
Stable
* from_utf8_unchecked
* CowString - after moving to std::string
* StrExt::as_bytes
* StrExt::as_ptr
* StrExt::bytes/Bytes - also made a struct instead of a typedef
* StrExt::char_indices/CharIndices - CharOffsets was renamed
* StrExt::chars/Chars
* StrExt::is_empty
* StrExt::len
* StrExt::lines/Lines
* StrExt::lines_any/LinesAny
* StrExt::slice_unchecked
* StrExt::trim
* StrExt::trim_left
* StrExt::trim_right
* StrExt::words/Words - also made a struct instead of a typedef
Unstable
* from_utf8 - the error type was changed to a `Result`, but the error type has
yet to prove itself
* from_c_str - this function will be handled by the c_str RFC
* FromStr - this trait will have an associated error type eventually
* StrExt::escape_default - needs iterators at least, unsure if it should make
the cut
* StrExt::escape_unicode - needs iterators at least, unsure if it should make
the cut
* StrExt::slice_chars - this function has yet to prove itself
* StrExt::slice_shift_char - awaiting conventions about slicing and shifting
* StrExt::graphemes/Graphemes - this functionality may only be in libunicode
* StrExt::grapheme_indices/GraphemeIndices - this functionality may only be in
libunicode
* StrExt::width - this functionality may only be in libunicode
* StrExt::utf16_units - this functionality may only be in libunicode
* StrExt::nfd_chars - this functionality may only be in libunicode
* StrExt::nfkd_chars - this functionality may only be in libunicode
* StrExt::nfc_chars - this functionality may only be in libunicode
* StrExt::nfkc_chars - this functionality may only be in libunicode
* StrExt::is_char_boundary - naming is uncertain with container conventions
* StrExt::char_range_at - naming is uncertain with container conventions
* StrExt::char_range_at_reverse - naming is uncertain with container conventions
* StrExt::char_at - naming is uncertain with container conventions
* StrExt::char_at_reverse - naming is uncertain with container conventions
* StrVector::concat - this functionality may be replaced with iterators, but
it's not certain at this time
* StrVector::connect - as with concat, may be deprecated in favor of iterators
Deprecated
* StrAllocating and UnicodeStrPrelude have been merged into StrExit
* eq_slice - compiler implementation detail
* from_str - use the inherent parse() method
* is_utf8 - call from_utf8 instead
* replace - call the method instead
* truncate_utf16_at_nul - this is an implementation detail of windows and does
not need to be exposed.
* utf8_char_width - moved to libunicode
* utf16_items - moved to libunicode
* is_utf16 - moved to libunicode
* Utf16Items - moved to libunicode
* Utf16Item - moved to libunicode
* Utf16Encoder - moved to libunicode
* AnyLines - renamed to LinesAny and made a struct
* SendStr - use CowString<'static> instead
* str::raw - all functionality is deprecated
* StrExt::into_string - call to_string() instead
* StrExt::repeat - use iterators instead
* StrExt::char_len - use .chars().count() instead
* StrExt::is_alphanumeric - use .chars().all(..)
* StrExt::is_whitespace - use .chars().all(..)
Pending deprecation -- while slicing syntax is being worked out, these methods
are all #[unstable]
* Str - while currently used for generic programming, this trait will be
replaced with one of [], deref coercions, or a generic conversion trait.
* StrExt::slice - use slicing syntax instead
* StrExt::slice_to - use slicing syntax instead
* StrExt::slice_from - use slicing syntax instead
* StrExt::lev_distance - deprecated with no replacement
Awaiting stabilization due to patterns and/or matching
* StrExt::contains
* StrExt::contains_char
* StrExt::split
* StrExt::splitn
* StrExt::split_terminator
* StrExt::rsplitn
* StrExt::match_indices
* StrExt::split_str
* StrExt::starts_with
* StrExt::ends_with
* StrExt::trim_chars
* StrExt::trim_left_chars
* StrExt::trim_right_chars
* StrExt::find
* StrExt::rfind
* StrExt::find_str
* StrExt::subslice_offset
2014-12-10 09:02:31 -08:00
|
|
|
impl StrExt for str {
|
2014-04-30 23:06:36 -07:00
|
|
|
#[inline]
|
2014-12-30 21:54:17 +01:00
|
|
|
fn contains<'a, P: Pattern<'a>>(&'a self, pat: P) -> bool {
|
|
|
|
pat.is_contained_in(self)
|
2014-04-30 23:06:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2014-12-30 21:54:17 +01:00
|
|
|
fn contains_char<'a, P: Pattern<'a>>(&'a self, pat: P) -> bool {
|
|
|
|
pat.is_contained_in(self)
|
2014-04-30 23:06:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2014-10-23 10:43:18 -05:00
|
|
|
fn chars(&self) -> Chars {
|
2014-07-17 19:34:07 +02:00
|
|
|
Chars{iter: self.as_bytes().iter()}
|
2014-04-30 23:06:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2014-10-23 10:43:18 -05:00
|
|
|
fn bytes(&self) -> Bytes {
|
2014-12-18 02:12:53 +01:00
|
|
|
Bytes(self.as_bytes().iter().map(BytesDeref))
|
2014-04-30 23:06:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
std: Stabilize the std::str module
This commit starts out by consolidating all `str` extension traits into one
`StrExt` trait to be included in the prelude. This means that
`UnicodeStrPrelude`, `StrPrelude`, and `StrAllocating` have all been merged into
one `StrExt` exported by the standard library. Some functionality is currently
duplicated with the `StrExt` present in libcore.
This commit also currently avoids any methods which require any form of pattern
to operate. These functions will be stabilized via a separate RFC.
Next, stability of methods and structures are as follows:
Stable
* from_utf8_unchecked
* CowString - after moving to std::string
* StrExt::as_bytes
* StrExt::as_ptr
* StrExt::bytes/Bytes - also made a struct instead of a typedef
* StrExt::char_indices/CharIndices - CharOffsets was renamed
* StrExt::chars/Chars
* StrExt::is_empty
* StrExt::len
* StrExt::lines/Lines
* StrExt::lines_any/LinesAny
* StrExt::slice_unchecked
* StrExt::trim
* StrExt::trim_left
* StrExt::trim_right
* StrExt::words/Words - also made a struct instead of a typedef
Unstable
* from_utf8 - the error type was changed to a `Result`, but the error type has
yet to prove itself
* from_c_str - this function will be handled by the c_str RFC
* FromStr - this trait will have an associated error type eventually
* StrExt::escape_default - needs iterators at least, unsure if it should make
the cut
* StrExt::escape_unicode - needs iterators at least, unsure if it should make
the cut
* StrExt::slice_chars - this function has yet to prove itself
* StrExt::slice_shift_char - awaiting conventions about slicing and shifting
* StrExt::graphemes/Graphemes - this functionality may only be in libunicode
* StrExt::grapheme_indices/GraphemeIndices - this functionality may only be in
libunicode
* StrExt::width - this functionality may only be in libunicode
* StrExt::utf16_units - this functionality may only be in libunicode
* StrExt::nfd_chars - this functionality may only be in libunicode
* StrExt::nfkd_chars - this functionality may only be in libunicode
* StrExt::nfc_chars - this functionality may only be in libunicode
* StrExt::nfkc_chars - this functionality may only be in libunicode
* StrExt::is_char_boundary - naming is uncertain with container conventions
* StrExt::char_range_at - naming is uncertain with container conventions
* StrExt::char_range_at_reverse - naming is uncertain with container conventions
* StrExt::char_at - naming is uncertain with container conventions
* StrExt::char_at_reverse - naming is uncertain with container conventions
* StrVector::concat - this functionality may be replaced with iterators, but
it's not certain at this time
* StrVector::connect - as with concat, may be deprecated in favor of iterators
Deprecated
* StrAllocating and UnicodeStrPrelude have been merged into StrExit
* eq_slice - compiler implementation detail
* from_str - use the inherent parse() method
* is_utf8 - call from_utf8 instead
* replace - call the method instead
* truncate_utf16_at_nul - this is an implementation detail of windows and does
not need to be exposed.
* utf8_char_width - moved to libunicode
* utf16_items - moved to libunicode
* is_utf16 - moved to libunicode
* Utf16Items - moved to libunicode
* Utf16Item - moved to libunicode
* Utf16Encoder - moved to libunicode
* AnyLines - renamed to LinesAny and made a struct
* SendStr - use CowString<'static> instead
* str::raw - all functionality is deprecated
* StrExt::into_string - call to_string() instead
* StrExt::repeat - use iterators instead
* StrExt::char_len - use .chars().count() instead
* StrExt::is_alphanumeric - use .chars().all(..)
* StrExt::is_whitespace - use .chars().all(..)
Pending deprecation -- while slicing syntax is being worked out, these methods
are all #[unstable]
* Str - while currently used for generic programming, this trait will be
replaced with one of [], deref coercions, or a generic conversion trait.
* StrExt::slice - use slicing syntax instead
* StrExt::slice_to - use slicing syntax instead
* StrExt::slice_from - use slicing syntax instead
* StrExt::lev_distance - deprecated with no replacement
Awaiting stabilization due to patterns and/or matching
* StrExt::contains
* StrExt::contains_char
* StrExt::split
* StrExt::splitn
* StrExt::split_terminator
* StrExt::rsplitn
* StrExt::match_indices
* StrExt::split_str
* StrExt::starts_with
* StrExt::ends_with
* StrExt::trim_chars
* StrExt::trim_left_chars
* StrExt::trim_right_chars
* StrExt::find
* StrExt::rfind
* StrExt::find_str
* StrExt::subslice_offset
2014-12-10 09:02:31 -08:00
|
|
|
fn char_indices(&self) -> CharIndices {
|
|
|
|
CharIndices { front_offset: 0, iter: self.chars() }
|
2014-04-30 23:06:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2015-01-27 14:09:18 +01:00
|
|
|
fn split<'a, P: Pattern<'a>>(&'a self, pat: P) -> Split<'a, P> {
|
2015-03-15 01:48:34 +01:00
|
|
|
Split(SplitInternal {
|
2015-01-27 14:09:18 +01:00
|
|
|
start: 0,
|
|
|
|
end: self.len(),
|
2015-02-17 22:57:14 +01:00
|
|
|
matcher: pat.into_searcher(self),
|
2014-04-30 23:06:36 -07:00
|
|
|
allow_trailing_empty: true,
|
|
|
|
finished: false,
|
2014-12-18 02:12:53 +01:00
|
|
|
})
|
2014-04-30 23:06:36 -07:00
|
|
|
}
|
|
|
|
|
2015-03-15 01:48:34 +01:00
|
|
|
#[inline]
|
|
|
|
fn rsplit<'a, P: Pattern<'a>>(&'a self, pat: P) -> RSplit<'a, P>
|
|
|
|
where P::Searcher: ReverseSearcher<'a>
|
|
|
|
{
|
|
|
|
RSplit(self.split(pat).0)
|
|
|
|
}
|
|
|
|
|
2014-04-30 23:06:36 -07:00
|
|
|
#[inline]
|
2015-01-27 14:09:18 +01:00
|
|
|
fn splitn<'a, P: Pattern<'a>>(&'a self, count: usize, pat: P) -> SplitN<'a, P> {
|
2015-03-15 01:48:34 +01:00
|
|
|
SplitN(SplitNInternal {
|
2014-12-18 02:12:53 +01:00
|
|
|
iter: self.split(pat).0,
|
2014-04-30 23:06:36 -07:00
|
|
|
count: count,
|
2014-12-18 02:12:53 +01:00
|
|
|
})
|
2014-04-30 23:06:36 -07:00
|
|
|
}
|
|
|
|
|
2015-03-15 01:48:34 +01:00
|
|
|
#[inline]
|
|
|
|
fn rsplitn<'a, P: Pattern<'a>>(&'a self, count: usize, pat: P) -> RSplitN<'a, P>
|
|
|
|
where P::Searcher: ReverseSearcher<'a>
|
|
|
|
{
|
|
|
|
RSplitN(self.splitn(count, pat).0)
|
|
|
|
}
|
|
|
|
|
2014-04-30 23:06:36 -07:00
|
|
|
#[inline]
|
2015-01-27 14:09:18 +01:00
|
|
|
fn split_terminator<'a, P: Pattern<'a>>(&'a self, pat: P) -> SplitTerminator<'a, P> {
|
2015-03-15 01:48:34 +01:00
|
|
|
SplitTerminator(SplitInternal {
|
2014-04-30 23:06:36 -07:00
|
|
|
allow_trailing_empty: false,
|
2014-12-18 02:12:53 +01:00
|
|
|
..self.split(pat).0
|
|
|
|
})
|
2014-04-30 23:06:36 -07:00
|
|
|
}
|
|
|
|
|
2015-03-14 19:34:21 -04:00
|
|
|
#[inline]
|
2015-03-15 01:48:34 +01:00
|
|
|
fn rsplit_terminator<'a, P: Pattern<'a>>(&'a self, pat: P) -> RSplitTerminator<'a, P>
|
2015-03-14 19:34:21 -04:00
|
|
|
where P::Searcher: ReverseSearcher<'a>
|
|
|
|
{
|
2015-03-15 01:48:34 +01:00
|
|
|
RSplitTerminator(self.split_terminator(pat).0)
|
2015-03-14 19:34:21 -04:00
|
|
|
}
|
|
|
|
|
2014-04-30 23:06:36 -07:00
|
|
|
#[inline]
|
2015-03-15 01:48:34 +01:00
|
|
|
fn matches<'a, P: Pattern<'a>>(&'a self, pat: P) -> Matches<'a, P> {
|
|
|
|
Matches(MatchesInternal(pat.into_searcher(self)))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn rmatches<'a, P: Pattern<'a>>(&'a self, pat: P) -> RMatches<'a, P>
|
2015-03-14 20:07:13 -04:00
|
|
|
where P::Searcher: ReverseSearcher<'a>
|
|
|
|
{
|
2015-03-15 01:48:34 +01:00
|
|
|
RMatches(self.matches(pat).0)
|
2014-04-30 23:06:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2015-01-14 20:45:51 +01:00
|
|
|
fn match_indices<'a, P: Pattern<'a>>(&'a self, pat: P) -> MatchIndices<'a, P> {
|
2015-03-15 01:48:34 +01:00
|
|
|
MatchIndices(MatchIndicesInternal(pat.into_searcher(self)))
|
2014-04-30 23:06:36 -07:00
|
|
|
}
|
|
|
|
|
2015-03-15 01:48:34 +01:00
|
|
|
#[inline]
|
|
|
|
fn rmatch_indices<'a, P: Pattern<'a>>(&'a self, pat: P) -> RMatchIndices<'a, P>
|
|
|
|
where P::Searcher: ReverseSearcher<'a>
|
|
|
|
{
|
|
|
|
RMatchIndices(self.match_indices(pat).0)
|
|
|
|
}
|
2014-04-30 23:06:36 -07:00
|
|
|
#[inline]
|
std: Stabilize the std::str module
This commit starts out by consolidating all `str` extension traits into one
`StrExt` trait to be included in the prelude. This means that
`UnicodeStrPrelude`, `StrPrelude`, and `StrAllocating` have all been merged into
one `StrExt` exported by the standard library. Some functionality is currently
duplicated with the `StrExt` present in libcore.
This commit also currently avoids any methods which require any form of pattern
to operate. These functions will be stabilized via a separate RFC.
Next, stability of methods and structures are as follows:
Stable
* from_utf8_unchecked
* CowString - after moving to std::string
* StrExt::as_bytes
* StrExt::as_ptr
* StrExt::bytes/Bytes - also made a struct instead of a typedef
* StrExt::char_indices/CharIndices - CharOffsets was renamed
* StrExt::chars/Chars
* StrExt::is_empty
* StrExt::len
* StrExt::lines/Lines
* StrExt::lines_any/LinesAny
* StrExt::slice_unchecked
* StrExt::trim
* StrExt::trim_left
* StrExt::trim_right
* StrExt::words/Words - also made a struct instead of a typedef
Unstable
* from_utf8 - the error type was changed to a `Result`, but the error type has
yet to prove itself
* from_c_str - this function will be handled by the c_str RFC
* FromStr - this trait will have an associated error type eventually
* StrExt::escape_default - needs iterators at least, unsure if it should make
the cut
* StrExt::escape_unicode - needs iterators at least, unsure if it should make
the cut
* StrExt::slice_chars - this function has yet to prove itself
* StrExt::slice_shift_char - awaiting conventions about slicing and shifting
* StrExt::graphemes/Graphemes - this functionality may only be in libunicode
* StrExt::grapheme_indices/GraphemeIndices - this functionality may only be in
libunicode
* StrExt::width - this functionality may only be in libunicode
* StrExt::utf16_units - this functionality may only be in libunicode
* StrExt::nfd_chars - this functionality may only be in libunicode
* StrExt::nfkd_chars - this functionality may only be in libunicode
* StrExt::nfc_chars - this functionality may only be in libunicode
* StrExt::nfkc_chars - this functionality may only be in libunicode
* StrExt::is_char_boundary - naming is uncertain with container conventions
* StrExt::char_range_at - naming is uncertain with container conventions
* StrExt::char_range_at_reverse - naming is uncertain with container conventions
* StrExt::char_at - naming is uncertain with container conventions
* StrExt::char_at_reverse - naming is uncertain with container conventions
* StrVector::concat - this functionality may be replaced with iterators, but
it's not certain at this time
* StrVector::connect - as with concat, may be deprecated in favor of iterators
Deprecated
* StrAllocating and UnicodeStrPrelude have been merged into StrExit
* eq_slice - compiler implementation detail
* from_str - use the inherent parse() method
* is_utf8 - call from_utf8 instead
* replace - call the method instead
* truncate_utf16_at_nul - this is an implementation detail of windows and does
not need to be exposed.
* utf8_char_width - moved to libunicode
* utf16_items - moved to libunicode
* is_utf16 - moved to libunicode
* Utf16Items - moved to libunicode
* Utf16Item - moved to libunicode
* Utf16Encoder - moved to libunicode
* AnyLines - renamed to LinesAny and made a struct
* SendStr - use CowString<'static> instead
* str::raw - all functionality is deprecated
* StrExt::into_string - call to_string() instead
* StrExt::repeat - use iterators instead
* StrExt::char_len - use .chars().count() instead
* StrExt::is_alphanumeric - use .chars().all(..)
* StrExt::is_whitespace - use .chars().all(..)
Pending deprecation -- while slicing syntax is being worked out, these methods
are all #[unstable]
* Str - while currently used for generic programming, this trait will be
replaced with one of [], deref coercions, or a generic conversion trait.
* StrExt::slice - use slicing syntax instead
* StrExt::slice_to - use slicing syntax instead
* StrExt::slice_from - use slicing syntax instead
* StrExt::lev_distance - deprecated with no replacement
Awaiting stabilization due to patterns and/or matching
* StrExt::contains
* StrExt::contains_char
* StrExt::split
* StrExt::splitn
* StrExt::split_terminator
* StrExt::rsplitn
* StrExt::match_indices
* StrExt::split_str
* StrExt::starts_with
* StrExt::ends_with
* StrExt::trim_chars
* StrExt::trim_left_chars
* StrExt::trim_right_chars
* StrExt::find
* StrExt::rfind
* StrExt::find_str
* StrExt::subslice_offset
2014-12-10 09:02:31 -08:00
|
|
|
fn lines(&self) -> Lines {
|
2015-03-15 01:48:34 +01:00
|
|
|
Lines(self.split_terminator('\n'))
|
2014-04-30 23:06:36 -07:00
|
|
|
}
|
|
|
|
|
2015-03-15 01:48:34 +01:00
|
|
|
#[inline]
|
std: Stabilize the std::str module
This commit starts out by consolidating all `str` extension traits into one
`StrExt` trait to be included in the prelude. This means that
`UnicodeStrPrelude`, `StrPrelude`, and `StrAllocating` have all been merged into
one `StrExt` exported by the standard library. Some functionality is currently
duplicated with the `StrExt` present in libcore.
This commit also currently avoids any methods which require any form of pattern
to operate. These functions will be stabilized via a separate RFC.
Next, stability of methods and structures are as follows:
Stable
* from_utf8_unchecked
* CowString - after moving to std::string
* StrExt::as_bytes
* StrExt::as_ptr
* StrExt::bytes/Bytes - also made a struct instead of a typedef
* StrExt::char_indices/CharIndices - CharOffsets was renamed
* StrExt::chars/Chars
* StrExt::is_empty
* StrExt::len
* StrExt::lines/Lines
* StrExt::lines_any/LinesAny
* StrExt::slice_unchecked
* StrExt::trim
* StrExt::trim_left
* StrExt::trim_right
* StrExt::words/Words - also made a struct instead of a typedef
Unstable
* from_utf8 - the error type was changed to a `Result`, but the error type has
yet to prove itself
* from_c_str - this function will be handled by the c_str RFC
* FromStr - this trait will have an associated error type eventually
* StrExt::escape_default - needs iterators at least, unsure if it should make
the cut
* StrExt::escape_unicode - needs iterators at least, unsure if it should make
the cut
* StrExt::slice_chars - this function has yet to prove itself
* StrExt::slice_shift_char - awaiting conventions about slicing and shifting
* StrExt::graphemes/Graphemes - this functionality may only be in libunicode
* StrExt::grapheme_indices/GraphemeIndices - this functionality may only be in
libunicode
* StrExt::width - this functionality may only be in libunicode
* StrExt::utf16_units - this functionality may only be in libunicode
* StrExt::nfd_chars - this functionality may only be in libunicode
* StrExt::nfkd_chars - this functionality may only be in libunicode
* StrExt::nfc_chars - this functionality may only be in libunicode
* StrExt::nfkc_chars - this functionality may only be in libunicode
* StrExt::is_char_boundary - naming is uncertain with container conventions
* StrExt::char_range_at - naming is uncertain with container conventions
* StrExt::char_range_at_reverse - naming is uncertain with container conventions
* StrExt::char_at - naming is uncertain with container conventions
* StrExt::char_at_reverse - naming is uncertain with container conventions
* StrVector::concat - this functionality may be replaced with iterators, but
it's not certain at this time
* StrVector::connect - as with concat, may be deprecated in favor of iterators
Deprecated
* StrAllocating and UnicodeStrPrelude have been merged into StrExit
* eq_slice - compiler implementation detail
* from_str - use the inherent parse() method
* is_utf8 - call from_utf8 instead
* replace - call the method instead
* truncate_utf16_at_nul - this is an implementation detail of windows and does
not need to be exposed.
* utf8_char_width - moved to libunicode
* utf16_items - moved to libunicode
* is_utf16 - moved to libunicode
* Utf16Items - moved to libunicode
* Utf16Item - moved to libunicode
* Utf16Encoder - moved to libunicode
* AnyLines - renamed to LinesAny and made a struct
* SendStr - use CowString<'static> instead
* str::raw - all functionality is deprecated
* StrExt::into_string - call to_string() instead
* StrExt::repeat - use iterators instead
* StrExt::char_len - use .chars().count() instead
* StrExt::is_alphanumeric - use .chars().all(..)
* StrExt::is_whitespace - use .chars().all(..)
Pending deprecation -- while slicing syntax is being worked out, these methods
are all #[unstable]
* Str - while currently used for generic programming, this trait will be
replaced with one of [], deref coercions, or a generic conversion trait.
* StrExt::slice - use slicing syntax instead
* StrExt::slice_to - use slicing syntax instead
* StrExt::slice_from - use slicing syntax instead
* StrExt::lev_distance - deprecated with no replacement
Awaiting stabilization due to patterns and/or matching
* StrExt::contains
* StrExt::contains_char
* StrExt::split
* StrExt::splitn
* StrExt::split_terminator
* StrExt::rsplitn
* StrExt::match_indices
* StrExt::split_str
* StrExt::starts_with
* StrExt::ends_with
* StrExt::trim_chars
* StrExt::trim_left_chars
* StrExt::trim_right_chars
* StrExt::find
* StrExt::rfind
* StrExt::find_str
* StrExt::subslice_offset
2014-12-10 09:02:31 -08:00
|
|
|
fn lines_any(&self) -> LinesAny {
|
2015-04-05 18:52:14 +02:00
|
|
|
LinesAny(self.lines().map(LinesAnyMap))
|
2014-04-30 23:06:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2015-01-27 14:09:18 +01:00
|
|
|
fn char_len(&self) -> usize { self.chars().count() }
|
2014-04-30 23:06:36 -07:00
|
|
|
|
2015-01-27 14:09:18 +01:00
|
|
|
fn slice_chars(&self, begin: usize, end: usize) -> &str {
|
2014-04-30 23:06:36 -07:00
|
|
|
assert!(begin <= end);
|
|
|
|
let mut count = 0;
|
|
|
|
let mut begin_byte = None;
|
|
|
|
let mut end_byte = None;
|
|
|
|
|
|
|
|
// This could be even more efficient by not decoding,
|
|
|
|
// only finding the char boundaries
|
|
|
|
for (idx, _) in self.char_indices() {
|
|
|
|
if count == begin { begin_byte = Some(idx); }
|
|
|
|
if count == end { end_byte = Some(idx); break; }
|
|
|
|
count += 1;
|
|
|
|
}
|
|
|
|
if begin_byte.is_none() && count == begin { begin_byte = Some(self.len()) }
|
|
|
|
if end_byte.is_none() && count == end { end_byte = Some(self.len()) }
|
|
|
|
|
|
|
|
match (begin_byte, end_byte) {
|
2014-10-09 15:17:22 -04:00
|
|
|
(None, _) => panic!("slice_chars: `begin` is beyond end of string"),
|
|
|
|
(_, None) => panic!("slice_chars: `end` is beyond end of string"),
|
2014-11-20 10:11:15 -08:00
|
|
|
(Some(a), Some(b)) => unsafe { self.slice_unchecked(a, b) }
|
2014-04-30 23:06:36 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-20 10:11:15 -08:00
|
|
|
#[inline]
|
2015-01-27 14:09:18 +01:00
|
|
|
unsafe fn slice_unchecked(&self, begin: usize, end: usize) -> &str {
|
2014-11-20 10:11:15 -08:00
|
|
|
mem::transmute(Slice {
|
2015-03-25 17:06:52 -07:00
|
|
|
data: self.as_ptr().offset(begin as isize),
|
2014-11-20 10:11:15 -08:00
|
|
|
len: end - begin,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-06-15 18:33:21 +02:00
|
|
|
#[inline]
|
|
|
|
unsafe fn slice_mut_unchecked(&mut self, begin: usize, end: usize) -> &mut str {
|
|
|
|
mem::transmute(Slice {
|
|
|
|
data: self.as_ptr().offset(begin as isize),
|
|
|
|
len: end - begin,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-04-30 23:06:36 -07:00
|
|
|
#[inline]
|
2015-01-27 14:09:18 +01:00
|
|
|
fn starts_with<'a, P: Pattern<'a>>(&'a self, pat: P) -> bool {
|
2015-02-19 14:36:58 +01:00
|
|
|
pat.is_prefix_of(self)
|
2014-04-30 23:06:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2015-01-27 14:09:18 +01:00
|
|
|
fn ends_with<'a, P: Pattern<'a>>(&'a self, pat: P) -> bool
|
2015-02-19 14:36:58 +01:00
|
|
|
where P::Searcher: ReverseSearcher<'a>
|
|
|
|
{
|
|
|
|
pat.is_suffix_of(self)
|
2014-04-30 23:06:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2014-12-30 21:54:17 +01:00
|
|
|
fn trim_matches<'a, P: Pattern<'a>>(&'a self, pat: P) -> &'a str
|
2015-02-19 14:36:58 +01:00
|
|
|
where P::Searcher: DoubleEndedSearcher<'a>
|
|
|
|
{
|
2014-12-30 21:54:17 +01:00
|
|
|
let mut i = 0;
|
2015-02-17 23:47:08 +01:00
|
|
|
let mut j = 0;
|
2015-02-17 22:57:14 +01:00
|
|
|
let mut matcher = pat.into_searcher(self);
|
2015-01-27 14:09:18 +01:00
|
|
|
if let Some((a, b)) = matcher.next_reject() {
|
|
|
|
i = a;
|
|
|
|
j = b; // Rember earliest known match, correct it below if
|
|
|
|
// last match is different
|
2014-12-30 21:54:17 +01:00
|
|
|
}
|
2015-01-27 14:09:18 +01:00
|
|
|
if let Some((_, b)) = matcher.next_reject_back() {
|
|
|
|
j = b;
|
2014-04-30 23:06:36 -07:00
|
|
|
}
|
2014-12-30 21:54:17 +01:00
|
|
|
unsafe {
|
2015-01-27 14:09:18 +01:00
|
|
|
// Searcher is known to return valid indices
|
2014-12-30 21:54:17 +01:00
|
|
|
self.slice_unchecked(i, j)
|
|
|
|
}
|
2014-04-30 23:06:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2015-01-27 14:09:18 +01:00
|
|
|
fn trim_left_matches<'a, P: Pattern<'a>>(&'a self, pat: P) -> &'a str {
|
2015-02-17 23:47:08 +01:00
|
|
|
let mut i = self.len();
|
2015-02-17 22:57:14 +01:00
|
|
|
let mut matcher = pat.into_searcher(self);
|
2015-01-27 14:09:18 +01:00
|
|
|
if let Some((a, _)) = matcher.next_reject() {
|
|
|
|
i = a;
|
2014-12-30 21:54:17 +01:00
|
|
|
}
|
|
|
|
unsafe {
|
2015-01-27 14:09:18 +01:00
|
|
|
// Searcher is known to return valid indices
|
2014-12-30 21:54:17 +01:00
|
|
|
self.slice_unchecked(i, self.len())
|
2014-04-30 23:06:36 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2015-01-27 14:09:18 +01:00
|
|
|
fn trim_right_matches<'a, P: Pattern<'a>>(&'a self, pat: P) -> &'a str
|
2015-02-19 14:36:58 +01:00
|
|
|
where P::Searcher: ReverseSearcher<'a>
|
|
|
|
{
|
2015-02-17 23:47:08 +01:00
|
|
|
let mut j = 0;
|
2015-02-17 22:57:14 +01:00
|
|
|
let mut matcher = pat.into_searcher(self);
|
2015-01-27 14:09:18 +01:00
|
|
|
if let Some((_, b)) = matcher.next_reject_back() {
|
|
|
|
j = b;
|
2014-04-30 23:06:36 -07:00
|
|
|
}
|
2014-12-30 21:54:17 +01:00
|
|
|
unsafe {
|
2015-01-27 14:09:18 +01:00
|
|
|
// Searcher is known to return valid indices
|
|
|
|
self.slice_unchecked(0, j)
|
2014-12-30 21:54:17 +01:00
|
|
|
}
|
2014-04-30 23:06:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2015-01-27 14:09:18 +01:00
|
|
|
fn is_char_boundary(&self, index: usize) -> bool {
|
2014-04-30 23:06:36 -07:00
|
|
|
if index == self.len() { return true; }
|
2014-08-23 12:30:08 +02:00
|
|
|
match self.as_bytes().get(index) {
|
|
|
|
None => false,
|
2015-03-03 10:42:26 +02:00
|
|
|
Some(&b) => b < 128 || b >= 192,
|
2014-08-23 12:30:08 +02:00
|
|
|
}
|
2014-04-30 23:06:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2015-01-27 14:09:18 +01:00
|
|
|
fn char_range_at(&self, i: usize) -> CharRange {
|
2015-01-21 15:55:31 -08:00
|
|
|
let (c, n) = char_range_at_raw(self.as_bytes(), i);
|
2015-07-24 03:04:55 +02:00
|
|
|
CharRange { ch: unsafe { char::from_u32_unchecked(c) }, next: n }
|
2014-04-30 23:06:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2015-01-27 14:09:18 +01:00
|
|
|
fn char_range_at_reverse(&self, start: usize) -> CharRange {
|
2014-04-30 23:06:36 -07:00
|
|
|
let mut prev = start;
|
|
|
|
|
|
|
|
prev = prev.saturating_sub(1);
|
2014-06-19 18:22:33 -07:00
|
|
|
if self.as_bytes()[prev] < 128 {
|
|
|
|
return CharRange{ch: self.as_bytes()[prev] as char, next: prev}
|
|
|
|
}
|
2014-04-30 23:06:36 -07:00
|
|
|
|
|
|
|
// Multibyte case is a fn to allow char_range_at_reverse to inline cleanly
|
2015-01-27 14:09:18 +01:00
|
|
|
fn multibyte_char_range_at_reverse(s: &str, mut i: usize) -> CharRange {
|
2014-04-30 23:06:36 -07:00
|
|
|
// while there is a previous byte == 10......
|
2014-07-18 00:59:49 +02:00
|
|
|
while i > 0 && s.as_bytes()[i] & !CONT_MASK == TAG_CONT_U8 {
|
2015-01-22 14:08:56 +00:00
|
|
|
i -= 1;
|
2014-04-30 23:06:36 -07:00
|
|
|
}
|
|
|
|
|
2015-03-10 12:06:44 +01:00
|
|
|
let first= s.as_bytes()[i];
|
|
|
|
let w = UTF8_CHAR_WIDTH[first as usize];
|
|
|
|
assert!(w != 0);
|
2014-04-30 23:06:36 -07:00
|
|
|
|
2015-03-10 12:06:44 +01:00
|
|
|
let mut val = utf8_first_byte(first, w as u32);
|
|
|
|
val = utf8_acc_cont_byte(val, s.as_bytes()[i + 1]);
|
|
|
|
if w > 2 { val = utf8_acc_cont_byte(val, s.as_bytes()[i + 2]); }
|
|
|
|
if w > 3 { val = utf8_acc_cont_byte(val, s.as_bytes()[i + 3]); }
|
2014-04-30 23:06:36 -07:00
|
|
|
|
2015-07-24 03:04:55 +02:00
|
|
|
return CharRange {ch: unsafe { char::from_u32_unchecked(val) }, next: i};
|
2014-04-30 23:06:36 -07:00
|
|
|
}
|
|
|
|
|
2014-10-23 10:43:18 -05:00
|
|
|
return multibyte_char_range_at_reverse(self, prev);
|
2014-04-30 23:06:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2015-01-27 14:09:18 +01:00
|
|
|
fn char_at(&self, i: usize) -> char {
|
2014-04-30 23:06:36 -07:00
|
|
|
self.char_range_at(i).ch
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2015-01-27 14:09:18 +01:00
|
|
|
fn char_at_reverse(&self, i: usize) -> char {
|
2014-04-30 23:06:36 -07:00
|
|
|
self.char_range_at_reverse(i).ch
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2014-10-23 10:43:18 -05:00
|
|
|
fn as_bytes(&self) -> &[u8] {
|
|
|
|
unsafe { mem::transmute(self) }
|
2014-04-30 23:06:36 -07:00
|
|
|
}
|
|
|
|
|
2015-01-27 14:09:18 +01:00
|
|
|
fn find<'a, P: Pattern<'a>>(&'a self, pat: P) -> Option<usize> {
|
2015-02-17 22:57:14 +01:00
|
|
|
pat.into_searcher(self).next_match().map(|(i, _)| i)
|
2014-04-30 23:06:36 -07:00
|
|
|
}
|
|
|
|
|
2015-01-27 14:09:18 +01:00
|
|
|
fn rfind<'a, P: Pattern<'a>>(&'a self, pat: P) -> Option<usize>
|
2015-02-19 14:36:58 +01:00
|
|
|
where P::Searcher: ReverseSearcher<'a>
|
|
|
|
{
|
2015-02-17 22:57:14 +01:00
|
|
|
pat.into_searcher(self).next_match_back().map(|(i, _)| i)
|
2014-04-30 23:06:36 -07:00
|
|
|
}
|
|
|
|
|
2015-01-27 14:09:18 +01:00
|
|
|
fn find_str<'a, P: Pattern<'a>>(&'a self, pat: P) -> Option<usize> {
|
|
|
|
self.find(pat)
|
2014-04-30 23:06:36 -07:00
|
|
|
}
|
|
|
|
|
2015-06-09 11:23:22 +02:00
|
|
|
fn split_at(&self, mid: usize) -> (&str, &str) {
|
|
|
|
// is_char_boundary checks that the index is in [0, .len()]
|
|
|
|
if self.is_char_boundary(mid) {
|
|
|
|
unsafe {
|
|
|
|
(self.slice_unchecked(0, mid),
|
|
|
|
self.slice_unchecked(mid, self.len()))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
slice_error_fail(self, 0, mid)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-15 19:24:52 +02:00
|
|
|
fn split_at_mut(&mut self, mid: usize) -> (&mut str, &mut str) {
|
|
|
|
// is_char_boundary checks that the index is in [0, .len()]
|
|
|
|
if self.is_char_boundary(mid) {
|
|
|
|
let len = self.len();
|
|
|
|
unsafe {
|
|
|
|
let self2: &mut str = mem::transmute_copy(&self);
|
|
|
|
(self.slice_mut_unchecked(0, mid),
|
|
|
|
self2.slice_mut_unchecked(mid, len))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
slice_error_fail(self, 0, mid)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-30 23:06:36 -07:00
|
|
|
#[inline]
|
change return type of slice_shift_char
`slice_shift_char` splits a `str` into it's leading `char` and the remainder
of the `str`. Currently, it returns a `(Option<char>, &str)` such that:
"bar".slice_shift_char() => (Some('b'), "ar")
"ar".slice_shift_char() => (Some('a'), "r")
"r".slice_shift_char() => (Some('r'), "")
"".slice_shift_char() => (None, "")
This is a little odd. Either a `str` can be split into both a head and a
tail or it cannot. So the return type should be `Option<(char, &str)>`.
With the current behaviour, in the case of the empty string, the `str`
returned is meaningless - it is always the empty string.
This commit changes slice_shift_char so that:
"bar".slice_shift_char() => Some(('b', "ar"))
"ar".slice_shift_char() => Some(('a', "r"))
"r".slice_shift_char() => Some(('r', ""))
"".slice_shift_char() => None
[breaking-change]
2014-11-17 17:35:18 +08:00
|
|
|
fn slice_shift_char(&self) -> Option<(char, &str)> {
|
2014-04-30 23:06:36 -07:00
|
|
|
if self.is_empty() {
|
change return type of slice_shift_char
`slice_shift_char` splits a `str` into it's leading `char` and the remainder
of the `str`. Currently, it returns a `(Option<char>, &str)` such that:
"bar".slice_shift_char() => (Some('b'), "ar")
"ar".slice_shift_char() => (Some('a'), "r")
"r".slice_shift_char() => (Some('r'), "")
"".slice_shift_char() => (None, "")
This is a little odd. Either a `str` can be split into both a head and a
tail or it cannot. So the return type should be `Option<(char, &str)>`.
With the current behaviour, in the case of the empty string, the `str`
returned is meaningless - it is always the empty string.
This commit changes slice_shift_char so that:
"bar".slice_shift_char() => Some(('b', "ar"))
"ar".slice_shift_char() => Some(('a', "r"))
"r".slice_shift_char() => Some(('r', ""))
"".slice_shift_char() => None
[breaking-change]
2014-11-17 17:35:18 +08:00
|
|
|
None
|
2014-04-30 23:06:36 -07:00
|
|
|
} else {
|
2015-03-10 16:29:02 -07:00
|
|
|
let ch = self.char_at(0);
|
|
|
|
let next_s = unsafe { self.slice_unchecked(ch.len_utf8(), self.len()) };
|
change return type of slice_shift_char
`slice_shift_char` splits a `str` into it's leading `char` and the remainder
of the `str`. Currently, it returns a `(Option<char>, &str)` such that:
"bar".slice_shift_char() => (Some('b'), "ar")
"ar".slice_shift_char() => (Some('a'), "r")
"r".slice_shift_char() => (Some('r'), "")
"".slice_shift_char() => (None, "")
This is a little odd. Either a `str` can be split into both a head and a
tail or it cannot. So the return type should be `Option<(char, &str)>`.
With the current behaviour, in the case of the empty string, the `str`
returned is meaningless - it is always the empty string.
This commit changes slice_shift_char so that:
"bar".slice_shift_char() => Some(('b', "ar"))
"ar".slice_shift_char() => Some(('a', "r"))
"r".slice_shift_char() => Some(('r', ""))
"".slice_shift_char() => None
[breaking-change]
2014-11-17 17:35:18 +08:00
|
|
|
Some((ch, next_s))
|
2014-04-30 23:06:36 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-27 14:09:18 +01:00
|
|
|
fn subslice_offset(&self, inner: &str) -> usize {
|
|
|
|
let a_start = self.as_ptr() as usize;
|
2014-04-30 23:06:36 -07:00
|
|
|
let a_end = a_start + self.len();
|
2015-01-27 14:09:18 +01:00
|
|
|
let b_start = inner.as_ptr() as usize;
|
2014-04-30 23:06:36 -07:00
|
|
|
let b_end = b_start + inner.len();
|
|
|
|
|
|
|
|
assert!(a_start <= b_start);
|
|
|
|
assert!(b_end <= a_end);
|
|
|
|
b_start - a_start
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2014-06-25 12:47:34 -07:00
|
|
|
fn as_ptr(&self) -> *const u8 {
|
2014-04-30 23:06:36 -07:00
|
|
|
self.repr().data
|
|
|
|
}
|
2014-05-31 13:02:29 +02:00
|
|
|
|
|
|
|
#[inline]
|
2015-01-27 14:09:18 +01:00
|
|
|
fn len(&self) -> usize { self.repr().len }
|
2014-10-30 13:43:24 -07:00
|
|
|
|
|
|
|
#[inline]
|
std: Stabilize the std::str module
This commit starts out by consolidating all `str` extension traits into one
`StrExt` trait to be included in the prelude. This means that
`UnicodeStrPrelude`, `StrPrelude`, and `StrAllocating` have all been merged into
one `StrExt` exported by the standard library. Some functionality is currently
duplicated with the `StrExt` present in libcore.
This commit also currently avoids any methods which require any form of pattern
to operate. These functions will be stabilized via a separate RFC.
Next, stability of methods and structures are as follows:
Stable
* from_utf8_unchecked
* CowString - after moving to std::string
* StrExt::as_bytes
* StrExt::as_ptr
* StrExt::bytes/Bytes - also made a struct instead of a typedef
* StrExt::char_indices/CharIndices - CharOffsets was renamed
* StrExt::chars/Chars
* StrExt::is_empty
* StrExt::len
* StrExt::lines/Lines
* StrExt::lines_any/LinesAny
* StrExt::slice_unchecked
* StrExt::trim
* StrExt::trim_left
* StrExt::trim_right
* StrExt::words/Words - also made a struct instead of a typedef
Unstable
* from_utf8 - the error type was changed to a `Result`, but the error type has
yet to prove itself
* from_c_str - this function will be handled by the c_str RFC
* FromStr - this trait will have an associated error type eventually
* StrExt::escape_default - needs iterators at least, unsure if it should make
the cut
* StrExt::escape_unicode - needs iterators at least, unsure if it should make
the cut
* StrExt::slice_chars - this function has yet to prove itself
* StrExt::slice_shift_char - awaiting conventions about slicing and shifting
* StrExt::graphemes/Graphemes - this functionality may only be in libunicode
* StrExt::grapheme_indices/GraphemeIndices - this functionality may only be in
libunicode
* StrExt::width - this functionality may only be in libunicode
* StrExt::utf16_units - this functionality may only be in libunicode
* StrExt::nfd_chars - this functionality may only be in libunicode
* StrExt::nfkd_chars - this functionality may only be in libunicode
* StrExt::nfc_chars - this functionality may only be in libunicode
* StrExt::nfkc_chars - this functionality may only be in libunicode
* StrExt::is_char_boundary - naming is uncertain with container conventions
* StrExt::char_range_at - naming is uncertain with container conventions
* StrExt::char_range_at_reverse - naming is uncertain with container conventions
* StrExt::char_at - naming is uncertain with container conventions
* StrExt::char_at_reverse - naming is uncertain with container conventions
* StrVector::concat - this functionality may be replaced with iterators, but
it's not certain at this time
* StrVector::connect - as with concat, may be deprecated in favor of iterators
Deprecated
* StrAllocating and UnicodeStrPrelude have been merged into StrExit
* eq_slice - compiler implementation detail
* from_str - use the inherent parse() method
* is_utf8 - call from_utf8 instead
* replace - call the method instead
* truncate_utf16_at_nul - this is an implementation detail of windows and does
not need to be exposed.
* utf8_char_width - moved to libunicode
* utf16_items - moved to libunicode
* is_utf16 - moved to libunicode
* Utf16Items - moved to libunicode
* Utf16Item - moved to libunicode
* Utf16Encoder - moved to libunicode
* AnyLines - renamed to LinesAny and made a struct
* SendStr - use CowString<'static> instead
* str::raw - all functionality is deprecated
* StrExt::into_string - call to_string() instead
* StrExt::repeat - use iterators instead
* StrExt::char_len - use .chars().count() instead
* StrExt::is_alphanumeric - use .chars().all(..)
* StrExt::is_whitespace - use .chars().all(..)
Pending deprecation -- while slicing syntax is being worked out, these methods
are all #[unstable]
* Str - while currently used for generic programming, this trait will be
replaced with one of [], deref coercions, or a generic conversion trait.
* StrExt::slice - use slicing syntax instead
* StrExt::slice_to - use slicing syntax instead
* StrExt::slice_from - use slicing syntax instead
* StrExt::lev_distance - deprecated with no replacement
Awaiting stabilization due to patterns and/or matching
* StrExt::contains
* StrExt::contains_char
* StrExt::split
* StrExt::splitn
* StrExt::split_terminator
* StrExt::rsplitn
* StrExt::match_indices
* StrExt::split_str
* StrExt::starts_with
* StrExt::ends_with
* StrExt::trim_chars
* StrExt::trim_left_chars
* StrExt::trim_right_chars
* StrExt::find
* StrExt::rfind
* StrExt::find_str
* StrExt::subslice_offset
2014-12-10 09:02:31 -08:00
|
|
|
fn is_empty(&self) -> bool { self.len() == 0 }
|
2015-01-01 23:53:35 -08:00
|
|
|
|
|
|
|
#[inline]
|
2015-01-27 22:52:32 -08:00
|
|
|
fn parse<T: FromStr>(&self) -> Result<T, T::Err> { FromStr::from_str(self) }
|
2014-04-30 23:06:36 -07:00
|
|
|
}
|
|
|
|
|
2015-05-06 15:53:34 -07:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
impl AsRef<[u8]> for str {
|
|
|
|
#[inline]
|
|
|
|
fn as_ref(&self) -> &[u8] {
|
|
|
|
self.as_bytes()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-21 15:55:31 -08:00
|
|
|
/// Pluck a code point out of a UTF-8-like byte slice and return the
|
|
|
|
/// index of the next code point.
|
|
|
|
#[inline]
|
2015-06-10 18:10:12 -07:00
|
|
|
fn char_range_at_raw(bytes: &[u8], i: usize) -> (u32, usize) {
|
2015-03-03 10:42:26 +02:00
|
|
|
if bytes[i] < 128 {
|
2015-01-21 15:55:31 -08:00
|
|
|
return (bytes[i] as u32, i + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Multibyte case is a fn to allow char_range_at to inline cleanly
|
2015-01-27 14:09:18 +01:00
|
|
|
fn multibyte_char_range_at(bytes: &[u8], i: usize) -> (u32, usize) {
|
2015-03-10 12:06:44 +01:00
|
|
|
let first = bytes[i];
|
|
|
|
let w = UTF8_CHAR_WIDTH[first as usize];
|
|
|
|
assert!(w != 0);
|
2015-01-21 15:55:31 -08:00
|
|
|
|
2015-03-10 12:06:44 +01:00
|
|
|
let mut val = utf8_first_byte(first, w as u32);
|
|
|
|
val = utf8_acc_cont_byte(val, bytes[i + 1]);
|
|
|
|
if w > 2 { val = utf8_acc_cont_byte(val, bytes[i + 2]); }
|
|
|
|
if w > 3 { val = utf8_acc_cont_byte(val, bytes[i + 3]); }
|
2015-01-21 15:55:31 -08:00
|
|
|
|
2015-03-10 12:06:44 +01:00
|
|
|
return (val, i + w as usize);
|
2015-01-21 15:55:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
multibyte_char_range_at(bytes, i)
|
|
|
|
}
|
|
|
|
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-04-30 23:06:36 -07:00
|
|
|
impl<'a> Default for &'a str {
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-04-30 23:06:36 -07:00
|
|
|
fn default() -> &'a str { "" }
|
|
|
|
}
|