-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Implement std::ascii::AsciiExt
for Cow<str>
.
#42872
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
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5e2db7f
Implement `std::ascii::AsciiExt` for `Cow<str>`.
frewsxcv 53ebadd
simplify to_ impls
frewsxcv ee73997
fix bugs do cleanup
frewsxcv 561fd03
consistent comment
frewsxcv c374c1b
refactor/rename
frewsxcv 9fc838e
fix/rename
frewsxcv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ | |
|
||
#![stable(feature = "rust1", since = "1.0.0")] | ||
|
||
use borrow::Cow; | ||
use fmt; | ||
use ops::Range; | ||
use iter::FusedIterator; | ||
|
@@ -947,6 +948,119 @@ impl AsciiExt for char { | |
} | ||
} | ||
|
||
#[stable(feature = "rust1", since = "1.0.0")] | ||
impl<'a> AsciiExt for Cow<'a, str> { | ||
type Owned = Cow<'a, str>; | ||
|
||
#[inline] | ||
fn is_ascii(&self) -> bool { | ||
(**self).is_ascii() | ||
} | ||
|
||
#[inline] | ||
fn to_ascii_uppercase(&self) -> Self::Owned { | ||
Cow::Owned((**self).to_ascii_uppercase()) | ||
} | ||
|
||
#[inline] | ||
fn to_ascii_lowercase(&self) -> Self::Owned { | ||
Cow::Owned((**self).to_ascii_lowercase()) | ||
} | ||
|
||
#[inline] | ||
fn eq_ignore_ascii_case(&self, other: &Self) -> bool { | ||
self.to_ascii_lowercase() == other.to_ascii_lowercase() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be |
||
} | ||
|
||
#[inline] | ||
fn make_ascii_uppercase(&mut self) { | ||
// Determine position of first lowercase byte. | ||
let pos_first_lower = match self.bytes().position(|c| c.is_ascii_lowercase()) { | ||
Some(p) => p, | ||
None => return, | ||
}; | ||
let mut bytes = Vec::with_capacity(self.len()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If |
||
unsafe { bytes.set_len(pos_first_lower); } | ||
bytes.copy_from_slice(&self.as_bytes()[..pos_first_lower]); | ||
bytes.extend( | ||
self.bytes() | ||
.skip(pos_first_lower) | ||
.map(|b| b.to_ascii_uppercase())); | ||
// make_ascii_uppercase() preserves the UTF-8 invariant. | ||
let s = unsafe { String::from_utf8_unchecked(bytes) }; | ||
*self = Cow::Owned(s) | ||
} | ||
|
||
#[inline] | ||
fn make_ascii_lowercase(&mut self) { | ||
// Determine position of first uppercase byte. | ||
let pos_first_upper = match self.bytes().position(|c| c.is_ascii_uppercase()) { | ||
Some(p) => p, | ||
None => return, | ||
}; | ||
let mut bytes = Vec::with_capacity(self.len()); | ||
unsafe { bytes.set_len(pos_first_upper); } | ||
bytes.copy_from_slice(&self.as_bytes()[..pos_first_upper]); | ||
bytes.extend( | ||
self.bytes() | ||
.skip(pos_first_upper) | ||
.map(|b| b.to_ascii_lowercase())); | ||
// make_ascii_uppercase() preserves the UTF-8 invariant. | ||
let s = unsafe { String::from_utf8_unchecked(bytes) }; | ||
*self = Cow::Owned(s) | ||
} | ||
|
||
#[inline] | ||
fn is_ascii_alphabetic(&self) -> bool { | ||
(**self).is_ascii_alphabetic() | ||
} | ||
|
||
#[inline] | ||
fn is_ascii_uppercase(&self) -> bool { | ||
(**self).is_ascii_uppercase() | ||
} | ||
|
||
#[inline] | ||
fn is_ascii_lowercase(&self) -> bool { | ||
(**self).is_ascii_lowercase() | ||
} | ||
|
||
#[inline] | ||
fn is_ascii_alphanumeric(&self) -> bool { | ||
(**self).is_ascii_alphanumeric() | ||
} | ||
|
||
#[inline] | ||
fn is_ascii_digit(&self) -> bool { | ||
(**self).is_ascii_digit() | ||
} | ||
|
||
#[inline] | ||
fn is_ascii_hexdigit(&self) -> bool { | ||
(**self).is_ascii_hexdigit() | ||
} | ||
|
||
#[inline] | ||
fn is_ascii_punctuation(&self) -> bool { | ||
(**self).is_ascii_punctuation() | ||
} | ||
|
||
#[inline] | ||
fn is_ascii_graphic(&self) -> bool { | ||
(**self).is_ascii_graphic() | ||
} | ||
|
||
#[inline] | ||
fn is_ascii_whitespace(&self) -> bool { | ||
(**self).is_ascii_whitespace() | ||
} | ||
|
||
#[inline] | ||
fn is_ascii_control(&self) -> bool { | ||
(**self).is_ascii_control() | ||
} | ||
} | ||
|
||
/// An iterator over the escaped version of a byte. | ||
/// | ||
/// This `struct` is created by the [`escape_default`] function. See its | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm pretty sure this is a breaking change.
Cow::Borrowed("Hello, world!").to_ascii_uppercase()
currently returns aString
. It's a shame asto_ascii_{upper,lower}case
could benifit from the same optimization asmake_ascii_{upper,lower}case
.