实现 CPeek 特性并在 MemReader 和 MemWriter 中添加支持;重构 V1Parser 和 BGIScript 中的字符串读取逻辑

This commit is contained in:
2025-06-07 15:17:24 +08:00
parent 4fcb4a4d24
commit 5533e901f7
3 changed files with 296 additions and 14 deletions

View File

@@ -441,10 +441,8 @@ impl<'a> V1Parser<'a> {
pub fn is_empty_string(&self, address: usize) -> Result<bool> {
let start = self.offset + address;
if start >= self.buf.data.len() {
return Err(anyhow::anyhow!("Address out of bounds"));
}
Ok(self.buf.data[start] == 0)
let data = self.buf.cpeek_u8_at(start)?;
Ok(data == 0)
}
pub fn read_string_at_address(&mut self, address: usize) -> Result<String> {

View File

@@ -93,15 +93,8 @@ impl BGIScript {
fn read_string(&self, offset: usize) -> Result<String> {
let start = self.offset + offset;
let mut end = start;
while self.data.data[end] != 0 {
end += 1;
if end >= self.data.data.len() {
return Err(anyhow::anyhow!("String not null-terminated"));
}
}
let string_data = &self.data.data[start..end];
let string = decode_to_string(self.encoding, string_data)?;
let string_data = self.data.cpeek_cstring_at(start)?;
let string = decode_to_string(self.encoding, string_data.as_bytes())?;
Ok(string)
}
}