mirror of
https://github.com/golang/go
synced 2024-11-20 04:04:41 -07:00
net/url: use strings.Index instead of a loop
We already depend on strings in this file, so use it. Plus strings.Index will be faster than a manual loop once issue 3751 is finished. R=golang-dev, khr CC=golang-dev https://golang.org/cl/8116043
This commit is contained in:
parent
cb79b2cf22
commit
731dcb7680
@ -317,23 +317,22 @@ func getscheme(rawurl string) (scheme, path string, err error) {
|
|||||||
// Maybe s is of the form t c u.
|
// Maybe s is of the form t c u.
|
||||||
// If so, return t, c u (or t, u if cutc == true).
|
// If so, return t, c u (or t, u if cutc == true).
|
||||||
// If not, return s, "".
|
// If not, return s, "".
|
||||||
func split(s string, c byte, cutc bool) (string, string) {
|
func split(s string, c string, cutc bool) (string, string) {
|
||||||
for i := 0; i < len(s); i++ {
|
i := strings.Index(s, c)
|
||||||
if s[i] == c {
|
if i < 0 {
|
||||||
|
return s, ""
|
||||||
|
}
|
||||||
if cutc {
|
if cutc {
|
||||||
return s[0:i], s[i+1:]
|
return s[0:i], s[i+len(c):]
|
||||||
}
|
}
|
||||||
return s[0:i], s[i:]
|
return s[0:i], s[i:]
|
||||||
}
|
|
||||||
}
|
|
||||||
return s, ""
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse parses rawurl into a URL structure.
|
// Parse parses rawurl into a URL structure.
|
||||||
// The rawurl may be relative or absolute.
|
// The rawurl may be relative or absolute.
|
||||||
func Parse(rawurl string) (url *URL, err error) {
|
func Parse(rawurl string) (url *URL, err error) {
|
||||||
// Cut off #frag
|
// Cut off #frag
|
||||||
u, frag := split(rawurl, '#', true)
|
u, frag := split(rawurl, "#", true)
|
||||||
if url, err = parse(u, false); err != nil {
|
if url, err = parse(u, false); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -380,7 +379,7 @@ func parse(rawurl string, viaRequest bool) (url *URL, err error) {
|
|||||||
}
|
}
|
||||||
url.Scheme = strings.ToLower(url.Scheme)
|
url.Scheme = strings.ToLower(url.Scheme)
|
||||||
|
|
||||||
rest, url.RawQuery = split(rest, '?', true)
|
rest, url.RawQuery = split(rest, "?", true)
|
||||||
|
|
||||||
if !strings.HasPrefix(rest, "/") {
|
if !strings.HasPrefix(rest, "/") {
|
||||||
if url.Scheme != "" {
|
if url.Scheme != "" {
|
||||||
@ -396,7 +395,7 @@ func parse(rawurl string, viaRequest bool) (url *URL, err error) {
|
|||||||
|
|
||||||
if (url.Scheme != "" || !viaRequest && !strings.HasPrefix(rest, "///")) && strings.HasPrefix(rest, "//") {
|
if (url.Scheme != "" || !viaRequest && !strings.HasPrefix(rest, "///")) && strings.HasPrefix(rest, "//") {
|
||||||
var authority string
|
var authority string
|
||||||
authority, rest = split(rest[2:], '/', false)
|
authority, rest = split(rest[2:], "/", false)
|
||||||
url.User, url.Host, err = parseAuthority(authority)
|
url.User, url.Host, err = parseAuthority(authority)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
goto Error
|
goto Error
|
||||||
@ -428,7 +427,7 @@ func parseAuthority(authority string) (user *Userinfo, host string, err error) {
|
|||||||
}
|
}
|
||||||
user = User(userinfo)
|
user = User(userinfo)
|
||||||
} else {
|
} else {
|
||||||
username, password := split(userinfo, ':', true)
|
username, password := split(userinfo, ":", true)
|
||||||
if username, err = unescape(username, encodeUserPassword); err != nil {
|
if username, err = unescape(username, encodeUserPassword); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user