use std::io::{self, Write};
use std::ops::Range;
use crate::jobs::{CancelToken, Progress};
use crate::search::{self, Matcher, SearchEnd};
use crate::text::TextBuf;
use regex_automata::util::captures::Captures;
#[derive(Clone, Debug)]
pub struct Template {
text: String,
interpolate: bool,
}
pub fn unescape(s: &str) -> String {
let mut out = String::with_capacity(s.len());
let mut it = s.chars().peekable();
while let Some(c) = it.next() {
if c != '\\' {
out.push(c);
continue;
}
match it.peek().copied() {
Some('n') => out.push('\n'),
Some('r') => out.push('\r'),
Some('t') => out.push('\t'),
Some('0') => out.push('\0'),
Some('\\') => out.push('\\'),
_ => {
out.push('\\');
continue;
}
}
it.next();
}
out
}
impl Template {
pub fn verbatim(replacement: &str) -> Template {
Template {
text: replacement.to_string(),
interpolate: false,
}
}
pub fn new(replacement: &str, regex_mode: bool) -> Template {
let text = unescape(replacement);
let interpolate = regex_mode && text.contains('$');
Template { text, interpolate }
}
pub fn expand(&self, buf: &TextBuf, m: &Matcher, s: u64, e: u64) -> Vec<u8> {
let mut out = Vec::new();
Expander::new(self, m).expand_into(buf, s, e, &mut out);
out
}
}
pub struct Expander<'a> {
tpl: &'a Template,
m: &'a Matcher,
fixed: Option<Vec<u8>>,
caps: Option<Captures>,
hay: Vec<u8>,
interp: Vec<u8>,
}
impl<'a> Expander<'a> {
pub fn new(tpl: &'a Template, m: &'a Matcher) -> Expander<'a> {
let codec = m.codec();
let fixed = (!tpl.interpolate).then(|| codec.encode(&tpl.text));
let caps = if tpl.interpolate {
m.regex().map(|r| r.create_captures())
} else {
None
};
Expander {
tpl,
m,
fixed,
caps,
hay: Vec::new(),
interp: Vec::new(),
}
}
pub fn fixed(&self) -> Option<&[u8]> {
self.fixed.as_deref()
}
pub fn expand_into(&mut self, buf: &TextBuf, s: u64, e: u64, out: &mut Vec<u8>) {
if let Some(f) = &self.fixed {
out.extend_from_slice(f);
return;
}
let codec = self.m.codec();
let found = match self.caps.as_mut() {
Some(caps) => search::captures_into(buf, self.m, s, e, caps, &mut self.hay).is_some(),
None => false,
};
if !found {
out.extend_from_slice(&codec.encode(&self.tpl.text));
return;
}
let caps = self.caps.as_ref().expect("captures");
if codec.enc == crate::encoding::Encoding::Utf8 {
caps.interpolate_bytes_into(&self.hay, self.tpl.text.as_bytes(), out);
} else {
self.interp.clear();
caps.interpolate_bytes_into(&self.hay, self.tpl.text.as_bytes(), &mut self.interp);
out.extend_from_slice(&codec.encode(&String::from_utf8_lossy(&self.interp)));
}
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct ReplaceStats {
pub count: u64,
pub bytes_in: u64,
pub bytes_out: u64,
}
pub enum Collected {
Edits(Vec<(Range<u64>, Vec<u8>)>),
TooMany,
Cancelled,
}
pub fn collect(
buf: &TextBuf,
m: &Matcher,
tpl: &Template,
range: Range<u64>,
threshold: usize,
cancel: &CancelToken,
progress: Option<&Progress>,
) -> Collected {
let mut edits = Vec::new();
let mut too_many = false;
let mut exp = Expander::new(tpl, m);
let end = search::search_forward(buf, m, range, cancel, progress, |s, e| {
if edits.len() >= threshold {
too_many = true;
return false;
}
let mut rep = Vec::new();
exp.expand_into(buf, s, e, &mut rep);
edits.push((s..e, rep));
true
});
match end {
SearchEnd::Cancelled | SearchEnd::Unavailable => Collected::Cancelled,
_ if too_many => Collected::TooMany,
_ => Collected::Edits(edits),
}
}
pub fn preview(
buf: &TextBuf,
m: &Matcher,
tpl: &Template,
range: Range<u64>,
n: usize,
cancel: &CancelToken,
) -> Vec<(u64, String, String)> {
let codec = buf.codec;
let mut out = Vec::new();
search::search_forward(buf, m, range, cancel, None, |s, e| {
let before = buf
.read(s..e.min(s + 200))
.map(|b| codec.decode_lossy(&b))
.unwrap_or_default();
let after = codec.decode_lossy(&tpl.expand(buf, m, s, e));
out.push((s, before, after));
out.len() < n
});
out
}
pub fn replace_all_to<W: Write>(
buf: &TextBuf,
m: &Matcher,
tpl: &Template,
range: Range<u64>,
out: &mut W,
cancel: &CancelToken,
progress: Option<&Progress>,
) -> io::Result<Option<ReplaceStats>> {
let mut stats = ReplaceStats {
bytes_in: buf.len(),
..Default::default()
};
let mut pos = 0u64;
let mut err: Option<io::Error> = None;
let mut scratch = Vec::new();
let mut copy = |from: u64,
to: u64,
hay: &[u8],
base: u64,
out: &mut W,
stats: &mut ReplaceStats|
-> io::Result<()> {
if from >= to {
return Ok(());
}
stats.bytes_out += to - from;
if from >= base && to <= base + hay.len() as u64 {
return out.write_all(&hay[(from - base) as usize..(to - base) as usize]);
}
const STEP: u64 = 4 * 1024 * 1024;
let mut p = from;
while p < to {
let e = (p + STEP).min(to);
let n = buf
.read_to(p..e, &mut scratch)
.ok_or_else(|| io::Error::other("could not read the document"))?;
out.write_all(&scratch[..n])?;
p = e;
}
Ok(())
};
let mut exp = Expander::new(tpl, m);
let mut rep = Vec::new();
let end = search::search_forward_ctx(buf, m, range, cancel, progress, |s, e, hay, base| {
let r = copy(pos, s, hay, base, out, &mut stats).and_then(|_| {
let rep: &[u8] = if exp.fixed().is_none() {
rep.clear();
exp.expand_into(buf, s, e, &mut rep);
&rep
} else {
exp.fixed().unwrap_or_default()
};
stats.bytes_out += rep.len() as u64;
out.write_all(rep)
});
if let Err(e) = r {
err = Some(e);
return false;
}
stats.count += 1;
pos = e;
true
});
if let Some(e) = err {
return Err(e);
}
match end {
SearchEnd::Cancelled => return Ok(None),
SearchEnd::Unavailable => return Err(io::Error::other("could not read the document")),
_ => {}
}
let len = buf.len();
copy(pos, len, &[], 0, out, &mut stats)?;
Ok(Some(stats))
}
#[cfg(test)]
mod tests {
use super::*;
use crate::encoding::Codec;
use crate::file::{OpenMode, Source};
use crate::index;
use crate::search::Query;
use std::sync::Arc;
fn buf_with(data: &[u8]) -> (tempfile::NamedTempFile, TextBuf) {
buf_in(data, Codec::utf8())
}
fn buf_in(data: &[u8], codec: Codec) -> (tempfile::NamedTempFile, TextBuf) {
let mut f = tempfile::NamedTempFile::new().unwrap();
f.write_all(data).unwrap();
f.flush().unwrap();
let src = Arc::new(Source::open(f.path(), OpenMode::Auto).unwrap());
let idx = index::build_blocking(&src, codec);
let mut t = TextBuf::new(src, idx, codec);
t.blocking = true;
(f, t)
}
fn replaced(t: &TextBuf, pattern: &str, regex: bool, tpl: &Template, r: Range<u64>) -> Vec<u8> {
let q = Query {
pattern: pattern.into(),
regex,
case_sensitive: true,
..Default::default()
};
let m = Matcher::new(&q, t.codec).unwrap();
let mut out = Vec::new();
replace_all_to(t, &m, tpl, r, &mut out, &CancelToken::new(), None)
.unwrap()
.unwrap();
out
}
#[test]
fn a_byte_order_mark_stays_in_front() {
let codec = Codec::new(
crate::encoding::Encoding::Utf8,
crate::encoding::NlByte::Lf,
3,
);
let (_f, t) = buf_in(b"\xEF\xBB\xBFid,x\nb\n", codec);
let tpl = Template::new("> ", true);
assert_eq!(
replaced(&t, "^", true, &tpl, 0..t.len()),
b"\xEF\xBB\xBF> id,x\n> b\n> "
);
let tpl = Template::new("ID,", true);
assert_eq!(
replaced(&t, "^id,", true, &tpl, 0..t.len()),
b"\xEF\xBB\xBFID,x\nb\n"
);
}
#[test]
fn no_empty_match_at_the_end_of_a_selection() {
let (_f, t) = buf_with(b"a\nb\nc\n");
let tpl = Template::new("// ", true);
assert_eq!(replaced(&t, "^", true, &tpl, 0..4), b"// a\n// b\nc\n");
}
#[test]
fn an_odd_length_utf16_file_is_searched_to_the_end() {
let codec = Codec::new(
crate::encoding::Encoding::Utf16Le,
crate::encoding::NlByte::Lf,
2,
);
let (_f, t) = buf_in(b"\xFF\xFEa\x00b", codec);
let q = Query {
pattern: "z".into(),
..Default::default()
};
let m = Matcher::new(&q, codec).unwrap();
let cancel = CancelToken::new();
let watchdog = cancel.clone();
std::thread::spawn(move || {
std::thread::sleep(std::time::Duration::from_secs(10));
watchdog.cancel();
});
let mut n = 0;
let end = search::search_forward(&t, &m, 0..t.len(), &cancel, None, |_, _| {
n += 1;
true
});
assert_eq!((end, n), (SearchEnd::Completed, 0));
}
#[test]
fn a_verbatim_replacement_keeps_backslashes_and_dollars() {
let (_f, t) = buf_with(br"path C:\old here");
let tpl = Template::verbatim(r"C:\new $1");
assert_eq!(
replaced(&t, r"C:\old", false, &tpl, 0..t.len()),
br"path C:\new $1 here"
);
}
fn run(data: &[u8], pattern: &str, regex: bool, rep: &str) -> String {
let (_f, t) = buf_with(data);
let q = Query {
pattern: pattern.into(),
regex,
case_sensitive: true,
..Default::default()
};
let m = Matcher::new(&q, Codec::utf8()).unwrap();
let tpl = Template::new(rep, regex);
let mut out = Vec::new();
replace_all_to(
&t,
&m,
&tpl,
0..t.len(),
&mut out,
&CancelToken::new(),
None,
)
.unwrap()
.unwrap();
if let Collected::Edits(edits) = collect(
&t,
&m,
&tpl,
0..t.len(),
1_000_000,
&CancelToken::new(),
None,
) {
let batch = t.build_batch(&edits);
let mut t2 = t.clone();
t2.apply_batch(batch);
assert_eq!(t2.read(0..t2.len()).unwrap().as_ref(), out.as_slice());
} else {
panic!("expected edits");
}
String::from_utf8(out).unwrap()
}
#[test]
fn literal_replace() {
assert_eq!(run(b"a-b-c", "-", false, "+"), "a+b+c");
assert_eq!(run(b"a-b-c", "-", false, "$1"), "a$1b$1c");
}
#[test]
fn capture_groups() {
assert_eq!(
run(b"2026-10-24", r"(\d+)-(\d+)-(\d+)", true, "$3.$2.$1"),
"24.10.2026"
);
assert_eq!(
run(b"key=value", r"(?P<k>\w+)=(?P<v>\w+)", true, "${v}=${k}"),
"value=key"
);
}
#[test]
fn expander_reuse_matches_single_expansion() {
use crate::encoding::{Encoding, NlByte};
let text: String = (0..300)
.map(|i| format!("k{i}={} x{}\n", "v".repeat(i % 17), i * 7))
.collect();
let encodings = [
(Encoding::Utf8, Codec::utf8()),
(
Encoding::Utf16Le,
Codec::new(Encoding::Utf16Le, NlByte::Lf, 0),
),
(
Encoding::Windows1251,
Codec::new(Encoding::Windows1251, NlByte::Lf, 0),
),
];
for (enc, codec) in encodings {
let data = codec.encode(&text);
let mut f = tempfile::NamedTempFile::new().unwrap();
f.write_all(&data).unwrap();
f.flush().unwrap();
let src = Arc::new(Source::open(f.path(), OpenMode::Auto).unwrap());
let idx = index::build_blocking(&src, codec);
let mut t = TextBuf::new(src, idx, codec);
t.blocking = true;
let q = Query {
pattern: r"(\w+)=(v*) x(\d+)".into(),
regex: true,
case_sensitive: true,
..Default::default()
};
let m = Matcher::new(&q, codec).unwrap();
for rep in ["[$3|$2|$1]", "fixed"] {
let tpl = Template::new(rep, true);
let mut exp = Expander::new(&tpl, &m);
let mut n = 0;
search::search_forward(&t, &m, 0..t.len(), &CancelToken::new(), None, |s, e| {
let mut out = Vec::new();
exp.expand_into(&t, s, e, &mut out);
assert_eq!(out, tpl.expand(&t, &m, s, e), "{enc:?} {rep}");
n += 1;
true
});
assert_eq!(n, 300, "{enc:?}");
}
let mut out = Vec::new();
let tpl = Template::new("[$3|$2|$1]", true);
replace_all_to(
&t,
&m,
&tpl,
0..t.len(),
&mut out,
&CancelToken::new(),
None,
)
.unwrap()
.unwrap();
let expected: String = (0..300)
.map(|i| format!("[{}|{}|k{i}]\n", i * 7, "v".repeat(i % 17)))
.collect();
assert_eq!(codec.decode_lossy(&out), expected, "{enc:?}");
}
}
#[test]
fn escapes() {
assert_eq!(run(b"a,b,c", ",", false, "\\n"), "a\nb\nc");
assert_eq!(run(b"a,b", ",", false, "\\t\\\\"), "a\t\\b");
}
#[test]
fn empty_matches_grow_file() {
assert_eq!(run(b"ab\ncd\n", "^", true, "> "), "> ab\n> cd\n> ");
assert_eq!(run(b"abc", "x*", true, "-"), "-a-b-c-");
}
#[test]
fn shrink_to_nothing() {
assert_eq!(run(b"xxxx", "x", false, ""), "");
}
#[test]
fn threshold_switches_to_streaming() {
let (_f, t) = buf_with(&b"a ".repeat(1000));
let q = Query {
pattern: "a".into(),
case_sensitive: true,
..Default::default()
};
let m = Matcher::new(&q, Codec::utf8()).unwrap();
let tpl = Template::new("b", false);
assert!(matches!(
collect(&t, &m, &tpl, 0..t.len(), 10, &CancelToken::new(), None),
Collected::TooMany
));
}
#[test]
fn cancel_leaves_no_result() {
let (_f, t) = buf_with(b"aaaa");
let q = Query {
pattern: "a".into(),
case_sensitive: true,
..Default::default()
};
let m = Matcher::new(&q, Codec::utf8()).unwrap();
let c = CancelToken::new();
c.cancel();
let mut out = Vec::new();
assert!(
replace_all_to(&t, &m, &Template::new("b", false), 0..4, &mut out, &c, None)
.unwrap()
.is_none()
);
}
}