use std::ffi::{CStr, CString};
#[repr(transparent)]
pub struct NappguiString {
pub(crate) inner: *mut nappgui_sys::String,
}
impl NappguiString {
pub fn new(text: &str) -> Self {
let text = CString::new(text).unwrap();
let inner = unsafe { nappgui_sys::str_c(text.as_ptr()) };
Self { inner }
}
pub fn as_cstr(&self) -> &CStr {
let cstr = unsafe { nappgui_sys::tc(self.inner) };
assert!(!cstr.is_null());
unsafe { CStr::from_ptr(cstr) }
}
pub fn to_string(&self) -> String {
self.as_cstr().to_string_lossy().into_owned()
}
pub fn len(&self) -> u32 {
unsafe { nappgui_sys::str_len(self.inner) }
}
pub fn nchars(&self) -> u32 {
unsafe { nappgui_sys::str_nchars(self.inner) }
}
pub fn is_empty(&self) -> bool {
unsafe { nappgui_sys::str_empty(self.inner) != 0 }
}
}
impl PartialEq for NappguiString {
fn eq(&self, other: &Self) -> bool {
unsafe { nappgui_sys::str_scmp(self.inner, other.inner) == 0 }
}
}
impl Eq for NappguiString {}
impl Clone for NappguiString {
fn clone(&self) -> Self {
let inner = unsafe { nappgui_sys::str_copy(self.inner) };
Self { inner }
}
}
impl Drop for NappguiString {
fn drop(&mut self) {
unsafe {
nappgui_sys::str_destroy(&mut self.inner);
}
}
}