/rust/registry/src/index.crates.io-6f17d22bba15001f/encoding_rs-0.8.35/src/variant.rs
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright Mozilla Foundation. See the COPYRIGHT |
2 | | // file at the top-level directory of this distribution. |
3 | | // |
4 | | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
5 | | // https://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
6 | | // <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your |
7 | | // option. This file may not be copied, modified, or distributed |
8 | | // except according to those terms. |
9 | | |
10 | | // THIS IS A GENERATED FILE. PLEASE DO NOT EDIT. |
11 | | // Instead, please regenerate using generate-encoding-data.py |
12 | | |
13 | | //! This module provides enums that wrap the various decoders and encoders. |
14 | | //! The purpose is to make `Decoder` and `Encoder` `Sized` by writing the |
15 | | //! dispatch explicitly for a finite set of specialized decoders and encoders. |
16 | | //! Unfortunately, this means the compiler doesn't generate the dispatch code |
17 | | //! and it has to be written here instead. |
18 | | //! |
19 | | //! The purpose of making `Decoder` and `Encoder` `Sized` is to allow stack |
20 | | //! allocation in Rust code, including the convenience methods on `Encoding`. |
21 | | |
22 | | use super::*; |
23 | | use big5::*; |
24 | | use euc_jp::*; |
25 | | use euc_kr::*; |
26 | | use gb18030::*; |
27 | | use iso_2022_jp::*; |
28 | | use replacement::*; |
29 | | use shift_jis::*; |
30 | | use single_byte::*; |
31 | | use utf_16::*; |
32 | | use utf_8::*; |
33 | | use x_user_defined::*; |
34 | | |
35 | | pub enum VariantDecoder { |
36 | | SingleByte(SingleByteDecoder), |
37 | | Utf8(Utf8Decoder), |
38 | | Gb18030(Gb18030Decoder), |
39 | | Big5(Big5Decoder), |
40 | | EucJp(EucJpDecoder), |
41 | | Iso2022Jp(Iso2022JpDecoder), |
42 | | ShiftJis(ShiftJisDecoder), |
43 | | EucKr(EucKrDecoder), |
44 | | Replacement(ReplacementDecoder), |
45 | | UserDefined(UserDefinedDecoder), |
46 | | Utf16(Utf16Decoder), |
47 | | } |
48 | | |
49 | | impl VariantDecoder { |
50 | 0 | pub fn max_utf16_buffer_length(&self, byte_length: usize) -> Option<usize> { |
51 | 0 | match *self { |
52 | 0 | VariantDecoder::SingleByte(ref v) => v.max_utf16_buffer_length(byte_length), |
53 | 0 | VariantDecoder::Utf8(ref v) => v.max_utf16_buffer_length(byte_length), |
54 | 0 | VariantDecoder::Gb18030(ref v) => v.max_utf16_buffer_length(byte_length), |
55 | 0 | VariantDecoder::Big5(ref v) => v.max_utf16_buffer_length(byte_length), |
56 | 0 | VariantDecoder::EucJp(ref v) => v.max_utf16_buffer_length(byte_length), |
57 | 0 | VariantDecoder::Iso2022Jp(ref v) => v.max_utf16_buffer_length(byte_length), |
58 | 0 | VariantDecoder::ShiftJis(ref v) => v.max_utf16_buffer_length(byte_length), |
59 | 0 | VariantDecoder::EucKr(ref v) => v.max_utf16_buffer_length(byte_length), |
60 | 0 | VariantDecoder::Replacement(ref v) => v.max_utf16_buffer_length(byte_length), |
61 | 0 | VariantDecoder::UserDefined(ref v) => v.max_utf16_buffer_length(byte_length), |
62 | 0 | VariantDecoder::Utf16(ref v) => v.max_utf16_buffer_length(byte_length), |
63 | | } |
64 | 0 | } |
65 | | |
66 | 0 | pub fn max_utf8_buffer_length_without_replacement(&self, byte_length: usize) -> Option<usize> { |
67 | 0 | match *self { |
68 | 0 | VariantDecoder::SingleByte(ref v) => { |
69 | 0 | v.max_utf8_buffer_length_without_replacement(byte_length) |
70 | | } |
71 | 0 | VariantDecoder::Utf8(ref v) => { |
72 | 0 | v.max_utf8_buffer_length_without_replacement(byte_length) |
73 | | } |
74 | 0 | VariantDecoder::Gb18030(ref v) => { |
75 | 0 | v.max_utf8_buffer_length_without_replacement(byte_length) |
76 | | } |
77 | 0 | VariantDecoder::Big5(ref v) => { |
78 | 0 | v.max_utf8_buffer_length_without_replacement(byte_length) |
79 | | } |
80 | 0 | VariantDecoder::EucJp(ref v) => { |
81 | 0 | v.max_utf8_buffer_length_without_replacement(byte_length) |
82 | | } |
83 | 0 | VariantDecoder::Iso2022Jp(ref v) => { |
84 | 0 | v.max_utf8_buffer_length_without_replacement(byte_length) |
85 | | } |
86 | 0 | VariantDecoder::ShiftJis(ref v) => { |
87 | 0 | v.max_utf8_buffer_length_without_replacement(byte_length) |
88 | | } |
89 | 0 | VariantDecoder::EucKr(ref v) => { |
90 | 0 | v.max_utf8_buffer_length_without_replacement(byte_length) |
91 | | } |
92 | 0 | VariantDecoder::Replacement(ref v) => { |
93 | 0 | v.max_utf8_buffer_length_without_replacement(byte_length) |
94 | | } |
95 | 0 | VariantDecoder::UserDefined(ref v) => { |
96 | 0 | v.max_utf8_buffer_length_without_replacement(byte_length) |
97 | | } |
98 | 0 | VariantDecoder::Utf16(ref v) => { |
99 | 0 | v.max_utf8_buffer_length_without_replacement(byte_length) |
100 | | } |
101 | | } |
102 | 0 | } |
103 | | |
104 | 0 | pub fn max_utf8_buffer_length(&self, byte_length: usize) -> Option<usize> { |
105 | 0 | match *self { |
106 | 0 | VariantDecoder::SingleByte(ref v) => v.max_utf8_buffer_length(byte_length), |
107 | 0 | VariantDecoder::Utf8(ref v) => v.max_utf8_buffer_length(byte_length), |
108 | 0 | VariantDecoder::Gb18030(ref v) => v.max_utf8_buffer_length(byte_length), |
109 | 0 | VariantDecoder::Big5(ref v) => v.max_utf8_buffer_length(byte_length), |
110 | 0 | VariantDecoder::EucJp(ref v) => v.max_utf8_buffer_length(byte_length), |
111 | 0 | VariantDecoder::Iso2022Jp(ref v) => v.max_utf8_buffer_length(byte_length), |
112 | 0 | VariantDecoder::ShiftJis(ref v) => v.max_utf8_buffer_length(byte_length), |
113 | 0 | VariantDecoder::EucKr(ref v) => v.max_utf8_buffer_length(byte_length), |
114 | 0 | VariantDecoder::Replacement(ref v) => v.max_utf8_buffer_length(byte_length), |
115 | 0 | VariantDecoder::UserDefined(ref v) => v.max_utf8_buffer_length(byte_length), |
116 | 0 | VariantDecoder::Utf16(ref v) => v.max_utf8_buffer_length(byte_length), |
117 | | } |
118 | 0 | } |
119 | | |
120 | 0 | pub fn decode_to_utf16_raw( |
121 | 0 | &mut self, |
122 | 0 | src: &[u8], |
123 | 0 | dst: &mut [u16], |
124 | 0 | last: bool, |
125 | 0 | ) -> (DecoderResult, usize, usize) { |
126 | 0 | match *self { |
127 | 0 | VariantDecoder::SingleByte(ref mut v) => v.decode_to_utf16_raw(src, dst, last), |
128 | 0 | VariantDecoder::Utf8(ref mut v) => v.decode_to_utf16_raw(src, dst, last), |
129 | 0 | VariantDecoder::Gb18030(ref mut v) => v.decode_to_utf16_raw(src, dst, last), |
130 | 0 | VariantDecoder::Big5(ref mut v) => v.decode_to_utf16_raw(src, dst, last), |
131 | 0 | VariantDecoder::EucJp(ref mut v) => v.decode_to_utf16_raw(src, dst, last), |
132 | 0 | VariantDecoder::Iso2022Jp(ref mut v) => v.decode_to_utf16_raw(src, dst, last), |
133 | 0 | VariantDecoder::ShiftJis(ref mut v) => v.decode_to_utf16_raw(src, dst, last), |
134 | 0 | VariantDecoder::EucKr(ref mut v) => v.decode_to_utf16_raw(src, dst, last), |
135 | 0 | VariantDecoder::Replacement(ref mut v) => v.decode_to_utf16_raw(src, dst, last), |
136 | 0 | VariantDecoder::UserDefined(ref mut v) => v.decode_to_utf16_raw(src, dst, last), |
137 | 0 | VariantDecoder::Utf16(ref mut v) => v.decode_to_utf16_raw(src, dst, last), |
138 | | } |
139 | 0 | } |
140 | | |
141 | 0 | pub fn decode_to_utf8_raw( |
142 | 0 | &mut self, |
143 | 0 | src: &[u8], |
144 | 0 | dst: &mut [u8], |
145 | 0 | last: bool, |
146 | 0 | ) -> (DecoderResult, usize, usize) { |
147 | 0 | match *self { |
148 | 0 | VariantDecoder::SingleByte(ref mut v) => v.decode_to_utf8_raw(src, dst, last), |
149 | 0 | VariantDecoder::Utf8(ref mut v) => v.decode_to_utf8_raw(src, dst, last), |
150 | 0 | VariantDecoder::Gb18030(ref mut v) => v.decode_to_utf8_raw(src, dst, last), |
151 | 0 | VariantDecoder::Big5(ref mut v) => v.decode_to_utf8_raw(src, dst, last), |
152 | 0 | VariantDecoder::EucJp(ref mut v) => v.decode_to_utf8_raw(src, dst, last), |
153 | 0 | VariantDecoder::Iso2022Jp(ref mut v) => v.decode_to_utf8_raw(src, dst, last), |
154 | 0 | VariantDecoder::ShiftJis(ref mut v) => v.decode_to_utf8_raw(src, dst, last), |
155 | 0 | VariantDecoder::EucKr(ref mut v) => v.decode_to_utf8_raw(src, dst, last), |
156 | 0 | VariantDecoder::Replacement(ref mut v) => v.decode_to_utf8_raw(src, dst, last), |
157 | 0 | VariantDecoder::UserDefined(ref mut v) => v.decode_to_utf8_raw(src, dst, last), |
158 | 0 | VariantDecoder::Utf16(ref mut v) => v.decode_to_utf8_raw(src, dst, last), |
159 | | } |
160 | 0 | } |
161 | | |
162 | 0 | pub fn latin1_byte_compatible_up_to(&self, buffer: &[u8]) -> Option<usize> { |
163 | 0 | match *self { |
164 | 0 | VariantDecoder::SingleByte(ref v) => { |
165 | 0 | return Some(v.latin1_byte_compatible_up_to(buffer)); |
166 | | } |
167 | 0 | VariantDecoder::Utf8(ref v) => { |
168 | 0 | if !v.in_neutral_state() { |
169 | 0 | return None; |
170 | 0 | } |
171 | | } |
172 | 0 | VariantDecoder::Gb18030(ref v) => { |
173 | 0 | if !v.in_neutral_state() { |
174 | 0 | return None; |
175 | 0 | } |
176 | | } |
177 | 0 | VariantDecoder::Big5(ref v) => { |
178 | 0 | if !v.in_neutral_state() { |
179 | 0 | return None; |
180 | 0 | } |
181 | | } |
182 | 0 | VariantDecoder::EucJp(ref v) => { |
183 | 0 | if !v.in_neutral_state() { |
184 | 0 | return None; |
185 | 0 | } |
186 | | } |
187 | 0 | VariantDecoder::Iso2022Jp(ref v) => { |
188 | 0 | if v.in_neutral_state() { |
189 | 0 | return Some(Encoding::iso_2022_jp_ascii_valid_up_to(buffer)); |
190 | 0 | } |
191 | 0 | return None; |
192 | | } |
193 | 0 | VariantDecoder::ShiftJis(ref v) => { |
194 | 0 | if !v.in_neutral_state() { |
195 | 0 | return None; |
196 | 0 | } |
197 | | } |
198 | 0 | VariantDecoder::EucKr(ref v) => { |
199 | 0 | if !v.in_neutral_state() { |
200 | 0 | return None; |
201 | 0 | } |
202 | | } |
203 | 0 | VariantDecoder::UserDefined(_) => {} |
204 | | VariantDecoder::Replacement(_) | VariantDecoder::Utf16(_) => { |
205 | 0 | return None; |
206 | | } |
207 | | }; |
208 | 0 | Some(Encoding::ascii_valid_up_to(buffer)) |
209 | 0 | } |
210 | | } |
211 | | |
212 | | pub enum VariantEncoder { |
213 | | SingleByte(SingleByteEncoder), |
214 | | Utf8(Utf8Encoder), |
215 | | Gb18030(Gb18030Encoder), |
216 | | Big5(Big5Encoder), |
217 | | EucJp(EucJpEncoder), |
218 | | Iso2022Jp(Iso2022JpEncoder), |
219 | | ShiftJis(ShiftJisEncoder), |
220 | | EucKr(EucKrEncoder), |
221 | | UserDefined(UserDefinedEncoder), |
222 | | } |
223 | | |
224 | | impl VariantEncoder { |
225 | 0 | pub fn has_pending_state(&self) -> bool { |
226 | 0 | match *self { |
227 | 0 | VariantEncoder::Iso2022Jp(ref v) => v.has_pending_state(), |
228 | 0 | _ => false, |
229 | | } |
230 | 0 | } |
231 | 0 | pub fn max_buffer_length_from_utf16_without_replacement( |
232 | 0 | &self, |
233 | 0 | u16_length: usize, |
234 | 0 | ) -> Option<usize> { |
235 | 0 | match *self { |
236 | 0 | VariantEncoder::SingleByte(ref v) => { |
237 | 0 | v.max_buffer_length_from_utf16_without_replacement(u16_length) |
238 | | } |
239 | 0 | VariantEncoder::Utf8(ref v) => { |
240 | 0 | v.max_buffer_length_from_utf16_without_replacement(u16_length) |
241 | | } |
242 | 0 | VariantEncoder::Gb18030(ref v) => { |
243 | 0 | v.max_buffer_length_from_utf16_without_replacement(u16_length) |
244 | | } |
245 | 0 | VariantEncoder::Big5(ref v) => { |
246 | 0 | v.max_buffer_length_from_utf16_without_replacement(u16_length) |
247 | | } |
248 | 0 | VariantEncoder::EucJp(ref v) => { |
249 | 0 | v.max_buffer_length_from_utf16_without_replacement(u16_length) |
250 | | } |
251 | 0 | VariantEncoder::Iso2022Jp(ref v) => { |
252 | 0 | v.max_buffer_length_from_utf16_without_replacement(u16_length) |
253 | | } |
254 | 0 | VariantEncoder::ShiftJis(ref v) => { |
255 | 0 | v.max_buffer_length_from_utf16_without_replacement(u16_length) |
256 | | } |
257 | 0 | VariantEncoder::EucKr(ref v) => { |
258 | 0 | v.max_buffer_length_from_utf16_without_replacement(u16_length) |
259 | | } |
260 | 0 | VariantEncoder::UserDefined(ref v) => { |
261 | 0 | v.max_buffer_length_from_utf16_without_replacement(u16_length) |
262 | | } |
263 | | } |
264 | 0 | } |
265 | | |
266 | 0 | pub fn max_buffer_length_from_utf8_without_replacement( |
267 | 0 | &self, |
268 | 0 | byte_length: usize, |
269 | 0 | ) -> Option<usize> { |
270 | 0 | match *self { |
271 | 0 | VariantEncoder::SingleByte(ref v) => { |
272 | 0 | v.max_buffer_length_from_utf8_without_replacement(byte_length) |
273 | | } |
274 | 0 | VariantEncoder::Utf8(ref v) => { |
275 | 0 | v.max_buffer_length_from_utf8_without_replacement(byte_length) |
276 | | } |
277 | 0 | VariantEncoder::Gb18030(ref v) => { |
278 | 0 | v.max_buffer_length_from_utf8_without_replacement(byte_length) |
279 | | } |
280 | 0 | VariantEncoder::Big5(ref v) => { |
281 | 0 | v.max_buffer_length_from_utf8_without_replacement(byte_length) |
282 | | } |
283 | 0 | VariantEncoder::EucJp(ref v) => { |
284 | 0 | v.max_buffer_length_from_utf8_without_replacement(byte_length) |
285 | | } |
286 | 0 | VariantEncoder::Iso2022Jp(ref v) => { |
287 | 0 | v.max_buffer_length_from_utf8_without_replacement(byte_length) |
288 | | } |
289 | 0 | VariantEncoder::ShiftJis(ref v) => { |
290 | 0 | v.max_buffer_length_from_utf8_without_replacement(byte_length) |
291 | | } |
292 | 0 | VariantEncoder::EucKr(ref v) => { |
293 | 0 | v.max_buffer_length_from_utf8_without_replacement(byte_length) |
294 | | } |
295 | 0 | VariantEncoder::UserDefined(ref v) => { |
296 | 0 | v.max_buffer_length_from_utf8_without_replacement(byte_length) |
297 | | } |
298 | | } |
299 | 0 | } |
300 | | |
301 | 0 | pub fn encode_from_utf16_raw( |
302 | 0 | &mut self, |
303 | 0 | src: &[u16], |
304 | 0 | dst: &mut [u8], |
305 | 0 | last: bool, |
306 | 0 | ) -> (EncoderResult, usize, usize) { |
307 | 0 | match *self { |
308 | 0 | VariantEncoder::SingleByte(ref mut v) => v.encode_from_utf16_raw(src, dst, last), |
309 | 0 | VariantEncoder::Utf8(ref mut v) => v.encode_from_utf16_raw(src, dst, last), |
310 | 0 | VariantEncoder::Gb18030(ref mut v) => v.encode_from_utf16_raw(src, dst, last), |
311 | 0 | VariantEncoder::Big5(ref mut v) => v.encode_from_utf16_raw(src, dst, last), |
312 | 0 | VariantEncoder::EucJp(ref mut v) => v.encode_from_utf16_raw(src, dst, last), |
313 | 0 | VariantEncoder::Iso2022Jp(ref mut v) => v.encode_from_utf16_raw(src, dst, last), |
314 | 0 | VariantEncoder::ShiftJis(ref mut v) => v.encode_from_utf16_raw(src, dst, last), |
315 | 0 | VariantEncoder::EucKr(ref mut v) => v.encode_from_utf16_raw(src, dst, last), |
316 | 0 | VariantEncoder::UserDefined(ref mut v) => v.encode_from_utf16_raw(src, dst, last), |
317 | | } |
318 | 0 | } |
319 | | |
320 | 0 | pub fn encode_from_utf8_raw( |
321 | 0 | &mut self, |
322 | 0 | src: &str, |
323 | 0 | dst: &mut [u8], |
324 | 0 | last: bool, |
325 | 0 | ) -> (EncoderResult, usize, usize) { |
326 | 0 | match *self { |
327 | 0 | VariantEncoder::SingleByte(ref mut v) => v.encode_from_utf8_raw(src, dst, last), |
328 | 0 | VariantEncoder::Utf8(ref mut v) => v.encode_from_utf8_raw(src, dst, last), |
329 | 0 | VariantEncoder::Gb18030(ref mut v) => v.encode_from_utf8_raw(src, dst, last), |
330 | 0 | VariantEncoder::Big5(ref mut v) => v.encode_from_utf8_raw(src, dst, last), |
331 | 0 | VariantEncoder::EucJp(ref mut v) => v.encode_from_utf8_raw(src, dst, last), |
332 | 0 | VariantEncoder::Iso2022Jp(ref mut v) => v.encode_from_utf8_raw(src, dst, last), |
333 | 0 | VariantEncoder::ShiftJis(ref mut v) => v.encode_from_utf8_raw(src, dst, last), |
334 | 0 | VariantEncoder::EucKr(ref mut v) => v.encode_from_utf8_raw(src, dst, last), |
335 | 0 | VariantEncoder::UserDefined(ref mut v) => v.encode_from_utf8_raw(src, dst, last), |
336 | | } |
337 | 0 | } |
338 | | } |
339 | | |
340 | | pub enum VariantEncoding { |
341 | | SingleByte(&'static [u16; 128], u16, u8, u8), |
342 | | Utf8, |
343 | | Gbk, |
344 | | Gb18030, |
345 | | Big5, |
346 | | EucJp, |
347 | | Iso2022Jp, |
348 | | ShiftJis, |
349 | | EucKr, |
350 | | Replacement, |
351 | | Utf16Be, |
352 | | Utf16Le, |
353 | | UserDefined, |
354 | | } |
355 | | |
356 | | impl VariantEncoding { |
357 | 0 | pub fn new_variant_decoder(&self) -> VariantDecoder { |
358 | 0 | match *self { |
359 | 0 | VariantEncoding::SingleByte(table, _, _, _) => SingleByteDecoder::new(table), |
360 | 0 | VariantEncoding::Utf8 => Utf8Decoder::new(), |
361 | 0 | VariantEncoding::Gbk | VariantEncoding::Gb18030 => Gb18030Decoder::new(), |
362 | 0 | VariantEncoding::Big5 => Big5Decoder::new(), |
363 | 0 | VariantEncoding::EucJp => EucJpDecoder::new(), |
364 | 0 | VariantEncoding::Iso2022Jp => Iso2022JpDecoder::new(), |
365 | 0 | VariantEncoding::ShiftJis => ShiftJisDecoder::new(), |
366 | 0 | VariantEncoding::EucKr => EucKrDecoder::new(), |
367 | 0 | VariantEncoding::Replacement => ReplacementDecoder::new(), |
368 | 0 | VariantEncoding::UserDefined => UserDefinedDecoder::new(), |
369 | 0 | VariantEncoding::Utf16Be => Utf16Decoder::new(true), |
370 | 0 | VariantEncoding::Utf16Le => Utf16Decoder::new(false), |
371 | | } |
372 | 0 | } |
373 | | |
374 | 0 | pub fn new_encoder(&self, encoding: &'static Encoding) -> Encoder { |
375 | 0 | match *self { |
376 | 0 | VariantEncoding::SingleByte(table, run_bmp_offset, run_byte_offset, run_length) => { |
377 | 0 | SingleByteEncoder::new(encoding, table, run_bmp_offset, run_byte_offset, run_length) |
378 | | } |
379 | 0 | VariantEncoding::Utf8 => Utf8Encoder::new(encoding), |
380 | 0 | VariantEncoding::Gbk => Gb18030Encoder::new(encoding, false), |
381 | 0 | VariantEncoding::Gb18030 => Gb18030Encoder::new(encoding, true), |
382 | 0 | VariantEncoding::Big5 => Big5Encoder::new(encoding), |
383 | 0 | VariantEncoding::EucJp => EucJpEncoder::new(encoding), |
384 | 0 | VariantEncoding::Iso2022Jp => Iso2022JpEncoder::new(encoding), |
385 | 0 | VariantEncoding::ShiftJis => ShiftJisEncoder::new(encoding), |
386 | 0 | VariantEncoding::EucKr => EucKrEncoder::new(encoding), |
387 | 0 | VariantEncoding::UserDefined => UserDefinedEncoder::new(encoding), |
388 | | VariantEncoding::Utf16Be | VariantEncoding::Replacement | VariantEncoding::Utf16Le => { |
389 | 0 | unreachable!() |
390 | | } |
391 | | } |
392 | 0 | } |
393 | | |
394 | 0 | pub fn is_single_byte(&self) -> bool { |
395 | 0 | match *self { |
396 | 0 | VariantEncoding::SingleByte(_, _, _, _) | VariantEncoding::UserDefined => true, |
397 | 0 | _ => false, |
398 | | } |
399 | 0 | } |
400 | | } |