Function script::dom::characterdata::split_at_utf16_code_unit_offset
source · fn split_at_utf16_code_unit_offset(
s: &str,
offset: u32,
replace_surrogates: bool,
) -> Result<(&str, Option<char>, &str), ()>
Expand description
Split the given string at the given position measured in UTF-16 code units from the start.
Err(())
indicates thatoffset
if after the end of the stringOk((before, None, after))
indicates thatoffset
is between Unicode code points. The two string slices are such that:before == s.to_utf16()[..offset].to_utf8()
andafter == s.to_utf16()[offset..].to_utf8()
Ok((before, Some(ch), after))
indicates thatoffset
is “in the middle” of a single Unicode code point that would be represented in UTF-16 by a surrogate pair of two 16-bit code units.ch
is that code point. The two string slices are such that:before == s.to_utf16()[..offset - 1].to_utf8()
andafter == s.to_utf16()[offset + 1..].to_utf8()
§Panics
Note that the third variant is only ever returned when the -Z replace-surrogates
command-line option is specified.
When it would be returned but the option is not specified, this function panics.