Files
game-driver/leaf/utils.go
2024-11-01 17:40:34 +08:00

65 lines
1.1 KiB
Go

package leaf
import (
"reflect"
"runtime"
"strings"
)
func assert1(guard bool, text string) {
if !guard {
panic(text)
}
}
func nameOfFunction(f any) string {
return runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
}
func match(route, topic string) bool {
return route == topic || routeIncludesTopic(route, topic)
}
func matchDeep(route []string, topic []string) bool {
if len(route) == 0 {
return len(topic) == 0
}
if len(topic) == 0 {
return route[0] == "#"
}
if route[0] == "#" {
return true
}
if (route[0] == "+") || (route[0] == topic[0]) {
return matchDeep(route[1:], topic[1:])
}
return false
}
func routeIncludesTopic(route, topic string) bool {
return matchDeep(routeSplit(route), topicSplit(topic))
}
func routeSplit(route string) []string {
if len(route) == 0 {
return nil
}
var result []string
if strings.HasPrefix(route, "$share") {
result = strings.Split(route, "/")[2:]
} else {
result = strings.Split(route, "/")
}
return result
}
func topicSplit(topic string) []string {
if len(topic) == 0 {
return nil
}
return strings.Split(topic, "/")
}