chore: Replace stack collection with list

This commit is contained in:
gVisor bot 2023-11-20 23:48:07 +08:00
parent bf64cede7d
commit a8005dfe02
2 changed files with 7 additions and 62 deletions

View file

@ -1,56 +0,0 @@
package collections
import "sync"
type (
stack struct {
top *node
length int
lock *sync.RWMutex
}
node struct {
value interface{}
prev *node
}
)
// NewStack Create a new stack
func NewStack() *stack {
return &stack{nil, 0, &sync.RWMutex{}}
}
// Len Return the number of items in the stack
func (this *stack) Len() int {
return this.length
}
// Peek View the top item on the stack
func (this *stack) Peek() interface{} {
if this.length == 0 {
return nil
}
return this.top.value
}
// Pop the top item of the stack and return it
func (this *stack) Pop() interface{} {
this.lock.Lock()
defer this.lock.Unlock()
if this.length == 0 {
return nil
}
n := this.top
this.top = n.prev
this.length--
return n.value
}
// Push a value onto the top of the stack
func (this *stack) Push(value interface{}) {
this.lock.Lock()
defer this.lock.Unlock()
n := &node{value, this.top}
this.top = n
this.length++
}

View file

@ -2,10 +2,10 @@ package logic
import (
"fmt"
list "github.com/bahlo/generic-list-go"
"regexp"
"strings"
"github.com/metacubex/mihomo/common/collections"
C "github.com/metacubex/mihomo/constant"
"github.com/metacubex/mihomo/rules/common"
)
@ -133,7 +133,7 @@ func (logic *Logic) payloadToRule(subPayload string, parseRule ParseRuleFunc) (C
}
func (logic *Logic) format(payload string) ([]Range, error) {
stack := collections.NewStack()
stack := list.New[Range]()
num := 0
subRanges := make([]Range, 0)
for i, c := range payload {
@ -144,15 +144,16 @@ func (logic *Logic) format(payload string) ([]Range, error) {
}
num++
stack.Push(sr)
stack.PushBack(sr)
} else if c == ')' {
if stack.Len() == 0 {
return nil, fmt.Errorf("missing '('")
}
sr := stack.Pop().(Range)
sr.end = i
subRanges = append(subRanges, sr)
sr := stack.Back()
stack.Remove(sr)
sr.Value.end = i
subRanges = append(subRanges, sr.Value)
}
}