use core::{slice, str};
#[repr(C)]
#[derive(Clone, Copy)]
pub struct StringTable {
ptr: *const u8,
size: usize,
}
impl StringTable {
#[inline(always)]
pub const fn new(ptr: *const u8, size: usize) -> Self {
Self { ptr, size }
}
#[inline(always)]
pub unsafe fn get(&self, index: usize) -> &'static str {
if self.ptr.is_null() || (index >= self.size) {
return "";
}
let start = self.ptr.add(index);
let len_max = self.size - index;
let mut len = 0usize;
while len < len_max {
if *start.add(len) == 0 {
break;
}
len += 1;
}
let bytes = slice::from_raw_parts(start, len);
str::from_utf8(bytes).unwrap_or("")
}
#[inline(always)]
pub unsafe fn get_bytes(&self, index: usize) -> &'static [u8] {
if self.ptr.is_null() || (index >= self.size) {
return &[];
}
let start = self.ptr.add(index);
let len_max = self.size - index;
let mut len = 0usize;
while len < len_max {
if *start.add(len) == 0 {
break;
}
len += 1;
}
slice::from_raw_parts(start, len)
}
#[inline(always)]
pub const fn into_inner(self) -> *const u8 {
self.ptr
}
}