2012-12-10 19:32:48 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
2014-07-21 17:57:14 -05:00
|
|
|
//
|
|
|
|
// ignore-lexer-test FIXME #15679
|
2012-12-10 19:32:48 -06:00
|
|
|
|
2013-05-24 21:35:29 -05:00
|
|
|
use std::str;
|
|
|
|
|
2013-02-01 21:43:17 -06:00
|
|
|
pub fn main() {
|
2011-07-27 07:19:39 -05:00
|
|
|
// Chars of 1, 2, 3, and 4 bytes
|
2014-03-05 17:28:08 -06:00
|
|
|
let chs: Vec<char> = vec!('e', 'é', '€', '\U00010000');
|
2015-01-02 01:53:35 -06:00
|
|
|
let s: String = chs.iter().cloned().collect();
|
|
|
|
let schs: Vec<char> = s.chars().collect();
|
2011-03-24 06:11:32 -05:00
|
|
|
|
2015-02-18 04:42:01 -06:00
|
|
|
assert!(s.len() == 10_usize);
|
|
|
|
assert!(s.chars().count() == 4_usize);
|
|
|
|
assert!(schs.len() == 4_usize);
|
2015-01-02 01:53:35 -06:00
|
|
|
assert!(schs.iter().cloned().collect::<String>() == s);
|
2015-02-18 04:42:01 -06:00
|
|
|
assert!(s.char_at(0_usize) == 'e');
|
|
|
|
assert!(s.char_at(1_usize) == 'é');
|
2011-03-24 06:11:32 -05:00
|
|
|
|
2015-01-02 01:53:35 -06:00
|
|
|
assert!((str::from_utf8(s.as_bytes()).is_ok()));
|
2013-07-10 16:06:16 -05:00
|
|
|
// invalid prefix
|
2015-01-02 01:53:35 -06:00
|
|
|
assert!((!str::from_utf8(&[0x80_u8]).is_ok()));
|
2013-07-10 16:06:16 -05:00
|
|
|
// invalid 2 byte prefix
|
2015-01-02 01:53:35 -06:00
|
|
|
assert!((!str::from_utf8(&[0xc0_u8]).is_ok()));
|
|
|
|
assert!((!str::from_utf8(&[0xc0_u8, 0x10_u8]).is_ok()));
|
2013-07-10 16:06:16 -05:00
|
|
|
// invalid 3 byte prefix
|
2015-01-02 01:53:35 -06:00
|
|
|
assert!((!str::from_utf8(&[0xe0_u8]).is_ok()));
|
|
|
|
assert!((!str::from_utf8(&[0xe0_u8, 0x10_u8]).is_ok()));
|
|
|
|
assert!((!str::from_utf8(&[0xe0_u8, 0xff_u8, 0x10_u8]).is_ok()));
|
2013-07-10 16:06:16 -05:00
|
|
|
// invalid 4 byte prefix
|
2015-01-02 01:53:35 -06:00
|
|
|
assert!((!str::from_utf8(&[0xf0_u8]).is_ok()));
|
|
|
|
assert!((!str::from_utf8(&[0xf0_u8, 0x10_u8]).is_ok()));
|
|
|
|
assert!((!str::from_utf8(&[0xf0_u8, 0xff_u8, 0x10_u8]).is_ok()));
|
|
|
|
assert!((!str::from_utf8(&[0xf0_u8, 0xff_u8, 0xff_u8, 0x10_u8]).is_ok()));
|
2011-08-10 11:27:22 -05:00
|
|
|
}
|