2012-12-10 17:32:48 -08: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-02-14 10:10:06 -08:00
|
|
|
|
extern crate extra;
|
2011-03-24 12:11:32 +01:00
|
|
|
|
|
2013-05-24 19:35:29 -07:00
|
|
|
|
use std::str;
|
|
|
|
|
|
2013-02-01 19:43:17 -08:00
|
|
|
|
pub fn main() {
|
2011-07-27 14:19:39 +02:00
|
|
|
|
// Chars of 1, 2, 3, and 4 bytes
|
2013-09-03 19:24:12 -04:00
|
|
|
|
let chs: ~[char] = ~['e', 'é', '€', '\U00010000'];
|
2012-07-13 22:57:48 -07:00
|
|
|
|
let s: ~str = str::from_chars(chs);
|
2013-11-23 11:18:51 +01:00
|
|
|
|
let schs: ~[char] = s.chars().collect();
|
2011-03-24 12:11:32 +01:00
|
|
|
|
|
2013-06-10 00:44:58 +10:00
|
|
|
|
assert!(s.len() == 10u);
|
2013-06-11 21:37:22 +10:00
|
|
|
|
assert!(s.char_len() == 4u);
|
2013-06-11 00:49:19 +10:00
|
|
|
|
assert!(schs.len() == 4u);
|
|
|
|
|
assert!(str::from_chars(schs) == s);
|
2013-06-10 21:46:36 +10:00
|
|
|
|
assert!(s.char_at(0u) == 'e');
|
|
|
|
|
assert!(s.char_at(1u) == 'é');
|
2011-03-24 12:11:32 +01:00
|
|
|
|
|
2013-06-11 13:10:37 +10:00
|
|
|
|
assert!((str::is_utf8(s.as_bytes())));
|
2013-07-10 17:06:16 -04:00
|
|
|
|
// invalid prefix
|
2013-08-17 08:37:42 -07:00
|
|
|
|
assert!((!str::is_utf8([0x80_u8])));
|
2013-07-10 17:06:16 -04:00
|
|
|
|
// invalid 2 byte prefix
|
2013-08-17 08:37:42 -07:00
|
|
|
|
assert!((!str::is_utf8([0xc0_u8])));
|
|
|
|
|
assert!((!str::is_utf8([0xc0_u8, 0x10_u8])));
|
2013-07-10 17:06:16 -04:00
|
|
|
|
// invalid 3 byte prefix
|
2013-08-17 08:37:42 -07:00
|
|
|
|
assert!((!str::is_utf8([0xe0_u8])));
|
|
|
|
|
assert!((!str::is_utf8([0xe0_u8, 0x10_u8])));
|
|
|
|
|
assert!((!str::is_utf8([0xe0_u8, 0xff_u8, 0x10_u8])));
|
2013-07-10 17:06:16 -04:00
|
|
|
|
// invalid 4 byte prefix
|
2013-08-17 08:37:42 -07:00
|
|
|
|
assert!((!str::is_utf8([0xf0_u8])));
|
|
|
|
|
assert!((!str::is_utf8([0xf0_u8, 0x10_u8])));
|
|
|
|
|
assert!((!str::is_utf8([0xf0_u8, 0xff_u8, 0x10_u8])));
|
|
|
|
|
assert!((!str::is_utf8([0xf0_u8, 0xff_u8, 0xff_u8, 0x10_u8])));
|
2011-03-24 12:11:32 +01:00
|
|
|
|
|
2012-07-13 22:57:48 -07:00
|
|
|
|
let mut stack = ~"a×c€";
|
2013-06-11 12:20:47 +10:00
|
|
|
|
assert_eq!(stack.pop_char(), '€');
|
|
|
|
|
assert_eq!(stack.pop_char(), 'c');
|
2013-06-10 17:42:24 +10:00
|
|
|
|
stack.push_char('u');
|
2013-05-18 22:02:45 -04:00
|
|
|
|
assert!(stack == ~"a×u");
|
2013-06-11 12:20:47 +10:00
|
|
|
|
assert_eq!(stack.shift_char(), 'a');
|
|
|
|
|
assert_eq!(stack.shift_char(), '×');
|
|
|
|
|
stack.unshift_char('ß');
|
2013-05-18 22:02:45 -04:00
|
|
|
|
assert!(stack == ~"ßu");
|
2011-08-10 09:27:22 -07:00
|
|
|
|
}
|