// 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.
usecrate::Utf16CharIndicesWithTrie; use core::iter::FusedIterator; use core::marker::PhantomData; use icu_collections::codepointtrie::AbstractCodePointTrie; use icu_collections::codepointtrie::TrieValue; use icu_collections::codepointtrie::WithTrie;
/// Iterator by `char` and `icu_collections::codepointtrie::TrieValue` /// over `&[u16]` that contains potentially-invalid UTF-16. See the /// crate documentation. #[derive(Debug)] pubstruct Utf16CharsWithTrie<'slice, 'trie, T, V> where
V: TrieValue,
T: AbstractCodePointTrie<'trie, V>,
{
remaining: &'slice [u16],
trie: &'trie T,
phantom: PhantomData<V>,
}
/// Views the current remaining data in the iterator as a subslice /// of the original slice. #[inline(always)] pubfn as_slice(&self) -> &'slice [u16] { self.remaining
}
impl<'slice, 'trie, T, V> FusedIterator for Utf16CharsWithTrie<'slice, 'trie, T, V> where
V: TrieValue,
T: AbstractCodePointTrie<'trie, V>,
{
}
/// Convenience trait that adds `chars_with_trie()` and `char_indices_with_trie()` methods /// similar to the ones `icu_collections::codepointtrie::CharsWithTrieEx` adds to string /// slices to `u16` slices. pubtrait Utf16CharsWithTrieEx<'slice, 'trie, T, V> where
V: TrieValue,
T: AbstractCodePointTrie<'trie, V>,
{ /// Convenience method for creating an UTF-16 iterator /// with trie values for the slice. fn chars_with_trie(&'slice self, trie: &'trie T) -> Utf16CharsWithTrie<'slice, 'trie, T, V>; /// Convenience method for creating a code unit index and /// UTF-16 iterator with trie values for the slice. fn char_indices_with_trie(
&'slice self,
trie: &'trie T,
) -> Utf16CharIndicesWithTrie<'slice, 'trie, T, V>;
}
impl<'slice, 'trie, T, V> Utf16CharsWithTrieEx<'slice, 'trie, T, V> for [u16] where
V: TrieValue,
T: AbstractCodePointTrie<'trie, V>,
{ /// Convenience method for creating an UTF-16 iterator /// with trie values for the slice. #[inline] fn chars_with_trie(&'slice self, trie: &'trie T) -> Utf16CharsWithTrie<'slice, 'trie, T, V> {
Utf16CharsWithTrie::new(self, trie)
}
/// Convenience method for creating a code unit index and /// UTF-16 iterator with trie values for the slice. #[inline] fn char_indices_with_trie(
&'slice self,
trie: &'trie T,
) -> Utf16CharIndicesWithTrie<'slice, 'trie, T, V> {
Utf16CharIndicesWithTrie::new(self, trie)
}
}
#[cfg(test)] mod tests { usesuper::*;
#[test] fn test_forward() { let trie = icu_collections::codepointtrie::planes::get_planes_trie(); let s = &[0xD83Eu16, 0xDD73u16, 0xD83Eu16, 0x00E4u16, 0xD83Eu16]; letmut iter = s.chars_with_trie(&trie);
assert_eq!(iter.next(), Some(('', 1)));
assert_eq!(iter.next(), Some(('\u{FFFD}', 0)));
assert_eq!(iter.next(), Some(('\u{00E4}', 0)));
assert_eq!(iter.next(), Some(('\u{FFFD}', 0)));
assert_eq!(iter.next(), None);
}
#[test] fn test_backwards() { let trie = icu_collections::codepointtrie::planes::get_planes_trie(); let s = &[0xD83Eu16, 0xDD73u16, 0xD83Eu16, 0x00E4u16, 0xD83Eu16]; letmut iter = s.chars_with_trie(&trie);
assert_eq!(iter.next_back(), Some(('\u{FFFD}', 0)));
assert_eq!(iter.next_back(), Some(('\u{00E4}', 0)));
assert_eq!(iter.next_back(), Some(('\u{FFFD}', 0)));
assert_eq!(iter.next_back(), Some(('', 1)));
assert_eq!(iter.next_back(), None);
}
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.13 Sekunden
(vorverarbeitet am 2026-08-27)
¤
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.