]> Repositorios git - scryer-prolog.git/commitdiff
fix read_s logic around HeapPtr::PStrLoc (#2894)
authorMark Thom <[email protected]>
Sun, 20 Apr 2025 06:51:26 +0000 (23:51 -0700)
committerMark Thom <[email protected]>
Tue, 8 Jul 2025 05:38:12 +0000 (22:38 -0700)
src/machine/machine_state_impl.rs

index 45c689a531c1c82c35c232981b9edb2b4991e931..0358cfd4baf2ea9d1ed76a53d3a5f15d57fa38a7 100644 (file)
@@ -345,8 +345,8 @@ impl MachineState {
     pub(crate) fn read_s(&mut self) -> HeapCellValue {
         match self.s {
             HeapPtr::HeapCell(h) => self.deref(self.heap[h + self.s_offset]),
-            HeapPtr::PStr(h) => {
-                let mut char_iter = self.heap.char_iter(h);
+            HeapPtr::PStr(byte_index) => {
+                let mut char_iter = self.heap.char_iter(byte_index);
 
                 if self.s_offset == 0 {
                     // read the car of the list
@@ -354,10 +354,9 @@ impl MachineState {
                     char_as_cell!(c)
                 } else {
                     // read the (self.s_offset)^{th} cdr of the list
-                    let byte_offset: usize =
-                        char_iter.take(self.s_offset).map(|c| c.len_utf8()).sum();
-                    let new_h = h + byte_offset;
-
+                    // self.s_offset is the number of bytes offset into the PStr
+                    // in this context, *not* the number of heap cells.
+                    let new_h = byte_index + self.s_offset;
                     self.s_offset = 0;
 
                     if self.heap.char_iter(new_h).next().is_some() {