fix: incorrect checking of strings.Split return value

strings.Split will never return a slice of length 0 if sep is not empty, so any code that checks if the return value is of length 0 is incorrect and useless.
This commit is contained in:
wwqgtxx
2025-06-25 16:17:19 +08:00
parent 166392fe17
commit 5b975275f5
5 changed files with 27 additions and 15 deletions

View File

@@ -2,8 +2,9 @@ package common
import (
"fmt"
C "github.com/metacubex/mihomo/constant"
"strings"
C "github.com/metacubex/mihomo/constant"
)
type InName struct {
@@ -36,8 +37,12 @@ func (u *InName) Payload() string {
func NewInName(iNames, adapter string) (*InName, error) {
names := strings.Split(iNames, "/")
if len(names) == 0 {
return nil, fmt.Errorf("in name couldn't be empty")
for i, name := range names {
name = strings.TrimSpace(name)
if len(name) == 0 {
return nil, fmt.Errorf("in name couldn't be empty")
}
names[i] = name
}
return &InName{

View File

@@ -2,8 +2,9 @@ package common
import (
"fmt"
C "github.com/metacubex/mihomo/constant"
"strings"
C "github.com/metacubex/mihomo/constant"
)
type InType struct {
@@ -36,8 +37,12 @@ func (u *InType) Payload() string {
func NewInType(iTypes, adapter string) (*InType, error) {
types := strings.Split(iTypes, "/")
if len(types) == 0 {
return nil, fmt.Errorf("in type couldn't be empty")
for i, tp := range types {
tp = strings.TrimSpace(tp)
if len(tp) == 0 {
return nil, fmt.Errorf("in type couldn't be empty")
}
types[i] = tp
}
tps, err := parseInTypes(types)

View File

@@ -2,8 +2,9 @@ package common
import (
"fmt"
C "github.com/metacubex/mihomo/constant"
"strings"
C "github.com/metacubex/mihomo/constant"
)
type InUser struct {
@@ -36,8 +37,12 @@ func (u *InUser) Payload() string {
func NewInUser(iUsers, adapter string) (*InUser, error) {
users := strings.Split(iUsers, "/")
if len(users) == 0 {
return nil, fmt.Errorf("in user couldn't be empty")
for i, user := range users {
user = strings.TrimSpace(user)
if len(user) == 0 {
return nil, fmt.Errorf("in user couldn't be empty")
}
users[i] = user
}
return &InUser{