mirror of
https://github.com/laravel-ls/uri
synced 2026-07-04 00:23:39 +02:00
uri: fix String method logic
This commit is contained in:
parent
754c1e1256
commit
e2736f1bb7
1 changed files with 22 additions and 19 deletions
39
uri.go
39
uri.go
|
|
@ -231,17 +231,16 @@ func (u *URI) UnmarshalJSON(b []byte) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// String implements fmt.Stringer.
|
// String implements fmt.Stringer.
|
||||||
func (u URI) String() string {
|
func (u *URI) String() string {
|
||||||
switch u.Scheme {
|
switch u.Scheme {
|
||||||
case FileScheme, HTTPScheme, HTTPSScheme:
|
case FileScheme, HTTPScheme, HTTPSScheme:
|
||||||
if u.skipEncoding {
|
if u.skipEncoding {
|
||||||
return format(u, false) // fast-path
|
return u.formatted
|
||||||
}
|
}
|
||||||
|
|
||||||
if u.formatted == "" {
|
u.formatted = format(*u, true)
|
||||||
u.formatted = format(u, true)
|
|
||||||
u.skipEncoding = true
|
u.skipEncoding = true
|
||||||
}
|
|
||||||
return u.formatted
|
return u.formatted
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
|
@ -271,12 +270,12 @@ var encodeTable = map[byte]string{
|
||||||
Space: "%20",
|
Space: "%20",
|
||||||
}
|
}
|
||||||
|
|
||||||
func encodeFast(uri string, allowSlash bool) string {
|
func encodeFast(uriComponent string, allowSlash bool) string {
|
||||||
b := new(strings.Builder)
|
b := new(strings.Builder)
|
||||||
nativeEncodePos := -1
|
nativeEncodePos := -1
|
||||||
|
|
||||||
for i := 0; i < len(uri); i++ {
|
for pos := 0; pos < len(uriComponent); pos++ {
|
||||||
code := uri[i]
|
code := uriComponent[pos]
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
case code >= LowerA && code <= LowerZ,
|
case code >= LowerA && code <= LowerZ,
|
||||||
|
|
@ -289,33 +288,37 @@ func encodeFast(uri string, allowSlash bool) string {
|
||||||
allowSlash && (code == Slash):
|
allowSlash && (code == Slash):
|
||||||
|
|
||||||
if nativeEncodePos != -1 {
|
if nativeEncodePos != -1 {
|
||||||
b.WriteString(url.PathEscape(uri[nativeEncodePos:i]))
|
b.WriteString(uriComponent[nativeEncodePos:pos])
|
||||||
nativeEncodePos = -1
|
nativeEncodePos = -1
|
||||||
}
|
}
|
||||||
if b.String() != "" {
|
str := b.String()
|
||||||
b.WriteString(uri[:i])
|
if str != "" {
|
||||||
|
b.WriteString(str)
|
||||||
|
b.WriteString(uriComponent[:pos])
|
||||||
}
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
if b.String() == "" {
|
str := b.String()
|
||||||
b.WriteString(uri[0:i])
|
if str != "" {
|
||||||
|
b.WriteString(str)
|
||||||
|
b.WriteString(uriComponent[0:pos])
|
||||||
}
|
}
|
||||||
|
|
||||||
escaped := encodeTable[code]
|
escaped := encodeTable[code]
|
||||||
if escaped != "" {
|
if escaped != "" {
|
||||||
if nativeEncodePos != -1 {
|
if nativeEncodePos != -1 {
|
||||||
b.WriteString(url.PathEscape(uri[nativeEncodePos:i]))
|
b.WriteString(uriComponent[nativeEncodePos:pos])
|
||||||
}
|
}
|
||||||
|
|
||||||
b.WriteString(escaped)
|
b.WriteString(escaped)
|
||||||
} else {
|
} else if nativeEncodePos == -1 {
|
||||||
nativeEncodePos = i
|
nativeEncodePos = pos
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if nativeEncodePos != -1 {
|
if nativeEncodePos != -1 {
|
||||||
b.WriteString(uri[:nativeEncodePos])
|
b.WriteString(uriComponent[:nativeEncodePos])
|
||||||
}
|
}
|
||||||
|
|
||||||
return b.String()
|
return b.String()
|
||||||
|
|
@ -404,7 +407,7 @@ func format(uri URI, skipEncoding bool) string {
|
||||||
if len(path) >= 3 && path[0] == Slash && path[2] == Colon {
|
if len(path) >= 3 && path[0] == Slash && path[2] == Colon {
|
||||||
code := path[1]
|
code := path[1]
|
||||||
if code >= UpperA && code <= UpperZ {
|
if code >= UpperA && code <= UpperZ {
|
||||||
path = string(code+32) + ":" + string(path[3]) // "/c:".length == 3
|
path = "/" + string(code+32) + ":" + string(path[3]) // "/c:".length == 3
|
||||||
}
|
}
|
||||||
} else if len(path) >= 2 && path[1] == Colon {
|
} else if len(path) >= 2 && path[1] == Colon {
|
||||||
code := path[0]
|
code := path[0]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue