use crate::object::{box_, box_uint32, count_object, dec, dec_ref, is_exclusive, is_scalar, new_header, unbox, unbox_uint32};
use crate::object::{alloc_ctor, ctor_get, ctor_set, Header, Obj, TAG_STRING};
#[repr(C)]
struct Str {
header: Header,
text: String,
}
pub(crate) unsafe fn free(o: Obj) {
count_object(-1);
drop(Box::from_raw(o.header().cast::<Str>()));
}
pub fn mk_string(text: &str) -> Obj {
from_string(text.to_owned())
}
pub fn from_string(text: String) -> Obj {
count_object(1);
let string = Box::new(Str { header: new_header(TAG_STRING, 0, 0), text });
Obj::from_header(Box::into_raw(string).cast())
}
pub unsafe fn into_string(s: Obj) -> String {
if is_exclusive(s) {
count_object(-1);
return Box::from_raw(s.header().cast::<Str>()).text;
}
let copy = text(s).to_owned();
dec_ref(s);
copy
}
pub unsafe fn text<'a>(s: Obj) -> &'a str {
&(*s.header().cast::<Str>()).text
}
unsafe fn exclusive<'a>(s: Obj) -> (Obj, &'a mut String) {
let s = if is_exclusive(s) {
s
} else {
let copy = mk_string(text(s));
dec_ref(s);
copy
};
(s, &mut (*s.header().cast::<Str>()).text)
}
unsafe fn index(position: Obj) -> Option<usize> {
is_scalar(position).then(|| unbox(position))
}
fn char_at(text: &str, i: usize) -> Option<char> {
text.get(i..)?.chars().next()
}
const DEFAULT_CHAR: u32 = 'A' as u32;
pub unsafe fn string_push(s: Obj, c: u32) -> Obj {
let (s, text) = exclusive(s);
text.push(crate::char_of_u32(c));
s
}
pub unsafe fn string_append(s1: Obj, s2: Obj) -> Obj {
let addition = text(s2).to_owned();
let (s1, text) = exclusive(s1);
text.push_str(&addition);
s1
}
pub unsafe fn string_length(s: Obj) -> Obj {
box_(text(s).chars().count())
}
pub unsafe fn string_utf8_byte_size(s: Obj) -> Obj {
box_(text(s).len())
}
pub unsafe fn string_mk(chars: Obj) -> Obj {
let mut out = String::new();
let mut rest = chars;
while !is_scalar(rest) {
out.push(crate::char_of_u32(unbox_uint32(ctor_get(rest, 0))));
rest = ctor_get(rest, 1);
}
dec(chars);
mk_string(&out)
}
pub unsafe fn string_data(s: Obj) -> Obj {
let mut list = box_(0);
for c in text(s).chars().rev() {
let cell = alloc_ctor(1, 2, 0, 0);
ctor_set(cell, 0, box_uint32(c as u32));
ctor_set(cell, 1, list);
list = cell;
}
dec(s);
list
}
pub unsafe fn string_utf8_get(s: Obj, i: Obj) -> u32 {
index(i).and_then(|i| char_at(text(s), i)).map_or(DEFAULT_CHAR, |c| c as u32)
}
pub unsafe fn string_utf8_get_fast(s: Obj, i: Obj) -> u32 {
string_utf8_get(s, i)
}
pub unsafe fn string_get_byte_fast(s: Obj, i: Obj) -> u8 {
text(s).as_bytes()[unbox(i)]
}
pub unsafe fn string_uget_byte_fast(s: Obj, i: usize) -> u8 {
text(s).as_bytes()[i]
}
pub unsafe fn string_utf8_next(s: Obj, i: Obj) -> Obj {
let width = index(i).and_then(|i| char_at(text(s), i)).map_or(1, char::len_utf8);
crate::nat::nat_add(i, box_(width))
}
pub unsafe fn string_utf8_next_fast(s: Obj, i: Obj) -> Obj {
string_utf8_next(s, i)
}
pub unsafe fn string_utf8_prev(s: Obj, i: Obj) -> Obj {
let text = text(s);
match index(i) {
Some(0) => box_(0),
Some(i) if i <= text.len() => {
let mut start = i - 1;
while !text.is_char_boundary(start) {
start -= 1;
}
box_(start)
}
_ => crate::nat::nat_sub(i, box_(1)),
}
}
pub unsafe fn string_utf8_set(s: Obj, i: Obj, c: u32) -> Obj {
let Some((i, old)) = index(i).and_then(|i| Some((i, char_at(text(s), i)?))) else {
return s;
};
let (s, text) = exclusive(s);
let mut buffer = [0u8; 4];
text.replace_range(i..i + old.len_utf8(), crate::char_of_u32(c).encode_utf8(&mut buffer));
s
}
pub unsafe fn string_utf8_at_end(s: Obj, i: Obj) -> u8 {
index(i).is_none_or(|i| i >= text(s).len()) as u8
}
pub unsafe fn string_utf8_extract(s: Obj, begin: Obj, end: Obj) -> Obj {
let text = text(s);
let Some(begin) = index(begin).filter(|&b| b < text.len() && text.is_char_boundary(b)) else {
return mk_string("");
};
let end = index(end).unwrap_or(usize::MAX);
if end <= begin {
return mk_string("");
}
let end = if end <= text.len() && text.is_char_boundary(end) { end } else { text.len() };
mk_string(&text[begin..end])
}
pub unsafe fn string_utf8_extract_fast(s: Obj, begin: Obj, end: Obj) -> Obj {
let (begin, end) = (unbox(begin), unbox(end));
mk_string(text(s).get(begin..end).unwrap_or(""))
}
pub unsafe fn string_pushn(s: Obj, c: u32, n: Obj) -> Obj {
let Some(n) = index(n) else {
panic!("inlean: out of memory: the string is too large");
};
let (s, text) = exclusive(s);
text.extend(std::iter::repeat_n(crate::char_of_u32(c), n));
s
}
pub unsafe fn string_dec_eq(s1: Obj, s2: Obj) -> u8 {
(text(s1) == text(s2)) as u8
}
pub unsafe fn string_dec_lt(s1: Obj, s2: Obj) -> u8 {
(text(s1) < text(s2)) as u8
}
pub unsafe fn string_compare(s1: Obj, s2: Obj) -> u8 {
(text(s1).cmp(text(s2)) as i8 + 1) as u8
}
pub unsafe fn string_memcmp(s1: Obj, s2: Obj, start1: Obj, start2: Obj, len: Obj) -> u8 {
let (start1, start2, len) = (unbox(start1), unbox(start2), unbox(len));
(text(s1).as_bytes()[start1..start1 + len] == text(s2).as_bytes()[start2..start2 + len]) as u8
}
pub fn string_of_usize(n: usize) -> Obj {
mk_string(&n.to_string())
}
pub unsafe fn string_hash(s: Obj) -> u64 {
hash_bytes(text(s).as_bytes())
}
pub(crate) fn hash_bytes(bytes: &[u8]) -> u64 {
const M: u64 = 0xc6a4a7935bd1e995;
const R: u32 = 47;
let mut h: u64 = 11 ^ (bytes.len() as u64).wrapping_mul(M);
let mut chunks = bytes.chunks_exact(8);
for chunk in &mut chunks {
let mut k = u64::from_le_bytes(chunk.try_into().expect("eight bytes"));
k = k.wrapping_mul(M);
k ^= k >> R;
k = k.wrapping_mul(M);
h ^= k;
h = h.wrapping_mul(M);
}
let tail = chunks.remainder();
if !tail.is_empty() {
for (i, &byte) in tail.iter().enumerate() {
h ^= (byte as u64) << (8 * i);
}
h = h.wrapping_mul(M);
}
h ^= h >> R;
h = h.wrapping_mul(M);
h ^ (h >> R)
}
pub(crate) const OPS: &[&str] = &[
"string_push", "string_append", "string_length", "string_utf8_byte_size", "string_mk", "string_data",
"string_utf8_get", "string_utf8_get_fast", "string_get_byte_fast", "string_uget_byte_fast",
"string_utf8_next", "string_utf8_next_fast", "string_utf8_prev", "string_utf8_set", "string_utf8_at_end",
"string_utf8_extract", "string_dec_eq", "string_dec_lt", "string_compare", "string_memcmp",
"string_of_usize", "string_hash", "string_utf8_extract_fast", "string_pushn",
];