@@ -3794,39 +3794,8 @@ impl u8 {
3794
3794
#[ stable( feature = "ascii_methods_on_intrinsics" , since = "1.23.0" ) ]
3795
3795
#[ inline]
3796
3796
pub fn to_ascii_uppercase ( & self ) -> u8 {
3797
- // See benchmarks in src/libcore/benches/ascii_case.rs
3798
-
3799
- // Lower-case ASCII 'a' is the first byte that has its highest bit set
3800
- // after wrap-adding 0x1F:
3801
- //
3802
- // b'a' + 0x1F == 0x80 == 0b1000_0000
3803
- // b'z' + 0x1F == 0x98 == 0b10011000
3804
- //
3805
- // Lower-case ASCII 'z' is the last byte that has its highest bit unset
3806
- // after wrap-adding 0x05:
3807
- //
3808
- // b'a' + 0x05 == 0x66 == 0b0110_0110
3809
- // b'z' + 0x05 == 0x7F == 0b0111_1111
3810
- //
3811
- // … except for 0xFB to 0xFF, but those are in the range of bytes
3812
- // that have the highest bit unset again after adding 0x1F.
3813
- //
3814
- // So `(byte + 0x1f) & !(byte + 5)` has its highest bit set
3815
- // iff `byte` is a lower-case ASCII letter.
3816
- //
3817
- // Lower-case ASCII letters all have the 0x20 bit set.
3818
- // (Two positions right of 0x80, the highest bit.)
3819
- // Unsetting that bit produces the same letter, in upper-case.
3820
- //
3821
- // Therefore:
3822
- * self &
3823
- !(
3824
- (
3825
- self . wrapping_add ( 0x1f ) &
3826
- !self . wrapping_add ( 0x05 ) &
3827
- 0x80
3828
- ) >> 2
3829
- )
3797
+ // Unset the fith bit if this is a lowercase letter
3798
+ * self & !( ( self . is_ascii_lowercase ( ) as u8 ) << 5 )
3830
3799
}
3831
3800
3832
3801
/// Makes a copy of the value in its ASCII lower case equivalent.
@@ -3848,15 +3817,8 @@ impl u8 {
3848
3817
#[ stable( feature = "ascii_methods_on_intrinsics" , since = "1.23.0" ) ]
3849
3818
#[ inline]
3850
3819
pub fn to_ascii_lowercase ( & self ) -> u8 {
3851
- // See comments in to_ascii_uppercase above.
3852
- * self |
3853
- (
3854
- (
3855
- self . wrapping_add ( 0x3f ) &
3856
- !self . wrapping_add ( 0x25 ) &
3857
- 0x80
3858
- ) >> 2
3859
- )
3820
+ // Set the fith bit if this is an uppercase letter
3821
+ * self | ( ( self . is_ascii_uppercase ( ) as u8 ) << 5 )
3860
3822
}
3861
3823
3862
3824
/// Checks that two values are an ASCII case-insensitive match.
0 commit comments