Add Utf16Encoder. Generalize Utf16CodeUnits for any Iterator<char>.

This allows encoding to UTF-16 something that is not in UTF-8, e.g. a
`[char]` UTF-32 string.

This might help with https://github.com/servo/servo/issues/4023
This commit is contained in:
Simon Sapin
2014-11-17 20:11:54 +00:00
parent 1d81776209
commit dff48a99d6
3 changed files with 34 additions and 4 deletions

View File

@@ -114,3 +114,10 @@ fn test_rev_split_char_iterator_no_trailing() {
split.reverse();
assert_eq!(split, vec!["", "Märy häd ä little lämb", "Little lämb"]);
}
#[test]
fn test_utf16_code_units() {
use core::str::Utf16Encoder;
assert_eq!(Utf16Encoder::new(vec!['é', '\U0001F4A9'].into_iter()).collect::<Vec<u16>>(),
vec![0xE9, 0xD83D, 0xDCA9])
}