diff options
| author | bors <bors@rust-lang.org> | 2017-11-05 11:42:59 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2017-11-05 11:42:59 +0000 |
| commit | 94ede93467beb4ac17956845607a7e8b226dda02 (patch) | |
| tree | efce44ea2cdc5c71fa03405dbd4bca2a4a20b99b /src/liballoc | |
| parent | 4efcc660f09ce3e83d194889f6d1161bf865f788 (diff) | |
| parent | ea55596d5bc29708232a0bb232bf35d5e2e6cbce (diff) | |
| download | rust-94ede93467beb4ac17956845607a7e8b226dda02.tar.gz rust-94ede93467beb4ac17956845607a7e8b226dda02.zip | |
Auto merge of #44042 - LukasKalbertodt:ascii-methods-on-instrinsics, r=alexcrichton
Copy all `AsciiExt` methods to the primitive types directly in order to deprecate it later
**EDIT:** [this PR is ready now](https://github.com/rust-lang/rust/pull/44042#issuecomment-333883548). I edited this post to reflect the current status of discussion, which is (apart from code review) pretty much settled.
---
This is my current progress in order to prepare stabilization of #39658. As discussed there (and in #39659), the idea is to deprecated `AsciiExt` and copy all methods to the type directly. Apparently there isn't really a reason to have those methods in an extension trait¹.
~~This is **work in progress**: copy&pasting code while slightly modifying the documentation isn't the most exciting thing to do. Therefore I wanted to already open this WIP PR after doing basically 1/4 of the job (copying methods to `&[u8]`, `char` and `&str` is still missing) to get some feedback before I continue. Some questions possibly worth discussing:~~
1. ~~Does everyone agree that deprecating `AsciiExt` is a good idea? Does everyone agree with the goal of this PR?~~ => apparently yes
2. ~~Are my changes OK so far? Did I do something wrong?~~
3. ~~The issue of the unstable-attribute is currently set to 0. I would wait until you say "Ok" to the whole thing, then create a tracking issue and then insert the correct issue id. Is that ok?~~
4. ~~I tweaked `eq_ignore_ascii_case()`: it now takes the argument `other: u8` instead of `other: &u8`. The latter was enforced by the trait. Since we're not bound to a trait anymore, we can drop the reference, ok?~~ => I reverted this, because the interface has to match the `AsciiExt` interface exactly.
¹ ~~Could it be that we can't write `impl [u8] {}`? This might be the reason for `AsciiExt`. If that is the case: is there a good reason we can't write such an impl block? What can we do instead?~~ => we couldn't at the time this PR was opened, but Simon made it possible.
/cc @SimonSapin @zackw
Diffstat (limited to 'src/liballoc')
| -rw-r--r-- | src/liballoc/benches/str.rs | 3 | ||||
| -rw-r--r-- | src/liballoc/borrow.rs | 1 | ||||
| -rw-r--r-- | src/liballoc/lib.rs | 1 | ||||
| -rw-r--r-- | src/liballoc/slice.rs | 209 | ||||
| -rw-r--r-- | src/liballoc/str.rs | 284 | ||||
| -rw-r--r-- | src/liballoc/string.rs | 2 | ||||
| -rw-r--r-- | src/liballoc/tests/str.rs | 1 | ||||
| -rw-r--r-- | src/liballoc/tests/vec.rs | 3 | ||||
| -rw-r--r-- | src/liballoc/vec.rs | 2 |
9 files changed, 490 insertions, 16 deletions
diff --git a/src/liballoc/benches/str.rs b/src/liballoc/benches/str.rs index fc4063fae92..38c94d4d8b5 100644 --- a/src/liballoc/benches/str.rs +++ b/src/liballoc/benches/str.rs @@ -272,15 +272,12 @@ make_test!(match_indices_a_str, s, s.match_indices("a").count()); make_test!(split_a_str, s, s.split("a").count()); make_test!(trim_ascii_char, s, { - use std::ascii::AsciiExt; s.trim_matches(|c: char| c.is_ascii()) }); make_test!(trim_left_ascii_char, s, { - use std::ascii::AsciiExt; s.trim_left_matches(|c: char| c.is_ascii()) }); make_test!(trim_right_ascii_char, s, { - use std::ascii::AsciiExt; s.trim_right_matches(|c: char| c.is_ascii()) }); diff --git a/src/liballoc/borrow.rs b/src/liballoc/borrow.rs index a662e4b1f4f..e8aff099871 100644 --- a/src/liballoc/borrow.rs +++ b/src/liballoc/borrow.rs @@ -191,7 +191,6 @@ impl<'a, B: ?Sized> Cow<'a, B> /// # Examples /// /// ``` - /// use std::ascii::AsciiExt; /// use std::borrow::Cow; /// /// let mut cow = Cow::Borrowed("foo"); diff --git a/src/liballoc/lib.rs b/src/liballoc/lib.rs index f654a6b5ba4..a40ed060604 100644 --- a/src/liballoc/lib.rs +++ b/src/liballoc/lib.rs @@ -83,6 +83,7 @@ #![cfg_attr(not(test), feature(generator_trait))] #![cfg_attr(test, feature(rand, test))] #![feature(allow_internal_unstable)] +#![feature(ascii_ctype)] #![feature(box_patterns)] #![feature(box_syntax)] #![feature(cfg_target_has_atomic)] diff --git a/src/liballoc/slice.rs b/src/liballoc/slice.rs index 0c5fec2cf74..b41cb912fe7 100644 --- a/src/liballoc/slice.rs +++ b/src/liballoc/slice.rs @@ -1533,6 +1533,215 @@ impl<T> [T] { } } +// FIXME(LukasKalbertodt): the `not(stage0)` constraint can be removed in the +// future once the stage0 compiler is new enough to know about the `slice_u8` +// lang item. +#[lang = "slice_u8"] +#[cfg(all(not(stage0), not(test)))] +impl [u8] { + /// Checks if all bytes in this slice are within the ASCII range. + #[stable(feature = "ascii_methods_on_intrinsics", since = "1.21.0")] + #[inline] + pub fn is_ascii(&self) -> bool { + self.iter().all(|b| b.is_ascii()) + } + + /// Returns a vector containing a copy of this slice where each byte + /// is mapped to its ASCII upper case equivalent. + /// + /// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z', + /// but non-ASCII letters are unchanged. + /// + /// To uppercase the value in-place, use [`make_ascii_uppercase`]. + /// + /// [`make_ascii_uppercase`]: #method.make_ascii_uppercase + #[stable(feature = "ascii_methods_on_intrinsics", since = "1.21.0")] + #[inline] + pub fn to_ascii_uppercase(&self) -> Vec<u8> { + let mut me = self.to_vec(); + me.make_ascii_uppercase(); + me + } + + /// Returns a vector containing a copy of this slice where each byte + /// is mapped to its ASCII lower case equivalent. + /// + /// ASCII letters 'A' to 'Z' are mapped to 'a' to 'z', + /// but non-ASCII letters are unchanged. + /// + /// To lowercase the value in-place, use [`make_ascii_lowercase`]. + /// + /// [`make_ascii_lowercase`]: #method.make_ascii_lowercase + #[stable(feature = "ascii_methods_on_intrinsics", since = "1.21.0")] + #[inline] + pub fn to_ascii_lowercase(&self) -> Vec<u8> { + let mut me = self.to_vec(); + me.make_ascii_lowercase(); + me + } + + /// Checks that two slices are an ASCII case-insensitive match. + /// + /// Same as `to_ascii_lowercase(a) == to_ascii_lowercase(b)`, + /// but without allocating and copying temporaries. + #[stable(feature = "ascii_methods_on_intrinsics", since = "1.21.0")] + #[inline] + pub fn eq_ignore_ascii_case(&self, other: &[u8]) -> bool { + self.len() == other.len() && + self.iter().zip(other).all(|(a, b)| { + a.eq_ignore_ascii_case(b) + }) + } + + /// Converts this slice to its ASCII upper case equivalent in-place. + /// + /// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z', + /// but non-ASCII letters are unchanged. + /// + /// To return a new uppercased value without modifying the existing one, use + /// [`to_ascii_uppercase`]. + /// + /// [`to_ascii_uppercase`]: #method.to_ascii_uppercase + #[stable(feature = "ascii_methods_on_intrinsics", since = "1.21.0")] + #[inline] + pub fn make_ascii_uppercase(&mut self) { + for byte in self { + byte.make_ascii_uppercase(); + } + } + + /// Converts this slice to its ASCII lower case equivalent in-place. + /// + /// ASCII letters 'A' to 'Z' are mapped to 'a' to 'z', + /// but non-ASCII letters are unchanged. + /// + /// To return a new lowercased value without modifying the existing one, use + /// [`to_ascii_lowercase`]. + /// + /// [`to_ascii_lowercase`]: #method.to_ascii_lowercase + #[stable(feature = "ascii_methods_on_intrinsics", since = "1.21.0")] + #[inline] + pub fn make_ascii_lowercase(&mut self) { + for byte in self { + byte.make_ascii_lowercase(); + } + } + + /// Checks if all bytes of this slice are ASCII alphabetic characters: + /// + /// - U+0041 'A' ... U+005A 'Z', or + /// - U+0061 'a' ... U+007A 'z'. + #[unstable(feature = "ascii_ctype", issue = "39658")] + #[inline] + pub fn is_ascii_alphabetic(&self) -> bool { + self.iter().all(|b| b.is_ascii_alphabetic()) + } + + /// Checks if all bytes of this slice are ASCII uppercase characters: + /// U+0041 'A' ... U+005A 'Z'. + #[unstable(feature = "ascii_ctype", issue = "39658")] + #[inline] + pub fn is_ascii_uppercase(&self) -> bool { + self.iter().all(|b| b.is_ascii_uppercase()) + } + + /// Checks if all bytes of this slice are ASCII lowercase characters: + /// U+0061 'a' ... U+007A 'z'. + #[unstable(feature = "ascii_ctype", issue = "39658")] + #[inline] + pub fn is_ascii_lowercase(&self) -> bool { + self.iter().all(|b| b.is_ascii_lowercase()) + } + + /// Checks if all bytes of this slice are ASCII alphanumeric characters: + /// + /// - U+0041 'A' ... U+005A 'Z', or + /// - U+0061 'a' ... U+007A 'z', or + /// - U+0030 '0' ... U+0039 '9'. + #[unstable(feature = "ascii_ctype", issue = "39658")] + #[inline] + pub fn is_ascii_alphanumeric(&self) -> bool { + self.iter().all(|b| b.is_ascii_alphanumeric()) + } + + /// Checks if all bytes of this slice are ASCII decimal digit: + /// U+0030 '0' ... U+0039 '9'. + #[unstable(feature = "ascii_ctype", issue = "39658")] + #[inline] + pub fn is_ascii_digit(&self) -> bool { + self.iter().all(|b| b.is_ascii_digit()) + } + + /// Checks if all bytes of this slice are ASCII hexadecimal digits: + /// + /// - U+0030 '0' ... U+0039 '9', or + /// - U+0041 'A' ... U+0046 'F', or + /// - U+0061 'a' ... U+0066 'f'. + #[unstable(feature = "ascii_ctype", issue = "39658")] + #[inline] + pub fn is_ascii_hexdigit(&self) -> bool { + self.iter().all(|b| b.is_ascii_hexdigit()) + } + + /// Checks if all bytes of this slice are ASCII punctuation characters: + /// + /// - U+0021 ... U+002F `! " # $ % & ' ( ) * + , - . /`, or + /// - U+003A ... U+0040 `: ; < = > ? @`, or + /// - U+005B ... U+0060 `[ \\ ] ^ _ \``, or + /// - U+007B ... U+007E `{ | } ~` + #[unstable(feature = "ascii_ctype", issue = "39658")] + #[inline] + pub fn is_ascii_punctuation(&self) -> bool { + self.iter().all(|b| b.is_ascii_punctuation()) + } + + /// Checks if all bytes of this slice are ASCII graphic characters: + /// U+0021 '@' ... U+007E '~'. + #[unstable(feature = "ascii_ctype", issue = "39658")] + #[inline] + pub fn is_ascii_graphic(&self) -> bool { + self.iter().all(|b| b.is_ascii_graphic()) + } + + /// Checks if all bytes of this slice are ASCII whitespace characters: + /// U+0020 SPACE, U+0009 HORIZONTAL TAB, U+000A LINE FEED, + /// U+000C FORM FEED, or U+000D CARRIAGE RETURN. + /// + /// Rust uses the WhatWG Infra Standard's [definition of ASCII + /// whitespace][infra-aw]. There are several other definitions in + /// wide use. For instance, [the POSIX locale][pct] includes + /// U+000B VERTICAL TAB as well as all the above characters, + /// but—from the very same specification—[the default rule for + /// "field splitting" in the Bourne shell][bfs] considers *only* + /// SPACE, HORIZONTAL TAB, and LINE FEED as whitespace. + /// + /// If you are writing a program that will process an existing + /// file format, check what that format's definition of whitespace is + /// before using this function. + /// + /// [infra-aw]: https://infra.spec.whatwg.org/#ascii-whitespace + /// [pct]: http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap07.html#tag_07_03_01 + /// [bfs]: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_05 + #[unstable(feature = "ascii_ctype", issue = "39658")] + #[inline] + pub fn is_ascii_whitespace(&self) -> bool { + self.iter().all(|b| b.is_ascii_whitespace()) + } + + /// Checks if all bytes of this slice are ASCII control characters: + /// + /// - U+0000 NUL ... U+001F UNIT SEPARATOR, or + /// - U+007F DELETE. + /// + /// Note that most ASCII whitespace characters are control + /// characters, but SPACE is not. + #[unstable(feature = "ascii_ctype", issue = "39658")] + #[inline] + pub fn is_ascii_control(&self) -> bool { + self.iter().all(|b| b.is_ascii_control()) + } +} + //////////////////////////////////////////////////////////////////////////////// // Extension traits for slices over specific kinds of data //////////////////////////////////////////////////////////////////////////////// diff --git a/src/liballoc/str.rs b/src/liballoc/str.rs index 895607ff8d4..5f0b4088fc0 100644 --- a/src/liballoc/str.rs +++ b/src/liballoc/str.rs @@ -390,8 +390,6 @@ impl str { /// # Examples /// /// ``` - /// use std::ascii::AsciiExt; - /// /// let mut v = String::from("hello"); /// // correct length /// assert!(v.get_mut(0..5).is_some()); @@ -617,8 +615,6 @@ impl str { /// Basic usage: /// /// ``` - /// use std::ascii::AsciiExt; - /// /// let mut s = "Per Martin-Löf".to_string(); /// { /// let (first, last) = s.split_at_mut(3); @@ -2070,6 +2066,286 @@ impl str { s.extend((0..n).map(|_| self)); s } + + /// Checks if all characters in this string are within the ASCII range. + /// + /// # Examples + /// + /// ``` + /// let ascii = "hello!\n"; + /// let non_ascii = "Grüße, Jürgen ❤"; + /// + /// assert!(ascii.is_ascii()); + /// assert!(!non_ascii.is_ascii()); + /// ``` + #[stable(feature = "ascii_methods_on_intrinsics", since = "1.21.0")] + #[inline] + pub fn is_ascii(&self) -> bool { + // We can treat each byte as character here: all multibyte characters + // start with a byte that is not in the ascii range, so we will stop + // there already. + self.bytes().all(|b| b.is_ascii()) + } + + /// Returns a copy of this string where each character is mapped to its + /// ASCII upper case equivalent. + /// + /// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z', + /// but non-ASCII letters are unchanged. + /// + /// To uppercase the value in-place, use [`make_ascii_uppercase`]. + /// + /// To uppercase ASCII characters in addition to non-ASCII characters, use + /// [`to_uppercase`]. + /// + /// # Examples + /// + /// ``` + /// let s = "Grüße, Jürgen ❤"; + /// + /// assert_eq!("GRüßE, JüRGEN ❤", s.to_ascii_uppercase()); + /// ``` + /// + /// [`make_ascii_uppercase`]: #method.make_ascii_uppercase + /// [`to_uppercase`]: #method.to_uppercase + #[stable(feature = "ascii_methods_on_intrinsics", since = "1.21.0")] + #[inline] + #[cfg(not(stage0))] + pub fn to_ascii_uppercase(&self) -> String { + let mut bytes = self.as_bytes().to_vec(); + bytes.make_ascii_uppercase(); + // make_ascii_uppercase() preserves the UTF-8 invariant. + unsafe { String::from_utf8_unchecked(bytes) } + } + + /// Returns a copy of this string where each character is mapped to its + /// ASCII lower case equivalent. + /// + /// ASCII letters 'A' to 'Z' are mapped to 'a' to 'z', + /// but non-ASCII letters are unchanged. + /// + /// To lowercase the value in-place, use [`make_ascii_lowercase`]. + /// + /// To lowercase ASCII characters in addition to non-ASCII characters, use + /// [`to_lowercase`]. + /// + /// # Examples + /// + /// ``` + /// let s = "Grüße, Jürgen ❤"; + /// + /// assert_eq!("grüße, jürgen ❤", s.to_ascii_lowercase()); + /// ``` + /// + /// [`make_ascii_lowercase`]: #method.make_ascii_lowercase + /// [`to_lowercase`]: #method.to_lowercase + #[stable(feature = "ascii_methods_on_intrinsics", since = "1.21.0")] + #[inline] + #[cfg(not(stage0))] + pub fn to_ascii_lowercase(&self) -> String { + let mut bytes = self.as_bytes().to_vec(); + bytes.make_ascii_lowercase(); + // make_ascii_lowercase() preserves the UTF-8 invariant. + unsafe { String::from_utf8_unchecked(bytes) } + } + + /// Checks that two strings are an ASCII case-insensitive match. + /// + /// Same as `to_ascii_lowercase(a) == to_ascii_lowercase(b)`, + /// but without allocating and copying temporaries. + /// + /// # Examples + /// + /// ``` + /// assert!("Ferris".eq_ignore_ascii_case("FERRIS")); + /// assert!("Ferrös".eq_ignore_ascii_case("FERRöS")); + /// assert!(!"Ferrös".eq_ignore_ascii_case("FERRÖS")); + /// ``` + #[stable(feature = "ascii_methods_on_intrinsics", since = "1.21.0")] + #[inline] + #[cfg(not(stage0))] + pub fn eq_ignore_ascii_case(&self, other: &str) -> bool { + self.as_bytes().eq_ignore_ascii_case(other.as_bytes()) + } + + /// Converts this string to its ASCII upper case equivalent in-place. + /// + /// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z', + /// but non-ASCII letters are unchanged. + /// + /// To return a new uppercased value without modifying the existing one, use + /// [`to_ascii_uppercase`]. + /// + /// [`to_ascii_uppercase`]: #method.to_ascii_uppercase + #[stable(feature = "ascii_methods_on_intrinsics", since = "1.21.0")] + #[cfg(not(stage0))] + pub fn make_ascii_uppercase(&mut self) { + let me = unsafe { self.as_bytes_mut() }; + me.make_ascii_uppercase() + } + + /// Converts this string to its ASCII lower case equivalent in-place. + /// + /// ASCII letters 'A' to 'Z' are mapped to 'a' to 'z', + /// but non-ASCII letters are unchanged. + /// + /// To return a new lowercased value without modifying the existing one, use + /// [`to_ascii_lowercase`]. + /// + /// [`to_ascii_lowercase`]: #method.to_ascii_lowercase + #[stable(feature = "ascii_methods_on_intrinsics", since = "1.21.0")] + #[cfg(not(stage0))] + pub fn make_ascii_lowercase(&mut self) { + let me = unsafe { self.as_bytes_mut() }; + me.make_ascii_lowercase() + } + + /// Checks if all characters of this string are ASCII alphabetic + /// characters: + /// + /// - U+0041 'A' ... U+005A 'Z', or + /// - U+0061 'a' ... U+007A 'z'. + #[unstable(feature = "ascii_ctype", issue = "39658")] + #[inline] + pub fn is_ascii_alphabetic(&self) -> bool { + self.bytes().all(|b| b.is_ascii_alphabetic()) + } + + /// Checks if all characters of this string are ASCII uppercase characters: + /// U+0041 'A' ... U+005A 'Z'. + /// + /// # Example + /// + /// ``` + /// #![feature(ascii_ctype)] + /// + /// // Only ascii uppercase characters + /// assert!("HELLO".is_ascii_uppercase()); + /// + /// // While all characters are ascii, 'y' and 'e' are not uppercase + /// assert!(!"Bye".is_ascii_uppercase()); + /// + /// // While all characters are uppercase, 'Ü' is not ascii + /// assert!(!"TSCHÜSS".is_ascii_uppercase()); + /// ``` + #[unstable(feature = "ascii_ctype", issue = "39658")] + #[inline] + pub fn is_ascii_uppercase(&self) -> bool { + self.bytes().all(|b| b.is_ascii_uppercase()) + } + + /// Checks if all characters of this string are ASCII lowercase characters: + /// U+0061 'a' ... U+007A 'z'. + /// + /// # Example + /// + /// ``` + /// #![feature(ascii_ctype)] + /// + /// // Only ascii uppercase characters + /// assert!("hello".is_ascii_lowercase()); + /// + /// // While all characters are ascii, 'B' is not lowercase + /// assert!(!"Bye".is_ascii_lowercase()); + /// + /// // While all characters are lowercase, 'Ü' is not ascii + /// assert!(!"tschüss".is_ascii_lowercase()); + /// ``` + #[unstable(feature = "ascii_ctype", issue = "39658")] + #[inline] + pub fn is_ascii_lowercase(&self) -> bool { + self.bytes().all(|b| b.is_ascii_lowercase()) + } + + /// Checks if all characters of this string are ASCII alphanumeric + /// characters: + /// + /// - U+0041 'A' ... U+005A 'Z', or + /// - U+0061 'a' ... U+007A 'z', or + /// - U+0030 '0' ... U+0039 '9'. + #[unstable(feature = "ascii_ctype", issue = "39658")] + #[inline] + pub fn is_ascii_alphanumeric(&self) -> bool { + self.bytes().all(|b| b.is_ascii_alphanumeric()) + } + + /// Checks if all characters of this string are ASCII decimal digit: + /// U+0030 '0' ... U+0039 '9'. + #[unstable(feature = "ascii_ctype", issue = "39658")] + #[inline] + pub fn is_ascii_digit(&self) -> bool { + self.bytes().all(|b| b.is_ascii_digit()) + } + + /// Checks if all characters of this string are ASCII hexadecimal digits: + /// + /// - U+0030 '0' ... U+0039 '9', or + /// - U+0041 'A' ... U+0046 'F', or + /// - U+0061 'a' ... U+0066 'f'. + #[unstable(feature = "ascii_ctype", issue = "39658")] + #[inline] + pub fn is_ascii_hexdigit(&self) -> bool { + self.bytes().all(|b| b.is_ascii_hexdigit()) + } + + /// Checks if all characters of this string are ASCII punctuation + /// characters: + /// + /// - U+0021 ... U+002F `! " # $ % & ' ( ) * + , - . /`, or + /// - U+003A ... U+0040 `: ; < = > ? @`, or + /// - U+005B ... U+0060 `[ \\ ] ^ _ \``, or + /// - U+007B ... U+007E `{ | } ~` + #[unstable(feature = "ascii_ctype", issue = "39658")] + #[inline] + pub fn is_ascii_punctuation(&self) -> bool { + self.bytes().all(|b| b.is_ascii_punctuation()) + } + + /// Checks if all characters of this string are ASCII graphic characters: + /// U+0021 '@' ... U+007E '~'. + #[unstable(feature = "ascii_ctype", issue = "39658")] + #[inline] + pub fn is_ascii_graphic(&self) -> bool { + self.bytes().all(|b| b.is_ascii_graphic()) + } + + /// Checks if all characters of this string are ASCII whitespace characters: + /// U+0020 SPACE, U+0009 HORIZONTAL TAB, U+000A LINE FEED, + /// U+000C FORM FEED, or U+000D CARRIAGE RETURN. + /// + /// Rust uses the WhatWG Infra Standard's [definition of ASCII + /// whitespace][infra-aw]. There are several other definitions in + /// wide use. For instance, [the POSIX locale][pct] includes + /// U+000B VERTICAL TAB as well as all the above characters, + /// but—from the very same specification—[the default rule for + /// "field splitting" in the Bourne shell][bfs] considers *only* + /// SPACE, HORIZONTAL TAB, and LINE FEED as whitespace. + /// + /// If you are writing a program that will process an existing + /// file format, check what that format's definition of whitespace is + /// before using this function. + /// + /// [infra-aw]: https://infra.spec.whatwg.org/#ascii-whitespace + /// [pct]: http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap07.html#tag_07_03_01 + /// [bfs]: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_05 + #[unstable(feature = "ascii_ctype", issue = "39658")] + #[inline] + pub fn is_ascii_whitespace(&self) -> bool { + self.bytes().all(|b| b.is_ascii_whitespace()) + } + + /// Checks if all characters of this string are ASCII control characters: + /// + /// - U+0000 NUL ... U+001F UNIT SEPARATOR, or + /// - U+007F DELETE. + /// + /// Note that most ASCII whitespace characters are control + /// characters, but SPACE is not. + #[unstable(feature = "ascii_ctype", issue = "39658")] + #[inline] + pub fn is_ascii_control(&self) -> bool { + self.bytes().all(|b| b.is_ascii_control()) + } } /// Converts a boxed slice of bytes to a boxed string slice without checking diff --git a/src/liballoc/string.rs b/src/liballoc/string.rs index 6d0bb264df1..25fcc1ccdab 100644 --- a/src/liballoc/string.rs +++ b/src/liballoc/string.rs @@ -773,8 +773,6 @@ impl String { /// Basic usage: /// /// ``` - /// use std::ascii::AsciiExt; - /// /// let mut s = String::from("foobar"); /// let s_mut_str = s.as_mut_str(); /// diff --git a/src/liballoc/tests/str.rs b/src/liballoc/tests/str.rs index b3178064505..6b075e7ac0e 100644 --- a/src/liballoc/tests/str.rs +++ b/src/liballoc/tests/str.rs @@ -706,7 +706,6 @@ fn test_split_at() { #[test] fn test_split_at_mut() { - use std::ascii::AsciiExt; let mut s = "Hello World".to_string(); { let (a, b) = s.split_at_mut(5); diff --git a/src/liballoc/tests/vec.rs b/src/liballoc/tests/vec.rs index 0e25da5bd30..9cfde5dcc73 100644 --- a/src/liballoc/tests/vec.rs +++ b/src/liballoc/tests/vec.rs @@ -8,7 +8,6 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use std::ascii::AsciiExt; use std::borrow::Cow; use std::mem::size_of; use std::panic; @@ -966,5 +965,3 @@ fn drain_filter_complex() { assert_eq!(vec, vec![1, 3, 5, 7, 9, 11, 13, 15, 17, 19]); } } - - diff --git a/src/liballoc/vec.rs b/src/liballoc/vec.rs index cf34e195dea..5aca199cf40 100644 --- a/src/liballoc/vec.rs +++ b/src/liballoc/vec.rs @@ -853,8 +853,6 @@ impl<T> Vec<T> { /// # Examples /// /// ``` - /// use std::ascii::AsciiExt; - /// /// let mut vec = vec!["foo", "bar", "Bar", "baz", "bar"]; /// /// vec.dedup_by(|a, b| a.eq_ignore_ascii_case(b)); |
