MCPcopy Create free account
hub / github.com/ashvardanian/StringZilla / decode_batch

Method decode_batch

rust/stringzilla.rs:2039–2070  ·  view source on GitHub ↗

Decodes the next batch of UTF-8 bytes into the runes buffer.

(&mut self)

Source from the content-addressed store, hash-verified

2037
2038 /// Decodes the next batch of UTF-8 bytes into the runes buffer.
2039 fn decode_batch(&mut self) {
2040 if self.octets_offset >= self.octets.len() {
2041 self.runes_count = 0;
2042 return;
2043 }
2044
2045 let remaining = self.octets.len() - self.octets_offset;
2046 let chunk_size = remaining.min(64);
2047 let octets_ptr = unsafe { self.octets.as_ptr().add(self.octets_offset) as *const c_void };
2048
2049 let mut unpacked_count: usize = 0;
2050
2051 let next_ptr = unsafe {
2052 sz_utf8_unpack_chunk(
2053 octets_ptr,
2054 chunk_size,
2055 self.runes.as_mut_ptr(),
2056 64, // Capacity of runes buffer
2057 &mut unpacked_count as *mut usize,
2058 )
2059 };
2060
2061 // Update position
2062 let bytes_consumed: usize = unsafe {
2063 let offset = (next_ptr as *const u8).offset_from(octets_ptr as *const u8);
2064 debug_assert!(offset >= 0, "sz_utf8_unpack_chunk returned a pointer before the input");
2065 offset.try_into().expect("offset should be non-negative")
2066 };
2067 self.octets_offset += bytes_consumed;
2068 self.runes_count = unpacked_count;
2069 self.runes_offset = 0;
2070 }
2071}
2072
2073impl<'a> Iterator for Utf8Chars<'a> {

Callers 2

newMethod · 0.80
nextMethod · 0.80

Calls 4

lenMethod · 0.80
as_ptrMethod · 0.80
sz_utf8_unpack_chunkFunction · 0.50
addMethod · 0.45

Tested by

no test coverage detected