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.
14 // Chars of 1, 2, 3, and 4 bytes
15 let chs: Vec<char> = vec!('e', 'é', '€', '\U00010000');
16 let s: StrBuf = str::from_chars(chs.as_slice()).to_strbuf();
17 let schs: Vec<char> = s.as_slice().chars().collect();
19 assert!(s.len() == 10u);
20 assert!(s.as_slice().char_len() == 4u);
21 assert!(schs.len() == 4u);
22 assert!(str::from_chars(schs.as_slice()).to_strbuf() == s);
23 assert!(s.as_slice().char_at(0u) == 'e');
24 assert!(s.as_slice().char_at(1u) == 'é');
26 assert!((str::is_utf8(s.as_bytes())));
28 assert!((!str::is_utf8([0x80_u8])));
29 // invalid 2 byte prefix
30 assert!((!str::is_utf8([0xc0_u8])));
31 assert!((!str::is_utf8([0xc0_u8, 0x10_u8])));
32 // invalid 3 byte prefix
33 assert!((!str::is_utf8([0xe0_u8])));
34 assert!((!str::is_utf8([0xe0_u8, 0x10_u8])));
35 assert!((!str::is_utf8([0xe0_u8, 0xff_u8, 0x10_u8])));
36 // invalid 4 byte prefix
37 assert!((!str::is_utf8([0xf0_u8])));
38 assert!((!str::is_utf8([0xf0_u8, 0x10_u8])));
39 assert!((!str::is_utf8([0xf0_u8, 0xff_u8, 0x10_u8])));
40 assert!((!str::is_utf8([0xf0_u8, 0xff_u8, 0xff_u8, 0x10_u8])));