use crate::ffi::{Buffer, Slice};
pub use crate::helpers::text::Text;
#[unsafe(no_mangle)]
pub extern "C" fn soyokaze_text_inline() -> usize {
crate::helpers::text::INLINE
}
#[unsafe(no_mangle)]
pub extern "C" fn soyokaze_text_new() -> *mut Text {
Box::into_raw(Box::new(Text::new()))
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn soyokaze_text_from_utf8(data: *const u8, data_len: usize) -> *mut Text {
if data.is_null() {
return soyokaze_text_new();
}
match unsafe { Slice::borrow_text(data, data_len) } {
Some(text) => Box::into_raw(Box::new(Text::from_str(text))),
None => std::ptr::null_mut(),
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn soyokaze_text_from_utf8_lossy(data: *const u8, data_len: usize) -> *mut Text {
let data = unsafe { Slice::borrow(data, data_len) }.unwrap_or_default();
Box::into_raw(Box::new(Text::from_utf8_lossy(data)))
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn soyokaze_text_from_ascii(data: *const u8, data_len: usize) -> *mut Text {
let data = unsafe { Slice::borrow(data, data_len) }.unwrap_or_default();
Box::into_raw(Box::new(Text::from_ascii(data)))
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn soyokaze_text_from_ascii_lowercase(data: *const u8, data_len: usize) -> *mut Text {
let data = unsafe { Slice::borrow(data, data_len) }.unwrap_or_default();
Box::into_raw(Box::new(Text::from_ascii_lowercase(data)))
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn soyokaze_text_copy_inline(data: *const u8, data_len: usize, out: *mut u8) -> bool {
let data = unsafe { Slice::borrow(data, data_len) }.unwrap_or_default();
if data.len() > crate::helpers::text::INLINE {
return false;
}
if !out.is_null() {
let octets = Text::copy_inline(data);
unsafe { std::ptr::copy_nonoverlapping(octets.as_ptr(), out, crate::helpers::text::INLINE) };
}
true
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn soyokaze_text_from_verified_ascii(data: *const u8, data_len: usize) -> *mut Text {
let data = unsafe { Slice::borrow(data, data_len) }.unwrap_or_default();
Box::into_raw(Box::new(unsafe { Text::from_verified_ascii(data) }))
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn soyokaze_text_from_verified_ascii_lowercase(data: *const u8, data_len: usize) -> *mut Text {
let data = unsafe { Slice::borrow(data, data_len) }.unwrap_or_default();
Box::into_raw(Box::new(unsafe { Text::from_verified_ascii_lowercase(data) }))
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn soyokaze_text_free(text: *mut Text) {
if !text.is_null() {
drop(unsafe { Box::from_raw(text) });
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn soyokaze_text_bytes(text: *const Text) -> Slice {
Slice::maybe(unsafe { text.as_ref() }.map(|text| text.as_str()))
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn soyokaze_text_len(text: *const Text) -> usize {
unsafe { text.as_ref() }.map_or(0, |text| text.len())
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn soyokaze_text_is_empty(text: *const Text) -> bool {
unsafe { text.as_ref() }.is_none_or(|text| text.is_empty())
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn soyokaze_text_is_inline(text: *const Text) -> bool {
unsafe { text.as_ref() }.is_some_and(|text| text.is_inline())
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn soyokaze_text_make_ascii_lowercase(text: *mut Text) -> bool {
match unsafe { text.as_mut() } {
Some(text) => {
text.make_ascii_lowercase();
true
}
None => false,
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn soyokaze_text_into_bytes(text: *mut Text) -> Buffer {
if text.is_null() {
return Buffer::EMPTY;
}
Buffer::new(unsafe { Box::from_raw(text) }.into_bytes())
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn soyokaze_text_equals(text: *const Text, other: *const Text) -> bool {
match (unsafe { text.as_ref() }, unsafe { other.as_ref() }) {
(Some(text), Some(other)) => text == other,
(None, None) => true,
_ => false,
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn soyokaze_text_compare(text: *const Text, other: *const Text) -> i32 {
match unsafe { text.as_ref() }.cmp(&unsafe { other.as_ref() }) {
std::cmp::Ordering::Less => -1,
std::cmp::Ordering::Equal => 0,
std::cmp::Ordering::Greater => 1,
}
}