Skip to main content

utf8_text

Function utf8_text 

Source
unsafe fn utf8_text(
    encoding: cetype_t,
    own: *const c_char,
    len: usize,
    translate: impl FnOnce() -> *const c_char,
) -> Result<Utf8Text, SexpError>
Expand description

A CHARSXP’s text as UTF-8, given its encoding mark, its bytes and R’s translateCharUTF8.

R’s translation returns the CHARSXP’s own bytes when none is needed: UTF-8 or ASCII strings, and native strings while the session’s locale is UTF-8 (R reads its current locale, so Sys.setlocale() is followed). The bytes are then borrowed and validated (INVALID_UTF8_REASON). A string marked as bytes is refused (BYTES_REASON) before translate runs, because Rf_translateCharUTF8 raises an R error for it.

§Safety

own / len are the CHARSXP’s R_CHAR and LENGTH; translate returns its Rf_translateCharUTF8.