Function cesu8::to_java_cesu8 [] [src]

pub fn to_java_cesu8(text: &str) -> Cow<[u8]>

Convert a Rust &str to Java's modified UTF-8 bytes.

use std::borrow::Cow;
use cesu8::to_java_cesu8;

// This string is valid as UTF-8 or CESU-8, so it doesn't change,
// and we can convert it without allocating memory.
assert_eq!(Cow::Borrowed("aé日".as_bytes()), to_java_cesu8("aé日"));

// This string is a 4-byte UTF-8 string, which becomes a 6-byte modified
// UTF-8 vector.
assert_eq!(Cow::Borrowed(&[0xED, 0xA0, 0x81, 0xED, 0xB0, 0x81]),
           to_java_cesu8("\u{10401}"));

// This string contains null, which becomes 2-byte modified UTF-8 encoding
assert_eq!(Cow::Borrowed(&[0xC0, 0x80, 0xC0, 0x80]),
           to_java_cesu8("\0\0"));