Skip to content

Commit cb29492

Browse files
committed
libunicode: optimize char functions for ascii characters
1 parent c509f79 commit cb29492

File tree

1 file changed

+33
-10
lines changed

1 file changed

+33
-10
lines changed

src/libunicode/u_char.rs

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,13 @@ use tables::{derived_property, property, general_category, conversions, charwidt
2020

2121
/// Returns whether the specified `char` is considered a Unicode alphabetic
2222
/// code point
23-
pub fn is_alphabetic(c: char) -> bool { derived_property::Alphabetic(c) }
23+
pub fn is_alphabetic(c: char) -> bool {
24+
match c {
25+
'a' .. 'z' | 'A' .. 'Z' => true,
26+
c if c > '\x7f' => derived_property::Alphabetic(c),
27+
_ => false
28+
}
29+
}
2430

2531
/// Returns whether the specified `char` satisfies the 'XID_Start' Unicode property
2632
///
@@ -44,15 +50,27 @@ pub fn is_XID_continue(c: char) -> bool { derived_property::XID_Continue(c) }
4450
/// This is defined according to the terms of the Unicode Derived Core Property 'Lowercase'.
4551
///
4652
#[inline]
47-
pub fn is_lowercase(c: char) -> bool { derived_property::Lowercase(c) }
53+
pub fn is_lowercase(c: char) -> bool {
54+
match c {
55+
'a' .. 'z' => true,
56+
c if c > '\x7f' => derived_property::Lowercase(c),
57+
_ => false
58+
}
59+
}
4860

4961
///
5062
/// Indicates whether a `char` is in upper case
5163
///
5264
/// This is defined according to the terms of the Unicode Derived Core Property 'Uppercase'.
5365
///
5466
#[inline]
55-
pub fn is_uppercase(c: char) -> bool { derived_property::Uppercase(c) }
67+
pub fn is_uppercase(c: char) -> bool {
68+
match c {
69+
'A' .. 'Z' => true,
70+
c if c > '\x7f' => derived_property::Uppercase(c),
71+
_ => false
72+
}
73+
}
5674

5775
///
5876
/// Indicates whether a `char` is whitespace
@@ -61,10 +79,11 @@ pub fn is_uppercase(c: char) -> bool { derived_property::Uppercase(c) }
6179
///
6280
#[inline]
6381
pub fn is_whitespace(c: char) -> bool {
64-
// As an optimization ASCII whitespace characters are checked separately
65-
c == ' '
66-
|| ('\x09' <= c && c <= '\x0d')
67-
|| property::White_Space(c)
82+
match c {
83+
' ' | '\x09' .. '\x0d' => true,
84+
c if c > '\x7f' => property::White_Space(c),
85+
_ => false
86+
}
6887
}
6988

7089
///
@@ -75,8 +94,8 @@ pub fn is_whitespace(c: char) -> bool {
7594
///
7695
#[inline]
7796
pub fn is_alphanumeric(c: char) -> bool {
78-
derived_property::Alphabetic(c)
79-
|| general_category::N(c)
97+
is_alphabetic(c)
98+
|| is_digit(c)
8099
}
81100

82101
///
@@ -91,7 +110,11 @@ pub fn is_control(c: char) -> bool { general_category::Cc(c) }
91110
/// Indicates whether the `char` is numeric (Nd, Nl, or No)
92111
#[inline]
93112
pub fn is_digit(c: char) -> bool {
94-
general_category::N(c)
113+
match c {
114+
'0' .. '9' => true,
115+
c if c > '\x7f' => general_category::N(c),
116+
_ => false
117+
}
95118
}
96119

97120
/// Convert a char to its uppercase equivalent

0 commit comments

Comments
 (0)