View difference between Paste ID: VgapJzMc and
SHOW:
|
|
- or go back to the newest paste.
1 | - | |
1 | + | sealed class MyDumper |
2 | { | |
3 | private class Helper | |
4 | { | |
5 | public Node Node; | |
6 | public Helper Parent; | |
7 | public string ParentIndent; | |
8 | public bool Last; | |
9 | } | |
10 | public static string Dump(Node root) | |
11 | { | |
12 | var sb = new StringBuilder(); | |
13 | var workStack = new Stack<Helper>(); | |
14 | var droot = new Helper {Parent = null, Node = root, ParentIndent = ""}; | |
15 | workStack.Push(droot); | |
16 | while (workStack.Count > 0) | |
17 | { | |
18 | var current = workStack.Pop(); | |
19 | if (current.Parent != null) | |
20 | { | |
21 | sb.Append(current.Parent.ParentIndent); | |
22 | current.ParentIndent = current.Parent.ParentIndent + | |
23 | (current.Last?" ":"│")+ | |
24 | " "; | |
25 | // my indent | |
26 | sb.Append(current.Last ? "└" : "├"); | |
27 | sb.Append("-"); | |
28 | } | |
29 | else current.ParentIndent = ""; | |
30 | sb.AppendLine(current.Node.Text); | |
31 | for (int index = current.Node.Children.Count-1; index>=0; index--) | |
32 | { | |
33 | var node = current.Node.Children[index]; | |
34 | var dnode = new Helper {Node = node, Parent = current, Last = index==current.Node.Children.Count-1}; | |
35 | workStack.Push(dnode); | |
36 | } | |
37 | } | |
38 | return sb.ToString(); | |
39 | } | |
40 | } |