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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#![allow(dead_code)]
#![allow(clippy::transmute_int_to_char)]
use std::cell::RefCell;
use std::fmt::{Debug, Formatter};
use std::io::{Bytes, Read};
use std::mem::transmute;
use crate::common::*;
use crate::decoder_error;
const SINGLE_BYTE_MASK: u32 = 0b0111_1111;
const DOUBLE_BYTE_MASK: u32 = 0b0001_1111;
const TRIPLE_BYTE_MASK: u32 = 0b0000_1111;
const QUAD_BYTE_MASK: u32 = 0b0000_0111;
const FOLLOWING_BYTE_MASK: u32 = 0b0011_1111;
macro_rules! single_byte_sequence {
($byte : expr) => {
$byte >> 7 == 0b0000_0000
};
}
macro_rules! double_byte_sequence {
($byte : expr) => {
$byte >> 5 == 0b0000_0110
};
}
macro_rules! triple_byte_sequence {
($byte : expr) => {
$byte >> 4 == 0b0000_1110
};
}
macro_rules! quad_byte_sequence {
($byte : expr) => {
$byte >> 3 == 0b0001_1110
};
}
#[inline(always)]
fn decode_double(a: u32, b: u32) -> u32 {
(b & FOLLOWING_BYTE_MASK) | ((a & DOUBLE_BYTE_MASK) << 6)
}
#[inline(always)]
fn decode_triple(a: u32, b: u32, c: u32) -> u32 {
(c & FOLLOWING_BYTE_MASK) | ((b & FOLLOWING_BYTE_MASK) << 6) | ((a & TRIPLE_BYTE_MASK) << 12)
}
#[inline(always)]
fn decode_quad(a: u32, b: u32, c: u32, d: u32) -> u32 {
(d & FOLLOWING_BYTE_MASK)
| ((c & FOLLOWING_BYTE_MASK) << 6)
| ((b & FOLLOWING_BYTE_MASK) << 12)
| ((a & QUAD_BYTE_MASK) << 18)
}
pub struct Utf8Decoder<Reader: Read + Debug> {
input: RefCell<Bytes<Reader>>,
}
impl<Reader: Read + Debug> Utf8Decoder<Reader> {
pub fn new(r: Reader) -> Self {
Utf8Decoder { input: RefCell::new(r.bytes()) }
}
pub fn decode_next(&self) -> DecoderResult<char> {
let leading_byte = self.next_packed_byte()?;
unsafe {
if single_byte_sequence!(leading_byte) {
return Ok(transmute(leading_byte));
}
if double_byte_sequence!(leading_byte) {
return Ok(transmute(decode_double(
leading_byte,
self.next_packed_byte()?,
)));
}
if triple_byte_sequence!(leading_byte) {
return Ok(transmute(decode_triple(
leading_byte,
self.next_packed_byte()?,
self.next_packed_byte()?,
)));
}
if quad_byte_sequence!(leading_byte) {
return Ok(transmute(decode_quad(
leading_byte,
self.next_packed_byte()?,
self.next_packed_byte()?,
self.next_packed_byte()?,
)));
}
}
decoder_error!(
DecoderErrorCode::InvalidByteSequence,
"failed to decode any valid UTF-8"
)
}
#[inline(always)]
fn next_packed_byte(&self) -> DecoderResult<u32> {
match self.input.borrow_mut().next() {
Some(result) => match result {
Ok(b) => Ok(b as u32),
Err(_) => decoder_error!(DecoderErrorCode::StreamFailure, "failed to read next byte"),
},
None => decoder_error!(DecoderErrorCode::EndOfInput, "no more bytes available"),
}
}
}
impl<Reader: Read + Debug> Debug for Utf8Decoder<Reader> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "rdr: {:?}", self.input)
}
}
impl<Reader: Read + Debug> Iterator for Utf8Decoder<Reader> {
type Item = char;
fn next(&mut self) -> Option<Self::Item> {
match self.decode_next() {
Ok(c) => Some(c),
Err(_) => None,
}
}
}