Skip to content

Add examples for std::ascii #23665

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 28, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 96 additions & 4 deletions src/libstd/ascii.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,44 +34,122 @@ pub trait OwnedAsciiExt {
fn into_ascii_lowercase(self) -> Self;
}

/// Extension methods for ASCII-subset only operations on string slices
/// Extension methods for ASCII-subset only operations on string slices.
#[stable(feature = "rust1", since = "1.0.0")]
pub trait AsciiExt {
/// Container type for copied ASCII characters.
#[stable(feature = "rust1", since = "1.0.0")]
type Owned;

/// Check if within the ASCII range.
///
/// # Examples
///
/// ```
/// use std::ascii::AsciiExt;
///
/// let ascii = 'a';
/// let utf8 = '❤';
///
/// assert_eq!(true, ascii.is_ascii());
/// assert_eq!(false, utf8.is_ascii())
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
fn is_ascii(&self) -> bool;

/// Makes a copy of the string in ASCII upper case:
/// Makes a copy of the string in ASCII upper case.
///
/// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z',
/// but non-ASCII letters are unchanged.
///
/// # Examples
///
/// ```
/// use std::ascii::AsciiExt;
///
/// let ascii = 'a';
/// let utf8 = '❤';
///
/// assert_eq!('A', ascii.to_ascii_uppercase());
/// assert_eq!('❤', utf8.to_ascii_uppercase());
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
fn to_ascii_uppercase(&self) -> Self::Owned;

/// Makes a copy of the string in ASCII lower case:
/// Makes a copy of the string in ASCII lower case.
///
/// ASCII letters 'A' to 'Z' are mapped to 'a' to 'z',
/// but non-ASCII letters are unchanged.
///
/// # Examples
///
/// ```
/// use std::ascii::AsciiExt;
///
/// let ascii = 'A';
/// let utf8 = '❤';
///
/// assert_eq!('a', ascii.to_ascii_lowercase());
/// assert_eq!('❤', utf8.to_ascii_lowercase());
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
fn to_ascii_lowercase(&self) -> Self::Owned;

/// Check that two strings are an ASCII case-insensitive match.
///
/// Same as `to_ascii_lowercase(a) == to_ascii_lowercase(b)`,
/// but without allocating and copying temporary strings.
///
/// # Examples
///
/// ```
/// use std::ascii::AsciiExt;
///
/// let ascii1 = 'A';
/// let ascii2 = 'a';
/// let ascii3 = 'A';
/// let ascii4 = 'z';
///
/// assert_eq!(true, ascii1.eq_ignore_ascii_case(&ascii2));
/// assert_eq!(true, ascii1.eq_ignore_ascii_case(&ascii3));
/// assert_eq!(false, ascii1.eq_ignore_ascii_case(&ascii4));
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
fn eq_ignore_ascii_case(&self, other: &Self) -> bool;

/// Convert this type to its ASCII upper case equivalent in-place.
///
/// See `to_ascii_uppercase` for more information.
///
/// # Examples
///
/// ```
/// use std::ascii::AsciiExt;
///
/// let mut ascii = 'a';
///
/// ascii.make_ascii_uppercase();
///
/// assert_eq!('A', ascii);
/// ```
#[unstable(feature = "ascii")]
fn make_ascii_uppercase(&mut self);

/// Convert this type to its ASCII lower case equivalent in-place.
///
/// See `to_ascii_lowercase` for more information.
///
/// # Examples
///
/// ```
/// use std::ascii::AsciiExt;
///
/// let mut ascii = 'A';
///
/// ascii.make_ascii_lowercase();
///
/// assert_eq!('a', ascii);
/// ```
#[unstable(feature = "ascii")]
fn make_ascii_lowercase(&mut self);
}
Expand Down Expand Up @@ -246,7 +324,7 @@ pub struct EscapeDefault {
data: [u8; 4],
}

/// Returns a 'default' ASCII and C++11-like literal escape of a `u8`
/// Returns an iterator that produces an escaped version of a `u8`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'default' is mentioned later too, could it be given some better context/antecedent?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what it means, actually. But you're right it needs one...

///
/// The default is chosen with a bias toward producing literals that are
/// legal in a variety of languages, including C++11 and similar C-family
Expand All @@ -257,6 +335,20 @@ pub struct EscapeDefault {
/// - Any other chars in the range [0x20,0x7e] are not escaped.
/// - Any other chars are given hex escapes of the form '\xNN'.
/// - Unicode escapes are never generated by this function.
///
/// # Examples
///
/// ```
/// use std::ascii;
///
/// let escaped = ascii::escape_default(b'0').next().unwrap();
/// assert_eq!(b'0', escaped);
///
/// let mut escaped = ascii::escape_default(b'\t');
///
/// assert_eq!(b'\\', escaped.next().unwrap());
/// assert_eq!(b't', escaped.next().unwrap());
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn escape_default(c: u8) -> EscapeDefault {
let (data, len) = match c {
Expand Down