summaryrefslogtreecommitdiffstats
path: root/src/replacer/mod.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/replacer/mod.rs')
-rw-r--r--src/replacer/mod.rs94
1 files changed, 5 insertions, 89 deletions
diff --git a/src/replacer/mod.rs b/src/replacer/mod.rs
index d871323..8db902e 100644
--- a/src/replacer/mod.rs
+++ b/src/replacer/mod.rs
@@ -1,6 +1,6 @@
-use std::{borrow::Cow, fs, fs::File, io::prelude::*, path::Path};
+use std::borrow::Cow;
-use crate::{utils, Error, Result};
+use crate::Result;
use regex::bytes::Regex;
@@ -32,7 +32,7 @@ impl Replacer {
(
look_for,
- utils::unescape(&replace_with)
+ unescape::unescape(&replace_with)
.unwrap_or(replace_with)
.into_bytes(),
)
@@ -74,20 +74,7 @@ impl Replacer {
})
}
- pub(crate) fn has_matches(&self, content: &[u8]) -> bool {
- self.regex.is_match(content)
- }
-
- pub(crate) fn check_not_empty(mut file: File) -> Result<()> {
- let mut buf: [u8; 1] = Default::default();
- file.read_exact(&mut buf)?;
- Ok(())
- }
-
- pub(crate) fn replace<'a>(
- &'a self,
- content: &'a [u8],
- ) -> std::borrow::Cow<'a, [u8]> {
+ pub(crate) fn replace<'a>(&'a self, content: &'a [u8]) -> Cow<'a, [u8]> {
let regex = &self.regex;
let limit = self.replacements;
let use_color = false;
@@ -116,7 +103,7 @@ impl Replacer {
regex: &regex::bytes::Regex,
limit: usize,
haystack: &'haystack [u8],
- use_color: bool,
+ _use_color: bool,
mut rep: R,
) -> Cow<'haystack, [u8]> {
let mut it = regex.captures_iter(haystack).enumerate().peekable();
@@ -129,17 +116,7 @@ impl Replacer {
// unwrap on 0 is OK because captures only reports matches
let m = cap.get(0).unwrap();
new.extend_from_slice(&haystack[last_match..m.start()]);
- if use_color {
- new.extend_from_slice(
- ansi_term::Color::Blue.prefix().to_string().as_bytes(),
- );
- }
rep.replace_append(&cap, &mut new);
- if use_color {
- new.extend_from_slice(
- ansi_term::Color::Blue.suffix().to_string().as_bytes(),
- );
- }
last_match = m.end();
if limit > 0 && i >= limit - 1 {
break;
@@ -148,65 +125,4 @@ impl Replacer {
new.extend_from_slice(&haystack[last_match..]);
Cow::Owned(new)
}
-
- pub(crate) fn replace_preview<'a>(
- &self,
- content: &'a [u8],
- ) -> std::borrow::Cow<'a, [u8]> {
- let regex = &self.regex;
- let limit = self.replacements;
- // TODO: refine this condition more
- let use_color = true;
- if self.is_literal {
- Self::replacen(
- regex,
- limit,
- content,
- use_color,
- regex::bytes::NoExpand(&self.replace_with),
- )
- } else {
- Self::replacen(
- regex,
- limit,
- content,
- use_color,
- &*self.replace_with,
- )
- }
- }
-
- pub(crate) fn replace_file(&self, path: &Path) -> Result<()> {
- use memmap2::{Mmap, MmapMut};
- use std::ops::DerefMut;
-
- if Self::check_not_empty(File::open(path)?).is_err() {
- return Ok(());
- }
-
- let source = File::open(path)?;
- let meta = fs::metadata(path)?;
- let mmap_source = unsafe { Mmap::map(&source)? };
- let replaced = self.replace(&mmap_source);
-
- let target = tempfile::NamedTempFile::new_in(
- path.parent()
- .ok_or_else(|| Error::InvalidPath(path.to_path_buf()))?,
- )?;
- let file = target.as_file();
- file.set_len(replaced.len() as u64)?;
- file.set_permissions(meta.permissions())?;
-
- if !replaced.is_empty() {
- let mut mmap_target = unsafe { MmapMut::map_mut(file)? };
- mmap_target.deref_mut().write_all(&replaced)?;
- mmap_target.flush_async()?;
- }
-
- drop(mmap_source);
- drop(source);
-
- target.persist(fs::canonicalize(path)?)?;
- Ok(())
- }
}