// 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.
pubfn decode_to_utf8_raw(
&mutself,
src: &[u8],
dst: &mut [u8],
_last: bool,
) -> (DecoderResult, usize, usize) { letmut source = ByteSource::new(src); letmut dest = Utf8Destination::new(dst); 'outermost: loop { match dest.copy_ascii_from_check_space_bmp(&mut source) {
CopyAsciiResult::Stop(ret) => return ret,
CopyAsciiResult::GoOn((mut non_ascii, mut handle)) => 'middle: loop { // Start non-boilerplate // // Since the non-ASCIIness of `non_ascii` is hidden from // the optimizer, it can't figure out that it's OK to // statically omit the bound check when accessing // `[u16; 128]` with an index // `non_ascii as usize - 0x80usize`. // // Safety: `non_ascii` is a u8 byte >=0x80, from the invariants // on Utf8Destination::copy_ascii_from_check_space_bmp() let mapped = unsafe { *(self.table.get_unchecked(non_ascii as usize - 0x80usize)) }; // let mapped = self.table[non_ascii as usize - 0x80usize]; if mapped == 0u16 { return (
DecoderResult::Malformed(1, 0),
source.consumed(),
handle.written(),
);
} let dest_again = handle.write_bmp_excl_ascii(mapped); // End non-boilerplate match source.check_available() {
Space::Full(src_consumed) => { return (
DecoderResult::InputEmpty,
src_consumed,
dest_again.written(),
);
}
Space::Available(source_handle) => { match dest_again.check_space_bmp() {
Space::Full(dst_written) => { return (
DecoderResult::OutputFull,
source_handle.consumed(),
dst_written,
);
}
Space::Available(mut destination_handle) => { let (mut b, unread_handle) = source_handle.read(); let source_again = unread_handle.commit(); 'innermost: loop { if b > 127 {
non_ascii = b;
handle = destination_handle; continue'middle;
} // Testing on Haswell says that we should write the // byte unconditionally instead of trying to unread it // to make it part of the next SIMD stride. let dest_again_again = destination_handle.write_ascii(b); if b < 60 { // We've got punctuation match source_again.check_available() {
Space::Full(src_consumed_again) => { return (
DecoderResult::InputEmpty,
src_consumed_again,
dest_again_again.written(),
);
}
Space::Available(source_handle_again) => { match dest_again_again.check_space_bmp() {
Space::Full(dst_written_again) => { return (
DecoderResult::OutputFull,
source_handle_again.consumed(),
dst_written_again,
);
}
Space::Available(
destination_handle_again,
) => { let (b_again, _unread_handle_again) =
source_handle_again.read();
b = b_again;
destination_handle =
destination_handle_again; continue'innermost;
}
}
}
}
} // We've got markup or ASCII text continue'outermost;
}
}
}
}
}
},
}
}
}
pubfn decode_to_utf16_raw(
&mutself,
src: &[u8],
dst: &mut [u16],
_last: bool,
) -> (DecoderResult, usize, usize) { let (pending, length) = if dst.len() < src.len() {
(DecoderResult::OutputFull, dst.len())
} else {
(DecoderResult::InputEmpty, src.len())
}; // Safety invariant: converted <= length. Quite often we have `converted < length` // which will be separately marked. letmut converted = 0usize; 'outermost: loop { matchunsafe { // Safety: length is the minimum length, `src/dst + x` will always be valid for reads/writes of `len - x`
ascii_to_basic_latin(
src.as_ptr().add(converted),
dst.as_mut_ptr().add(converted),
length - converted,
)
} {
None => { return (pending, length, length);
}
Some((mut non_ascii, consumed)) => { // Safety invariant: `converted <= length` upheld, since this can only consume // up to `length - converted` bytes. // // Furthermore, in this context, // we can assume `converted < length` since this branch is only ever hit when // ascii_to_basic_latin fails to consume the entire slice
converted += consumed; 'middle: loop { // `converted` doesn't count the reading of `non_ascii` yet. // Since the non-ASCIIness of `non_ascii` is hidden from // the optimizer, it can't figure out that it's OK to // statically omit the bound check when accessing // `[u16; 128]` with an index // `non_ascii as usize - 0x80usize`. // // Safety: We can rely on `non_ascii` being between `0x80` and `0xFF` due to // the invariants of `ascii_to_basic_latin()`, and our table has enough space for that. let mapped = unsafe { *(self.table.get_unchecked(non_ascii as usize - 0x80usize)) }; // let mapped = self.table[non_ascii as usize - 0x80usize]; if mapped == 0u16 { return (
DecoderResult::Malformed(1, 0),
converted + 1, // +1 `for non_ascii`
converted,
);
} unsafe { // Safety: As mentioned above, `converted < length`
*(dst.get_unchecked_mut(converted)) = mapped;
} // Safety: `converted <= length` upheld, since `converted < length` before this
converted += 1; // Next, handle ASCII punctuation and non-ASCII without // going back to ASCII acceleration. Non-ASCII scripts // use ASCII punctuation, so this avoid going to // acceleration just for punctuation/space and then // failing. This is a significant boost to non-ASCII // scripts. // TODO: Split out Latin converters without this part // this stuff makes Latin script-conversion slower. if converted == length { return (pending, length, length);
} // Safety: We are back to `converted < length` because of the == above // and can perform this check. letmut b = unsafe { *(src.get_unchecked(converted)) }; // Safety: `converted < length` is upheld for this loop 'innermost: loop { if b > 127 {
non_ascii = b; continue'middle;
} // Testing on Haswell says that we should write the // byte unconditionally instead of trying to unread it // to make it part of the next SIMD stride. unsafe { // Safety: `converted < length` is true for this loop
*(dst.get_unchecked_mut(converted)) = u16::from(b);
} // Safety: We are now at `converted <= length`. We should *not* `continue` // the loop without reverifying
converted += 1; if b < 60 { // We've got punctuation if converted == length { return (pending, length, length);
} // Safety: we're back to `converted <= length` because of the == above
b = unsafe { *(src.get_unchecked(converted)) }; // Safety: The loop continues as `converted < length` continue'innermost;
} // We've got markup or ASCII text continue'outermost;
}
}
}
}
}
}
pubfn latin1_byte_compatible_up_to(&self, buffer: &[u8]) -> usize { letmut bytes = buffer; letmut total = 0; loop { iflet Some((non_ascii, offset)) = validate_ascii(bytes) {
total += offset; // Safety: We can rely on `non_ascii` being between `0x80` and `0xFF` due to // the invariants of `ascii_to_basic_latin()`, and our table has enough space for that. let mapped = unsafe { *(self.table.get_unchecked(non_ascii as usize - 0x80usize)) }; if mapped != u16::from(non_ascii) { return total;
}
total += 1;
bytes = &bytes[offset + 1..];
} else { return total;
}
}
}
}
#[inline(always)] fn encode_u16(&self, code_unit: u16) -> Option<u8> { // First, we see if the code unit falls into a run of consecutive // code units that can be mapped by offset. This is very efficient // for most non-Latin encodings as well as Latin1-ish encodings. // // For encodings that don't fit this pattern, the run (which may // have the length of just one) just establishes the starting point // for the next rule. // // Next, we do a forward linear search in the part of the index // after the run. Even in non-Latin1-ish Latin encodings (except // macintosh), the lower case letters are here. // // Next, we search the third quadrant up to the start of the run // (upper case letters in Latin encodings except macintosh, in // Greek and in KOI encodings) and then the second quadrant, // except if the run stared before the third quadrant, we search // the second quadrant up to the run. // // Last, we search the first quadrant, which has unused controls // or punctuation in most encodings. This is bad for macintosh // and IBM866, but those are rare.
// Run of consecutive units let unit_as_usize = code_unit as usize; let offset = unit_as_usize.wrapping_sub(self.run_bmp_offset); if offset < self.run_length { return Some((128 + self.run_byte_offset + offset) as u8);
}
// Search after the run let tail_start = self.run_byte_offset + self.run_length; iflet Some(pos) = position(&self.table[tail_start..], code_unit) { return Some((128 + tail_start + pos) as u8);
}
ifself.run_byte_offset >= 64 { // Search third quadrant before the run iflet Some(pos) = position(&self.table[64..self.run_byte_offset], code_unit) { return Some(((128 + 64) + pos) as u8);
}
// Search second quadrant iflet Some(pos) = position(&self.table[32..64], code_unit) { return Some(((128 + 32) + pos) as u8);
}
} elseiflet Some(pos) = position(&self.table[32..self.run_byte_offset], code_unit) { // windows-1252, windows-874, ISO-8859-15 and ISO-8859-5 // Search second quadrant before the run return Some(((128 + 32) + pos) as u8);
}
// Search first quadrant iflet Some(pos) = position(&self.table[..32], code_unit) { return Some((128 + pos) as u8);
}
pubfn encode_from_utf16_raw(
&mutself,
src: &[u16],
dst: &mut [u8],
_last: bool,
) -> (EncoderResult, usize, usize) { let (pending, length) = if dst.len() < src.len() {
(EncoderResult::OutputFull, dst.len())
} else {
(EncoderResult::InputEmpty, src.len())
}; // Safety invariant: converted <= length. Quite often we have `converted < length` // which will be separately marked. letmut converted = 0usize; 'outermost: loop { matchunsafe { // Safety: length is the minimum length, `src/dst + x` will always be valid for reads/writes of `len - x`
basic_latin_to_ascii(
src.as_ptr().add(converted),
dst.as_mut_ptr().add(converted),
length - converted,
)
} {
None => { return (pending, length, length);
}
Some((mut non_ascii, consumed)) => { // Safety invariant: `converted <= length` upheld, since this can only consume // up to `length - converted` bytes. // // Furthermore, in this context, // we can assume `converted < length` since this branch is only ever hit when // ascii_to_basic_latin fails to consume the entire slice
converted += consumed; 'middle: loop { // `converted` doesn't count the reading of `non_ascii` yet. matchself.encode_u16(non_ascii) {
Some(byte) => { unsafe { // Safety: we're allowed this access since `converted < length`
*(dst.get_unchecked_mut(converted)) = byte;
}
converted += 1; // `converted <= length` now
}
None => { // At this point, we need to know if we // have a surrogate. let high_bits = non_ascii & 0xFC00u16; if high_bits == 0xD800u16 { // high surrogate if converted + 1 == length { // End of buffer. This surrogate is unpaired. return (
EncoderResult::Unmappable('\u{FFFD}'),
converted + 1, // +1 `for non_ascii`
converted,
);
} // Safety: convered < length from outside the match, and `converted + 1 != length`, // So `converted + 1 < length` as well. We're in bounds let second =
u32::from(unsafe { *src.get_unchecked(converted + 1) }); if second & 0xFC00u32 != 0xDC00u32 { return (
EncoderResult::Unmappable('\u{FFFD}'),
converted + 1, // +1 `for non_ascii`
converted,
);
} // The next code unit is a low surrogate. let astral: char = unsafe { // Safety: We can rely on non_ascii being 0xD800-0xDBFF since the high bits are 0xD800 // Then, (non_ascii << 10 - 0xD800 << 10) becomes between (0 to 0x3FF) << 10, which is between // 0x400 to 0xffc00. Adding the 0x10000 gives a range of 0x10400 to 0x10fc00. Subtracting the 0xDC00 // gives 0x2800 to 0x102000 // The second term is between 0xDC00 and 0xDFFF from the check above. This gives a maximum // possible range of (0x10400 + 0xDC00) to (0x102000 + 0xDFFF) which is 0x1E000 to 0x10ffff. // This is in range. // // From a Unicode principles perspective this can also be verified as we have checked that `non_ascii` is a high surrogate // (0xD800..=0xDBFF), and that `second` is a low surrogate (`0xDC00..=0xDFFF`), and we are applying reverse of the UTC16 transformation // algorithm <https://en.wikipedia.org/wiki/UTF-16#Code_points_from_U+010000_to_U+10FFFF>, by applying the high surrogate - 0xD800 to the // high ten bits, and the low surrogate - 0xDc00 to the low ten bits, and then adding 0x10000
::core::char::from_u32_unchecked(
(u32::from(non_ascii) << 10) + second
- (((0xD800u32 << 10) - 0x1_0000u32) + 0xDC00u32),
)
}; return (
EncoderResult::Unmappable(astral),
converted + 2, // +2 `for non_ascii` and `second`
converted,
);
} if high_bits == 0xDC00u16 { // Unpaired low surrogate return (
EncoderResult::Unmappable('\u{FFFD}'),
converted + 1, // +1 `for non_ascii`
converted,
);
} return (
EncoderResult::unmappable_from_bmp(non_ascii),
converted + 1, // +1 `for non_ascii`
converted,
); // Safety: This branch diverges, so no need to uphold invariants on `converted`
}
} // Next, handle ASCII punctuation and non-ASCII without // going back to ASCII acceleration. Non-ASCII scripts // use ASCII punctuation, so this avoid going to // acceleration just for punctuation/space and then // failing. This is a significant boost to non-ASCII // scripts. // TODO: Split out Latin converters without this part // this stuff makes Latin script-conversion slower. if converted == length { return (pending, length, length);
} // Safety: we're back to `converted < length` due to the == above and can perform // the unchecked read letmut unit = unsafe { *(src.get_unchecked(converted)) }; 'innermost: loop { // Safety: This loop always begins with `converted < length`, see // the invariant outside and the comment on the continue below if unit > 127 {
non_ascii = unit; continue'middle;
} // Testing on Haswell says that we should write the // byte unconditionally instead of trying to unread it // to make it part of the next SIMD stride. unsafe { // Safety: Can rely on converted < length
*(dst.get_unchecked_mut(converted)) = unit as u8;
}
converted += 1; // `converted <= length` here if unit < 60 { // We've got punctuation if converted == length { return (pending, length, length);
} // Safety: `converted < length` due to the == above. The read is safe.
unit = unsafe { *(src.get_unchecked(converted)) }; // Safety: This only happens if `converted < length`, maintaining it continue'innermost;
} // We've got markup or ASCII text continue'outermost; // Safety: All other routes to here diverge so the continue is the only // way to run the innermost loop.
}
}
}
}
}
}
}
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.