32 lines
531 B
Go
32 lines
531 B
Go
package url
|
|
|
|
type URL struct {
|
|
Scheme string
|
|
Host string
|
|
Path string
|
|
}
|
|
|
|
func Parse(rawURL string) (*URL, error) {
|
|
schemeIndex := -1
|
|
hostStartIndex := -1
|
|
hostEndIndex := -1
|
|
for i := 0; i < len(rawURL); i++ {
|
|
if schemeIndex == -1 && rawURL[i] == ':' {
|
|
schemeIndex = i
|
|
hostStartIndex = i + 3
|
|
i += 3
|
|
continue
|
|
}
|
|
if rawURL[i] == '/' {
|
|
hostEndIndex = i
|
|
break
|
|
}
|
|
}
|
|
|
|
return &URL{
|
|
Scheme: rawURL[:schemeIndex],
|
|
Host: rawURL[hostStartIndex:hostEndIndex],
|
|
Path: rawURL[hostEndIndex:],
|
|
}, nil
|
|
}
|