|
14 | 14 |
|
15 | 15 | #[macro_use] |
16 | 16 | extern crate clap; |
17 | | -#[macro_use] |
18 | | -extern crate lazy_static; |
19 | | -extern crate regex; |
20 | 17 | extern crate rustc_demangle; |
21 | 18 |
|
22 | | -use rustc_demangle::demangle; |
23 | | -use regex::{Regex, Captures}; |
| 19 | +use rustc_demangle::demangle_stream; |
24 | 20 |
|
25 | | -use std::borrow::Cow; |
26 | 21 | use std::fs::File; |
27 | | -use std::io::{self, BufRead, BufReader, BufWriter, Write, stdin, stdout, stderr}; |
| 22 | +use std::io::{self, BufReader, BufWriter, Write, stdin, stdout, stderr}; |
28 | 23 | use std::path::{Path, PathBuf}; |
29 | 24 | use std::str::FromStr; |
30 | 25 | use std::process::exit; |
31 | 26 |
|
32 | 27 | mod tests; |
33 | 28 |
|
34 | | -lazy_static! { |
35 | | - // NOTE: Use [[:alnum::]] instead of \w to only match ASCII word characters, not unicode |
36 | | - static ref MANGLED_NAME_PATTERN: Regex = Regex::new(r"_(ZN|R)[\$\._[:alnum:]]*").unwrap(); |
37 | | -} |
38 | | - |
39 | | -#[inline] // Except for the nested functions (which don't count), this is a very small function |
40 | | -pub fn demangle_line(line: &str, include_hash: bool) -> Cow<str> { |
41 | | - MANGLED_NAME_PATTERN.replace_all(line, |captures: &Captures| { |
42 | | - let demangled = demangle(&captures[0]); |
43 | | - if include_hash { |
44 | | - demangled.to_string() |
45 | | - } else { |
46 | | - // Use alternate formatting to exclude the hash from the result |
47 | | - format!("{:#}", demangled) |
48 | | - } |
49 | | - }) |
50 | | -} |
51 | | - |
52 | | -fn demangle_stream<R: BufRead, W: Write>(input: &mut R, output: &mut W, include_hash: bool) -> io::Result<()> { |
53 | | - // NOTE: this is actually more efficient than lines(), since it re-uses the buffer |
54 | | - let mut buf = String::new(); |
55 | | - while input.read_line(&mut buf)? > 0 { |
56 | | - { |
57 | | - // NOTE: This includes the line-ending, and leaves it untouched |
58 | | - let demangled_line = demangle_line(&buf, include_hash); |
59 | | - if cfg!(debug_assertions) && buf.ends_with('\n') { |
60 | | - let line_ending = if buf.ends_with("\r\n") { "\r\n" } else { "\n" }; |
61 | | - debug_assert!(demangled_line.ends_with(line_ending), "Demangled line has incorrect line ending"); |
62 | | - } |
63 | | - output.write_all(demangled_line.as_bytes())?; |
64 | | - } |
65 | | - buf.clear(); // Reset the buffer's position, without freeing it's underlying memory |
66 | | - } |
67 | | - Ok(()) // Successfully hit EOF |
68 | | -} |
69 | | - |
70 | 29 | enum InputType { |
71 | 30 | Stdin, |
72 | 31 | File(PathBuf) |
@@ -133,7 +92,7 @@ impl OutputType { |
133 | 92 | #[inline] // It's only used twice ;) |
134 | 93 | fn demangle_names_to<S: AsRef<str>, O: io::Write>(names: &[S], output: &mut O, include_hash: bool) -> io::Result<()> { |
135 | 94 | for name in names { |
136 | | - let demangled = demangle(name.as_ref()); |
| 95 | + let demangled = rustc_demangle::demangle(name.as_ref()); |
137 | 96 | if include_hash { |
138 | 97 | writeln!(output, "{}", demangled)? |
139 | 98 | } else { |
|
0 commit comments