* change recursive to iterative

---------

Co-authored-by: Mattermost Build <build@mattermost.com>
Этот коммит содержится в:
Vishal
2023-05-01 19:53:18 +05:30
коммит произвёл GitHub
родитель aac4f23379
Коммит 609ab9c765

Просмотреть файл

@@ -26,53 +26,89 @@ func Inspect(markdown string, f func(any) bool) {
// InspectBlock traverses the blocks in depth-first order, starting with block. If f returns true, // InspectBlock traverses the blocks in depth-first order, starting with block. If f returns true,
// InspectBlock invokes f recursively for each child of the block, followed by a call of f(nil). // InspectBlock invokes f recursively for each child of the block, followed by a call of f(nil).
func InspectBlock(block Block, f func(Block) bool) { func InspectBlock(block Block, f func(Block) bool) {
if !f(block) { stack := []Block{block}
return // Using seen for backtracking
} seen := map[Block]bool{}
switch v := block.(type) {
case *Document: for len(stack) > 0 {
for _, child := range v.Children { // "peek" the node from the stack
InspectBlock(child, f) block := stack[len(stack)-1]
if seen[block] {
// "pop" the node only when backtracking(seen)
stack = stack[:len(stack)-1]
f(nil)
continue
} }
case *List: seen[block] = true
for _, child := range v.Children {
InspectBlock(child, f) // Process the node
if !f(block) {
continue
} }
case *ListItem:
for _, child := range v.Children { switch v := block.(type) {
InspectBlock(child, f) case *Document:
} for i := len(v.Children) - 1; i >= 0; i-- {
case *BlockQuote: stack = append(stack, v.Children[i])
for _, child := range v.Children { }
InspectBlock(child, f) case *List:
for i := len(v.Children) - 1; i >= 0; i-- {
stack = append(stack, v.Children[i])
}
case *ListItem:
for i := len(v.Children) - 1; i >= 0; i-- {
stack = append(stack, v.Children[i])
}
case *BlockQuote:
for i := len(v.Children) - 1; i >= 0; i-- {
stack = append(stack, v.Children[i])
}
} }
} }
f(nil)
} }
// InspectInline traverses the blocks in depth-first order, starting with block. If f returns true, // InspectInline traverses the blocks in depth-first order, starting with block. If f returns true,
// InspectInline invokes f recursively for each child of the block, followed by a call of f(nil). // InspectInline invokes f recursively for each child of the block, followed by a call of f(nil).
func InspectInline(inline Inline, f func(Inline) bool) { func InspectInline(inline Inline, f func(Inline) bool) {
if !f(inline) { stack := []Inline{inline}
return // Using seen for backtracking
} seen := map[Inline]bool{}
switch v := inline.(type) {
case *InlineImage: for len(stack) > 0 {
for _, child := range v.Children { // "peek" the node from the stack
InspectInline(child, f) inline := stack[len(stack)-1]
if seen[inline] {
// "pop" the node only when backtracking(seen)
stack = stack[:len(stack)-1]
f(nil)
continue
} }
case *InlineLink: seen[inline] = true
for _, child := range v.Children {
InspectInline(child, f) // Process the node
if !f(inline) {
continue
} }
case *ReferenceImage:
for _, child := range v.Children { switch v := inline.(type) {
InspectInline(child, f) case *InlineImage:
} for i := len(v.Children) - 1; i >= 0; i-- {
case *ReferenceLink: stack = append(stack, v.Children[i])
for _, child := range v.Children { }
InspectInline(child, f) case *InlineLink:
for i := len(v.Children) - 1; i >= 0; i-- {
stack = append(stack, v.Children[i])
}
case *ReferenceImage:
for i := len(v.Children) - 1; i >= 0; i-- {
stack = append(stack, v.Children[i])
}
case *ReferenceLink:
for i := len(v.Children) - 1; i >= 0; i-- {
stack = append(stack, v.Children[i])
}
} }
} }
f(nil)
} }