forked from dlang/tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathddemangle.d
78 lines (72 loc) · 2.3 KB
/
ddemangle.d
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
/**
* Demangler filter for D symbols: demangle the first D mangled symbol
* found on each line (if any) from standard input and send the
* result to standard output.
*
* Copyright: 2011 Michel Fortin
* License: <a href="http://www.boost.org/LICENSE_1_0.txt">Boost License 1.0</a>.
* Author: Michel Fortin
*/
/* Copyright Michel Fortin 2011.
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE_1_0.txt or copy at
* http://www.boost.org/LICENSE_1_0.txt)
*/
module ddemangle;
import std.stdio;
import std.ascii;
import core.demangle;
int main(string[] args)
{
if (args.length != 1)
{ // this takes care of the --help / -h case too!
stderr.writeln("Usage: ", args[0], " [-h|--help]");
stderr.writeln("Demangler filter for D symbols: demangle the first D mangled symbol
found on each line (if any) from standard input and send the result
to standard output.");
if (args.length != 2 || (args[1] != "--help" && args[1] != "-h"))
return 1; // invalid arguments
return 0; // help called normally
}
foreach (line; stdin.byLine(File.KeepTerminator.yes))
{
size_t beginIdx, endIdx;
enum State { searching_, searchingD, searchingEnd, done }
State state;
foreach (i, char c; line)
{
switch (state)
{
case State.searching_:
if (c == '_')
{
beginIdx = i;
state = State.searchingD;
}
break;
case State.searchingD:
if (c == 'D')
state = State.searchingEnd;
else if (c != '_')
state = State.searching_;
break;
case State.searchingEnd:
if (!isAlphaNum(c) && c != '_')
{
endIdx = i;
state = State.done;
}
break;
default:
break;
}
if (state == State.done)
break;
}
if (endIdx > beginIdx)
write(line[0..beginIdx], demangle(line[beginIdx..endIdx]), line[endIdx..$]);
else
write(line);
}
return 0;
}