// Copyright Mozilla Foundation. See the COPYRIGHT // file at the top-level directory of this distribution. // // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or // https://www.apache.org/licenses/LICENSE-2.0> or the MIT license // <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your // option. This file may not be copied, modified, or distributed // except according to those terms.
usesuper::*; usecrate::data::*; usecrate::handles::*; usecrate::variant::*; // Rust 1.14.0 requires the following despite the asterisk above. usesuper::in_inclusive_range32;
pubfn max_utf16_buffer_length(&self, byte_length: usize) -> Option<usize> { // If there is a lead but the next byte isn't a valid trail, an // error is generated for the lead (+1). Then another iteration checks // space, which needs +1 to account for the possibility of astral // output or combining pair.
checked_add(1, self.plus_one_if_lead(byte_length))
}
pubfn max_utf8_buffer_length_without_replacement(&self, byte_length: usize) -> Option<usize> { // No need to account for REPLACEMENT CHARACTERS. // Cases: // ASCII: 1 to 1 // Valid pair: 2 to 2, 2 to 3 or 2 to 4, i.e. worst case 2 to 4 // lead set and first byte is trail: 1 to 4 worst case // // When checking for space for the last byte: // no lead: the last byte must be ASCII (or fatal error): 1 to 1 // lead set: space for 4 bytes was already checked when reading the // lead, hence the last lead and the last trail together are worst // case 2 to 4. // // If lead set and the input is a single trail byte, the worst-case // output is 4, so we need to add one before multiplying if lead is // set. // // Finally, add two so that if input is non-zero, the output is at // least 4.
checked_add(2, checked_mul(2, self.plus_one_if_lead(byte_length)))
}
pubfn max_utf8_buffer_length(&self, byte_length: usize) -> Option<usize> { // If there is a lead but the next byte isn't a valid trail, an // error is generated for the lead (+(1*3)). Then another iteration // checks space, which needs +3 to account for the possibility of astral // output or combining pair. In between start and end, the worst case // is that every byte is bad: *3.
checked_add(3, checked_mul(3, self.plus_one_if_lead(byte_length)))
}
ascii_compatible_two_byte_decoder_functions!(
{ // If lead is between 0x81 and 0xFE, inclusive, // subtract offset 0x81. let non_ascii_minus_offset =
non_ascii.wrapping_sub(0x81); if non_ascii_minus_offset > (0xFE - 0x81) { return (DecoderResult::Malformed(1, 0),
source.consumed(),
handle.written());
}
non_ascii_minus_offset
},
{ // If trail is between 0x40 and 0x7E, inclusive, // subtract offset 0x40. Else if trail is // between 0xA1 and 0xFE, inclusive, subtract // offset 0x62. // TODO: Find out which range is more probable. letmut trail_minus_offset =
byte.wrapping_sub(0x40); if trail_minus_offset > (0x7E - 0x40) { let trail_minus_range_start =
byte.wrapping_sub(0xA1); if trail_minus_range_start >
(0xFE - 0xA1) { if byte < 0x80 { return (DecoderResult::Malformed(1, 0),
unread_handle_trail.unread(),
handle.written());
} return (DecoderResult::Malformed(2, 0),
unread_handle_trail.consumed(),
handle.written());
}
trail_minus_offset = byte - 0x62;
} let pointer = lead_minus_offset as usize * 157usize +
trail_minus_offset as usize; let rebased_pointer = pointer.wrapping_sub(942); let low_bits = big5_low_bits(rebased_pointer); if low_bits == 0 { match pointer { 1133 => {
handle.write_big5_combination(0x00CAu16, 0x0304u16)
} 1135 => {
handle.write_big5_combination(0x00CAu16, 0x030Cu16)
} 1164 => {
handle.write_big5_combination(0x00EAu16, 0x0304u16)
} 1166 => {
handle.write_big5_combination(0x00EAu16, 0x030Cu16)
}
_ => { if byte < 0x80 { return (DecoderResult::Malformed(1, 0),
unread_handle_trail.unread(),
handle.written());
} return (DecoderResult::Malformed(2, 0),
unread_handle_trail.consumed(),
handle.written());
}
}
} elseif big5_is_astral(rebased_pointer) {
handle.write_astral(u32::from(low_bits) | 0x20000u32)
} else {
handle.write_bmp_excl_ascii(low_bits)
}
}, self,
non_ascii,
byte,
lead_minus_offset,
unread_handle_trail,
source,
handle, 'outermost,
copy_ascii_from_check_space_astral,
check_space_astral, false);
}
// prefer last
encode_big5("\u{2550}", b"\xF9\xF9");
}
#[test] #[cfg_attr(miri, ignore)] // Miri is too slow fn test_big5_decode_all() { let input = include_bytes!("test_data/big5_in.txt"); let expectation = include_str!("test_data/big5_in_ref.txt"); let (cow, had_errors) = BIG5.decode_without_bom_handling(input);
assert!(had_errors, "Should have had errors.");
assert_eq!(&cow[..], expectation);
}
#[test] #[cfg_attr(miri, ignore)] // Miri is too slow fn test_big5_encode_all() { let input = include_str!("test_data/big5_out.txt"); let expectation = include_bytes!("test_data/big5_out_ref.txt"); let (cow, encoding, had_errors) = BIG5.encode(input);
assert!(!had_errors, "Should not have had errors.");
assert_eq!(encoding, BIG5);
assert_eq!(&cow[..], &expectation[..]);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.