Skip to content

Commit c7b5c79

Browse files
committed
update
1 parent 7761c5f commit c7b5c79

File tree

3 files changed

+68
-17
lines changed

3 files changed

+68
-17
lines changed

string.go

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -532,23 +532,6 @@ func StripSlashes(s string) string {
532532
return s
533533
}
534534

535-
// SplitHost localhost:8080 => localhost,8080
536-
func SplitHost(host string) (domain string, port string) {
537-
pos := strings.LastIndex(host, `:`)
538-
if pos >= 0 {
539-
domain = host[0:pos]
540-
port = host[pos:]
541-
port = strings.TrimPrefix(port, `:`)
542-
if strings.HasPrefix(port, `[`) {
543-
port = strings.TrimPrefix(port, `[`)
544-
port = strings.TrimSuffix(port, `]`)
545-
}
546-
} else {
547-
domain = host
548-
}
549-
return
550-
}
551-
552535
// MaskString 0123456789 => 012****789
553536
func MaskString(v string, width ...float64) string {
554537
size := len(v)

url.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,36 @@ func IsLocalhost(host string) bool {
137137
return localIPRegexp.MatchString(host)
138138
}
139139
}
140+
141+
// SplitHost localhost:8080 => localhost
142+
func SplitHost(hostport string) string {
143+
host, _ := SplitHostPort(hostport)
144+
return host
145+
}
146+
147+
func SplitHostPort(hostport string) (host string, port string) {
148+
if strings.HasSuffix(hostport, `]`) {
149+
host = hostport
150+
return
151+
}
152+
sep := `]:`
153+
pos := strings.LastIndex(hostport, sep)
154+
if pos > -1 {
155+
host = hostport[0 : pos+1]
156+
if len(hostport) > pos+2 {
157+
port = hostport[pos+2:]
158+
}
159+
return
160+
}
161+
sep = `:`
162+
pos = strings.LastIndex(hostport, sep)
163+
if pos > -1 {
164+
host = hostport[0:pos]
165+
if len(hostport) > pos+1 {
166+
port = hostport[pos+1:]
167+
}
168+
return
169+
}
170+
host = hostport
171+
return
172+
}

url_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestSplitHostPort(t *testing.T) {
10+
host, port := SplitHostPort(`[1:2:2:3]:9999`)
11+
assert.Equal(t, `[1:2:2:3]`, host)
12+
assert.Equal(t, `9999`, port)
13+
14+
host, port = SplitHostPort(`example.com:9999`)
15+
assert.Equal(t, `example.com`, host)
16+
assert.Equal(t, `9999`, port)
17+
18+
host, port = SplitHostPort(`127.0.0.1:9999`)
19+
assert.Equal(t, `127.0.0.1`, host)
20+
assert.Equal(t, `9999`, port)
21+
}
22+
23+
func TestSplitHostPort2(t *testing.T) {
24+
host, port := SplitHostPort(`[1:2:2:3]`)
25+
assert.Equal(t, `[1:2:2:3]`, host)
26+
assert.Equal(t, ``, port)
27+
28+
host, port = SplitHostPort(`example.com`)
29+
assert.Equal(t, `example.com`, host)
30+
assert.Equal(t, ``, port)
31+
32+
host, port = SplitHostPort(`127.0.0.1`)
33+
assert.Equal(t, `127.0.0.1`, host)
34+
assert.Equal(t, ``, port)
35+
}

0 commit comments

Comments
 (0)