1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#![no_std]
#![deny(missing_docs)]
#[cfg(test)]
#[macro_use]
extern crate std;
use core::fmt;
pub struct Demangle<'a> {
original: &'a str,
inner: &'a str,
valid: bool,
}
pub fn demangle(s: &str) -> Demangle {
let mut valid = true;
let mut inner = s;
if s.len() > 4 && s.starts_with("_ZN") && s.ends_with("E") {
inner = &s[3 .. s.len() - 1];
} else if s.len() > 3 && s.starts_with("ZN") && s.ends_with("E") {
inner = &s[2 .. s.len() - 1];
} else {
valid = false;
}
if valid {
let mut chars = inner.chars();
while valid {
let mut i = 0;
for c in chars.by_ref() {
if c.is_digit(10) {
i = i * 10 + c as usize - '0' as usize;
} else {
break
}
}
if i == 0 {
valid = chars.next().is_none();
break
} else if chars.by_ref().take(i - 1).count() != i - 1 {
valid = false;
}
}
}
Demangle { inner: inner, valid: valid, original: s }
}
impl<'a> Demangle<'a> {
pub fn as_str(&self) -> &'a str {
self.original
}
}
impl<'a> fmt::Display for Demangle<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if !self.valid {
return f.write_str(self.inner);
}
let mut inner = self.inner;
let mut first = true;
while !inner.is_empty() {
if !first {
try!(f.write_str("::"));
} else {
first = false;
}
let mut rest = inner;
while rest.chars().next().unwrap().is_digit(10) {
rest = &rest[1..];
}
let i: usize = inner[..(inner.len() - rest.len())].parse().unwrap();
inner = &rest[i..];
rest = &rest[..i];
while !rest.is_empty() {
if rest.starts_with("$") {
macro_rules! demangle {
($($pat:expr, => $demangled:expr),*) => ({
$(if rest.starts_with($pat) {
try!(f.write_str($demangled));
rest = &rest[$pat.len()..];
} else)*
{
try!(f.write_str(rest));
break;
}
})
}
demangle! {
"$SP$", => "@",
"$BP$", => "*",
"$RF$", => "&",
"$LT$", => "<",
"$GT$", => ">",
"$LP$", => "(",
"$RP$", => ")",
"$C$", => ",",
"$u7e$", => "~",
"$u20$", => " ",
"$u27$", => "'",
"$u5b$", => "[",
"$u5d$", => "]",
"$u7b$", => "{",
"$u7d$", => "}"
}
} else {
let idx = rest.find('$').unwrap_or(rest.len());
try!(f.write_str(&rest[..idx]));
rest = &rest[idx..];
}
}
}
Ok(())
}
}
impl<'a> fmt::Debug for Demangle<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(self, f)
}
}
#[cfg(test)]
mod tests {
use std::prelude::v1::*;
macro_rules! t {
($a:expr, $b:expr) => ({
assert_eq!(super::demangle($a).to_string(), $b);
})
}
#[test]
fn demangle() {
t!("test", "test");
t!("_ZN4testE", "test");
t!("_ZN4test", "_ZN4test");
t!("_ZN4test1a2bcE", "test::a::bc");
}
#[test]
fn demangle_dollars() {
t!("_ZN4$RP$E", ")");
t!("_ZN8$RF$testE", "&test");
t!("_ZN8$BP$test4foobE", "*test::foob");
t!("_ZN9$u20$test4foobE", " test::foob");
}
#[test]
fn demangle_many_dollars() {
t!("_ZN13test$u20$test4foobE", "test test::foob");
t!("_ZN12test$BP$test4foobE", "test*test::foob");
}
#[test]
fn demangle_windows() {
t!("ZN4testE", "test");
t!("ZN13test$u20$test4foobE", "test test::foob");
t!("ZN12test$RF$test4foobE", "test&test::foob");
}
}