Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 25 additions & 91 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ readme = "README.md"
license = "Apache-2.0"

[dependencies]
rustc-demangle = "^0.1.4"
lazy_static = "^1"
regex = "^1"
rustc-demangle = { version = "0.1.23", features = ["std"] }

[dependencies.clap]
version = "^2.21.1"
Expand Down
47 changes: 3 additions & 44 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,59 +14,18 @@

#[macro_use]
extern crate clap;
#[macro_use]
extern crate lazy_static;
extern crate regex;
extern crate rustc_demangle;

use rustc_demangle::demangle;
use regex::{Regex, Captures};
use rustc_demangle::demangle_stream;

use std::borrow::Cow;
use std::fs::File;
use std::io::{self, BufRead, BufReader, BufWriter, Write, stdin, stdout, stderr};
use std::io::{self, BufReader, BufWriter, Write, stdin, stdout, stderr};
use std::path::{Path, PathBuf};
use std::str::FromStr;
use std::process::exit;

mod tests;

lazy_static! {
// NOTE: Use [[:alnum::]] instead of \w to only match ASCII word characters, not unicode
static ref MANGLED_NAME_PATTERN: Regex = Regex::new(r"_(ZN|R)[\$\._[:alnum:]]*").unwrap();
}

#[inline] // Except for the nested functions (which don't count), this is a very small function
pub fn demangle_line(line: &str, include_hash: bool) -> Cow<str> {
MANGLED_NAME_PATTERN.replace_all(line, |captures: &Captures| {
let demangled = demangle(&captures[0]);
if include_hash {
demangled.to_string()
} else {
// Use alternate formatting to exclude the hash from the result
format!("{:#}", demangled)
}
})
}

fn demangle_stream<R: BufRead, W: Write>(input: &mut R, output: &mut W, include_hash: bool) -> io::Result<()> {
// NOTE: this is actually more efficient than lines(), since it re-uses the buffer
let mut buf = String::new();
while input.read_line(&mut buf)? > 0 {
{
// NOTE: This includes the line-ending, and leaves it untouched
let demangled_line = demangle_line(&buf, include_hash);
if cfg!(debug_assertions) && buf.ends_with('\n') {
let line_ending = if buf.ends_with("\r\n") { "\r\n" } else { "\n" };
debug_assert!(demangled_line.ends_with(line_ending), "Demangled line has incorrect line ending");
}
output.write_all(demangled_line.as_bytes())?;
}
buf.clear(); // Reset the buffer's position, without freeing it's underlying memory
}
Ok(()) // Successfully hit EOF
}

enum InputType {
Stdin,
File(PathBuf)
Expand Down Expand Up @@ -133,7 +92,7 @@ impl OutputType {
#[inline] // It's only used twice ;)
fn demangle_names_to<S: AsRef<str>, O: io::Write>(names: &[S], output: &mut O, include_hash: bool) -> io::Result<()> {
for name in names {
let demangled = demangle(name.as_ref());
let demangled = rustc_demangle::demangle(name.as_ref());
if include_hash {
writeln!(output, "{}", demangled)?
} else {
Expand Down
9 changes: 7 additions & 2 deletions src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#![cfg(test)]
///! Tests demangle_line, based upon the assumption rustc_demangle works properly
///! Tests demangle_stream, based upon the assumption rustc_demangle works properly

use rustc_demangle::demangle;
use super::demangle_line;
use super::demangle_stream;

static MANGLED_NAMES: &'static [&'static str] = &[
Expand All @@ -24,6 +23,12 @@ static MANGLED_NAMES: &'static [&'static str] = &[
"_RNvNvCs1234_7mycrate4QUUX3FOO",
];

fn demangle_line(s: &str, include_hash: bool) -> std::borrow::Cow<str> {
let mut out = Vec::new();
demangle_stream(&mut s.as_bytes(), &mut out, include_hash).unwrap();
std::borrow::Cow::Owned(String::from_utf8(out).unwrap())
}

#[test]
fn ignores_text() {
for text in &["boom de yada\tboom de yada\n", "bananas are fun for everyone"] {
Expand Down