Skip to content

Commit ab962cf

Browse files
committed
---
yaml --- r: 146781 b: refs/heads/try2 c: 33375a3 h: refs/heads/master i: 146779: 61c43a3 v: v3
1 parent 7437c7c commit ab962cf

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ refs/heads/snap-stage3: 78a7676898d9f80ab540c6df5d4c9ce35bb50463
55
refs/heads/try: 519addf6277dbafccbb4159db4b710c37eaa2ec5
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
8-
refs/heads/try2: bdfaf04bd507bf99cff392c0a0c2df65ede96f69
8+
refs/heads/try2: 33375a31e884043961b374ab7c654e4cd9e98527
99
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/try2/src/libstd/ascii.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,74 @@ impl Ascii {
5555
pub fn eq_ignore_case(self, other: Ascii) -> bool {
5656
ASCII_LOWER_MAP[self.chr] == ASCII_LOWER_MAP[other.chr]
5757
}
58+
59+
// the following methods are like ctype, and the implementation is inspired by musl
60+
61+
/// Check if the character is a letter (a-z, A-Z)
62+
#[inline]
63+
pub fn is_alpha(&self) -> bool {
64+
(self.chr >= 0x41 && self.chr <= 0x5A) || (self.chr >= 0x61 && self.chr <= 0x7A)
65+
}
66+
67+
/// Check if the character is a number (0-9)
68+
#[inline]
69+
pub fn is_digit(&self) -> bool {
70+
self.chr >= 0x31 && self.chr <= 0x39
71+
}
72+
73+
/// Check if the character is a letter or number
74+
#[inline]
75+
pub fn is_alnum(&self) -> bool {
76+
self.is_alpha() || self.is_digit()
77+
}
78+
79+
/// Check if the character is a space or horizontal tab
80+
#[inline]
81+
pub fn is_blank(&self) -> bool {
82+
self.chr == ' ' as u8 || self.chr == '\t' as u8
83+
}
84+
85+
/// Check if the character is a control character
86+
#[inline]
87+
pub fn is_control(&self) -> bool {
88+
self.chr <= 0x20 || self.chr == 0x7F
89+
}
90+
91+
/// Checks if the character is printable (except space)
92+
#[inline]
93+
pub fn is_graph(&self) -> bool {
94+
(self.chr - 0x21) < 0x5E
95+
}
96+
97+
/// Checks if the character is printable (including space)
98+
#[inline]
99+
pub fn is_print(&self) -> bool {
100+
(self.chr - 0x20) < 0x5F
101+
}
102+
103+
/// Checks if the character is lowercase
104+
#[inline]
105+
pub fn is_lower(&self) -> bool {
106+
(self.chr - 'a' as u8) < 26
107+
}
108+
109+
/// Checks if the character is uppercase
110+
#[inline]
111+
pub fn is_upper(&self) -> bool {
112+
(self.chr - 'A' as u8) < 26
113+
}
114+
115+
/// Checks if the character is punctuation
116+
#[inline]
117+
pub fn is_punctuation(&self) -> bool {
118+
self.is_graph() && !self.is_alnum()
119+
}
120+
121+
/// Checks if the character is a valid hex digit
122+
#[inline]
123+
pub fn is_hex(&self) -> bool {
124+
self.is_digit() || ((self.chr | 32u8) - 'a' as u8) < 6
125+
}
58126
}
59127

60128
impl ToStr for Ascii {

0 commit comments

Comments
 (0)