Skip to content

Commit

Permalink
Add numeric test, change is_ascii_alphabetic name
Browse files Browse the repository at this point in the history
Change the name from is_ascii_alpha to is_ascii_alphabetic to be
consistent with std::char. Add methods for all-digits.
  • Loading branch information
raphlinus committed Aug 23, 2019
1 parent 2dba9ec commit 72bbca7
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 18 deletions.
11 changes: 10 additions & 1 deletion src/tinystr16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl TinyStr16 {
unsafe { Self(NonZeroU128::new_unchecked(result)) }
}

pub fn is_ascii_alpha(self) -> bool {
pub fn is_ascii_alphabetic(self) -> bool {
let word = self.0.get();
let mask =
(word + 0x7f7f7f7f_7f7f7f7f_7f7f7f7f_7f7f7f7f) & 0x80808080_80808080_80808080_80808080;
Expand All @@ -65,6 +65,15 @@ impl TinyStr16 {
(alpha & numeric & mask) == 0
}

pub fn is_ascii_numeric(self) -> bool {
let word = self.0.get();
let mask =
(word + 0x7f7f7f7f_7f7f7f7f_7f7f7f7f_7f7f7f7f) & 0x80808080_80808080_80808080_80808080;
let numeric = !(word + 0x50505050_50505050_50505050_50505050)
| (word + 0x46464646_46464646_46464646_46464646);
(numeric & mask) == 0
}

pub fn to_ascii_titlecase(self) -> Self {
let word = self.0.get().to_le();
let mask = ((word + 0x3f3f3f3f_3f3f3f3f_3f3f3f3f_3f3f3f1f)
Expand Down
9 changes: 8 additions & 1 deletion src/tinystr4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl TinyStr4 {
unsafe { Self(NonZeroU32::new_unchecked(result)) }
}

pub fn is_ascii_alpha(self) -> bool {
pub fn is_ascii_alphabetic(self) -> bool {
let word = self.0.get();
let mask = (word + 0x7f7f_7f7f) & 0x8080_8080;
let lower = word | 0x2020_2020;
Expand All @@ -52,6 +52,13 @@ impl TinyStr4 {
(alpha & numeric & mask) == 0
}

pub fn is_ascii_numeric(self) -> bool {
let word = self.0.get();
let mask = (word + 0x7f7f_7f7f) & 0x8080_8080;
let numeric = !(word + 0x5050_5050) | (word + 0x4646_4646);
(numeric & mask) == 0
}

/// Makes the string all lowercase except for the first character,
/// which is made uppercase.
pub fn to_ascii_titlecase(self) -> Self {
Expand Down
9 changes: 8 additions & 1 deletion src/tinystr8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl TinyStr8 {
unsafe { Self(NonZeroU64::new_unchecked(result)) }
}

pub fn is_ascii_alpha(self) -> bool {
pub fn is_ascii_alphabetic(self) -> bool {
let word = self.0.get();
let mask = (word + 0x7f7f7f7f_7f7f7f7f) & 0x80808080_80808080;
let lower = word | 0x20202020_20202020;
Expand All @@ -60,6 +60,13 @@ impl TinyStr8 {
(alpha & numeric & mask) == 0
}

pub fn is_ascii_numeric(self) -> bool {
let word = self.0.get();
let mask = (word + 0x7f7f7f7f_7f7f7f7f) & 0x80808080_80808080;
let numeric = !(word + 0x50505050_50505050) | (word + 0x46464646_46464646);
(numeric & mask) == 0
}

pub fn to_ascii_titlecase(self) -> Self {
let word = self.0.get().to_le();
let mask =
Expand Down
51 changes: 36 additions & 15 deletions tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,23 @@ fn tiny4_nonascii() {
#[test]
fn tiny4_alpha() {
let s: TinyStr4 = "@aZ[".parse().unwrap();
assert!(!s.is_ascii_alpha());
assert!(!s.is_ascii_alphabetic());
assert!(!s.is_ascii_alphanumeric());
assert_eq!(s.to_ascii_uppercase().as_str(), "@AZ[");
assert_eq!(s.to_ascii_lowercase().as_str(), "@az[");

assert!("abYZ".parse::<TinyStr4>().unwrap().is_ascii_alpha());
assert!("abYZ".parse::<TinyStr4>().unwrap().is_ascii_alphabetic());
assert!("abYZ".parse::<TinyStr4>().unwrap().is_ascii_alphanumeric());
assert!("a123".parse::<TinyStr4>().unwrap().is_ascii_alphanumeric());
assert!(!"a123".parse::<TinyStr4>().unwrap().is_ascii_alpha());
assert!(!"a123".parse::<TinyStr4>().unwrap().is_ascii_alphabetic());
}

#[test]
fn tiny4_numeric() {
let s: TinyStr4 = "@aZ[".parse().unwrap();
assert!(!s.is_ascii_numeric());

assert!("0123".parse::<TinyStr4>().unwrap().is_ascii_numeric());
}

#[test]
Expand Down Expand Up @@ -172,29 +180,31 @@ fn tiny8_nonascii() {
#[test]
fn tiny8_alpha() {
let s: TinyStr8 = "@abcXYZ[".parse().unwrap();
assert!(!s.is_ascii_alpha());
assert!(!s.is_ascii_alphabetic());
assert!(!s.is_ascii_alphanumeric());
assert_eq!(s.to_ascii_uppercase().as_str(), "@ABCXYZ[");
assert_eq!(s.to_ascii_lowercase().as_str(), "@abcxyz[");

assert!("abcXYZ"
.parse::<TinyStr8>()
.unwrap()
.is_ascii_alpha());
assert!("abcXYZ".parse::<TinyStr8>().unwrap().is_ascii_alphabetic());
assert!("abcXYZ"
.parse::<TinyStr8>()
.unwrap()
.is_ascii_alphanumeric());
assert!(!"abc123"
.parse::<TinyStr8>()
.unwrap()
.is_ascii_alpha());
assert!(!"abc123".parse::<TinyStr8>().unwrap().is_ascii_alphabetic());
assert!("abc123"
.parse::<TinyStr8>()
.unwrap()
.is_ascii_alphanumeric());
}

#[test]
fn tiny8_numeric() {
let s: TinyStr8 = "@abcXYZ[".parse().unwrap();
assert!(!s.is_ascii_numeric());

assert!("01234567".parse::<TinyStr8>().unwrap().is_ascii_numeric());
}

#[test]
fn tiny8_titlecase() {
assert_eq!(
Expand Down Expand Up @@ -320,29 +330,40 @@ fn tiny16_nonascii() {
#[test]
fn tiny16_alpha() {
let s: TinyStr16 = "@abcdefgTUVWXYZ[".parse().unwrap();
assert!(!s.is_ascii_alpha());
assert!(!s.is_ascii_alphabetic());
assert!(!s.is_ascii_alphanumeric());
assert_eq!(s.to_ascii_uppercase().as_str(), "@ABCDEFGTUVWXYZ[");
assert_eq!(s.to_ascii_lowercase().as_str(), "@abcdefgtuvwxyz[");

assert!("abcdefgTUVWXYZ"
.parse::<TinyStr16>()
.unwrap()
.is_ascii_alpha());
.is_ascii_alphabetic());
assert!("abcdefgTUVWXYZ"
.parse::<TinyStr16>()
.unwrap()
.is_ascii_alphanumeric());
assert!(!"abcdefg0123456"
.parse::<TinyStr16>()
.unwrap()
.is_ascii_alpha());
.is_ascii_alphabetic());
assert!("abcdefgTUVWXYZ"
.parse::<TinyStr16>()
.unwrap()
.is_ascii_alphanumeric());
}

#[test]
fn tiny16_numeric() {
let s: TinyStr16 = "@abcdefgTUVWXYZ[".parse().unwrap();
assert!(!s.is_ascii_numeric());

assert!("0123456789"
.parse::<TinyStr16>()
.unwrap()
.is_ascii_numeric());
}

#[test]
fn tiny16_titlecase() {
assert_eq!(
Expand Down

0 comments on commit 72bbca7

Please sign in to comment.