-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Implement std::ascii::AsciiExt
for Cow<str>
.
#42872
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @BurntSushi (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
That seems sensible? This example shows how to call |
Here's a scenario mutating the underlying let mut x = Cow::Borrowed("hello world");
x.to_mut().make_ascii_lowercase();
assert_eq!(Cow::Owned("hello world"), x); The gain with my changes here would be: let mut x = Cow::Borrowed("hello world");
x.make_ascii_lowercase();
assert_eq!(Cow::Borrowed("hello world"), x); Notice that the |
@@ -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>; |
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 a String
. It's a shame as to_ascii_{upper,lower}case
could benifit from the same optimization as make_ascii_{upper,lower}case
.
|
||
#[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 comment
The reason will be displayed to describe this comment to others. Learn more.
This should be (**self).eq_ignore_ascii_case(**other)
to avoid allocations.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
If self
is a Cow::Owned
already then this shouldn't create a new Vec
.
Ugh. As per this comment, this seems like a breaking change. I'm going to close this unless we're overlooking something. |
You might be able to use |
Although |
I'm going to close this since it unfortunately doesn't seem possible. Thanks, though! |
I was in a situation where I wanted to call
make_ascii_uppercase
on aCow<str>
and noticed the only way to do that is to clone the data in theCow
(I think?).Opening this a little prematurely to make sure this eligible consideration. If it is,
i'll need to clean it up a little, fix a but where it doesn't process a byte (see the FIXME), andadd some tests. Also, if this is desired, I can extend this to work onCow<[u8]>
.