Skip to content

Commit

Permalink
use regex.replace_all instead of loops
Browse files Browse the repository at this point in the history
  • Loading branch information
ithinuel committed Mar 24, 2017
1 parent 79718d7 commit 840551a
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cargo-demangle"
version = "0.1.0"
version = "0.1.1"
authors = ["Wilfried C. <ithinuel@gmail.com>"]
license = "MIT"
keywords = ["cargo", "demangle", "subcommand"]
Expand Down
17 changes: 3 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ extern crate regex;

use std::io::{Read, Write};
use std::fs::File;
use regex::Regex;
use regex::{Regex, Captures};
use rustc_demangle::demangle;

const CARGO: &'static str = "cargo";
Expand All @@ -28,27 +28,16 @@ fn main() {
}

fn do_demangle(filename: String) {
let mut symbols = std::collections::BTreeMap::new();

let re = Regex::new(r"(?m)(?P<symbol>(_ZN[0-9]+.*E))").unwrap();

let mut txt = String::new();
let mut file = File::open(&filename).unwrap();
file.read_to_string(&mut txt).unwrap();
drop(file);

for cap in re.captures_iter(&txt) {
let symbol = &cap["symbol"];
if !symbols.contains_key(symbol) {
symbols.insert(symbol.to_string(), format!("{}", demangle(symbol)));
}
}

for (mangled, demangled) in symbols.iter() {
txt = txt.replace(mangled, demangled);
}
let result = re.replace_all(&txt, |caps: &Captures| format!("{}", demangle(&caps["symbol"])));

let mut file = File::create(&filename).unwrap();
file.write_all(txt.as_bytes()).unwrap();
file.write_all(result.as_bytes()).unwrap();
drop(file);
}

0 comments on commit 840551a

Please sign in to comment.