Skip to content

Instantly share code, notes, and snippets.

@crumblingstatue
Created December 11, 2014 20:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crumblingstatue/37e5d73e88fdd9f9735c to your computer and use it in GitHub Desktop.
Save crumblingstatue/37e5d73e88fdd9f9735c to your computer and use it in GitHub Desktop.
use std::io::File;
fn main() {
let args = ::std::os::args();
let string = &args[1];
let replace = &args[2];
let path = &args[3];
assert!(replace.len() == string.len());
println!("Replacing`{}` with `{}` in `{}`...", string, replace, path);
let chars: Vec<char> = string.as_slice().chars().collect();
let rchars: Vec<char> = replace.as_slice().chars().collect();
let mut bytes = File::open(&Path::new(path)).read_to_end().unwrap();
'each_byte: for i in range(0, bytes.len() - chars.len()) {
for j in range(0, chars.len() - 1) {
let chdiff = chars[j] as i32 - chars[j + 1] as i32;
let bdiff = bytes[i + j] as i32 - bytes[i + j + 1] as i32;
if chdiff != bdiff {
continue 'each_byte;
}
}
for j in range(0, chars.len()) {
let diff = chars[j] as i32 - rchars[j] as i32;
bytes[i + j] = (bytes[i + j] as i32 - diff) as u8;
}
}
let mut file = File::create(&Path::new(*path + ".modif"));
file.write(bytes.as_slice()).unwrap();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment