Function cesu8::to_cesu8[][src]

pub fn to_cesu8(str: &str) -> Cow<'_, [u8]>
Expand description

Converts a string slice to CESU-8 bytes.

If the string slice’s representation in CESU-8 would be identical to its present UTF-8 representation, this function is functionally no different than (&str).as_bytes(); this means that to_cesu8 does not need to perform any further operations and doesn’t need to allocate any additional memory.

If the string slice’s representation in UTF-8 is not equivalent in CESU-8, to_cesu8 encodes the string slice to its CESU-8 representation as a slice of bytes.

Examples

Basic usage:

use std::borrow::Cow;
use cesu8::to_cesu8;

let str = "Hello, world!";
// Since 'str' is valid UTF-8 and CESU-8 data, 'to_cesu8' can encode
// data without allocating memory.
assert_eq!(to_cesu8(str), Cow::Borrowed(str.as_bytes()));

let utf8_data = "\u{10401}";
let cesu8_data = Cow::Borrowed(&[0xED, 0xA0, 0x81, 0xED, 0xB0, 0x81]);
// 'utf8_data' is a 4-byte UTF-8 representation, which becomes a 6-byte
// CESU-8 representation.
assert_eq!(to_cesu8(utf8_data), cesu8_data);