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 crate::big5::*; 24 use crate::euc_jp::*; 25 use crate::euc_kr::*; 26 use crate::gb18030::*; 27 use crate::iso_2022_jp::*; 28 use crate::replacement::*; 29 use crate::shift_jis::*; 30 use crate::single_byte::*; 31 use crate::utf_16::*; 32 use crate::utf_8::*; 33 use crate::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 { max_utf16_buffer_length(&self, byte_length: usize) -> Option<usize>50 pub fn max_utf16_buffer_length(&self, byte_length: usize) -> Option<usize> { 51 match *self { 52 VariantDecoder::SingleByte(ref v) => v.max_utf16_buffer_length(byte_length), 53 VariantDecoder::Utf8(ref v) => v.max_utf16_buffer_length(byte_length), 54 VariantDecoder::Gb18030(ref v) => v.max_utf16_buffer_length(byte_length), 55 VariantDecoder::Big5(ref v) => v.max_utf16_buffer_length(byte_length), 56 VariantDecoder::EucJp(ref v) => v.max_utf16_buffer_length(byte_length), 57 VariantDecoder::Iso2022Jp(ref v) => v.max_utf16_buffer_length(byte_length), 58 VariantDecoder::ShiftJis(ref v) => v.max_utf16_buffer_length(byte_length), 59 VariantDecoder::EucKr(ref v) => v.max_utf16_buffer_length(byte_length), 60 VariantDecoder::Replacement(ref v) => v.max_utf16_buffer_length(byte_length), 61 VariantDecoder::UserDefined(ref v) => v.max_utf16_buffer_length(byte_length), 62 VariantDecoder::Utf16(ref v) => v.max_utf16_buffer_length(byte_length), 63 } 64 } 65 max_utf8_buffer_length_without_replacement(&self, byte_length: usize) -> Option<usize>66 pub fn max_utf8_buffer_length_without_replacement(&self, byte_length: usize) -> Option<usize> { 67 match *self { 68 VariantDecoder::SingleByte(ref v) => { 69 v.max_utf8_buffer_length_without_replacement(byte_length) 70 } 71 VariantDecoder::Utf8(ref v) => { 72 v.max_utf8_buffer_length_without_replacement(byte_length) 73 } 74 VariantDecoder::Gb18030(ref v) => { 75 v.max_utf8_buffer_length_without_replacement(byte_length) 76 } 77 VariantDecoder::Big5(ref v) => { 78 v.max_utf8_buffer_length_without_replacement(byte_length) 79 } 80 VariantDecoder::EucJp(ref v) => { 81 v.max_utf8_buffer_length_without_replacement(byte_length) 82 } 83 VariantDecoder::Iso2022Jp(ref v) => { 84 v.max_utf8_buffer_length_without_replacement(byte_length) 85 } 86 VariantDecoder::ShiftJis(ref v) => { 87 v.max_utf8_buffer_length_without_replacement(byte_length) 88 } 89 VariantDecoder::EucKr(ref v) => { 90 v.max_utf8_buffer_length_without_replacement(byte_length) 91 } 92 VariantDecoder::Replacement(ref v) => { 93 v.max_utf8_buffer_length_without_replacement(byte_length) 94 } 95 VariantDecoder::UserDefined(ref v) => { 96 v.max_utf8_buffer_length_without_replacement(byte_length) 97 } 98 VariantDecoder::Utf16(ref v) => { 99 v.max_utf8_buffer_length_without_replacement(byte_length) 100 } 101 } 102 } 103 max_utf8_buffer_length(&self, byte_length: usize) -> Option<usize>104 pub fn max_utf8_buffer_length(&self, byte_length: usize) -> Option<usize> { 105 match *self { 106 VariantDecoder::SingleByte(ref v) => v.max_utf8_buffer_length(byte_length), 107 VariantDecoder::Utf8(ref v) => v.max_utf8_buffer_length(byte_length), 108 VariantDecoder::Gb18030(ref v) => v.max_utf8_buffer_length(byte_length), 109 VariantDecoder::Big5(ref v) => v.max_utf8_buffer_length(byte_length), 110 VariantDecoder::EucJp(ref v) => v.max_utf8_buffer_length(byte_length), 111 VariantDecoder::Iso2022Jp(ref v) => v.max_utf8_buffer_length(byte_length), 112 VariantDecoder::ShiftJis(ref v) => v.max_utf8_buffer_length(byte_length), 113 VariantDecoder::EucKr(ref v) => v.max_utf8_buffer_length(byte_length), 114 VariantDecoder::Replacement(ref v) => v.max_utf8_buffer_length(byte_length), 115 VariantDecoder::UserDefined(ref v) => v.max_utf8_buffer_length(byte_length), 116 VariantDecoder::Utf16(ref v) => v.max_utf8_buffer_length(byte_length), 117 } 118 } 119 decode_to_utf16_raw( &mut self, src: &[u8], dst: &mut [u16], last: bool, ) -> (DecoderResult, usize, usize)120 pub fn decode_to_utf16_raw( 121 &mut self, 122 src: &[u8], 123 dst: &mut [u16], 124 last: bool, 125 ) -> (DecoderResult, usize, usize) { 126 match *self { 127 VariantDecoder::SingleByte(ref mut v) => v.decode_to_utf16_raw(src, dst, last), 128 VariantDecoder::Utf8(ref mut v) => v.decode_to_utf16_raw(src, dst, last), 129 VariantDecoder::Gb18030(ref mut v) => v.decode_to_utf16_raw(src, dst, last), 130 VariantDecoder::Big5(ref mut v) => v.decode_to_utf16_raw(src, dst, last), 131 VariantDecoder::EucJp(ref mut v) => v.decode_to_utf16_raw(src, dst, last), 132 VariantDecoder::Iso2022Jp(ref mut v) => v.decode_to_utf16_raw(src, dst, last), 133 VariantDecoder::ShiftJis(ref mut v) => v.decode_to_utf16_raw(src, dst, last), 134 VariantDecoder::EucKr(ref mut v) => v.decode_to_utf16_raw(src, dst, last), 135 VariantDecoder::Replacement(ref mut v) => v.decode_to_utf16_raw(src, dst, last), 136 VariantDecoder::UserDefined(ref mut v) => v.decode_to_utf16_raw(src, dst, last), 137 VariantDecoder::Utf16(ref mut v) => v.decode_to_utf16_raw(src, dst, last), 138 } 139 } 140 decode_to_utf8_raw( &mut self, src: &[u8], dst: &mut [u8], last: bool, ) -> (DecoderResult, usize, usize)141 pub fn decode_to_utf8_raw( 142 &mut self, 143 src: &[u8], 144 dst: &mut [u8], 145 last: bool, 146 ) -> (DecoderResult, usize, usize) { 147 match *self { 148 VariantDecoder::SingleByte(ref mut v) => v.decode_to_utf8_raw(src, dst, last), 149 VariantDecoder::Utf8(ref mut v) => v.decode_to_utf8_raw(src, dst, last), 150 VariantDecoder::Gb18030(ref mut v) => v.decode_to_utf8_raw(src, dst, last), 151 VariantDecoder::Big5(ref mut v) => v.decode_to_utf8_raw(src, dst, last), 152 VariantDecoder::EucJp(ref mut v) => v.decode_to_utf8_raw(src, dst, last), 153 VariantDecoder::Iso2022Jp(ref mut v) => v.decode_to_utf8_raw(src, dst, last), 154 VariantDecoder::ShiftJis(ref mut v) => v.decode_to_utf8_raw(src, dst, last), 155 VariantDecoder::EucKr(ref mut v) => v.decode_to_utf8_raw(src, dst, last), 156 VariantDecoder::Replacement(ref mut v) => v.decode_to_utf8_raw(src, dst, last), 157 VariantDecoder::UserDefined(ref mut v) => v.decode_to_utf8_raw(src, dst, last), 158 VariantDecoder::Utf16(ref mut v) => v.decode_to_utf8_raw(src, dst, last), 159 } 160 } 161 latin1_byte_compatible_up_to(&self, buffer: &[u8]) -> Option<usize>162 pub fn latin1_byte_compatible_up_to(&self, buffer: &[u8]) -> Option<usize> { 163 match *self { 164 VariantDecoder::SingleByte(ref v) => { 165 return Some(v.latin1_byte_compatible_up_to(buffer)); 166 } 167 VariantDecoder::Utf8(ref v) => { 168 if !v.in_neutral_state() { 169 return None; 170 } 171 } 172 VariantDecoder::Gb18030(ref v) => { 173 if !v.in_neutral_state() { 174 return None; 175 } 176 } 177 VariantDecoder::Big5(ref v) => { 178 if !v.in_neutral_state() { 179 return None; 180 } 181 } 182 VariantDecoder::EucJp(ref v) => { 183 if !v.in_neutral_state() { 184 return None; 185 } 186 } 187 VariantDecoder::Iso2022Jp(ref v) => { 188 if v.in_neutral_state() { 189 return Some(Encoding::iso_2022_jp_ascii_valid_up_to(buffer)); 190 } 191 return None; 192 } 193 VariantDecoder::ShiftJis(ref v) => { 194 if !v.in_neutral_state() { 195 return None; 196 } 197 } 198 VariantDecoder::EucKr(ref v) => { 199 if !v.in_neutral_state() { 200 return None; 201 } 202 } 203 VariantDecoder::UserDefined(_) => {} 204 VariantDecoder::Replacement(_) | VariantDecoder::Utf16(_) => { 205 return None; 206 } 207 }; 208 Some(Encoding::ascii_valid_up_to(buffer)) 209 } 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 { has_pending_state(&self) -> bool225 pub fn has_pending_state(&self) -> bool { 226 match *self { 227 VariantEncoder::Iso2022Jp(ref v) => v.has_pending_state(), 228 _ => false, 229 } 230 } max_buffer_length_from_utf16_without_replacement( &self, u16_length: usize, ) -> Option<usize>231 pub fn max_buffer_length_from_utf16_without_replacement( 232 &self, 233 u16_length: usize, 234 ) -> Option<usize> { 235 match *self { 236 VariantEncoder::SingleByte(ref v) => { 237 v.max_buffer_length_from_utf16_without_replacement(u16_length) 238 } 239 VariantEncoder::Utf8(ref v) => { 240 v.max_buffer_length_from_utf16_without_replacement(u16_length) 241 } 242 VariantEncoder::Gb18030(ref v) => { 243 v.max_buffer_length_from_utf16_without_replacement(u16_length) 244 } 245 VariantEncoder::Big5(ref v) => { 246 v.max_buffer_length_from_utf16_without_replacement(u16_length) 247 } 248 VariantEncoder::EucJp(ref v) => { 249 v.max_buffer_length_from_utf16_without_replacement(u16_length) 250 } 251 VariantEncoder::Iso2022Jp(ref v) => { 252 v.max_buffer_length_from_utf16_without_replacement(u16_length) 253 } 254 VariantEncoder::ShiftJis(ref v) => { 255 v.max_buffer_length_from_utf16_without_replacement(u16_length) 256 } 257 VariantEncoder::EucKr(ref v) => { 258 v.max_buffer_length_from_utf16_without_replacement(u16_length) 259 } 260 VariantEncoder::UserDefined(ref v) => { 261 v.max_buffer_length_from_utf16_without_replacement(u16_length) 262 } 263 } 264 } 265 max_buffer_length_from_utf8_without_replacement( &self, byte_length: usize, ) -> Option<usize>266 pub fn max_buffer_length_from_utf8_without_replacement( 267 &self, 268 byte_length: usize, 269 ) -> Option<usize> { 270 match *self { 271 VariantEncoder::SingleByte(ref v) => { 272 v.max_buffer_length_from_utf8_without_replacement(byte_length) 273 } 274 VariantEncoder::Utf8(ref v) => { 275 v.max_buffer_length_from_utf8_without_replacement(byte_length) 276 } 277 VariantEncoder::Gb18030(ref v) => { 278 v.max_buffer_length_from_utf8_without_replacement(byte_length) 279 } 280 VariantEncoder::Big5(ref v) => { 281 v.max_buffer_length_from_utf8_without_replacement(byte_length) 282 } 283 VariantEncoder::EucJp(ref v) => { 284 v.max_buffer_length_from_utf8_without_replacement(byte_length) 285 } 286 VariantEncoder::Iso2022Jp(ref v) => { 287 v.max_buffer_length_from_utf8_without_replacement(byte_length) 288 } 289 VariantEncoder::ShiftJis(ref v) => { 290 v.max_buffer_length_from_utf8_without_replacement(byte_length) 291 } 292 VariantEncoder::EucKr(ref v) => { 293 v.max_buffer_length_from_utf8_without_replacement(byte_length) 294 } 295 VariantEncoder::UserDefined(ref v) => { 296 v.max_buffer_length_from_utf8_without_replacement(byte_length) 297 } 298 } 299 } 300 encode_from_utf16_raw( &mut self, src: &[u16], dst: &mut [u8], last: bool, ) -> (EncoderResult, usize, usize)301 pub fn encode_from_utf16_raw( 302 &mut self, 303 src: &[u16], 304 dst: &mut [u8], 305 last: bool, 306 ) -> (EncoderResult, usize, usize) { 307 match *self { 308 VariantEncoder::SingleByte(ref mut v) => v.encode_from_utf16_raw(src, dst, last), 309 VariantEncoder::Utf8(ref mut v) => v.encode_from_utf16_raw(src, dst, last), 310 VariantEncoder::Gb18030(ref mut v) => v.encode_from_utf16_raw(src, dst, last), 311 VariantEncoder::Big5(ref mut v) => v.encode_from_utf16_raw(src, dst, last), 312 VariantEncoder::EucJp(ref mut v) => v.encode_from_utf16_raw(src, dst, last), 313 VariantEncoder::Iso2022Jp(ref mut v) => v.encode_from_utf16_raw(src, dst, last), 314 VariantEncoder::ShiftJis(ref mut v) => v.encode_from_utf16_raw(src, dst, last), 315 VariantEncoder::EucKr(ref mut v) => v.encode_from_utf16_raw(src, dst, last), 316 VariantEncoder::UserDefined(ref mut v) => v.encode_from_utf16_raw(src, dst, last), 317 } 318 } 319 encode_from_utf8_raw( &mut self, src: &str, dst: &mut [u8], last: bool, ) -> (EncoderResult, usize, usize)320 pub fn encode_from_utf8_raw( 321 &mut self, 322 src: &str, 323 dst: &mut [u8], 324 last: bool, 325 ) -> (EncoderResult, usize, usize) { 326 match *self { 327 VariantEncoder::SingleByte(ref mut v) => v.encode_from_utf8_raw(src, dst, last), 328 VariantEncoder::Utf8(ref mut v) => v.encode_from_utf8_raw(src, dst, last), 329 VariantEncoder::Gb18030(ref mut v) => v.encode_from_utf8_raw(src, dst, last), 330 VariantEncoder::Big5(ref mut v) => v.encode_from_utf8_raw(src, dst, last), 331 VariantEncoder::EucJp(ref mut v) => v.encode_from_utf8_raw(src, dst, last), 332 VariantEncoder::Iso2022Jp(ref mut v) => v.encode_from_utf8_raw(src, dst, last), 333 VariantEncoder::ShiftJis(ref mut v) => v.encode_from_utf8_raw(src, dst, last), 334 VariantEncoder::EucKr(ref mut v) => v.encode_from_utf8_raw(src, dst, last), 335 VariantEncoder::UserDefined(ref mut v) => v.encode_from_utf8_raw(src, dst, last), 336 } 337 } 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 { new_variant_decoder(&self) -> VariantDecoder357 pub fn new_variant_decoder(&self) -> VariantDecoder { 358 match *self { 359 VariantEncoding::SingleByte(table, _, _, _) => SingleByteDecoder::new(table), 360 VariantEncoding::Utf8 => Utf8Decoder::new(), 361 VariantEncoding::Gbk | VariantEncoding::Gb18030 => Gb18030Decoder::new(), 362 VariantEncoding::Big5 => Big5Decoder::new(), 363 VariantEncoding::EucJp => EucJpDecoder::new(), 364 VariantEncoding::Iso2022Jp => Iso2022JpDecoder::new(), 365 VariantEncoding::ShiftJis => ShiftJisDecoder::new(), 366 VariantEncoding::EucKr => EucKrDecoder::new(), 367 VariantEncoding::Replacement => ReplacementDecoder::new(), 368 VariantEncoding::UserDefined => UserDefinedDecoder::new(), 369 VariantEncoding::Utf16Be => Utf16Decoder::new(true), 370 VariantEncoding::Utf16Le => Utf16Decoder::new(false), 371 } 372 } 373 new_encoder(&self, encoding: &'static Encoding) -> Encoder374 pub fn new_encoder(&self, encoding: &'static Encoding) -> Encoder { 375 match *self { 376 VariantEncoding::SingleByte(table, run_bmp_offset, run_byte_offset, run_length) => { 377 SingleByteEncoder::new(encoding, table, run_bmp_offset, run_byte_offset, run_length) 378 } 379 VariantEncoding::Utf8 => Utf8Encoder::new(encoding), 380 VariantEncoding::Gbk => Gb18030Encoder::new(encoding, false), 381 VariantEncoding::Gb18030 => Gb18030Encoder::new(encoding, true), 382 VariantEncoding::Big5 => Big5Encoder::new(encoding), 383 VariantEncoding::EucJp => EucJpEncoder::new(encoding), 384 VariantEncoding::Iso2022Jp => Iso2022JpEncoder::new(encoding), 385 VariantEncoding::ShiftJis => ShiftJisEncoder::new(encoding), 386 VariantEncoding::EucKr => EucKrEncoder::new(encoding), 387 VariantEncoding::UserDefined => UserDefinedEncoder::new(encoding), 388 VariantEncoding::Utf16Be | VariantEncoding::Replacement | VariantEncoding::Utf16Le => { 389 unreachable!() 390 } 391 } 392 } 393 is_single_byte(&self) -> bool394 pub fn is_single_byte(&self) -> bool { 395 match *self { 396 VariantEncoding::SingleByte(_, _, _, _) | VariantEncoding::UserDefined => true, 397 _ => false, 398 } 399 } 400 } 401