Add PartialEq and Eq impls to the character case iterators#154287
Add PartialEq and Eq impls to the character case iterators#154287Jules-Bertholet wants to merge 2 commits into
PartialEq and Eq impls to the character case iterators#154287Conversation
Allows easily checking whether a string is in a particular case.
|
rustbot has assigned @Mark-Simulacrum. Use Why was this reviewer chosen?The reviewer was selected based on:
|
And also add a test for its claims.
35b6f10 to
831794e
Compare
Isn't this done with |
Those methods do not currently exist. It might make sense to add e.g.
Not that I know of. However, |
|
Hm, okay. I think that makes some sense. Let's see what libs-api thinks (should be in their meeting queue). |
|
An additional minor benefit of the proposed API is that it would allow removing |
|
It seems T-libs-api wasn't a fan of this, and had some alternative suggestions (https://hackmd.io/3iG4g-ofShaMgcP_OYHCEw#waiting-on-team-rusttf154287-Add-PartialEq-and-Eq-impls-to-the-character-case-iterators). I'll go through them one by one:
I would be open to adding those. However, as I explained above, they don't extend neatly to titlecase. Also, they don't work for people who can't easily use It doesn't necessarily have to be this PR's API, however. For example, we could have methods on
Overhead should not be a problem. Case folding is primarily based on the lowercase mapping, so we would only need a small table for the difference. However, there are other issues:
It doesn't optimize well, unfortunately. |
|
We discussed this PR in this week's @rust-lang/libs-api meeting and decided against this proposal. We would, however, be open to addressing this usecase through any or all of adding |
…imulacrum Add APIs for case folding to the standard library [Libs-api requested these](rust-lang#154287 (comment)), so here they are. New public API (gated behind `#[feature(casefold)]`): ```rust impl char { pub fn to_casefold(self) -> ToCasefold; } impl str { pub fn to_casefold(&self) -> String; pub fn eq_ignore_case(&self) -> bool; } pub struct ToCasefold { ... } impl Iterator for ToCasefold { type Item = char; ... } impl DoubleEndedIterator for ToCasefold { ... } impl FusedIterator for ToCasefold { } impl ExactSizeIterator for ToCasefold { ... } impl fmt::Display for ToCasefold { ... } ``` ## Notes - This only adds a negligible amount of static data to `core::unicode`. To accomplish that, we compute the case-folding for most characters as the lowercase of their uppercase; this double mapping adds some complexity to the implementation. - No normalization (e.g. NFC) is performed, so visually and semantically equivalent strings can compare unequal. - I have not put any effort into optimizing `eq_ignore_case()`; there may be a more performant implementation. - `char::eq_ignore_case()` is left to future work—it's a potential footgun, so we may want to think more deeply about how to expose and document that API. @rustbot label T-libs-api A-unicode
…imulacrum Add APIs for case folding to the standard library [Libs-api requested these](rust-lang#154287 (comment)), so here they are. New public API (gated behind `#[feature(casefold)]`): ```rust impl char { pub fn to_casefold(self) -> ToCasefold; } impl str { pub fn to_casefold(&self) -> String; pub fn eq_ignore_case(&self) -> bool; } pub struct ToCasefold { ... } impl Iterator for ToCasefold { type Item = char; ... } impl DoubleEndedIterator for ToCasefold { ... } impl FusedIterator for ToCasefold { } impl ExactSizeIterator for ToCasefold { ... } impl fmt::Display for ToCasefold { ... } ``` ## Notes - This only adds a negligible amount of static data to `core::unicode`. To accomplish that, we compute the case-folding for most characters as the lowercase of their uppercase; this double mapping adds some complexity to the implementation. - No normalization (e.g. NFC) is performed, so visually and semantically equivalent strings can compare unequal. - I have not put any effort into optimizing `eq_ignore_case()`; there may be a more performant implementation. - `char::eq_ignore_case()` is left to future work—it's a potential footgun, so we may want to think more deeply about how to expose and document that API. @rustbot label T-libs-api A-unicode
…imulacrum Add APIs for case folding to the standard library [Libs-api requested these](rust-lang#154287 (comment)), so here they are. New public API (gated behind `#[feature(casefold)]`): ```rust impl char { pub fn to_casefold(self) -> ToCasefold; } impl str { pub fn to_casefold(&self) -> String; pub fn eq_ignore_case(&self) -> bool; } pub struct ToCasefold { ... } impl Iterator for ToCasefold { type Item = char; ... } impl DoubleEndedIterator for ToCasefold { ... } impl FusedIterator for ToCasefold { } impl ExactSizeIterator for ToCasefold { ... } impl fmt::Display for ToCasefold { ... } ``` ## Notes - This only adds a negligible amount of static data to `core::unicode`. To accomplish that, we compute the case-folding for most characters as the lowercase of their uppercase; this double mapping adds some complexity to the implementation. - No normalization (e.g. NFC) is performed, so visually and semantically equivalent strings can compare unequal. - I have not put any effort into optimizing `eq_ignore_case()`; there may be a more performant implementation. - `char::eq_ignore_case()` is left to future work—it's a potential footgun, so we may want to think more deeply about how to expose and document that API. @rustbot label T-libs-api A-unicode
…imulacrum Add APIs for case folding to the standard library [Libs-api requested these](rust-lang#154287 (comment)), so here they are. New public API (gated behind `#[feature(casefold)]`): ```rust impl char { pub fn to_casefold(self) -> ToCasefold; } impl str { pub fn to_casefold(&self) -> String; pub fn eq_ignore_case(&self) -> bool; } pub struct ToCasefold { ... } impl Iterator for ToCasefold { type Item = char; ... } impl DoubleEndedIterator for ToCasefold { ... } impl FusedIterator for ToCasefold { } impl ExactSizeIterator for ToCasefold { ... } impl fmt::Display for ToCasefold { ... } ``` ## Notes - This only adds a negligible amount of static data to `core::unicode`. To accomplish that, we compute the case-folding for most characters as the lowercase of their uppercase; this double mapping adds some complexity to the implementation. - No normalization (e.g. NFC) is performed, so visually and semantically equivalent strings can compare unequal. - I have not put any effort into optimizing `eq_ignore_case()`; there may be a more performant implementation. - `char::eq_ignore_case()` is left to future work—it's a potential footgun, so we may want to think more deeply about how to expose and document that API. @rustbot label T-libs-api A-unicode
…imulacrum Add APIs for case folding to the standard library [Libs-api requested these](rust-lang#154287 (comment)), so here they are. New public API (gated behind `#[feature(casefold)]`): ```rust impl char { pub fn to_casefold(self) -> ToCasefold; } impl str { pub fn to_casefold(&self) -> String; pub fn eq_ignore_case(&self) -> bool; } pub struct ToCasefold { ... } impl Iterator for ToCasefold { type Item = char; ... } impl DoubleEndedIterator for ToCasefold { ... } impl FusedIterator for ToCasefold { } impl ExactSizeIterator for ToCasefold { ... } impl fmt::Display for ToCasefold { ... } ``` ## Notes - This only adds a negligible amount of static data to `core::unicode`. To accomplish that, we compute the case-folding for most characters as the lowercase of their uppercase; this double mapping adds some complexity to the implementation. - No normalization (e.g. NFC) is performed, so visually and semantically equivalent strings can compare unequal. - I have not put any effort into optimizing `eq_ignore_case()`; there may be a more performant implementation. - `char::eq_ignore_case()` is left to future work—it's a potential footgun, so we may want to think more deeply about how to expose and document that API. @rustbot label T-libs-api A-unicode
…imulacrum Add APIs for case folding to the standard library [Libs-api requested these](rust-lang#154287 (comment)), so here they are. New public API (gated behind `#[feature(casefold)]`): ```rust impl char { pub fn to_casefold(self) -> ToCasefold; } impl str { pub fn to_casefold(&self) -> String; pub fn eq_ignore_case(&self) -> bool; } pub struct ToCasefold { ... } impl Iterator for ToCasefold { type Item = char; ... } impl DoubleEndedIterator for ToCasefold { ... } impl FusedIterator for ToCasefold { } impl ExactSizeIterator for ToCasefold { ... } impl fmt::Display for ToCasefold { ... } ``` ## Notes - This only adds a negligible amount of static data to `core::unicode`. To accomplish that, we compute the case-folding for most characters as the lowercase of their uppercase; this double mapping adds some complexity to the implementation. - No normalization (e.g. NFC) is performed, so visually and semantically equivalent strings can compare unequal. - I have not put any effort into optimizing `eq_ignore_case()`; there may be a more performant implementation. - `char::eq_ignore_case()` is left to future work—it's a potential footgun, so we may want to think more deeply about how to expose and document that API. @rustbot label T-libs-api A-unicode
Add APIs for case folding to the standard library [Libs-api requested these](rust-lang/rust#154287 (comment)), so here they are. New public API (gated behind `#[feature(casefold)]`): ```rust impl char { pub fn to_casefold(self) -> ToCasefold; } impl str { pub fn to_casefold(&self) -> String; pub fn eq_ignore_case(&self) -> bool; } pub struct ToCasefold { ... } impl Iterator for ToCasefold { type Item = char; ... } impl DoubleEndedIterator for ToCasefold { ... } impl FusedIterator for ToCasefold { } impl ExactSizeIterator for ToCasefold { ... } impl fmt::Display for ToCasefold { ... } ``` ## Notes - This only adds a negligible amount of static data to `core::unicode`. To accomplish that, we compute the case-folding for most characters as the lowercase of their uppercase; this double mapping adds some complexity to the implementation. - No normalization (e.g. NFC) is performed, so visually and semantically equivalent strings can compare unequal. - I have not put any effort into optimizing `eq_ignore_case()`; there may be a more performant implementation. - `char::eq_ignore_case()` is left to future work—it's a potential footgun, so we may want to think more deeply about how to expose and document that API. @rustbot label T-libs-api A-unicode
Add APIs for case folding to the standard library [Libs-api requested these](rust-lang/rust#154287 (comment)), so here they are. New public API (gated behind `#[feature(casefold)]`): ```rust impl char { pub fn to_casefold(self) -> ToCasefold; } impl str { pub fn to_casefold(&self) -> String; pub fn eq_ignore_case(&self) -> bool; } pub struct ToCasefold { ... } impl Iterator for ToCasefold { type Item = char; ... } impl DoubleEndedIterator for ToCasefold { ... } impl FusedIterator for ToCasefold { } impl ExactSizeIterator for ToCasefold { ... } impl fmt::Display for ToCasefold { ... } ``` ## Notes - This only adds a negligible amount of static data to `core::unicode`. To accomplish that, we compute the case-folding for most characters as the lowercase of their uppercase; this double mapping adds some complexity to the implementation. - No normalization (e.g. NFC) is performed, so visually and semantically equivalent strings can compare unequal. - I have not put any effort into optimizing `eq_ignore_case()`; there may be a more performant implementation. - `char::eq_ignore_case()` is left to future work—it's a potential footgun, so we may want to think more deeply about how to expose and document that API. @rustbot label T-libs-api A-unicode
Allows easily checking whether a string is in a particular case.
Adds
impl PartialEq<{ToUppercase, ToLowercase, ToTitlecase, char}> for {ToUppercase, ToLowercase, ToTitlecase}andimpl Eq for {ToUppercase, ToLowercase, ToTitlecase}.@rustbot label needs-fcp T-libs-api A-unicode