private static string ToStringUnsafe <T>(this ITreeNode <T> root, StringFormat sf) { switch (sf) { case StringFormat.Default: return($"{root.GetType()}: cur: {root.Current}, deep: [{root.ChildrenToString(sf, ", ")}]"); case StringFormat.NewLine: { string[] lines = $"{root.Current}{(root.Count > 0 ? ":" : "")}\n<NEEDTAB_TreeNode>{root.ChildrenToString(sf, "\n")}\n</NEEDTAB_TreeNode>".Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries); int count = 0, countOpen, countClose; Regex regOpen = new Regex("<NEEDTAB_TreeNode>"); Regex regClose = new Regex("</NEEDTAB_TreeNode>"); for (long i = 0; i < lines.LongLength; i++) { countOpen = regOpen.Matches(lines[i]).Count; countClose = regClose.Matches(lines[i]).Count; count += countOpen - countClose; if (count > 0) { lines[i] = lines[i].Insert(0, new string('\t', count)); } if (countOpen != 0) { lines[i] = lines[i].Replace("<NEEDTAB_TreeNode>", ""); } if (countClose != 0) { lines[i] = lines[i].Replace("</NEEDTAB_TreeNode>", ""); } } Regex needRemove = new Regex("^[\\t| ]+$"); List <string> LinesWithoutSpace = new List <string>(lines); LinesWithoutSpace.RemoveAll(str => needRemove.Match(str).Success || str.Length == 0); return(string.Join("\n", LinesWithoutSpace)); } case StringFormat.Base: return(root.GetType().ToString()); default: throw new NotSupportedException($"Формат {sf} не поддерживается."); } }