summary refs log tree commit diff
path: root/src/test/run-pass/utf8_chars.rs
blob: db258d48f9f1c4a9568f8713f88edc8ebbd83cae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// 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.

extern mod extra;

use std::str;

pub fn main() {
    // Chars of 1, 2, 3, and 4 bytes
    let chs: ~[char] = ~['e', 'é', '€', '\U00010000'];
    let s: ~str = str::from_chars(chs);
    let schs: ~[char] = s.iter().collect();

    assert!(s.len() == 10u);
    assert!(s.char_len() == 4u);
    assert!(schs.len() == 4u);
    assert!(str::from_chars(schs) == s);
    assert!(s.char_at(0u) == 'e');
    assert!(s.char_at(1u) == 'é');

    assert!((str::is_utf8(s.as_bytes())));
    // invalid prefix
    assert!((!str::is_utf8([0x80_u8])));
    // invalid 2 byte prefix
    assert!((!str::is_utf8([0xc0_u8])));
    assert!((!str::is_utf8([0xc0_u8, 0x10_u8])));
    // invalid 3 byte prefix
    assert!((!str::is_utf8([0xe0_u8])));
    assert!((!str::is_utf8([0xe0_u8, 0x10_u8])));
    assert!((!str::is_utf8([0xe0_u8, 0xff_u8, 0x10_u8])));
    // invalid 4 byte prefix
    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])));

    let mut stack = ~"a×c€";
    assert_eq!(stack.pop_char(), '€');
    assert_eq!(stack.pop_char(), 'c');
    stack.push_char('u');
    assert!(stack == ~"a×u");
    assert_eq!(stack.shift_char(), 'a');
    assert_eq!(stack.shift_char(), '×');
    stack.unshift_char('ß');
    assert!(stack == ~"ßu");
}