add url package
This commit is contained in:
+31
@@ -0,0 +1,31 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user