1 // Copyright 2012 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 // ignore-lexer-test FIXME #15679
16 // Chars of 1, 2, 3, and 4 bytes
17 let chs: Vec<char> = vec!('e', 'é', '€', '\U00010000');
18 let s: String = String::from_chars(chs.as_slice()).to_string();
19 let schs: Vec<char> = s.as_slice().chars().collect();
21 assert!(s.len() == 10u);
22 assert!(s.as_slice().char_len() == 4u);
23 assert!(schs.len() == 4u);
24 assert!(String::from_chars(schs.as_slice()) == s);
25 assert!(s.as_slice().char_at(0u) == 'e');
26 assert!(s.as_slice().char_at(1u) == 'é');
28 assert!((str::is_utf8(s.as_bytes())));
30 assert!((!str::is_utf8(&[0x80_u8])));
31 // invalid 2 byte prefix
32 assert!((!str::is_utf8(&[0xc0_u8])));
33 assert!((!str::is_utf8(&[0xc0_u8, 0x10_u8])));
34 // invalid 3 byte prefix
35 assert!((!str::is_utf8(&[0xe0_u8])));
36 assert!((!str::is_utf8(&[0xe0_u8, 0x10_u8])));
37 assert!((!str::is_utf8(&[0xe0_u8, 0xff_u8, 0x10_u8])));
38 // invalid 4 byte prefix
39 assert!((!str::is_utf8(&[0xf0_u8])));
40 assert!((!str::is_utf8(&[0xf0_u8, 0x10_u8])));
41 assert!((!str::is_utf8(&[0xf0_u8, 0xff_u8, 0x10_u8])));
42 assert!((!str::is_utf8(&[0xf0_u8, 0xff_u8, 0xff_u8, 0x10_u8])));