use super::*;
#[derive(Clone)]
pub struct Rstr {
pub(crate) robj: Robj,
}
pub(crate) unsafe fn sexp_to_str(sexp: SEXP) -> &'static str {
if sexp == R_NaString {
<&str>::na()
} else {
std::mem::transmute(to_str(R_CHAR(sexp) as *const u8))
}
}
impl Rstr {
pub fn from_string(val: &str) -> Self {
Rstr {
robj: Robj::from_sexp(str_to_character(val)),
}
}
pub fn as_str(&self) -> &str {
unsafe { sexp_to_str(self.robj.get()) }
}
}
impl AsRef<str> for Rstr {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl From<String> for Rstr {
fn from(s: String) -> Self {
Rstr::from_string(&s)
}
}
impl From<&str> for Rstr {
fn from(s: &str) -> Self {
Rstr::from_string(s)
}
}
impl From<Option<String>> for Rstr {
fn from(value: Option<String>) -> Self {
if let Some(string) = value {
Self::from(string)
} else {
Self { robj: na_string() }
}
}
}
impl Deref for Rstr {
type Target = str;
fn deref(&self) -> &Self::Target {
self.as_str()
}
}
impl<T> PartialEq<T> for Rstr
where
T: AsRef<str>,
{
fn eq(&self, other: &T) -> bool {
self.as_str() == other.as_ref()
}
}
impl PartialEq<str> for Rstr {
fn eq(&self, other: &str) -> bool {
self.as_str() == other
}
}
impl std::fmt::Debug for Rstr {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if self.is_na() {
write!(f, "NA_CHARACTER")
} else {
let s = self.as_str();
write!(f, "{:?}", s)
}
}
}
impl std::fmt::Display for Rstr {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let s = self.as_str();
write!(f, "{}", s)
}
}
impl CanBeNA for Rstr {
fn is_na(&self) -> bool {
unsafe { self.robj.get() == R_NaString }
}
fn na() -> Self {
unsafe {
Self {
robj: Robj::from_sexp(R_NaString),
}
}
}
}