2016-03-04 17:37:11 -05:00
|
|
|
//! Operations on ASCII strings and characters.
|
2017-04-01 00:47:58 -07:00
|
|
|
//!
|
|
|
|
//! Most string operations in Rust act on UTF-8 strings. However, at times it
|
|
|
|
//! makes more sense to only consider the ASCII character set for a specific
|
|
|
|
//! operation.
|
|
|
|
//!
|
|
|
|
//! The [`AsciiExt`] trait provides methods that allow for character
|
|
|
|
//! operations that only act on the ASCII subset and leave non-ASCII characters
|
|
|
|
//! alone.
|
|
|
|
//!
|
|
|
|
//! The [`escape_default`] function provides an iterator over the bytes of an
|
|
|
|
//! escaped version of the character given.
|
2013-05-03 09:20:01 +03:00
|
|
|
|
2015-02-17 13:58:34 -08:00
|
|
|
#![stable(feature = "rust1", since = "1.0.0")]
|
2014-06-30 17:22:40 -07:00
|
|
|
|
2018-03-05 16:39:09 -05:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2019-11-27 10:29:00 -08:00
|
|
|
pub use core::ascii::{escape_default, EscapeDefault};
|
2018-03-05 16:39:09 -05:00
|
|
|
|
2023-04-29 14:45:36 -07:00
|
|
|
#[unstable(feature = "ascii_char", issue = "110998")]
|
|
|
|
pub use core::ascii::Char;
|
|
|
|
|
2017-03-26 21:47:22 +10:30
|
|
|
/// Extension methods for ASCII-subset only operations.
|
2016-02-04 00:11:18 -05:00
|
|
|
///
|
|
|
|
/// Be aware that operations on seemingly non-ASCII characters can sometimes
|
|
|
|
/// have unexpected results. Consider this example:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::ascii::AsciiExt;
|
|
|
|
///
|
2017-10-02 09:50:36 +02:00
|
|
|
/// assert_eq!(AsciiExt::to_ascii_uppercase("café"), "CAFÉ");
|
|
|
|
/// assert_eq!(AsciiExt::to_ascii_uppercase("café"), "CAFé");
|
2016-02-04 00:11:18 -05:00
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// In the first example, the lowercased string is represented `"cafe\u{301}"`
|
|
|
|
/// (the last character is an acute accent [combining character]). Unlike the
|
|
|
|
/// other characters in the string, the combining character will not get mapped
|
|
|
|
/// to an uppercase variant, resulting in `"CAFE\u{301}"`. In the second
|
|
|
|
/// example, the lowercased string is represented `"caf\u{e9}"` (the last
|
|
|
|
/// character is a single Unicode character representing an 'e' with an acute
|
|
|
|
/// accent). Since the last character is defined outside the scope of ASCII,
|
|
|
|
/// it will not get mapped to an uppercase variant, resulting in `"CAF\u{e9}"`.
|
|
|
|
///
|
|
|
|
/// [combining character]: https://en.wikipedia.org/wiki/Combining_character
|
2015-02-17 13:58:34 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2022-04-07 21:20:32 -04:00
|
|
|
#[deprecated(since = "1.26.0", note = "use inherent methods instead")]
|
2015-02-17 13:58:34 -08:00
|
|
|
pub trait AsciiExt {
|
2015-02-23 11:05:55 -08:00
|
|
|
/// Container type for copied ASCII characters.
|
2015-02-17 13:58:34 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
type Owned;
|
|
|
|
|
2016-02-11 22:59:34 +02:00
|
|
|
/// Checks if the value is within the ASCII range.
|
2015-03-24 15:47:47 -04:00
|
|
|
///
|
2017-09-29 15:23:02 +02:00
|
|
|
/// # Note
|
2015-03-24 15:47:47 -04:00
|
|
|
///
|
2020-08-12 22:58:18 +02:00
|
|
|
/// This method is deprecated in favor of the identically-named
|
2017-09-29 15:23:02 +02:00
|
|
|
/// inherent methods on `u8`, `char`, `[u8]` and `str`.
|
2015-02-17 13:58:34 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2013-04-20 19:39:15 +02:00
|
|
|
fn is_ascii(&self) -> bool;
|
|
|
|
|
2017-03-27 07:46:43 +10:30
|
|
|
/// Makes a copy of the value in its ASCII upper case equivalent.
|
2015-03-24 15:47:47 -04:00
|
|
|
///
|
2013-09-09 01:45:14 +02:00
|
|
|
/// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z',
|
|
|
|
/// but non-ASCII letters are unchanged.
|
2015-03-24 15:47:47 -04:00
|
|
|
///
|
2017-03-26 21:47:22 +10:30
|
|
|
/// To uppercase the value in-place, use [`make_ascii_uppercase`].
|
2017-01-21 17:35:15 -05:00
|
|
|
///
|
|
|
|
/// To uppercase ASCII characters in addition to non-ASCII characters, use
|
|
|
|
/// [`str::to_uppercase`].
|
|
|
|
///
|
2017-09-29 15:23:02 +02:00
|
|
|
/// # Note
|
2015-03-24 15:47:47 -04:00
|
|
|
///
|
2020-08-12 22:58:18 +02:00
|
|
|
/// This method is deprecated in favor of the identically-named
|
2017-09-29 15:23:02 +02:00
|
|
|
/// inherent methods on `u8`, `char`, `[u8]` and `str`.
|
2017-01-21 17:35:15 -05:00
|
|
|
///
|
2020-08-12 22:58:18 +02:00
|
|
|
/// [`make_ascii_uppercase`]: AsciiExt::make_ascii_uppercase
|
2015-02-17 13:58:34 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2018-03-17 12:15:24 +01:00
|
|
|
#[allow(deprecated)]
|
2015-02-17 13:58:34 -08:00
|
|
|
fn to_ascii_uppercase(&self) -> Self::Owned;
|
2013-04-20 19:39:15 +02:00
|
|
|
|
2017-03-27 07:46:43 +10:30
|
|
|
/// Makes a copy of the value in its ASCII lower case equivalent.
|
2015-03-24 15:47:47 -04:00
|
|
|
///
|
2013-09-09 01:45:14 +02:00
|
|
|
/// ASCII letters 'A' to 'Z' are mapped to 'a' to 'z',
|
|
|
|
/// but non-ASCII letters are unchanged.
|
2015-03-24 15:47:47 -04:00
|
|
|
///
|
2017-03-26 21:47:22 +10:30
|
|
|
/// To lowercase the value in-place, use [`make_ascii_lowercase`].
|
2017-01-21 17:35:15 -05:00
|
|
|
///
|
|
|
|
/// To lowercase ASCII characters in addition to non-ASCII characters, use
|
|
|
|
/// [`str::to_lowercase`].
|
|
|
|
///
|
2017-09-29 15:23:02 +02:00
|
|
|
/// # Note
|
2015-03-24 15:47:47 -04:00
|
|
|
///
|
2020-08-12 22:58:18 +02:00
|
|
|
/// This method is deprecated in favor of the identically-named
|
2017-09-29 15:23:02 +02:00
|
|
|
/// inherent methods on `u8`, `char`, `[u8]` and `str`.
|
2017-01-21 17:35:15 -05:00
|
|
|
///
|
2020-08-12 22:58:18 +02:00
|
|
|
/// [`make_ascii_lowercase`]: AsciiExt::make_ascii_lowercase
|
2015-02-17 13:58:34 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2018-03-17 12:15:24 +01:00
|
|
|
#[allow(deprecated)]
|
2015-02-17 13:58:34 -08:00
|
|
|
fn to_ascii_lowercase(&self) -> Self::Owned;
|
2013-04-20 19:39:15 +02:00
|
|
|
|
2017-03-26 21:47:22 +10:30
|
|
|
/// Checks that two values are an ASCII case-insensitive match.
|
2015-03-24 15:47:47 -04:00
|
|
|
///
|
2015-01-05 08:44:18 +01:00
|
|
|
/// Same as `to_ascii_lowercase(a) == to_ascii_lowercase(b)`,
|
2017-03-26 21:47:22 +10:30
|
|
|
/// but without allocating and copying temporaries.
|
2015-03-24 15:47:47 -04:00
|
|
|
///
|
2017-09-29 15:23:02 +02:00
|
|
|
/// # Note
|
2015-03-24 15:47:47 -04:00
|
|
|
///
|
2020-08-12 22:58:18 +02:00
|
|
|
/// This method is deprecated in favor of the identically-named
|
2017-09-29 15:23:02 +02:00
|
|
|
/// inherent methods on `u8`, `char`, `[u8]` and `str`.
|
2015-02-17 13:58:34 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-10-23 10:43:18 -05:00
|
|
|
fn eq_ignore_ascii_case(&self, other: &Self) -> bool;
|
2015-02-17 13:58:34 -08:00
|
|
|
|
2015-04-13 10:21:32 -04:00
|
|
|
/// Converts this type to its ASCII upper case equivalent in-place.
|
2015-02-17 13:58:34 -08:00
|
|
|
///
|
2017-01-21 17:35:15 -05:00
|
|
|
/// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z',
|
|
|
|
/// but non-ASCII letters are unchanged.
|
|
|
|
///
|
2017-03-26 21:47:22 +10:30
|
|
|
/// To return a new uppercased value without modifying the existing one, use
|
2017-01-21 17:35:15 -05:00
|
|
|
/// [`to_ascii_uppercase`].
|
2015-03-24 15:47:47 -04:00
|
|
|
///
|
2017-09-29 15:23:02 +02:00
|
|
|
/// # Note
|
2015-03-24 15:47:47 -04:00
|
|
|
///
|
2020-08-12 22:58:18 +02:00
|
|
|
/// This method is deprecated in favor of the identically-named
|
2017-09-29 15:23:02 +02:00
|
|
|
/// inherent methods on `u8`, `char`, `[u8]` and `str`.
|
2017-01-21 17:35:15 -05:00
|
|
|
///
|
2020-08-12 22:58:18 +02:00
|
|
|
/// [`to_ascii_uppercase`]: AsciiExt::to_ascii_uppercase
|
2016-04-07 10:42:53 -07:00
|
|
|
#[stable(feature = "ascii", since = "1.9.0")]
|
2015-02-17 13:58:34 -08:00
|
|
|
fn make_ascii_uppercase(&mut self);
|
|
|
|
|
2015-04-13 10:21:32 -04:00
|
|
|
/// Converts this type to its ASCII lower case equivalent in-place.
|
2015-02-17 13:58:34 -08:00
|
|
|
///
|
2017-01-21 17:35:15 -05:00
|
|
|
/// ASCII letters 'A' to 'Z' are mapped to 'a' to 'z',
|
|
|
|
/// but non-ASCII letters are unchanged.
|
|
|
|
///
|
2017-03-26 21:47:22 +10:30
|
|
|
/// To return a new lowercased value without modifying the existing one, use
|
2017-01-21 17:35:15 -05:00
|
|
|
/// [`to_ascii_lowercase`].
|
2015-03-24 15:47:47 -04:00
|
|
|
///
|
2017-09-29 15:23:02 +02:00
|
|
|
/// # Note
|
2015-03-24 15:47:47 -04:00
|
|
|
///
|
2020-08-12 22:58:18 +02:00
|
|
|
/// This method is deprecated in favor of the identically-named
|
2017-09-29 15:23:02 +02:00
|
|
|
/// inherent methods on `u8`, `char`, `[u8]` and `str`.
|
2017-01-21 17:35:15 -05:00
|
|
|
///
|
2020-08-12 22:58:18 +02:00
|
|
|
/// [`to_ascii_lowercase`]: AsciiExt::to_ascii_lowercase
|
2016-04-07 10:42:53 -07:00
|
|
|
#[stable(feature = "ascii", since = "1.9.0")]
|
2015-02-17 13:58:34 -08:00
|
|
|
fn make_ascii_lowercase(&mut self);
|
2013-08-02 21:39:57 +01:00
|
|
|
}
|
2013-08-04 13:22:56 -07:00
|
|
|
|
2017-11-09 11:05:28 +01:00
|
|
|
macro_rules! delegating_ascii_methods {
|
|
|
|
() => {
|
|
|
|
#[inline]
|
2020-04-22 16:16:43 -04:00
|
|
|
fn is_ascii(&self) -> bool {
|
|
|
|
self.is_ascii()
|
|
|
|
}
|
2017-02-08 17:43:48 -05:00
|
|
|
|
2017-11-09 11:05:28 +01:00
|
|
|
#[inline]
|
2020-04-22 16:16:43 -04:00
|
|
|
fn to_ascii_uppercase(&self) -> Self::Owned {
|
|
|
|
self.to_ascii_uppercase()
|
|
|
|
}
|
2017-02-08 17:43:48 -05:00
|
|
|
|
2017-11-09 11:05:28 +01:00
|
|
|
#[inline]
|
2020-04-22 16:16:43 -04:00
|
|
|
fn to_ascii_lowercase(&self) -> Self::Owned {
|
|
|
|
self.to_ascii_lowercase()
|
|
|
|
}
|
2017-02-08 17:43:48 -05:00
|
|
|
|
2017-11-09 11:05:28 +01:00
|
|
|
#[inline]
|
2020-04-22 16:16:43 -04:00
|
|
|
fn eq_ignore_ascii_case(&self, o: &Self) -> bool {
|
|
|
|
self.eq_ignore_ascii_case(o)
|
|
|
|
}
|
2017-02-08 17:43:48 -05:00
|
|
|
|
2017-11-09 11:05:28 +01:00
|
|
|
#[inline]
|
2020-04-22 16:16:43 -04:00
|
|
|
fn make_ascii_uppercase(&mut self) {
|
|
|
|
self.make_ascii_uppercase();
|
|
|
|
}
|
2017-02-08 17:43:48 -05:00
|
|
|
|
2017-11-09 11:05:28 +01:00
|
|
|
#[inline]
|
2020-04-22 16:16:43 -04:00
|
|
|
fn make_ascii_lowercase(&mut self) {
|
|
|
|
self.make_ascii_lowercase();
|
|
|
|
}
|
|
|
|
};
|
2017-11-09 11:05:28 +01:00
|
|
|
}
|
2017-02-08 17:43:48 -05:00
|
|
|
|
2017-11-09 11:05:28 +01:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2018-03-17 12:15:24 +01:00
|
|
|
#[allow(deprecated)]
|
2017-11-09 11:05:28 +01:00
|
|
|
impl AsciiExt for u8 {
|
|
|
|
type Owned = u8;
|
|
|
|
|
|
|
|
delegating_ascii_methods!();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2018-03-17 12:15:24 +01:00
|
|
|
#[allow(deprecated)]
|
2017-11-09 11:05:28 +01:00
|
|
|
impl AsciiExt for char {
|
|
|
|
type Owned = char;
|
|
|
|
|
|
|
|
delegating_ascii_methods!();
|
|
|
|
}
|
2017-09-28 18:25:16 +02:00
|
|
|
|
2017-11-09 11:05:28 +01:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2018-03-17 12:15:24 +01:00
|
|
|
#[allow(deprecated)]
|
2017-11-09 11:05:28 +01:00
|
|
|
impl AsciiExt for [u8] {
|
|
|
|
type Owned = Vec<u8>;
|
|
|
|
|
|
|
|
delegating_ascii_methods!();
|
|
|
|
}
|
2017-10-01 21:13:49 +02:00
|
|
|
|
2017-11-09 11:05:28 +01:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2018-03-17 12:15:24 +01:00
|
|
|
#[allow(deprecated)]
|
2017-11-09 11:05:28 +01:00
|
|
|
impl AsciiExt for str {
|
|
|
|
type Owned = String;
|
|
|
|
|
|
|
|
delegating_ascii_methods!();
|
|
|
|
}
|