/// Similar to [`core::str::CharIndices`] for Latin-1 strings, represented as `[u8]`. /// /// Contrary to [`core::str::CharIndices`], the second element of the /// [`Iterator::Item`] is a [`u8`], representing a Unicode scalar value in the /// range U+0000–U+00FF. #[derive(Clone, Debug)] pubstruct Latin1Indices<'a> {
front_offset: usize,
iter: &'a [u8],
}
/// Similar to [`core::str::CharIndices`] for UTF-16 strings, represented as `[u16]`. /// /// Contrary to [`core::str::CharIndices`], the second element of the /// [`Iterator::Item`] is a Unicode code point represented by a [`u32`], /// rather than a Unicode scalar value represented by a [`char`], because this /// iterator preserves unpaired surrogates. #[derive(Clone, Debug)] pubstruct Utf16Indices<'a> {
front_offset: usize,
iter: &'a [u16],
}
letmut ch = ch as u32; if (ch & 0xfc00) != 0xd800 { return Some((index, ch));
}
iflet Some(next) = self.iter.get(self.front_offset) { let next = *next as u32; if (next & 0xfc00) == 0xdc00 { // Combine low and high surrogates to UTF-32 code point.
ch = ((ch & 0x3ff) << 10) + (next & 0x3ff) + 0x10000; self.front_offset += 1;
}
}
Some((index, ch))
}
}
#[cfg(test)] mod tests { usecrate::indices::*;
#[test] fn latin1_indices() { let latin1 = [0x30, 0x31, 0x32]; letmut indices = Latin1Indices::new(&latin1); let n = indices.next().unwrap();
assert_eq!(n.0, 0);
assert_eq!(n.1, 0x30); let n = indices.next().unwrap();
assert_eq!(n.0, 1);
assert_eq!(n.1, 0x31); let n = indices.next().unwrap();
assert_eq!(n.0, 2);
assert_eq!(n.1, 0x32); let n = indices.next();
assert_eq!(n, None);
}
#[test] fn utf16_indices() { let utf16 = [0xd83d, 0xde03, 0x0020, 0xd83c, 0xdf00, 0xd800, 0x0020]; letmut indices = Utf16Indices::new(&utf16); let n = indices.next().unwrap();
assert_eq!(n.0, 0);
assert_eq!(n.1, 0x1f603); let n = indices.next().unwrap();
assert_eq!(n.0, 2);
assert_eq!(n.1, 0x20); let n = indices.next().unwrap();
assert_eq!(n.0, 3);
assert_eq!(n.1, 0x1f300); // This is invalid surrogate pair. let n = indices.next().unwrap();
assert_eq!(n.0, 5);
assert_eq!(n.1, 0xd800); let n = indices.next().unwrap();
assert_eq!(n.0, 6);
assert_eq!(n.1, 0x0020); let n = indices.next();
assert_eq!(n, None);
}
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.11 Sekunden
(vorverarbeitet am 2026-06-19)
¤
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.