forge
Explore
Log in

Don't you dare go Hollow

project_note

Clone
★ 0

0 watching0 forksRust3.6 MiBc49f5e7Tahinli1 month ago2 commits

utf16.rs139 lines, 3.6 KiBHistoryRawDownload
1pub fn count_utf16_len(utf8_str: &[u8]) -> usize {
2 str_lens(utf8_str).1
3}
4
5#[allow(dead_code)]
6fn count_utf16_len_slow(utf8_str: &[u8]) -> usize {
7 let mut utf16_count = 0;
8
9 let mut iter = utf8_str.iter();
10
11 while let Some(&byte) = iter.next() {
12 if byte & 0b1000_0000 == 0 {
13 utf16_count += 1;
14 } else if byte & 0b1110_0000 == 0b1100_0000 {
15 let _ = iter.next();
16
17 utf16_count += 1;
18 } else if byte & 0b1111_0000 == 0b1110_0000 {
19 let _ = iter.next();
20 let _ = iter.next();
21
22 utf16_count += 1;
23 } else if byte & 0b1111_1000 == 0b1111_0000 {
24 let u = (((byte & 0b0000_0111) as u32) << 18)
25 | (((iter.next().unwrap() & 0b0011_1111) as u32) << 12);
26
27 let _ = iter.next();
28 let _ = iter.next();
29 if u >= 0x10000 {
30 utf16_count += 2;
31 } else {
32 utf16_count += 1;
33 }
34 } else {
35 unreachable!()
36 }
37 }
38
39 utf16_count
40}
41
42// TODO: FIXME: Tests
43/// Count unicode chars in a utf8 string
44pub fn count_unicode_chars(s: &[u8]) -> usize {
45 str_lens(s).0
46}
47
48/// (unicode, utf16) lengths of valid UTF-8 by counting lead bytes; no per-char decode.
49/// (project_note patch)
50pub fn str_lens(s: &[u8]) -> (usize, usize) {
51 // Mostly-ASCII text (say code with one separator) skips its ASCII chunks.
52 let (mut unicode, mut wide) = (0usize, 0usize);
53 for c in s.chunks(4096) {
54 if c.is_ascii() {
55 unicode += c.len();
56 continue;
57 }
58 for &b in c {
59 unicode += ((b as i8) >= -0x40) as usize;
60 wide += (b >= 0xF0) as usize;
61 }
62 }
63 (unicode, unicode + wide)
64}
65
66#[cfg(test)]
67mod tests {
68 use super::*;
69
70 #[test]
71 fn test_ascii() {
72 let input = "Hello, world!";
73 let expected = 13;
74 assert_eq!(count_utf16_len(input.as_bytes()), expected);
75 }
76
77 #[test]
78 fn test_2_byte() {
79 let input = "Привет, мир!";
80 let expected = 12;
81 assert_eq!(count_utf16_len(input.as_bytes()), expected);
82 }
83
84 #[test]
85 fn test_3_byte() {
86 let input = "こんにちは世界";
87 let expected = 7;
88 assert_eq!(count_utf16_len(input.as_bytes()), expected);
89 }
90
91 #[test]
92 fn test_4_byte() {
93 let input = "👋🌍";
94 let expected = 4;
95 assert_eq!(count_utf16_len(input.as_bytes()), expected);
96 }
97
98 #[test]
99 fn test_crazy_emoji_bytes() {
100 let input = "👩‍👩‍👧‍👧👨‍👨‍👧";
101 let expected = 19;
102 assert_eq!(count_utf16_len(input.as_bytes()), expected);
103 }
104
105 #[test]
106 fn test_empty_string() {
107 let input = "";
108 let expected = 0;
109 assert_eq!(count_utf16_len(input.as_bytes()), expected);
110 }
111
112 #[test]
113 fn test_single_char() {
114 let input = "a";
115 let expected = 1;
116 assert_eq!(count_utf16_len(input.as_bytes()), expected);
117 }
118
119 #[test]
120 fn test_utf8_with_bom() {
121 let input = "\u{FEFF}Hello, world!"; // UTF-8 with BOM
122 let expected = 14;
123 assert_eq!(count_utf16_len(input.as_bytes()), expected);
124 }
125
126 #[test]
127 fn test_long_string() {
128 let input = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
129 let expected = 52;
130 assert_eq!(count_utf16_len(input.as_bytes()), expected);
131 }
132
133 #[test]
134 fn test_utf8_with_null_char() {
135 let input = "Hello\u{0000}world!";
136 let expected = 12;
137 assert_eq!(count_utf16_len(input.as_bytes()), expected);
138 }
139}