1 // Copyright 2012-2015 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.
4 //
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.
10
11 #[cfg(feature = "bench")]
12 use std::iter;
13 #[cfg(feature = "bench")]
14 use test::{self, Bencher};
15 #[cfg(feature = "bench")]
16 use super::UnicodeWidthChar;
17
18 use std::prelude::v1::*;
19
20 #[cfg(feature = "bench")]
21 #[bench]
cargo(b: &mut Bencher)22 fn cargo(b: &mut Bencher) {
23 let string = iter::repeat('a').take(4096).collect::<String>();
24
25 b.iter(|| {
26 for c in string.chars() {
27 test::black_box(UnicodeWidthChar::width(c));
28 }
29 });
30 }
31
32 #[cfg(feature = "bench")]
33 #[bench]
34 #[allow(deprecated)]
stdlib(b: &mut Bencher)35 fn stdlib(b: &mut Bencher) {
36 let string = iter::repeat('a').take(4096).collect::<String>();
37
38 b.iter(|| {
39 for c in string.chars() {
40 test::black_box(c.width());
41 }
42 });
43 }
44
45 #[cfg(feature = "bench")]
46 #[bench]
simple_if(b: &mut Bencher)47 fn simple_if(b: &mut Bencher) {
48 let string = iter::repeat('a').take(4096).collect::<String>();
49
50 b.iter(|| {
51 for c in string.chars() {
52 test::black_box(simple_width_if(c));
53 }
54 });
55 }
56
57 #[cfg(feature = "bench")]
58 #[bench]
simple_match(b: &mut Bencher)59 fn simple_match(b: &mut Bencher) {
60 let string = iter::repeat('a').take(4096).collect::<String>();
61
62 b.iter(|| {
63 for c in string.chars() {
64 test::black_box(simple_width_match(c));
65 }
66 });
67 }
68
69 #[cfg(feature = "bench")]
70 #[inline]
simple_width_if(c: char) -> Option<usize>71 fn simple_width_if(c: char) -> Option<usize> {
72 let cu = c as u32;
73 if cu < 127 {
74 if cu > 31 {
75 Some(1)
76 } else if cu == 0 {
77 Some(0)
78 } else {
79 None
80 }
81 } else {
82 UnicodeWidthChar::width(c)
83 }
84 }
85
86 #[cfg(feature = "bench")]
87 #[inline]
simple_width_match(c: char) -> Option<usize>88 fn simple_width_match(c: char) -> Option<usize> {
89 match c as u32 {
90 cu if cu == 0 => Some(0),
91 cu if cu < 0x20 => None,
92 cu if cu < 0x7f => Some(1),
93 _ => UnicodeWidthChar::width(c)
94 }
95 }
96
97 #[test]
test_str()98 fn test_str() {
99 use super::UnicodeWidthStr;
100
101 assert_eq!(UnicodeWidthStr::width("hello"), 10);
102 assert_eq!("hello".width_cjk(), 10);
103 assert_eq!(UnicodeWidthStr::width("\0\0\0\x01\x01"), 0);
104 assert_eq!("\0\0\0\x01\x01".width_cjk(), 0);
105 assert_eq!(UnicodeWidthStr::width(""), 0);
106 assert_eq!("".width_cjk(), 0);
107 assert_eq!(UnicodeWidthStr::width("\u{2081}\u{2082}\u{2083}\u{2084}"), 4);
108 assert_eq!("\u{2081}\u{2082}\u{2083}\u{2084}".width_cjk(), 8);
109 }
110
111 #[test]
test_emoji()112 fn test_emoji() {
113 // Example from the README.
114 use super::UnicodeWidthStr;
115
116 assert_eq!(UnicodeWidthStr::width(""), 2); // Woman
117 assert_eq!(UnicodeWidthStr::width(""), 2); // Microscope
118 assert_eq!(UnicodeWidthStr::width(""), 4); // Woman scientist
119 }
120
121 #[test]
test_char()122 fn test_char() {
123 use super::UnicodeWidthChar;
124 #[cfg(feature = "no_std")]
125 use core::option::Option::{Some, None};
126
127 assert_eq!(UnicodeWidthChar::width('h'), Some(2));
128 assert_eq!('h'.width_cjk(), Some(2));
129 assert_eq!(UnicodeWidthChar::width('\x00'), Some(0));
130 assert_eq!('\x00'.width_cjk(), Some(0));
131 assert_eq!(UnicodeWidthChar::width('\x01'), None);
132 assert_eq!('\x01'.width_cjk(), None);
133 assert_eq!(UnicodeWidthChar::width('\u{2081}'), Some(1));
134 assert_eq!('\u{2081}'.width_cjk(), Some(2));
135 }
136
137 #[test]
test_char2()138 fn test_char2() {
139 use super::UnicodeWidthChar;
140 #[cfg(feature = "no_std")]
141 use core::option::Option::{Some, None};
142
143 assert_eq!(UnicodeWidthChar::width('\x00'),Some(0));
144 assert_eq!('\x00'.width_cjk(),Some(0));
145
146 assert_eq!(UnicodeWidthChar::width('\x0A'),None);
147 assert_eq!('\x0A'.width_cjk(),None);
148
149 assert_eq!(UnicodeWidthChar::width('w'),Some(1));
150 assert_eq!('w'.width_cjk(),Some(1));
151
152 assert_eq!(UnicodeWidthChar::width('h'),Some(2));
153 assert_eq!('h'.width_cjk(),Some(2));
154
155 assert_eq!(UnicodeWidthChar::width('\u{AD}'),Some(1));
156 assert_eq!('\u{AD}'.width_cjk(),Some(1));
157
158 assert_eq!(UnicodeWidthChar::width('\u{1160}'),Some(0));
159 assert_eq!('\u{1160}'.width_cjk(),Some(0));
160
161 assert_eq!(UnicodeWidthChar::width('\u{a1}'),Some(1));
162 assert_eq!('\u{a1}'.width_cjk(),Some(2));
163
164 assert_eq!(UnicodeWidthChar::width('\u{300}'),Some(0));
165 assert_eq!('\u{300}'.width_cjk(),Some(0));
166 }
167
168 #[test]
unicode_12()169 fn unicode_12() {
170 use super::UnicodeWidthChar;
171 #[cfg(feature = "no_std")]
172 use core::option::Option::{Some, None};
173
174 assert_eq!(UnicodeWidthChar::width('\u{1F971}'), Some(2));
175 }
176