correct GetPartialString (#2887)

This commit is contained in:
Mark Thom
2025-04-16 22:20:31 -07:00
committed by Mark Thom
parent e563fd8e4f
commit 4c46f0e54d
4 changed files with 184 additions and 107 deletions

View File

@@ -2806,62 +2806,142 @@ impl Machine {
self.machine_st.p += 1;
}
&Instruction::GetPartialString(_, ref string, reg) => {
use crate::machine::partial_string::{HeapPStrIter, PStrCmpResult};
self.machine_st.heap[0] = self.machine_st[reg];
let deref_v = self.machine_st.deref(self.machine_st[reg]);
let store_v = self.machine_st.store(deref_v);
let mut h = 0;
let mut string_cursor = string.as_str();
read_heap_cell!(store_v,
(HeapCellValueTag::Str |
HeapCellValueTag::Lis |
HeapCellValueTag::PStrLoc) => {
self.machine_st.heap[0] = store_v;
let heap_pstr_iter = HeapPStrIter::new(&self.machine_st.heap, 0);
while let Some(c) = string_cursor.chars().next() {
read_heap_cell!(self.machine_st.heap[h],
(HeapCellValueTag::PStrLoc, pstr_loc) => {
let heap_slice = &self.machine_st.heap.as_slice()[pstr_loc ..];
match heap_pstr_iter.compare_pstr_to_string(string) {
Some(PStrCmpResult::CompletePStrMatch { chars_matched, pstr_loc }) => {
self.machine_st.s_offset = chars_matched;
self.machine_st.s = HeapPtr::PStr(pstr_loc);
self.machine_st.mode = MachineMode::Read;
}
Some(PStrCmpResult::PartialPStrMatch { string, var_loc }) => {
let cell = backtrack_on_resource_error!(
self.machine_st,
self.machine_st.heap.allocate_pstr(string)
);
self.machine_st.mode = MachineMode::Write;
unify!(self.machine_st, cell, heap_loc_as_cell!(var_loc));
}
Some(PStrCmpResult::ListMatch { list_loc }) => {
match compare_pstr_slices(heap_slice, string_cursor.as_bytes()) {
PStrSegmentCmpResult::Continue(v1, v2) => {
// for v2, the value of a TailIndex mustn't ever be read
// since string does not lie in the heap.
match (v1, v2) {
(PStrContinuable::TailIndex(tail_idx), PStrContinuable::TailIndex(_)) => {
self.machine_st.s = HeapPtr::HeapCell(tail_idx + cell_index!(pstr_loc));
self.machine_st.s_offset = 0;
self.machine_st.s = HeapPtr::HeapCell(list_loc);
self.machine_st.mode = MachineMode::Read;
break;
}
None => {
(PStrContinuable::TailIndex(tail_idx), PStrContinuable::PStrOffset(pos)) => {
h = tail_idx + cell_index!(pstr_loc);
string_cursor = &string_cursor[pos ..];
}
(PStrContinuable::PStrOffset(pos), PStrContinuable::TailIndex(_)) => {
self.machine_st.s = HeapPtr::PStr(pstr_loc);
self.machine_st.s_offset = pos;
self.machine_st.mode = MachineMode::Read;
break;
}
_ => unreachable!(),
}
}
_ => {
self.machine_st.fail = true;
break;
}
}
}
(HeapCellValueTag::Lis, l) => {
let cell = self.machine_st.store(self.machine_st.deref(self.machine_st.heap[l]));
if let Some(d) = cell.as_char() {
if c != d {
self.machine_st.fail = true;
break;
}
} else if let Some(r) = cell.as_var() {
self.machine_st.bind(r, char_as_cell!(c));
} else {
self.machine_st.fail = true;
}
if self.machine_st.fail {
break;
} else {
h = l+1;
string_cursor = &string_cursor[c.len_utf8() ..];
if string_cursor.is_empty() {
self.machine_st.s = HeapPtr::HeapCell(h);
self.machine_st.s_offset = 0;
self.machine_st.mode = MachineMode::Read;
}
}
(HeapCellValueTag::AttrVar |
HeapCellValueTag::StackVar |
HeapCellValueTag::Var) => {
}
(HeapCellValueTag::Str, s) => {
let cell = self.machine_st.store(self.machine_st.deref(self.machine_st.heap[s+1]));
if let Some(d) = cell.as_char() {
if c != d {
self.machine_st.fail = true;
break;
}
} else if let Some(r) = cell.as_var() {
self.machine_st.bind(r, char_as_cell!(c));
} else {
self.machine_st.fail = true;
}
if self.machine_st.fail {
break;
}
h = s+2;
string_cursor = &string_cursor[c.len_utf8() ..];
if string_cursor.is_empty() {
self.machine_st.s = HeapPtr::HeapCell(h);
self.machine_st.s_offset = 0;
self.machine_st.mode = MachineMode::Read;
}
}
(HeapCellValueTag::AttrVar | HeapCellValueTag::Var, v) => {
if h == v {
let target_cell = backtrack_on_resource_error!(
self.machine_st,
self.machine_st.heap.allocate_pstr(string)
self.machine_st.heap.allocate_pstr(string_cursor)
);
self.machine_st.bind(
store_v.as_var().unwrap(),
self.machine_st.heap[h].as_var().unwrap(),
target_cell,
);
self.machine_st.mode = MachineMode::Write;
break;
} else {
h = v;
}
}
(HeapCellValueTag::StackVar, s) => {
debug_assert_eq!(h, 0);
let target_cell = backtrack_on_resource_error!(
self.machine_st,
self.machine_st.heap.allocate_pstr(string_cursor)
);
self.machine_st.bind(
Ref::stack_cell(s),
target_cell,
);
self.machine_st.mode = MachineMode::Write;
break;
}
_ => {
self.machine_st.fail = true;
break;
}
);
}
step_or_fail!(self, self.machine_st.p += 1);
}

View File

@@ -135,28 +135,25 @@ pub(crate) enum PStrSegmentCmpResult {
}
pub(crate) fn compare_pstr_slices(slice1: &[u8], slice2: &[u8]) -> PStrSegmentCmpResult {
use std::cmp::Ordering;
debug_assert!(!slice1.is_empty() && !slice2.is_empty());
let find_tail = |slice| unsafe { scan_slice_to_str(slice).tail_idx };
match slice1
.iter()
.zip(slice2.iter())
.position(|(b1, b2)| b1 != b2 || *b1 == 0 || *b2 == 0)
{
Some(pos) => {
if slice1[pos] == 0 {
let calculate_result = |pos| {
use std::cmp::Ordering;
if slice1.get(pos).cloned().unwrap_or(0) == 0 {
// subtract 1 from pos to offset the increment of scan_slice_to_str if the
// string is "\0\".
let tail1_idx = find_tail(&slice1[pos..]);
let offset_pos_1 = (ALIGN - slice1.as_ptr().align_offset(ALIGN)) % ALIGN;
if slice2[pos] == 0 {
if slice2.get(pos).cloned().unwrap_or(0) == 0 {
let tail2_idx = find_tail(&slice2[pos..]);
let offset_pos_2 = (ALIGN - slice2.as_ptr().align_offset(ALIGN)) % ALIGN;
PStrSegmentCmpResult::Continue(
PStrContinuable::TailIndex(tail1_idx + cell_index!(pos)),
PStrContinuable::TailIndex(tail2_idx + cell_index!(pos)),
PStrContinuable::TailIndex(tail1_idx + cell_index!(pos + offset_pos_1)),
PStrContinuable::TailIndex(tail2_idx + cell_index!(pos + offset_pos_2)),
)
} else {
PStrSegmentCmpResult::Continue(
@@ -164,12 +161,13 @@ pub(crate) fn compare_pstr_slices(slice1: &[u8], slice2: &[u8]) -> PStrSegmentCm
PStrContinuable::PStrOffset(pos),
)
}
} else if slice2[pos] == 0 {
} else if slice2.get(pos).cloned().unwrap_or(0) == 0 {
let tail2_idx = find_tail(&slice2[pos..]);
let offset_pos_2 = (ALIGN - slice2.as_ptr().align_offset(ALIGN)) % ALIGN;
PStrSegmentCmpResult::Continue(
PStrContinuable::PStrOffset(pos),
PStrContinuable::TailIndex(tail2_idx + cell_index!(pos)),
PStrContinuable::TailIndex(tail2_idx + cell_index!(pos + offset_pos_2)),
)
} else {
// Compute 7-byte chunks with the mismatching character at pos in the middle of
@@ -194,10 +192,15 @@ pub(crate) fn compare_pstr_slices(slice1: &[u8], slice2: &[u8]) -> PStrSegmentCm
unreachable!()
}
}
None => {
unreachable!()
}
};
match slice1
.iter()
.zip(slice2.iter())
.position(|(b1, b2)| b1 != b2 || *b1 == 0 || *b2 == 0)
{
Some(pos) => calculate_result(pos),
None => calculate_result(slice1.len().min(slice2.len())),
}
}

View File

@@ -618,14 +618,8 @@ impl Machine {
}
);
}
&Instruction::GetPartialString(
Level::Shallow,
ref string,
RegType::Temp(t),
// has_tail,
) => {
&Instruction::GetPartialString(Level::Shallow, ref string, RegType::Temp(t)) => {
use crate::machine::partial_string::HeapPStrIter;
let cell = self.deref_register(t);
read_heap_cell!(cell,
@@ -633,7 +627,7 @@ impl Machine {
self.machine_st.heap[0] = cell;
let iter = HeapPStrIter::new(&self.machine_st.heap, 0);
if iter.compare_pstr_to_string(&string).is_none() {
if iter.compare_pstr_to_string(string).is_none() {
return false;
}

View File

@@ -60,7 +60,7 @@ impl<'a> HeapPStrIter<'a> {
self.brent_st.hare
}
pub fn compare_pstr_to_string<'b>(self, mut s: &'b str) -> Option<PStrCmpResult<'b>> {
pub fn compare_pstr_to_string(self, mut s: &str) -> Option<PStrCmpResult> {
let mut curr_hare = self.brent_st.hare;
while !s.is_empty() {