- update url

master
dtapps 3 weeks ago
parent 91b4b06938
commit dd954924f3

@ -13,5 +13,5 @@
#### 安装
```shell
go get -v -u go.dtapp.net/gostring@v1.0.14
go get -v -u go.dtapp.net/gostring@v1.0.15
```

@ -1,27 +1,41 @@
package gostring
import (
"bytes"
"strings"
)
const (
prefixHTTP = "http://"
prefixHTTPS = "https://"
)
// CompleteUrlHttp 补全 URL
func CompleteUrlHttp(url string) string {
if url == "" {
return url
}
if strings.HasPrefix(url, "http://") {
if strings.HasPrefix(url, prefixHTTP) {
return url
}
var buffer bytes.Buffer
if strings.HasPrefix(url, "//") {
url = "http:" + url
buffer.WriteString("http:")
buffer.WriteString(url)
} else if strings.HasPrefix(url, "://") {
url = "http" + url
} else if strings.HasPrefix(url, "https://") {
url = Replace(url, "https://", "http://")
buffer.WriteString("http")
buffer.WriteString(url)
} else if strings.HasPrefix(url, prefixHTTPS) {
buffer.WriteString(Replace(url, prefixHTTPS, prefixHTTP))
} else {
url = "http://" + url
buffer.WriteString(prefixHTTP)
buffer.WriteString(url)
}
return url
return buffer.String()
}
// CompleteUrlHttps 补全 URL
@ -29,17 +43,25 @@ func CompleteUrlHttps(url string) string {
if url == "" {
return url
}
if strings.HasPrefix(url, "https://") {
if strings.HasPrefix(url, prefixHTTPS) {
return url
}
var buffer bytes.Buffer
if strings.HasPrefix(url, "//") {
url = "https:" + url
buffer.WriteString("https:")
buffer.WriteString(url)
} else if strings.HasPrefix(url, "://") {
url = "https" + url
} else if strings.HasPrefix(url, "http://") {
url = Replace(url, "http://", "https://")
buffer.WriteString("https")
buffer.WriteString(url)
} else if strings.HasPrefix(url, prefixHTTP) {
buffer.WriteString(Replace(url, prefixHTTP, prefixHTTPS))
} else {
url = "https://" + url
buffer.WriteString(prefixHTTPS)
buffer.WriteString(url)
}
return url
return buffer.String()
}

@ -1,3 +1,3 @@
package gostring
const Version = "1.0.14"
const Version = "1.0.15"

Loading…
Cancel
Save