pub fn strip_comments(src: &str) -> String {
let b = src.as_bytes();
let mut out = String::with_capacity(src.len());
let mut i = 0usize;
let mut start = 0usize;
while i < b.len() {
match b[i] {
b'"' => {
i += 1;
while i < b.len() {
match b[i] {
b'\\' => i += 2,
b'"' => {
i += 1;
break;
}
_ => i += 1,
}
}
}
b'/' if b.get(i + 1) == Some(&b'/') => {
out.push_str(&src[start..i]); while i < b.len() && b[i] != b'\n' {
i += 1;
}
start = i; }
b'/' if b.get(i + 1) == Some(&b'*') => {
out.push_str(&src[start..i]);
i += 2;
while i + 1 < b.len() && !(b[i] == b'*' && b[i + 1] == b'/') {
i += 1;
}
i = (i + 2).min(b.len());
start = i;
}
_ => i += 1,
}
}
out.push_str(&src[start..]);
let mut result = String::with_capacity(out.len());
for line in out.lines() {
if !line.trim().is_empty() {
result.push_str(line.trim_end());
result.push('\n');
}
}
result
}
pub fn strip_cfg_test_blocks(src: &str) -> String {
let lines: Vec<&str> = src.lines().collect();
let mut out: Vec<&str> = Vec::with_capacity(lines.len());
let mut i = 0usize;
while i < lines.len() {
let trimmed = lines[i].trim();
if trimmed == "#[cfg(test)]" || trimmed.starts_with("#[cfg(test)]") {
let mut j = i + 1;
let mut depth: i32 = 0;
let mut opened = false;
let mut in_str = false;
let mut escaped = false;
while j < lines.len() {
for c in lines[j].chars() {
if in_str {
if escaped {
escaped = false;
} else if c == '\\' {
escaped = true;
} else if c == '"' {
in_str = false;
}
continue;
}
match c {
'"' => in_str = true,
'{' => {
depth += 1;
opened = true;
}
'}' => depth -= 1,
_ => {}
}
}
if opened && depth <= 0 {
j += 1;
break;
}
if !opened && lines[j].trim_end().ends_with(';') {
j += 1;
break;
}
j += 1;
}
i = j;
continue;
}
out.push(lines[i]);
i += 1;
}
out.join("\n")
}
pub fn reduce_source(src: &str) -> String {
strip_cfg_test_blocks(&strip_comments(src))
}
struct FnBody {
name: String,
open: usize,
close: usize,
}
fn scan_fn_bodies(content: &str) -> Vec<FnBody> {
let b = content.as_bytes();
let mut out = Vec::new();
let mut i = 0usize;
while i + 3 <= b.len() {
let is_fn_kw = b[i] == b'f'
&& b[i + 1] == b'n'
&& b[i + 2] == b' '
&& (i == 0 || matches!(b[i - 1], b' ' | b'\t' | b'\n' | b'(' | b'<'));
if !is_fn_kw {
i += 1;
continue;
}
let after = i + 3;
let name: String = content[after..]
.chars()
.take_while(|c| c.is_alphanumeric() || *c == '_')
.collect();
if name.is_empty() {
i += 3;
continue;
}
let mut j = after + name.len();
let mut open = None;
while j < b.len() {
match b[j] {
b'{' => {
open = Some(j);
break;
}
b';' => break,
_ => j += 1,
}
}
let Some(open) = open else {
i = (j + 1).max(i + 3);
continue;
};
let (mut depth, mut k) = (0i32, open);
let (mut in_str, mut escaped) = (false, false);
let close = loop {
if k >= b.len() {
break b.len();
}
let c = b[k];
if in_str {
if escaped {
escaped = false;
} else if c == b'\\' {
escaped = true;
} else if c == b'"' {
in_str = false;
}
} else {
match c {
b'"' => in_str = true,
b'{' => depth += 1,
b'}' => {
depth -= 1;
if depth == 0 {
break k + 1;
}
}
_ => {}
}
}
k += 1;
};
out.push(FnBody { name, open, close });
i = close;
}
out
}
fn normalize_body(body: &str) -> String {
body.lines()
.map(str::trim)
.filter(|l| !l.is_empty())
.collect::<Vec<_>>()
.join("\n")
}
pub fn dedup_context(files: &[(String, String)]) -> (Vec<(String, String)>, usize) {
use std::collections::HashMap;
let mut canonical: HashMap<String, String> = HashMap::new(); let mut deduped = Vec::with_capacity(files.len());
let mut elided = 0usize;
for (path, content) in files {
let bodies = scan_fn_bodies(content);
let mut cursor = 0usize;
let mut out = String::with_capacity(content.len());
for f in &bodies {
let body = &content[f.open..f.close];
let key = normalize_body(body);
if key.lines().count() < 3 {
continue; }
match canonical.get(&key) {
Some(src) => {
let stub = format!("{{ /* deduped: identical to {src} */ }}");
if stub.len() * 2 >= body.len() {
continue;
}
out.push_str(&content[cursor..f.open]);
out.push_str(&stub);
cursor = f.close;
elided += 1;
}
None => {
canonical.insert(key, format!("{path}::{}", f.name));
}
}
}
out.push_str(&content[cursor..]);
deduped.push((path.clone(), out));
}
(deduped, elided)
}
#[cfg(test)]
#[path = "../../tests/unit/evolve/context_reduce_test.rs"]
mod context_reduce_test;