use std::borrow::Cow;
use std::str;
use regex::{Captures, Regex};
pub fn demangle(bytes: &[u8]) -> Cow<'_, [u8]> {
let re = Regex::new(r"_Z.+?E\b").expect("BUG: Malformed Regex");
if let Ok(text) = str::from_utf8(bytes) {
match re.replace_all(text, |cs: &Captures<'_>| {
format!("{}", rustc_demangle::demangle(cs.get(0).unwrap().as_str()))
}) {
Cow::Borrowed(s) => s.as_bytes().into(),
Cow::Owned(s) => s.into_bytes().into(),
}
} else {
bytes.into()
}
}
pub fn size(bytes: &[u8]) -> Cow<'_, [u8]> {
if let Ok(text) = str::from_utf8(bytes) {
let mut s = text
.lines()
.map(|line| -> Cow<'_, str> {
match line
.split_whitespace()
.nth(2)
.and_then(|part| part.parse::<u64>().ok().map(|addr| (part, addr)))
{
Some((needle, addr)) if line.starts_with('.') => {
let pos = line.rfind(needle).unwrap();
let hex_addr = format!("{addr:#x}");
let start = pos + needle.len() - hex_addr.len();
format!("{}{}", &line[..start], hex_addr).into()
}
_ => line.into(),
}
})
.collect::<Vec<_>>()
.join("\n");
s.push('\n');
s.into_bytes().into()
} else {
bytes.into()
}
}