Add scan_slice_to_str_from_start
This commit is contained in:
@@ -89,7 +89,7 @@ pub struct HeapStringScan<'a> {
|
|||||||
pub tail_idx: usize,
|
pub tail_idx: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
// return the string at ptr and the tail location relative to ptr.
|
// The heap_slice should be inside the heap
|
||||||
unsafe fn scan_slice_to_str(heap_slice: &[u8]) -> HeapStringScan {
|
unsafe fn scan_slice_to_str(heap_slice: &[u8]) -> HeapStringScan {
|
||||||
let string_len = heap_slice
|
let string_len = heap_slice
|
||||||
.iter()
|
.iter()
|
||||||
@@ -111,6 +111,28 @@ unsafe fn scan_slice_to_str(heap_slice: &[u8]) -> HeapStringScan {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Same as scan_slice_to_str but assumes that the slice is from the start of a string.
|
||||||
|
// Can be used on strings out of the heap.
|
||||||
|
unsafe fn scan_slice_to_str_from_start(heap_slice: &[u8]) -> HeapStringScan {
|
||||||
|
let string_len = heap_slice
|
||||||
|
.iter()
|
||||||
|
.position(|b| *b == 0u8)
|
||||||
|
.unwrap_or(heap_slice.len());
|
||||||
|
|
||||||
|
let sentinel_len = pstr_sentinel_length(string_len);
|
||||||
|
let tail_idx = cell_index!(
|
||||||
|
(string_len + sentinel_len).next_multiple_of(ALIGN)
|
||||||
|
+ if sentinel_len <= 1 { heap_index!(1) } else { 0 }
|
||||||
|
);
|
||||||
|
|
||||||
|
let str_slice = &heap_slice[..string_len];
|
||||||
|
|
||||||
|
HeapStringScan {
|
||||||
|
string: std::str::from_utf8_unchecked(str_slice),
|
||||||
|
tail_idx,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
pub(crate) enum PStrContinuable {
|
pub(crate) enum PStrContinuable {
|
||||||
PStrOffset(usize),
|
PStrOffset(usize),
|
||||||
@@ -904,7 +926,8 @@ impl Heap {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
let HeapStringScan { string, tail_idx } = unsafe { scan_slice_to_str(src_bytes) };
|
let HeapStringScan { string, tail_idx } =
|
||||||
|
unsafe { scan_slice_to_str_from_start(src_bytes) };
|
||||||
|
|
||||||
src_bytes = &src_bytes[string.len()..];
|
src_bytes = &src_bytes[string.len()..];
|
||||||
byte_size += heap_index!(tail_idx);
|
byte_size += heap_index!(tail_idx);
|
||||||
|
|||||||
Reference in New Issue
Block a user