Core - Update PckAudioFile class

This commit is contained in:
miku-666
2025-12-08 16:47:18 +01:00
parent c5bfe97219
commit 13516e204e
10 changed files with 284 additions and 279 deletions

View File

@@ -25,23 +25,35 @@ namespace PckStudio.Core.Extensions
return node;
}
public static TreeNode BuildNodeTreeBySeperator(this TreeNodeCollection root, string path, char seperator)
private static (string, string) Slice(this string s, int i)
=> (s.Substring(0, i), s.Substring(i + 1));
public static TreeNode BuildNodeTreeBySeperator(this TreeNodeCollection root, string path, char seperator, int maxDepth = -1)
=> root.BuildNodeTreeBySeperator(path, seperator.ToString(), maxDepth);
public static TreeNode BuildNodeTreeBySeperator(this TreeNodeCollection root, string path, string seperator, int maxDepth = -1)
{
_ = root ?? throw new ArgumentNullException(nameof(root));
if (!path.Contains(seperator))
{
if (maxDepth == 0 || !path.Contains(seperator))
return root.CreateNode(path);
}
string nodeText = path.Substring(0, path.IndexOf(seperator));
string subPath = path.Substring(path.IndexOf(seperator) + 1);
(string nodeText, string subPath) = path.Slice(path.IndexOf(seperator));
if (string.IsNullOrWhiteSpace(nodeText))
{
return BuildNodeTreeBySeperator(root, subPath, seperator);
}
return BuildNodeTreeBySeperator(root, subPath, seperator, maxDepth - 1);
TreeNode subNode = root.ContainsKey(nodeText) ? root[nodeText] : root.CreateNode(nodeText);
return BuildNodeTreeBySeperator(subNode.Nodes, subPath, seperator);
return BuildNodeTreeBySeperator(subNode.Nodes, subPath, seperator, maxDepth - 1);
}
public static IEnumerable<TreeNode> GetLeafNodes(this TreeNodeCollection root)
{
foreach (TreeNode node in root)
{
if (node.Nodes.Count == 0)
yield return node;
foreach (TreeNode ln in node.Nodes.GetLeafNodes())
yield return ln;
}
}
}
}