lookup maps every byte through a 256-entry table; for example to lowercase ASCII:
use stringzilla as sz;let mut to_lower: [u8; 256] = core::array::from_fn(|i| i as u8);for (upper, lower) in ('A'..='Z').zip('a'..='z') { to_lower[upper as usize] = lower as u8;}let mut text = *b"HELLO WORLD!";sz::lookup_inplace(&mut text, to_lower);assert_eq!(&text, b"hello world!");
lookup returns Status::UnexpectedDimensions, writing nothing, when the target is shorter than the source.
Both replace all non-overlapping occurrences in place, returning the replacement count.
Equal-length replacements overwrite, shorter ones compact forward without allocating, and longer ones resize once and rewrite from the back:
use stringzilla::{self as sz, Byteset};let mut buffer = b"a-b-c".to_vec();let n = sz::replace_all(&mut buffer, b"-", b"__").unwrap();assert_eq!(n, 2);assert_eq!(buffer, b"a__b__c");let mut spaced = b"a, b ,c".to_vec();sz::replace_all_byteset(&mut spaced, Byteset::from(", "), b"").unwrap();assert_eq!(spaced, b"abc");
API
Functions
lookupPerforms a lookup-table transformation, mapping contents of a buffer into the same or other memory region, taking a byte substitution value from the provided table.
To convert uppercase ASCII characters to lowercase:
use stringzilla as sz;let mut to_lower: [u8; 256] = core::array::from_fn(|i| i as u8);for (upper, lower) in ('A'..='Z').zip('a'..='z') { to_lower[upper as usize] = lower as u8;}let source = "HELLO WORLD!";let mut target = vec![0u8; source.len()];sz::lookup(&mut target, &source, to_lower).unwrap();let result = String::from_utf8(target).expect("Invalid UTF-8 sequence");assert_eq!(result, "hello world!");
lookup_inplacePerforms a lookup-table transformation, mapping contents of a buffer into the same or other memory region, taking a byte substitution value from the provided table.
use stringzilla as sz;let mut to_lower: [u8; 256] = core::array::from_fn(|i| i as u8);for (upper, lower) in ('A'..='Z').zip('a'..='z') { to_lower[upper as usize] = lower as u8;}let mut text = *b"HELLO WORLD!";sz::lookup_inplace(&mut text, to_lower);assert_eq!(text, *b"hello world!");
replace_allReplaces all non-overlapping occurrences of needle inside buffer in place.