add: multiple fixes, docs for deployment

This commit is contained in:
2025-04-19 15:06:54 -07:00
parent eb46dccc61
commit 05acb6c7aa
6 changed files with 206 additions and 24 deletions

View File

@@ -58,23 +58,34 @@ impl Rewriter {
// Open 2 files; one to read and translate, and one to write.
let source_file = File::open(&src_path)?;
let mut dest_file = File::create(&dst_path)?;
let reader = BufReader::new(source_file);
let mut buff = Vec::with_capacity(2048);
let mut reader = BufReader::new(source_file);
for line in reader.lines() {
let line = line?;
while let Ok(count) = reader.read_until(b'\n', &mut buff) {
if count == 0 {
break;
}
// If the line is not subject to replacement, copy it and
// carry on.
if !line.contains(&self.source) {
writeln!(dest_file, "{}", line)?;
let line = &buff[0..count];
let m = contains(&self.source.as_bytes(), line);
if m.is_none() {
dest_file.write(&line)?;
buff.clear();
continue;
}
let m = m.unwrap();
let start = &line[0..m.0];
let end = &line[m.1..];
// Else, repeat the line multiple times, replacing the string
// in question
for replacement in &replacements {
let new_line = line.replace(&self.source, &replacement);
writeln!(dest_file, "{}", new_line)?;
dest_file.write(start)?;
dest_file.write(replacement.as_bytes())?;
dest_file.write(end)?;
}
buff.clear();
}
}
}
@@ -82,6 +93,21 @@ impl Rewriter {
}
}
fn contains(needle: &[u8], haystack: &[u8]) -> Option<(usize, usize)> {
let mut i = 0;
while i + needle.len() <= haystack.len() {
let mut j = 0;
while i+j < haystack.len() && j < needle.len() && haystack[i+j] == needle[j] {
j += 1;
}
if j == needle.len() {
return Some((i, i+j));
}
i += 1;
}
None
}
#[cfg(test)]
mod tests {
use include_directory::{Dir, include_directory};