Function std::char::decode_utf16 [] [src]

pub fn decode_utf16<I>(iterable: I) -> DecodeUtf16<I::IntoIter> where I: IntoIterator<Item=u16>
Unstable (decode_utf16 #27830)

: recently exposed

Create an iterator over the UTF-16 encoded code points in iterable, returning unpaired surrogates as Errs.

Examples

Basic usage:

#![feature(decode_utf16)] use std::char::decode_utf16; fn main() { // 𝄞mus<invalid>ic<invalid> let v = [0xD834, 0xDD1E, 0x006d, 0x0075, 0x0073, 0xDD1E, 0x0069, 0x0063, 0xD834]; assert_eq!(decode_utf16(v.iter().cloned()).collect::<Vec<_>>(), vec![Ok('𝄞'), Ok('m'), Ok('u'), Ok('s'), Err(0xDD1E), Ok('i'), Ok('c'), Err(0xD834)]); }
#![feature(decode_utf16)]

use std::char::decode_utf16;

fn main() {
    // 𝄞mus<invalid>ic<invalid>
    let v = [0xD834, 0xDD1E, 0x006d, 0x0075,
             0x0073, 0xDD1E, 0x0069, 0x0063,
             0xD834];

    assert_eq!(decode_utf16(v.iter().cloned()).collect::<Vec<_>>(),
               vec![Ok('𝄞'),
                    Ok('m'), Ok('u'), Ok('s'),
                    Err(0xDD1E),
                    Ok('i'), Ok('c'),
                    Err(0xD834)]);
}

A lossy decoder can be obtained by replacing Err results with the replacement character:

#![feature(decode_utf16)] use std::char::{decode_utf16, REPLACEMENT_CHARACTER}; fn main() { // 𝄞mus<invalid>ic<invalid> let v = [0xD834, 0xDD1E, 0x006d, 0x0075, 0x0073, 0xDD1E, 0x0069, 0x0063, 0xD834]; assert_eq!(decode_utf16(v.iter().cloned()) .map(|r| r.unwrap_or(REPLACEMENT_CHARACTER)) .collect::<String>(), "𝄞mus�ic�"); }
#![feature(decode_utf16)]

use std::char::{decode_utf16, REPLACEMENT_CHARACTER};

fn main() {
    // 𝄞mus<invalid>ic<invalid>
    let v = [0xD834, 0xDD1E, 0x006d, 0x0075,
             0x0073, 0xDD1E, 0x0069, 0x0063,
             0xD834];

    assert_eq!(decode_utf16(v.iter().cloned())
                   .map(|r| r.unwrap_or(REPLACEMENT_CHARACTER))
                   .collect::<String>(),
               "𝄞mus�ic�");
}