MCPcopy Create free account
hub / github.com/explodingcamera/tinywasm / read_cstring_until_null

Method read_cstring_until_null

crates/tinywasm/src/reference.rs:299–307  ·  view source on GitHub ↗

Reads a C-style string from memory, stopping at the first null byte.

(&self, store: &Store, offset: usize, max_len: usize)

Source from the content-addressed store, hash-verified

297
298 /// Reads a C-style string from memory, stopping at the first null byte.
299 pub fn read_cstring_until_null(&self, store: &Store, offset: usize, max_len: usize) -> Result<CString> {
300 let bytes = self.read_vec(store, offset, max_len)?;
301 let Some(null) = bytes.iter().position(|byte| *byte == 0) else {
302 return Err(crate::Error::Other("Invalid C-style string: missing null terminator".to_string()));
303 };
304
305 CString::from_vec_with_nul(bytes[..=null].to_vec())
306 .map_err(|e| crate::Error::Other(format!("Invalid C-style string: {e}")))
307 }
308
309 /// Reads a UTF-8 string from memory.
310 pub fn read_string(&self, store: &Store, offset: usize, len: usize) -> Result<String> {

Callers 1

build_importsFunction · 0.80

Calls 2

positionMethod · 0.80
read_vecMethod · 0.45

Tested by

no test coverage detected