Use a custom callback to encode UCS-2 into UTF-8
(input: &[u16], mut output: F)
| 81 | |
| 82 | // Use a custom callback to encode UCS-2 into UTF-8 |
| 83 | pub fn dec_w<F>(input: &[u16], mut output: F) -> Result<usize> |
| 84 | where |
| 85 | F: FnMut(&[u8]) -> Result<()>, |
| 86 | { |
| 87 | let mut written = 0; |
| 88 | for ch in input.iter() |
| 89 | { |
| 90 | if (0x000..0x0080).contains(ch) |
| 91 | { |
| 92 | output(&[*ch as u8])?; |
| 93 | written += 1; |
| 94 | } |
| 95 | else if (0x0080..0x0800).contains(ch) |
| 96 | { |
| 97 | let first = 0b1100_0000 + ch.get_bits(6..11) as u8; |
| 98 | let last = 0b1000_0000 + ch.get_bits(0..6) as u8; |
| 99 | output(&[first, last])?; |
| 100 | written += 2; |
| 101 | } |
| 102 | else |
| 103 | { |
| 104 | let first = 0b1110_0000 + ch.get_bits(12..16) as u8; |
| 105 | let mid = 0b1000_0000 + ch.get_bits(6..12) as u8; |
| 106 | let last = 0b1000_0000 + ch.get_bits(0..6) as u8; |
| 107 | output(&[first, mid, last])?; |
| 108 | written += 3; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | Ok(written) |
| 113 | } |
| 114 | |
| 115 | |
| 116 | // Encode UTF-8 into UCS-2 |