1 use core::char::from_u32;
5 assert!(b"".is_ascii());
6 assert!(b"banana\0\x7F".is_ascii());
7 assert!(b"banana\0\x7F".iter().all(|b| b.is_ascii()));
8 assert!(!b"Vi\xe1\xbb\x87t Nam".is_ascii());
9 assert!(!b"Vi\xe1\xbb\x87t Nam".iter().all(|b| b.is_ascii()));
10 assert!(!b"\xe1\xbb\x87".iter().any(|b| b.is_ascii()));
12 assert!("".is_ascii());
13 assert!("banana\0\u{7F}".is_ascii());
14 assert!("banana\0\u{7F}".chars().all(|c| c.is_ascii()));
15 assert!(!"ประเทศไทย中华Việt Nam".chars().all(|c| c.is_ascii()));
16 assert!(!"ประเทศไทย中华ệ ".chars().any(|c| c.is_ascii()));
20 fn test_to_ascii_uppercase() {
21 assert_eq!("url()URL()uRl()ürl".to_ascii_uppercase(), "URL()URL()URL()üRL");
22 assert_eq!("hıKß".to_ascii_uppercase(), "HıKß");
25 let upper = if 'a' as u32 <= i && i <= 'z' as u32 { i + 'A' as u32 - 'a' as u32 }
27 assert_eq!((from_u32(i).unwrap()).to_string().to_ascii_uppercase(),
28 (from_u32(upper).unwrap()).to_string());
33 fn test_to_ascii_lowercase() {
34 assert_eq!("url()URL()uRl()Ürl".to_ascii_lowercase(), "url()url()url()Ürl");
35 // Dotted capital I, Kelvin sign, Sharp S.
36 assert_eq!("HİKß".to_ascii_lowercase(), "hİKß");
39 let lower = if 'A' as u32 <= i && i <= 'Z' as u32 { i + 'a' as u32 - 'A' as u32 }
41 assert_eq!((from_u32(i).unwrap()).to_string().to_ascii_lowercase(),
42 (from_u32(lower).unwrap()).to_string());
47 fn test_make_ascii_lower_case() {
49 ($from: expr, $to: expr) => {
52 x.make_ascii_lowercase();
64 test!(b"H\xc3\x89".to_vec(), b"h\xc3\x89");
65 test!("HİKß".to_string(), "hİKß");
70 fn test_make_ascii_upper_case() {
72 ($from: expr, $to: expr) => {
75 x.make_ascii_uppercase();
87 test!(b"h\xc3\xa9".to_vec(), b"H\xc3\xa9");
88 test!("hıKß".to_string(), "HıKß");
90 let mut x = "Hello".to_string();
91 x[..3].make_ascii_uppercase(); // Test IndexMut on String.
92 assert_eq!(x, "HELlo")
96 fn test_eq_ignore_ascii_case() {
97 assert!("url()URL()uRl()Ürl".eq_ignore_ascii_case("url()url()url()Ürl"));
98 assert!(!"Ürl".eq_ignore_ascii_case("ürl"));
99 // Dotted capital I, Kelvin sign, Sharp S.
100 assert!("HİKß".eq_ignore_ascii_case("hİKß"));
101 assert!(!"İ".eq_ignore_ascii_case("i"));
102 assert!(!"K".eq_ignore_ascii_case("k"));
103 assert!(!"ß".eq_ignore_ascii_case("s"));
106 let lower = if 'A' as u32 <= i && i <= 'Z' as u32 { i + 'a' as u32 - 'A' as u32 }
108 assert!((from_u32(i).unwrap()).to_string().eq_ignore_ascii_case(
109 &from_u32(lower).unwrap().to_string()));
114 fn inference_works() {
115 let x = "a".to_string();
116 x.eq_ignore_ascii_case("A");
119 // Shorthands used by the is_ascii_* tests.
120 macro_rules! assert_all {
121 ($what:ident, $($str:tt),+) => {{
123 for b in $str.chars() {
125 panic!("expected {}({}) but it isn't",
126 stringify!($what), b);
129 for b in $str.as_bytes().iter() {
131 panic!("expected {}(0x{:02x})) but it isn't",
132 stringify!($what), b);
137 ($what:ident, $($str:tt),+,) => (assert_all!($what,$($str),+))
139 macro_rules! assert_none {
140 ($what:ident, $($str:tt),+) => {{
142 for b in $str.chars() {
144 panic!("expected not-{}({}) but it is",
145 stringify!($what), b);
148 for b in $str.as_bytes().iter() {
150 panic!("expected not-{}(0x{:02x})) but it is",
151 stringify!($what), b);
156 ($what:ident, $($str:tt),+,) => (assert_none!($what,$($str),+))
160 fn test_is_ascii_alphabetic() {
161 assert_all!(is_ascii_alphabetic,
163 "abcdefghijklmnopqrstuvwxyz",
164 "ABCDEFGHIJKLMNOQPRSTUVWXYZ",
166 assert_none!(is_ascii_alphabetic,
168 "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~",
170 "\x00\x01\x02\x03\x04\x05\x06\x07",
171 "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f",
172 "\x10\x11\x12\x13\x14\x15\x16\x17",
173 "\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f",
179 fn test_is_ascii_uppercase() {
180 assert_all!(is_ascii_uppercase,
182 "ABCDEFGHIJKLMNOQPRSTUVWXYZ",
184 assert_none!(is_ascii_uppercase,
185 "abcdefghijklmnopqrstuvwxyz",
187 "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~",
189 "\x00\x01\x02\x03\x04\x05\x06\x07",
190 "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f",
191 "\x10\x11\x12\x13\x14\x15\x16\x17",
192 "\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f",
198 fn test_is_ascii_lowercase() {
199 assert_all!(is_ascii_lowercase,
200 "abcdefghijklmnopqrstuvwxyz",
202 assert_none!(is_ascii_lowercase,
203 "ABCDEFGHIJKLMNOQPRSTUVWXYZ",
205 "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~",
207 "\x00\x01\x02\x03\x04\x05\x06\x07",
208 "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f",
209 "\x10\x11\x12\x13\x14\x15\x16\x17",
210 "\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f",
216 fn test_is_ascii_alphanumeric() {
217 assert_all!(is_ascii_alphanumeric,
219 "abcdefghijklmnopqrstuvwxyz",
220 "ABCDEFGHIJKLMNOQPRSTUVWXYZ",
223 assert_none!(is_ascii_alphanumeric,
224 "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~",
226 "\x00\x01\x02\x03\x04\x05\x06\x07",
227 "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f",
228 "\x10\x11\x12\x13\x14\x15\x16\x17",
229 "\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f",
235 fn test_is_ascii_digit() {
236 assert_all!(is_ascii_digit,
240 assert_none!(is_ascii_digit,
241 "abcdefghijklmnopqrstuvwxyz",
242 "ABCDEFGHIJKLMNOQPRSTUVWXYZ",
243 "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~",
245 "\x00\x01\x02\x03\x04\x05\x06\x07",
246 "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f",
247 "\x10\x11\x12\x13\x14\x15\x16\x17",
248 "\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f",
254 fn test_is_ascii_hexdigit() {
255 assert_all!(is_ascii_hexdigit,
260 assert_none!(is_ascii_hexdigit,
261 "ghijklmnopqrstuvwxyz",
262 "GHIJKLMNOQPRSTUVWXYZ",
263 "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~",
265 "\x00\x01\x02\x03\x04\x05\x06\x07",
266 "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f",
267 "\x10\x11\x12\x13\x14\x15\x16\x17",
268 "\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f",
274 fn test_is_ascii_punctuation() {
275 assert_all!(is_ascii_punctuation,
277 "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~",
279 assert_none!(is_ascii_punctuation,
280 "abcdefghijklmnopqrstuvwxyz",
281 "ABCDEFGHIJKLMNOQPRSTUVWXYZ",
284 "\x00\x01\x02\x03\x04\x05\x06\x07",
285 "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f",
286 "\x10\x11\x12\x13\x14\x15\x16\x17",
287 "\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f",
293 fn test_is_ascii_graphic() {
294 assert_all!(is_ascii_graphic,
296 "abcdefghijklmnopqrstuvwxyz",
297 "ABCDEFGHIJKLMNOQPRSTUVWXYZ",
299 "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~",
301 assert_none!(is_ascii_graphic,
303 "\x00\x01\x02\x03\x04\x05\x06\x07",
304 "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f",
305 "\x10\x11\x12\x13\x14\x15\x16\x17",
306 "\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f",
312 fn test_is_ascii_whitespace() {
313 assert_all!(is_ascii_whitespace,
317 assert_none!(is_ascii_whitespace,
318 "abcdefghijklmnopqrstuvwxyz",
319 "ABCDEFGHIJKLMNOQPRSTUVWXYZ",
321 "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~",
322 "\x00\x01\x02\x03\x04\x05\x06\x07",
324 "\x10\x11\x12\x13\x14\x15\x16\x17",
325 "\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f",
331 fn test_is_ascii_control() {
332 assert_all!(is_ascii_control,
334 "\x00\x01\x02\x03\x04\x05\x06\x07",
335 "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f",
336 "\x10\x11\x12\x13\x14\x15\x16\x17",
337 "\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f",
340 assert_none!(is_ascii_control,
341 "abcdefghijklmnopqrstuvwxyz",
342 "ABCDEFGHIJKLMNOQPRSTUVWXYZ",
344 "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~",