CodeKicker.BBCode.SyntaxTree.SyntaxTreeVisitor.GetModifiedSubNodes C# (CSharp) Method

GetModifiedSubNodes() private method

private GetModifiedSubNodes ( SyntaxTreeNode node ) : SyntaxTreeNodeCollection
node SyntaxTreeNode
return SyntaxTreeNodeCollection
        SyntaxTreeNodeCollection GetModifiedSubNodes(SyntaxTreeNode node)
        {
            SyntaxTreeNodeCollection modifiedSubNodes = null; //lazy

            for (int i = 0; i < node.SubNodes.Count; i++)
            {
                var subNode = node.SubNodes[i];

                var replacement = Visit(subNode);
                if (replacement != subNode)
                {
                    if (modifiedSubNodes == null) //lazy init
                    {
                        modifiedSubNodes = new SyntaxTreeNodeCollection();
                        for (int j = 0; j < i; j++) //copy unmodified nodes
                            modifiedSubNodes.Add(node.SubNodes[j]);
                    }

                    if (replacement != null) //insert replacement
                        modifiedSubNodes.Add(replacement);
                }
                else
                {
                    if (modifiedSubNodes != null) //only insert unmodified subnode if the lazy collection has been initialized
                        modifiedSubNodes.Add(subNode);
                }
            }
            return modifiedSubNodes;
        }
    }