The OpenD Programming Language

1 // Written in the D programming language.
2 
3 /**
4  * Demangle D mangled names.
5  *
6  * Copyright: Copyright The D Language Foundation 2000 - 2009.
7  * License:   $(HTTP www.boost.org/LICENSE_1_0.txt, Boost License 1.0).
8  * Authors:   $(HTTP digitalmars.com, Walter Bright),
9  *                        Thomas K$(UUML)hne, Frits van Bommel
10  * Source:    $(PHOBOSSRC std/demangle.d)
11  * $(SCRIPT inhibitQuickIndex = 1;)
12  */
13 /*
14  *          Copyright The D Language Foundation 2000 - 2009.
15  * Distributed under the Boost Software License, Version 1.0.
16  *    (See accompanying file LICENSE_1_0.txt or copy at
17  *          http://www.boost.org/LICENSE_1_0.txt)
18  */
19 module std.demangle;
20 
21 /**
22 Demangle D mangled names.
23 
24 Params:
25     name = the mangled name
26 Returns:
27     A `string`. If it is not a D mangled name, it returns its argument name.
28  */
29 string demangle(string name) @safe pure nothrow
30 {
31     import core.demangle : demangle;
32     import std.exception : assumeUnique;
33     auto ret = demangle(name);
34     return () @trusted { return ret.assumeUnique; } ();
35 }
36 
37 ///
38 @safe pure unittest
39 {
40     // int b in module a
41     assert(demangle("_D1a1bi") == "int a.b");
42     // char array foo in module test
43     assert(demangle("_D4test3fooAa") == "char[] test.foo");
44 }
45 
46 /**
47 This program reads standard in and writes it to standard out,
48 pretty-printing any found D mangled names.
49  */
50 @system unittest
51 {
52     import std.ascii : isAlphaNum;
53     import std.algorithm.iteration : chunkBy, joiner, map;
54     import std.algorithm.mutation : copy;
55     import std.conv : to;
56     import std.demangle : demangle;
57     import std.functional : pipe;
58     import std.stdio : stdin, stdout;
59 
60     void main()
61     {
62         stdin.byLineCopy
63             .map!(
64                 l => l.chunkBy!(a => isAlphaNum(a) || a == '_')
65                       .map!(a => a[1].pipe!(to!string, demangle)).joiner
66             )
67             .copy(stdout.lockingTextWriter);
68     }
69 }