1pub const SURROGATE_MIN: u32 = 0xD800;
2pub const SURROGATE_MAX: u32 = 0xDFFF;
3pub const UNICODE_MAX: u32 = 0x10FFFF;
4pub const ERROR_UNICODE: &str = "invalid unicode";
5
6pub fn valid_unicode(r: u32) -> bool {
7 (0..SURROGATE_MIN).contains(&r) || (SURROGATE_MAX < r && r <= UNICODE_MAX)
8}
9
10pub fn encode(input_runes: &[u32]) -> Vec<u8> {
11 let mut output = Vec::with_capacity(input_runes.len());
12 for &r in input_runes {
13 if r < 0x80 {
14 output.push((r & 0xFF) as u8);
15 } else if r < 0x4000 {
16 output.push(((r >> 7) & 0xFF) as u8);
17 output.push(((0x80 | (r & 0x7F)) & 0xFF) as u8);
18 } else {
19 output.push(((r >> 14) & 0xFF) as u8);
20 output.push(((0x80 | ((r >> 7) & 0x7F)) & 0xFF) as u8);
21 output.push(((0x80 | (r & 0x7F)) & 0xFF) as u8);
22 }
23 }
24 output
25}
26
27pub fn encode_from_string(input_str: &str) -> Vec<u8> {
28 let runes: Vec<u32> = input_str.chars().map(|c| c as u32).collect();
29 encode(&runes)
30}
31
32pub fn encode_from_bytes(input_bytes: &[u8]) -> Result<Vec<u8>, String> {
33 let input_str = std::str::from_utf8(input_bytes).map_err(|e| e.to_string())?;
34 Ok(encode_from_string(input_str))
35}
36
37pub fn decode(input_bytes: &[u8]) -> Result<Vec<u32>, String> {
38 if input_bytes.is_empty() {
39 return Err(ERROR_UNICODE.to_string());
40 }
41 let mut start_idx = None;
42 for (i, &b) in input_bytes.iter().enumerate() {
43 if (b & 0x80) == 0 {
44 start_idx = Some(i);
45 break;
46 }
47 }
48 let start_idx = start_idx.ok_or(ERROR_UNICODE)?;
49 let mut output = Vec::new();
50 let mut r: u32 = 0;
51 for (i, &b) in input_bytes.iter().enumerate().skip(start_idx) {
52 let b = b as u32;
53 if (b >> 7) == 0 {
54 if i > start_idx {
55 if !valid_unicode(r) {
56 return Err(ERROR_UNICODE.to_string());
57 }
58 output.push(r);
59 }
60 r = b;
61 } else {
62 if r > (UNICODE_MAX >> 7) {
63 return Err(ERROR_UNICODE.to_string());
64 }
65 r = (r << 7) | (b & 0x7F);
66 }
67 }
68 if !valid_unicode(r) {
69 return Err(ERROR_UNICODE.to_string());
70 }
71 output.push(r);
72 Ok(output)
73}
74
75pub fn decode_to_string(input_bytes: &[u8]) -> Result<String, String> {
76 let runes = decode(input_bytes)?;
77 let chars: Result<Vec<char>, String> = runes
78 .iter()
79 .map(|&r| char::try_from(r).map_err(|e| e.to_string()))
80 .collect();
81 Ok(chars?.into_iter().collect())
82}
83
84pub fn decode_to_bytes(input_bytes: &[u8]) -> Result<Vec<u8>, String> {
85 let s = decode_to_string(input_bytes)?;
86 Ok(s.into_bytes())
87}