small string split join optimization

This commit is contained in:
yusing
2024-12-19 00:54:31 +08:00
parent 654194b274
commit e7be27413c
20 changed files with 160 additions and 50 deletions

View File

@@ -2,17 +2,17 @@ package types
import (
"net/http"
"strings"
E "github.com/yusing/go-proxy/internal/error"
"github.com/yusing/go-proxy/internal/utils/strutils"
)
func ValidateHTTPHeaders(headers map[string]string) (http.Header, E.Error) {
h := make(http.Header)
for k, v := range headers {
vSplit := strings.Split(v, ",")
vSplit := strutils.CommaSeperatedList(v)
for _, header := range vSplit {
h.Add(k, strings.TrimSpace(header))
h.Add(k, header)
}
}
return h, nil

View File

@@ -173,14 +173,14 @@ func (e *RawEntry) Finalize() {
}
func (e *RawEntry) splitPorts() (lp string, pp string, extra string) {
portSplit := strings.Split(e.Port, ":")
portSplit := strutils.SplitRune(e.Port, ':')
if len(portSplit) == 1 {
pp = portSplit[0]
} else {
lp = portSplit[0]
pp = portSplit[1]
if len(portSplit) > 2 {
extra = strings.Join(portSplit[2:], ":")
extra = strutils.JoinRune(portSplit[2:], ':')
}
}
return
@@ -197,7 +197,7 @@ func joinPorts(lp string, pp string, extra string) string {
if extra != "" {
s = append(s, extra)
}
return strings.Join(s, ":")
return strutils.JoinRune(s, ':')
}
func lowestPort(ports map[string]types.Port) string {

View File

@@ -1,9 +1,8 @@
package types
import (
"strings"
E "github.com/yusing/go-proxy/internal/error"
"github.com/yusing/go-proxy/internal/utils/strutils"
)
type StreamPort struct {
@@ -14,7 +13,7 @@ type StreamPort struct {
var ErrStreamPortTooManyColons = E.New("too many colons")
func ValidateStreamPort(p string) (StreamPort, error) {
split := strings.Split(p, ":")
split := strutils.SplitRune(p, ':')
switch len(split) {
case 1:

View File

@@ -1,10 +1,8 @@
package types
import (
"fmt"
"strings"
E "github.com/yusing/go-proxy/internal/error"
"github.com/yusing/go-proxy/internal/utils/strutils"
)
type StreamScheme struct {
@@ -14,7 +12,7 @@ type StreamScheme struct {
func ValidateStreamScheme(s string) (*StreamScheme, error) {
ss := &StreamScheme{}
parts := strings.Split(s, ":")
parts := strutils.SplitRune(s, ':')
if len(parts) == 1 {
parts = []string{s, s}
} else if len(parts) != 2 {
@@ -33,7 +31,7 @@ func ValidateStreamScheme(s string) (*StreamScheme, error) {
}
func (s StreamScheme) String() string {
return fmt.Sprintf("%s -> %s", s.ListeningScheme, s.ProxyScheme)
return string(s.ListeningScheme) + " -> " + string(s.ProxyScheme)
}
// IsCoherent checks if the ListeningScheme and ProxyScheme of the StreamScheme are equal.