Add u16::is_utf16_surrogate by clarfonthey · Pull Request #94713 · rust-lang/rust (original) (raw)
Right now, there are methods in the standard library for encoding and decoding UTF-16, but at least for the moment, there aren't any methods specifically for u16
to help work with UTF-16 data. Since the full logic already exists, this wouldn't really add any code, just expose what's already there.
This method in particular is useful for working with the data returned by Windows OsStrExt::encode_wide
. Initially, I was planning to also offer a TryFrom<u16> for char
, but decided against it for now. There is plenty of code in rustc that could be rewritten to use this method, but I only checked within the standard library to replace them.
I think that offering more UTF-16-related methods to u16 would be useful, but I think this one is a good start. For example, one useful method might be u16::is_pattern_whitespace
, which would check if something is the Unicode Pattern_Whitespace
category. We can get away with this because all of the Pattern_Whitespace
characters are in the basic multilingual plane, and hence we don't need to check for surrogates.