set utf8 flag as needed when serializing strings

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
Wolfgang Bumiller 2021-07-02 11:31:50 +02:00
parent 726fa9b484
commit 3f7917309b
3 changed files with 15 additions and 1 deletions

View File

@ -60,6 +60,7 @@ extern "C" {
pub fn RSPL_newSVuv(v: usize) -> *mut SV;
pub fn RSPL_newSVnv(v: f64) -> *mut SV;
pub fn RSPL_newSVpvn(v: *const libc::c_char, len: libc::size_t) -> *mut SV;
pub fn RSPL_newSVpvn_utf8(v: *const libc::c_char, len: libc::size_t) -> *mut SV;
pub fn RSPL_SvREFCNT_inc(sv: *mut SV) -> *mut SV;
pub fn RSPL_SvREFCNT_dec(sv: *mut SV);
pub fn RSPL_is_reference(sv: *mut SV) -> bool;

View File

@ -127,6 +127,10 @@ extern SV* RSPL_newSVpvn(const char *v, size_t len) {
return newSVpvn(v, len);
}
extern SV* RSPL_newSVpvn_utf8(const char *v, size_t len) {
return newSVpvn_utf8(v, len, 1);
}
extern SV* RSPL_SvREFCNT_inc(SV *sv) {
return SvREFCNT_inc(sv);
}

View File

@ -88,7 +88,16 @@ impl Scalar {
/// Create a new string value.
pub fn new_string(s: &str) -> Self {
Self::new_bytes(s.as_bytes())
if s.as_bytes().iter().any(|&b| b >= 0x80) {
unsafe {
Self::from_raw_move(ffi::RSPL_newSVpvn_utf8(
s.as_bytes().as_ptr() as *const libc::c_char,
s.as_bytes().len() as libc::size_t,
))
}
} else {
Self::new_bytes(s.as_bytes())
}
}
/// Create a new byte string.