// Copyright Mozilla Foundation // // Licensed under the Apache License (Version 2.0), or the MIT license, // (the "Licenses") at your option. You may not use this file except in // compliance with one of the Licenses. You may obtain copies of the // Licenses at: // // https://www.apache.org/licenses/LICENSE-2.0 // https://opensource.org/licenses/MIT // // Unless required by applicable law or agreed to in writing, software // distributed under the Licenses is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the Licenses for the specific language governing permissions and // limitations under the Licenses.
#![no_std]
//! Provides iteration by `char` over `&[u16]` containing potentially-invalid //! UTF-16 such that errors are replaced with the REPLACEMENT CHARACTER. //! //! The trait `Utf16CharsEx` provides the convenience method `chars()` on //! byte slices themselves instead of having to use the more verbose //! `Utf16Chars::new(slice)`.
mod indices; mod report;
pubusecrate::indices::Utf16CharIndices; pubusecrate::report::ErrorReportingUtf16Chars; pubusecrate::report::Utf16CharsError; use core::iter::FusedIterator;
/// Iterator by `char` over `&[u16]` that contains /// potentially-invalid UTF-16. See the crate documentation. #[derive(Debug, Clone)] pubstruct Utf16Chars<'a> {
remaining: &'a [u16],
}
impl<'a> Utf16Chars<'a> { #[inline(always)] /// Creates the iterator from a `u16` slice. pubfn new(code_units: &'a [u16]) -> Self {
Utf16Chars::<'a> {
remaining: code_units,
}
}
/// Views the current remaining data in the iterator as a subslice /// of the original slice. #[inline(always)] pubfn as_slice(&self) -> &'a [u16] { self.remaining
}
impl<'a> Iterator for Utf16Chars<'a> { type Item = char;
#[inline(always)] fn next(&mutself) -> Option<char> { // It might be OK to delegate to `ErrorReportingUtf16Chars`, but since // the methods are rather small, copypaste is probably clearer. Also, // copypaste would _not_ be equivalent if any part of this was delegated // to an `inline(never)` helper. However, previous experimentation indicated // that such a helper didn't help performance here. let (&first, tail) = self.remaining.split_first()?; self.remaining = tail; let surrogate_base = first.wrapping_sub(0xD800); if surrogate_base > (0xDFFF - 0xD800) { return Some(unsafe { char::from_u32_unchecked(u32::from(first)) });
}
Some(self.surrogate_next(surrogate_base, first))
}
}
/// Convenience trait that adds `chars()` and `char_indices()` methods /// similar to the ones on string slices to `u16` slices. pubtrait Utf16CharsEx { fn chars(&self) -> Utf16Chars<'_>; fn char_indices(&self) -> Utf16CharIndices<'_>;
}
impl Utf16CharsEx for [u16] { /// Convenience method for creating an UTF-16 iterator /// for the slice. #[inline] fn chars(&self) -> Utf16Chars<'_> {
Utf16Chars::new(self)
} /// Convenience method for creating a code unit index and /// UTF-16 iterator for the slice. #[inline] fn char_indices(&self) -> Utf16CharIndices<'_> {
Utf16CharIndices::new(self)
}
}
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.