pub(super) fn strip_macro_bodies(source: &str) -> String {
let identity: Vec<usize> = (0..source.len()).collect();
strip_macro_bodies_tracked(source, &identity).0
}
pub(super) fn strip_macro_bodies_tracked(
source: &str,
input_positions: &[usize],
) -> (String, Vec<usize>) {
let bytes = source.as_bytes();
let mut out: Vec<u8> = Vec::with_capacity(bytes.len());
let mut positions: Vec<usize> = Vec::with_capacity(bytes.len());
let mut i = 0;
while i < bytes.len() {
if let Some(end) = macro_rules_body_end(bytes, i) {
out.push(b' ');
positions.push(input_positions[i]);
i = end;
} else if bytes[i] == b'!' && preceding_macro_name(bytes, i) {
match macro_invocation_body_end(bytes, i) {
Some(end) => {
out.push(b'!');
positions.push(input_positions[i]);
out.push(b' ');
positions.push(input_positions[i]);
i = end;
}
None => {
out.push(bytes[i]);
positions.push(input_positions[i]);
i += 1;
}
}
} else {
out.push(bytes[i]);
positions.push(input_positions[i]);
i += 1;
}
}
(String::from_utf8_lossy(&out).into_owned(), positions)
}
fn macro_rules_body_end(bytes: &[u8], i: usize) -> Option<usize> {
const KW: &[u8] = b"macro_rules";
if !bytes[i..].starts_with(KW) || (i > 0 && is_ident_byte(bytes[i - 1])) {
return None;
}
let skip_ws = |mut j: usize| {
while j < bytes.len() && bytes[j].is_ascii_whitespace() {
j += 1;
}
j
};
let mut j = skip_ws(i + KW.len());
if bytes.get(j) != Some(&b'!') {
return None;
}
j = skip_ws(j + 1);
if bytes[j..].starts_with(b"r#") {
j += 2;
}
let name_start = j;
while j < bytes.len() && is_ident_byte(bytes[j]) {
j += 1;
}
if j == name_start {
return None;
}
balanced_group_end(bytes, skip_ws(j))
}
fn macro_invocation_body_end(bytes: &[u8], i: usize) -> Option<usize> {
let mut j = i + 1;
while j < bytes.len() && bytes[j].is_ascii_whitespace() {
j += 1;
}
balanced_group_end(bytes, j)
}
fn preceding_macro_name(bytes: &[u8], bang: usize) -> bool {
let mut end = bang;
while end > 0 && bytes[end - 1].is_ascii_whitespace() {
end -= 1;
}
let mut start = end;
while start > 0 && is_ident_byte(bytes[start - 1]) {
start -= 1;
}
if start == end {
return false; }
is_raw_ident_prefixed(bytes, start) || !is_rust_keyword(&bytes[start..end])
}
fn is_rust_keyword(word: &[u8]) -> bool {
let Ok(word) = std::str::from_utf8(word) else {
return false;
};
matches!(
word,
"as" | "break"
| "const"
| "continue"
| "crate"
| "dyn"
| "else"
| "enum"
| "extern"
| "false"
| "fn"
| "for"
| "if"
| "impl"
| "in"
| "let"
| "loop"
| "match"
| "mod"
| "move"
| "mut"
| "pub"
| "ref"
| "return"
| "self"
| "Self"
| "static"
| "struct"
| "super"
| "trait"
| "true"
| "type"
| "unsafe"
| "use"
| "where"
| "while"
| "async"
| "await"
| "abstract"
| "become"
| "box"
| "do"
| "final"
| "macro"
| "override"
| "priv"
| "typeof"
| "unsized"
| "virtual"
| "yield"
| "try"
| "gen"
)
}
pub(super) fn balanced_group_end(bytes: &[u8], j: usize) -> Option<usize> {
let (open, close) = match bytes.get(j) {
Some(b'{') => (b'{', b'}'),
Some(b'(') => (b'(', b')'),
Some(b'[') => (b'[', b']'),
_ => return None,
};
let mut depth = 0usize;
let mut k = j;
while k < bytes.len() {
if bytes[k] == open {
depth += 1;
} else if bytes[k] == close {
depth -= 1;
if depth == 0 {
return Some(k + 1);
}
}
k += 1;
}
Some(bytes.len())
}
pub(super) fn strip_comments_and_strings(source: &str) -> String {
strip_comments_and_strings_tracked(source).0
}
pub(super) fn strip_comments_and_strings_tracked(source: &str) -> (String, Vec<usize>) {
let bytes = source.as_bytes();
let mut out: Vec<u8> = Vec::with_capacity(bytes.len());
let mut positions: Vec<usize> = Vec::with_capacity(bytes.len());
let mut i = 0;
while i < bytes.len() {
if bytes[i] == b'/' && i + 1 < bytes.len() && bytes[i + 1] == b'/' {
while i < bytes.len() && bytes[i] != b'\n' {
i += 1;
}
} else if bytes[i] == b'/' && i + 1 < bytes.len() && bytes[i + 1] == b'*' {
i += 2;
let mut depth = 1usize;
while i + 1 < bytes.len() && depth > 0 {
if bytes[i] == b'/' && bytes[i + 1] == b'*' {
depth += 1;
i += 2;
} else if bytes[i] == b'*' && bytes[i + 1] == b'/' {
depth -= 1;
i += 2;
} else {
i += 1;
}
}
out.push(b' ');
positions.push(i);
} else if let Some((hashes, quote)) = raw_string_prefix(bytes, i) {
i = quote + 1;
while i < bytes.len() {
if bytes[i] == b'"' && raw_closing_matches(bytes, i + 1, hashes) {
i += 1 + hashes;
break;
}
i += 1;
}
} else if bytes[i] == b'"' {
i += 1;
while i < bytes.len() && bytes[i] != b'"' {
i += if bytes[i] == b'\\' { 2 } else { 1 };
}
i += 1;
} else if bytes[i] == b'\'' {
if i + 1 < bytes.len() && bytes[i + 1] == b'\\' {
i += 2;
if i < bytes.len() {
i += 1;
}
while i < bytes.len() && bytes[i] != b'\'' {
i += 1;
}
i += 1;
} else if i + 2 < bytes.len() && bytes[i + 2] == b'\'' {
i += 3;
} else {
out.push(bytes[i]);
positions.push(i);
i += 1;
}
} else {
out.push(bytes[i]);
positions.push(i);
i += 1;
}
}
(String::from_utf8_lossy(&out).into_owned(), positions)
}
fn raw_string_prefix(bytes: &[u8], i: usize) -> Option<(usize, usize)> {
if i > 0 && is_ident_byte(bytes[i - 1]) {
return None;
}
let mut j = i;
if matches!(bytes.get(j), Some(&b'b') | Some(&b'c')) {
j += 1;
}
if bytes.get(j) != Some(&b'r') {
return None;
}
j += 1;
let mut hashes = 0;
while bytes.get(j) == Some(&b'#') {
hashes += 1;
j += 1;
}
if bytes.get(j) == Some(&b'"') {
Some((hashes, j))
} else {
None
}
}
fn raw_closing_matches(bytes: &[u8], at: usize, hashes: usize) -> bool {
(0..hashes).all(|k| bytes.get(at + k) == Some(&b'#'))
}
pub(super) fn keyword_starts_at(bytes: &[u8], i: usize, keyword: &[u8]) -> bool {
if !bytes[i..].starts_with(keyword) {
return false;
}
let before_ok = !is_raw_ident_prefixed(bytes, i) && (i == 0 || !is_ident_byte(bytes[i - 1]));
let after = i + keyword.len();
let after_ok = after >= bytes.len() || !is_ident_byte(bytes[after]);
before_ok && after_ok
}
fn is_raw_ident_prefixed(bytes: &[u8], pos: usize) -> bool {
pos >= 2
&& bytes[pos - 1] == b'#'
&& bytes[pos - 2] == b'r'
&& (pos == 2 || !is_ident_byte(bytes[pos - 3]))
}
pub(super) fn is_ident_byte(byte: u8) -> bool {
byte == b'_' || byte.is_ascii_alphanumeric() || byte >= 0x80
}
pub(super) fn clean_with_positions(source: &str) -> (String, Vec<usize>) {
let (stripped, positions) = strip_comments_and_strings_tracked(source);
strip_macro_bodies_tracked(&stripped, &positions)
}
pub(super) fn read_path_string(bytes: &[u8], start: usize, end: usize) -> Option<String> {
let mut i = start;
while i < end {
if bytes[i].is_ascii_whitespace() {
i += 1;
continue;
}
if bytes[i] == b'/' && matches!(bytes.get(i + 1), Some(&b'/') | Some(&b'*')) {
if bytes[i + 1] == b'/' {
while i < end && bytes[i] != b'\n' {
i += 1;
}
} else {
i += 2;
let mut depth = 1usize;
while i + 1 < end && depth > 0 {
if bytes[i] == b'/' && bytes[i + 1] == b'*' {
depth += 1;
i += 2;
} else if bytes[i] == b'*' && bytes[i + 1] == b'/' {
depth -= 1;
i += 2;
} else {
i += 1;
}
}
}
continue;
}
break;
}
if bytes.get(i) == Some(&b'r') {
let mut hashes = 0usize;
let mut j = i + 1;
while bytes.get(j) == Some(&b'#') {
hashes += 1;
j += 1;
}
if bytes.get(j) != Some(&b'"') {
return None;
}
j += 1;
let content_start = j;
while j < end {
if bytes[j] == b'"' {
let mut k = j + 1;
let mut seen = 0usize;
while seen < hashes && bytes.get(k) == Some(&b'#') {
k += 1;
seen += 1;
}
if seen == hashes {
return String::from_utf8(bytes[content_start..j].to_vec()).ok();
}
}
j += 1;
}
return None;
}
if bytes.get(i) != Some(&b'"') {
return None;
}
i += 1;
let content_start = i;
while i < end {
match bytes[i] {
b'"' => return decode_str_escapes(&bytes[content_start..i]),
b'\\' => i += 2,
_ => i += 1,
}
}
None
}
fn decode_str_escapes(inner: &[u8]) -> Option<String> {
let s = std::str::from_utf8(inner).ok()?;
let mut out = String::with_capacity(s.len());
let mut chars = s.chars();
while let Some(c) = chars.next() {
if c != '\\' {
out.push(c);
continue;
}
match chars.next()? {
'n' => out.push('\n'),
'r' => out.push('\r'),
't' => out.push('\t'),
'\\' => out.push('\\'),
'0' => out.push('\0'),
'\'' => out.push('\''),
'"' => out.push('"'),
'x' => {
let hi = chars.next()?.to_digit(16)?;
let lo = chars.next()?.to_digit(16)?;
let v = hi * 16 + lo;
if v > 0x7F {
return None;
}
out.push(char::from_u32(v)?);
}
'u' => {
if chars.next()? != '{' {
return None;
}
let mut value: u32 = 0;
let mut digits = 0;
loop {
match chars.next()? {
'}' => break,
'_' if digits == 0 => return None,
'_' => continue,
d => {
let hd = d.to_digit(16)?;
digits += 1;
if digits > 6 {
return None;
}
value = value * 16 + hd;
}
}
}
if digits == 0 {
return None;
}
out.push(char::from_u32(value)?);
}
_ => return None,
}
}
Some(out)
}