mirror of
https://github.com/chmln/sd
synced 2024-10-02 21:53:33 +00:00
parent
4a57005959
commit
39c29b20cb
12
src/input.rs
12
src/input.rs
|
@ -84,6 +84,13 @@ impl Replacer {
|
|||
self.regex.is_match(content)
|
||||
}
|
||||
|
||||
fn check_not_empty(path: &Path) -> Result<()> {
|
||||
let mut buf: [u8; 1] = Default::default();
|
||||
let mut file = std::fs::File::open(path)?;
|
||||
file.read_exact(&mut buf)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn replace<'a>(&'a self, content: &'a [u8]) -> std::borrow::Cow<'a, [u8]> {
|
||||
if self.is_literal {
|
||||
self.regex.replace_all(
|
||||
|
@ -99,7 +106,10 @@ impl Replacer {
|
|||
use memmap::{Mmap, MmapMut};
|
||||
use std::ops::DerefMut;
|
||||
|
||||
let path = std::path::Path::new(path);
|
||||
if let Err(_) = Self::check_not_empty(path) {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let source = File::open(path)?;
|
||||
let meta = source.metadata()?;
|
||||
let mmap_source = unsafe { Mmap::map(&source)? };
|
||||
|
|
Loading…
Reference in a new issue