Skip to content

Commit 6f55568

Browse files
Use direct implementation on u8/char to implement AsciiExt
The methods were copied to u8/char, so we can just use it in AsciiExt impls to avoid duplicate code.
1 parent d425f8d commit 6f55568

File tree

1 file changed

+41
-280
lines changed

1 file changed

+41
-280
lines changed

src/libstd/ascii.rs

Lines changed: 41 additions & 280 deletions
Original file line numberDiff line numberDiff line change
@@ -753,202 +753,66 @@ impl AsciiExt for [u8] {
753753
}
754754
}
755755

756-
#[stable(feature = "rust1", since = "1.0.0")]
757-
impl AsciiExt for u8 {
758-
type Owned = u8;
759-
#[inline]
760-
fn is_ascii(&self) -> bool { *self & 128 == 0 }
761-
#[inline]
762-
fn to_ascii_uppercase(&self) -> u8 { ASCII_UPPERCASE_MAP[*self as usize] }
763-
#[inline]
764-
fn to_ascii_lowercase(&self) -> u8 { ASCII_LOWERCASE_MAP[*self as usize] }
765-
#[inline]
766-
fn eq_ignore_ascii_case(&self, other: &u8) -> bool {
767-
self.to_ascii_lowercase() == other.to_ascii_lowercase()
768-
}
769-
#[inline]
770-
fn make_ascii_uppercase(&mut self) { *self = self.to_ascii_uppercase(); }
771-
#[inline]
772-
fn make_ascii_lowercase(&mut self) { *self = self.to_ascii_lowercase(); }
756+
macro_rules! impl_by_delegating {
757+
($ty:ty, $owned:ty) => {
758+
#[stable(feature = "rust1", since = "1.0.0")]
759+
impl AsciiExt for $ty {
760+
type Owned = $owned;
773761

774-
#[inline]
775-
fn is_ascii_alphabetic(&self) -> bool {
776-
if *self >= 0x80 { return false; }
777-
match ASCII_CHARACTER_CLASS[*self as usize] {
778-
L|Lx|U|Ux => true,
779-
_ => false
780-
}
781-
}
762+
#[inline]
763+
fn is_ascii(&self) -> bool { self.is_ascii() }
782764

783-
#[inline]
784-
fn is_ascii_uppercase(&self) -> bool {
785-
if *self >= 0x80 { return false }
786-
match ASCII_CHARACTER_CLASS[*self as usize] {
787-
U|Ux => true,
788-
_ => false
789-
}
790-
}
765+
#[inline]
766+
fn to_ascii_uppercase(&self) -> Self::Owned { self.to_ascii_uppercase() }
791767

792-
#[inline]
793-
fn is_ascii_lowercase(&self) -> bool {
794-
if *self >= 0x80 { return false }
795-
match ASCII_CHARACTER_CLASS[*self as usize] {
796-
L|Lx => true,
797-
_ => false
798-
}
799-
}
768+
#[inline]
769+
fn to_ascii_lowercase(&self) -> Self::Owned { self.to_ascii_lowercase() }
800770

801-
#[inline]
802-
fn is_ascii_alphanumeric(&self) -> bool {
803-
if *self >= 0x80 { return false }
804-
match ASCII_CHARACTER_CLASS[*self as usize] {
805-
D|L|Lx|U|Ux => true,
806-
_ => false
807-
}
808-
}
771+
#[inline]
772+
fn eq_ignore_ascii_case(&self, o: &Self) -> bool { self.eq_ignore_ascii_case(o) }
809773

810-
#[inline]
811-
fn is_ascii_digit(&self) -> bool {
812-
if *self >= 0x80 { return false }
813-
match ASCII_CHARACTER_CLASS[*self as usize] {
814-
D => true,
815-
_ => false
816-
}
817-
}
774+
#[inline]
775+
fn make_ascii_uppercase(&mut self) { self.make_ascii_uppercase(); }
818776

819-
#[inline]
820-
fn is_ascii_hexdigit(&self) -> bool {
821-
if *self >= 0x80 { return false }
822-
match ASCII_CHARACTER_CLASS[*self as usize] {
823-
D|Lx|Ux => true,
824-
_ => false
825-
}
826-
}
777+
#[inline]
778+
fn make_ascii_lowercase(&mut self) { self.make_ascii_lowercase(); }
827779

828-
#[inline]
829-
fn is_ascii_punctuation(&self) -> bool {
830-
if *self >= 0x80 { return false }
831-
match ASCII_CHARACTER_CLASS[*self as usize] {
832-
P => true,
833-
_ => false
834-
}
835-
}
780+
#[inline]
781+
fn is_ascii_alphabetic(&self) -> bool { self.is_ascii_alphabetic() }
836782

837-
#[inline]
838-
fn is_ascii_graphic(&self) -> bool {
839-
if *self >= 0x80 { return false; }
840-
match ASCII_CHARACTER_CLASS[*self as usize] {
841-
Ux|U|Lx|L|D|P => true,
842-
_ => false
843-
}
844-
}
783+
#[inline]
784+
fn is_ascii_uppercase(&self) -> bool { self.is_ascii_uppercase() }
845785

846-
#[inline]
847-
fn is_ascii_whitespace(&self) -> bool {
848-
if *self >= 0x80 { return false; }
849-
match ASCII_CHARACTER_CLASS[*self as usize] {
850-
Cw|W => true,
851-
_ => false
852-
}
853-
}
786+
#[inline]
787+
fn is_ascii_lowercase(&self) -> bool { self.is_ascii_lowercase() }
854788

855-
#[inline]
856-
fn is_ascii_control(&self) -> bool {
857-
if *self >= 0x80 { return false; }
858-
match ASCII_CHARACTER_CLASS[*self as usize] {
859-
C|Cw => true,
860-
_ => false
861-
}
862-
}
863-
}
789+
#[inline]
790+
fn is_ascii_alphanumeric(&self) -> bool { self.is_ascii_alphanumeric() }
864791

865-
#[stable(feature = "rust1", since = "1.0.0")]
866-
impl AsciiExt for char {
867-
type Owned = char;
868-
#[inline]
869-
fn is_ascii(&self) -> bool {
870-
*self as u32 <= 0x7F
871-
}
792+
#[inline]
793+
fn is_ascii_digit(&self) -> bool { self.is_ascii_digit() }
872794

873-
#[inline]
874-
fn to_ascii_uppercase(&self) -> char {
875-
if self.is_ascii() {
876-
(*self as u8).to_ascii_uppercase() as char
877-
} else {
878-
*self
879-
}
880-
}
881-
882-
#[inline]
883-
fn to_ascii_lowercase(&self) -> char {
884-
if self.is_ascii() {
885-
(*self as u8).to_ascii_lowercase() as char
886-
} else {
887-
*self
888-
}
889-
}
890-
891-
#[inline]
892-
fn eq_ignore_ascii_case(&self, other: &char) -> bool {
893-
self.to_ascii_lowercase() == other.to_ascii_lowercase()
894-
}
895-
896-
#[inline]
897-
fn make_ascii_uppercase(&mut self) { *self = self.to_ascii_uppercase(); }
898-
#[inline]
899-
fn make_ascii_lowercase(&mut self) { *self = self.to_ascii_lowercase(); }
900-
901-
#[inline]
902-
fn is_ascii_alphabetic(&self) -> bool {
903-
(*self as u32 <= 0x7f) && (*self as u8).is_ascii_alphabetic()
904-
}
905-
906-
#[inline]
907-
fn is_ascii_uppercase(&self) -> bool {
908-
(*self as u32 <= 0x7f) && (*self as u8).is_ascii_uppercase()
909-
}
910-
911-
#[inline]
912-
fn is_ascii_lowercase(&self) -> bool {
913-
(*self as u32 <= 0x7f) && (*self as u8).is_ascii_lowercase()
914-
}
915-
916-
#[inline]
917-
fn is_ascii_alphanumeric(&self) -> bool {
918-
(*self as u32 <= 0x7f) && (*self as u8).is_ascii_alphanumeric()
919-
}
920-
921-
#[inline]
922-
fn is_ascii_digit(&self) -> bool {
923-
(*self as u32 <= 0x7f) && (*self as u8).is_ascii_digit()
924-
}
795+
#[inline]
796+
fn is_ascii_hexdigit(&self) -> bool { self.is_ascii_hexdigit() }
925797

926-
#[inline]
927-
fn is_ascii_hexdigit(&self) -> bool {
928-
(*self as u32 <= 0x7f) && (*self as u8).is_ascii_hexdigit()
929-
}
930-
931-
#[inline]
932-
fn is_ascii_punctuation(&self) -> bool {
933-
(*self as u32 <= 0x7f) && (*self as u8).is_ascii_punctuation()
934-
}
798+
#[inline]
799+
fn is_ascii_punctuation(&self) -> bool { self.is_ascii_punctuation() }
935800

936-
#[inline]
937-
fn is_ascii_graphic(&self) -> bool {
938-
(*self as u32 <= 0x7f) && (*self as u8).is_ascii_graphic()
939-
}
801+
#[inline]
802+
fn is_ascii_graphic(&self) -> bool { self.is_ascii_graphic() }
940803

941-
#[inline]
942-
fn is_ascii_whitespace(&self) -> bool {
943-
(*self as u32 <= 0x7f) && (*self as u8).is_ascii_whitespace()
944-
}
804+
#[inline]
805+
fn is_ascii_whitespace(&self) -> bool { self.is_ascii_whitespace() }
945806

946-
#[inline]
947-
fn is_ascii_control(&self) -> bool {
948-
(*self as u32 <= 0x7f) && (*self as u8).is_ascii_control()
807+
#[inline]
808+
fn is_ascii_control(&self) -> bool { self.is_ascii_control() }
809+
}
949810
}
950811
}
951812

813+
impl_by_delegating!(u8, u8);
814+
impl_by_delegating!(char, char);
815+
952816
/// An iterator over the escaped version of a byte.
953817
///
954818
/// This `struct` is created by the [`escape_default`] function. See its
@@ -1066,109 +930,6 @@ impl fmt::Debug for EscapeDefault {
1066930
}
1067931

1068932

1069-
static ASCII_LOWERCASE_MAP: [u8; 256] = [
1070-
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
1071-
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
1072-
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
1073-
0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
1074-
b' ', b'!', b'"', b'#', b'$', b'%', b'&', b'\'',
1075-
b'(', b')', b'*', b'+', b',', b'-', b'.', b'/',
1076-
b'0', b'1', b'2', b'3', b'4', b'5', b'6', b'7',
1077-
b'8', b'9', b':', b';', b'<', b'=', b'>', b'?',
1078-
b'@',
1079-
1080-
b'a', b'b', b'c', b'd', b'e', b'f', b'g',
1081-
b'h', b'i', b'j', b'k', b'l', b'm', b'n', b'o',
1082-
b'p', b'q', b'r', b's', b't', b'u', b'v', b'w',
1083-
b'x', b'y', b'z',
1084-
1085-
b'[', b'\\', b']', b'^', b'_',
1086-
b'`', b'a', b'b', b'c', b'd', b'e', b'f', b'g',
1087-
b'h', b'i', b'j', b'k', b'l', b'm', b'n', b'o',
1088-
b'p', b'q', b'r', b's', b't', b'u', b'v', b'w',
1089-
b'x', b'y', b'z', b'{', b'|', b'}', b'~', 0x7f,
1090-
0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
1091-
0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
1092-
0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
1093-
0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
1094-
0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
1095-
0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
1096-
0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7,
1097-
0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
1098-
0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
1099-
0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
1100-
0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
1101-
0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
1102-
0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
1103-
0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
1104-
0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
1105-
0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff,
1106-
];
1107-
1108-
static ASCII_UPPERCASE_MAP: [u8; 256] = [
1109-
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
1110-
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
1111-
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
1112-
0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
1113-
b' ', b'!', b'"', b'#', b'$', b'%', b'&', b'\'',
1114-
b'(', b')', b'*', b'+', b',', b'-', b'.', b'/',
1115-
b'0', b'1', b'2', b'3', b'4', b'5', b'6', b'7',
1116-
b'8', b'9', b':', b';', b'<', b'=', b'>', b'?',
1117-
b'@', b'A', b'B', b'C', b'D', b'E', b'F', b'G',
1118-
b'H', b'I', b'J', b'K', b'L', b'M', b'N', b'O',
1119-
b'P', b'Q', b'R', b'S', b'T', b'U', b'V', b'W',
1120-
b'X', b'Y', b'Z', b'[', b'\\', b']', b'^', b'_',
1121-
b'`',
1122-
1123-
b'A', b'B', b'C', b'D', b'E', b'F', b'G',
1124-
b'H', b'I', b'J', b'K', b'L', b'M', b'N', b'O',
1125-
b'P', b'Q', b'R', b'S', b'T', b'U', b'V', b'W',
1126-
b'X', b'Y', b'Z',
1127-
1128-
b'{', b'|', b'}', b'~', 0x7f,
1129-
0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
1130-
0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
1131-
0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
1132-
0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
1133-
0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
1134-
0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
1135-
0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7,
1136-
0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
1137-
0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
1138-
0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
1139-
0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
1140-
0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
1141-
0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
1142-
0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
1143-
0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
1144-
0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff,
1145-
];
1146-
1147-
enum AsciiCharacterClass {
1148-
C, // control
1149-
Cw, // control whitespace
1150-
W, // whitespace
1151-
D, // digit
1152-
L, // lowercase
1153-
Lx, // lowercase hex digit
1154-
U, // uppercase
1155-
Ux, // uppercase hex digit
1156-
P, // punctuation
1157-
}
1158-
use self::AsciiCharacterClass::*;
1159-
1160-
static ASCII_CHARACTER_CLASS: [AsciiCharacterClass; 128] = [
1161-
// _0 _1 _2 _3 _4 _5 _6 _7 _8 _9 _a _b _c _d _e _f
1162-
C, C, C, C, C, C, C, C, C, Cw,Cw,C, Cw,Cw,C, C, // 0_
1163-
C, C, C, C, C, C, C, C, C, C, C, C, C, C, C, C, // 1_
1164-
W, P, P, P, P, P, P, P, P, P, P, P, P, P, P, P, // 2_
1165-
D, D, D, D, D, D, D, D, D, D, P, P, P, P, P, P, // 3_
1166-
P, Ux,Ux,Ux,Ux,Ux,Ux,U, U, U, U, U, U, U, U, U, // 4_
1167-
U, U, U, U, U, U, U, U, U, U, U, P, P, P, P, P, // 5_
1168-
P, Lx,Lx,Lx,Lx,Lx,Lx,L, L, L, L, L, L, L, L, L, // 6_
1169-
L, L, L, L, L, L, L, L, L, L, L, P, P, P, P, C, // 7_
1170-
];
1171-
1172933
#[cfg(test)]
1173934
mod tests {
1174935
use super::*;

0 commit comments

Comments
 (0)