1 // Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use core::char::from_u32;
12 use std::ascii::AsciiExt;
16 assert!(b"".is_ascii());
17 assert!(b"banana\0\x7F".is_ascii());
18 assert!(b"banana\0\x7F".iter().all(|b| b.is_ascii()));
19 assert!(!b"Vi\xe1\xbb\x87t Nam".is_ascii());
20 assert!(!b"Vi\xe1\xbb\x87t Nam".iter().all(|b| b.is_ascii()));
21 assert!(!b"\xe1\xbb\x87".iter().any(|b| b.is_ascii()));
23 assert!("".is_ascii());
24 assert!("banana\0\u{7F}".is_ascii());
25 assert!("banana\0\u{7F}".chars().all(|c| c.is_ascii()));
26 assert!(!"ประเทศไทย中华Việt Nam".chars().all(|c| c.is_ascii()));
27 assert!(!"ประเทศไทย中华ệ ".chars().any(|c| c.is_ascii()));
31 fn test_to_ascii_uppercase() {
32 assert_eq!("url()URL()uRl()ürl".to_ascii_uppercase(), "URL()URL()URL()üRL");
33 assert_eq!("hıKß".to_ascii_uppercase(), "HıKß");
36 let upper = if 'a' as u32 <= i && i <= 'z' as u32 { i + 'A' as u32 - 'a' as u32 }
38 assert_eq!((from_u32(i).unwrap()).to_string().to_ascii_uppercase(),
39 (from_u32(upper).unwrap()).to_string());
44 fn test_to_ascii_lowercase() {
45 assert_eq!("url()URL()uRl()Ürl".to_ascii_lowercase(), "url()url()url()Ürl");
46 // Dotted capital I, Kelvin sign, Sharp S.
47 assert_eq!("HİKß".to_ascii_lowercase(), "hİKß");
50 let lower = if 'A' as u32 <= i && i <= 'Z' as u32 { i + 'a' as u32 - 'A' as u32 }
52 assert_eq!((from_u32(i).unwrap()).to_string().to_ascii_lowercase(),
53 (from_u32(lower).unwrap()).to_string());
58 fn test_make_ascii_lower_case() {
60 ($from: expr, $to: expr) => {
63 x.make_ascii_lowercase();
75 test!(b"H\xc3\x89".to_vec(), b"h\xc3\x89");
76 test!("HİKß".to_string(), "hİKß");
81 fn test_make_ascii_upper_case() {
83 ($from: expr, $to: expr) => {
86 x.make_ascii_uppercase();
98 test!(b"h\xc3\xa9".to_vec(), b"H\xc3\xa9");
99 test!("hıKß".to_string(), "HıKß");
101 let mut x = "Hello".to_string();
102 x[..3].make_ascii_uppercase(); // Test IndexMut on String.
103 assert_eq!(x, "HELlo")
107 fn test_eq_ignore_ascii_case() {
108 assert!("url()URL()uRl()Ürl".eq_ignore_ascii_case("url()url()url()Ürl"));
109 assert!(!"Ürl".eq_ignore_ascii_case("ürl"));
110 // Dotted capital I, Kelvin sign, Sharp S.
111 assert!("HİKß".eq_ignore_ascii_case("hİKß"));
112 assert!(!"İ".eq_ignore_ascii_case("i"));
113 assert!(!"K".eq_ignore_ascii_case("k"));
114 assert!(!"ß".eq_ignore_ascii_case("s"));
117 let lower = if 'A' as u32 <= i && i <= 'Z' as u32 { i + 'a' as u32 - 'A' as u32 }
119 assert!((from_u32(i).unwrap()).to_string().eq_ignore_ascii_case(
120 &from_u32(lower).unwrap().to_string()));
125 fn inference_works() {
126 let x = "a".to_string();
127 x.eq_ignore_ascii_case("A");
130 // Shorthands used by the is_ascii_* tests.
131 macro_rules! assert_all {
132 ($what:ident, $($str:tt),+) => {{
134 for b in $str.chars() {
136 panic!("expected {}({}) but it isn't",
137 stringify!($what), b);
140 for b in $str.as_bytes().iter() {
142 panic!("expected {}(0x{:02x})) but it isn't",
143 stringify!($what), b);
146 assert!($str.$what());
147 assert!($str.as_bytes().$what());
150 ($what:ident, $($str:tt),+,) => (assert_all!($what,$($str),+))
152 macro_rules! assert_none {
153 ($what:ident, $($str:tt),+) => {{
155 for b in $str.chars() {
157 panic!("expected not-{}({}) but it is",
158 stringify!($what), b);
161 for b in $str.as_bytes().iter() {
163 panic!("expected not-{}(0x{:02x})) but it is",
164 stringify!($what), b);
169 ($what:ident, $($str:tt),+,) => (assert_none!($what,$($str),+))
173 fn test_is_ascii_alphabetic() {
174 assert_all!(is_ascii_alphabetic,
176 "abcdefghijklmnopqrstuvwxyz",
177 "ABCDEFGHIJKLMNOQPRSTUVWXYZ",
179 assert_none!(is_ascii_alphabetic,
181 "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~",
183 "\x00\x01\x02\x03\x04\x05\x06\x07",
184 "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f",
185 "\x10\x11\x12\x13\x14\x15\x16\x17",
186 "\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f",
192 fn test_is_ascii_uppercase() {
193 assert_all!(is_ascii_uppercase,
195 "ABCDEFGHIJKLMNOQPRSTUVWXYZ",
197 assert_none!(is_ascii_uppercase,
198 "abcdefghijklmnopqrstuvwxyz",
200 "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~",
202 "\x00\x01\x02\x03\x04\x05\x06\x07",
203 "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f",
204 "\x10\x11\x12\x13\x14\x15\x16\x17",
205 "\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f",
211 fn test_is_ascii_lowercase() {
212 assert_all!(is_ascii_lowercase,
213 "abcdefghijklmnopqrstuvwxyz",
215 assert_none!(is_ascii_lowercase,
216 "ABCDEFGHIJKLMNOQPRSTUVWXYZ",
218 "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~",
220 "\x00\x01\x02\x03\x04\x05\x06\x07",
221 "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f",
222 "\x10\x11\x12\x13\x14\x15\x16\x17",
223 "\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f",
229 fn test_is_ascii_alphanumeric() {
230 assert_all!(is_ascii_alphanumeric,
232 "abcdefghijklmnopqrstuvwxyz",
233 "ABCDEFGHIJKLMNOQPRSTUVWXYZ",
236 assert_none!(is_ascii_alphanumeric,
237 "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~",
239 "\x00\x01\x02\x03\x04\x05\x06\x07",
240 "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f",
241 "\x10\x11\x12\x13\x14\x15\x16\x17",
242 "\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f",
248 fn test_is_ascii_digit() {
249 assert_all!(is_ascii_digit,
253 assert_none!(is_ascii_digit,
254 "abcdefghijklmnopqrstuvwxyz",
255 "ABCDEFGHIJKLMNOQPRSTUVWXYZ",
256 "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~",
258 "\x00\x01\x02\x03\x04\x05\x06\x07",
259 "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f",
260 "\x10\x11\x12\x13\x14\x15\x16\x17",
261 "\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f",
267 fn test_is_ascii_hexdigit() {
268 assert_all!(is_ascii_hexdigit,
273 assert_none!(is_ascii_hexdigit,
274 "ghijklmnopqrstuvwxyz",
275 "GHIJKLMNOQPRSTUVWXYZ",
276 "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~",
278 "\x00\x01\x02\x03\x04\x05\x06\x07",
279 "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f",
280 "\x10\x11\x12\x13\x14\x15\x16\x17",
281 "\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f",
287 fn test_is_ascii_punctuation() {
288 assert_all!(is_ascii_punctuation,
290 "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~",
292 assert_none!(is_ascii_punctuation,
293 "abcdefghijklmnopqrstuvwxyz",
294 "ABCDEFGHIJKLMNOQPRSTUVWXYZ",
297 "\x00\x01\x02\x03\x04\x05\x06\x07",
298 "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f",
299 "\x10\x11\x12\x13\x14\x15\x16\x17",
300 "\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f",
306 fn test_is_ascii_graphic() {
307 assert_all!(is_ascii_graphic,
309 "abcdefghijklmnopqrstuvwxyz",
310 "ABCDEFGHIJKLMNOQPRSTUVWXYZ",
312 "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~",
314 assert_none!(is_ascii_graphic,
316 "\x00\x01\x02\x03\x04\x05\x06\x07",
317 "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f",
318 "\x10\x11\x12\x13\x14\x15\x16\x17",
319 "\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f",
325 fn test_is_ascii_whitespace() {
326 assert_all!(is_ascii_whitespace,
330 assert_none!(is_ascii_whitespace,
331 "abcdefghijklmnopqrstuvwxyz",
332 "ABCDEFGHIJKLMNOQPRSTUVWXYZ",
334 "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~",
335 "\x00\x01\x02\x03\x04\x05\x06\x07",
337 "\x10\x11\x12\x13\x14\x15\x16\x17",
338 "\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f",
344 fn test_is_ascii_control() {
345 assert_all!(is_ascii_control,
347 "\x00\x01\x02\x03\x04\x05\x06\x07",
348 "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f",
349 "\x10\x11\x12\x13\x14\x15\x16\x17",
350 "\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f",
353 assert_none!(is_ascii_control,
354 "abcdefghijklmnopqrstuvwxyz",
355 "ABCDEFGHIJKLMNOQPRSTUVWXYZ",
357 "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~",