chore: Replace stack collection with list

This commit is contained in:
H1JK 2023-11-20 23:48:07 +08:00
parent a6b816b1c6
commit b05cf14b98
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 ( import (
"fmt" "fmt"
list "github.com/bahlo/generic-list-go"
"regexp" "regexp"
"strings" "strings"
"github.com/metacubex/mihomo/common/collections"
C "github.com/metacubex/mihomo/constant" C "github.com/metacubex/mihomo/constant"
"github.com/metacubex/mihomo/rules/common" "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) { func (logic *Logic) format(payload string) ([]Range, error) {
stack := collections.NewStack() stack := list.New[Range]()
num := 0 num := 0
subRanges := make([]Range, 0) subRanges := make([]Range, 0)
for i, c := range payload { for i, c := range payload {
@ -144,15 +144,16 @@ func (logic *Logic) format(payload string) ([]Range, error) {
} }
num++ num++
stack.Push(sr) stack.PushBack(sr)
} else if c == ')' { } else if c == ')' {
if stack.Len() == 0 { if stack.Len() == 0 {
return nil, fmt.Errorf("missing '('") return nil, fmt.Errorf("missing '('")
} }
sr := stack.Pop().(Range) sr := stack.Back()
sr.end = i stack.Remove(sr)
subRanges = append(subRanges, sr) sr.Value.end = i
subRanges = append(subRanges, sr.Value)
} }
} }