mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-17 05:42:09 +08:00
Replace Index*, SplitN with Cut
This commit is contained in:
parent
8fe209a447
commit
e1f453c27e
@ -369,11 +369,8 @@ func setTimeDuration(val string, value reflect.Value) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func head(str, sep string) (head string, tail string) {
|
func head(str, sep string) (head string, tail string) {
|
||||||
idx := strings.Index(str, sep)
|
head, tail, _ = strings.Cut(str, sep)
|
||||||
if idx < 0 {
|
return head, tail
|
||||||
return str, ""
|
|
||||||
}
|
|
||||||
return str[:idx], str[idx+len(sep):]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func setFormMap(ptr any, form map[string][]string) error {
|
func setFormMap(ptr any, form map[string][]string) error {
|
||||||
|
@ -562,10 +562,10 @@ func (c *Context) get(m map[string][]string, key string) (map[string]string, boo
|
|||||||
dicts := make(map[string]string)
|
dicts := make(map[string]string)
|
||||||
exist := false
|
exist := false
|
||||||
for k, v := range m {
|
for k, v := range m {
|
||||||
if i := strings.IndexByte(k, '['); i >= 1 && k[0:i] == key {
|
if before, after, found := strings.Cut(k, "["); found && before == key {
|
||||||
if j := strings.IndexByte(k[i+1:], ']'); j >= 1 {
|
if before, _, found := strings.Cut(after, "]"); found && before != "" {
|
||||||
exist = true
|
exist = true
|
||||||
dicts[k[i+1:][:j]] = v[0]
|
dicts[before] = v[0]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
7
tree.go
7
tree.go
@ -241,9 +241,10 @@ walk:
|
|||||||
// Wildcard conflict
|
// Wildcard conflict
|
||||||
pathSeg := path
|
pathSeg := path
|
||||||
if n.nType != catchAll {
|
if n.nType != catchAll {
|
||||||
pathSeg = strings.SplitN(pathSeg, "/", 2)[0]
|
pathSeg, _, _ = strings.Cut(pathSeg, "/")
|
||||||
}
|
}
|
||||||
prefix := fullPath[:strings.Index(fullPath, pathSeg)] + n.path
|
prefix, _, _ := strings.Cut(fullPath, pathSeg)
|
||||||
|
prefix = prefix + n.path
|
||||||
panic("'" + pathSeg +
|
panic("'" + pathSeg +
|
||||||
"' in new path '" + fullPath +
|
"' in new path '" + fullPath +
|
||||||
"' conflicts with existing wildcard '" + n.path +
|
"' conflicts with existing wildcard '" + n.path +
|
||||||
@ -351,7 +352,7 @@ func (n *node) insertChild(path string, fullPath string, handlers HandlersChain)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len(n.path) > 0 && n.path[len(n.path)-1] == '/' {
|
if len(n.path) > 0 && n.path[len(n.path)-1] == '/' {
|
||||||
pathSeg := strings.SplitN(n.children[0].path, "/", 2)[0]
|
pathSeg, _, _ := strings.Cut(n.children[0].path, "/")
|
||||||
panic("catch-all wildcard '" + path +
|
panic("catch-all wildcard '" + path +
|
||||||
"' in new path '" + fullPath +
|
"' in new path '" + fullPath +
|
||||||
"' conflicts with existing path segment '" + pathSeg +
|
"' conflicts with existing path segment '" + pathSeg +
|
||||||
|
4
utils.go
4
utils.go
@ -104,8 +104,8 @@ func parseAccept(acceptHeader string) []string {
|
|||||||
parts := strings.Split(acceptHeader, ",")
|
parts := strings.Split(acceptHeader, ",")
|
||||||
out := make([]string, 0, len(parts))
|
out := make([]string, 0, len(parts))
|
||||||
for _, part := range parts {
|
for _, part := range parts {
|
||||||
if i := strings.IndexByte(part, ';'); i > 0 {
|
if before, _, found := strings.Cut(part, ";"); found {
|
||||||
part = part[:i]
|
part = before
|
||||||
}
|
}
|
||||||
if part = strings.TrimSpace(part); part != "" {
|
if part = strings.TrimSpace(part); part != "" {
|
||||||
out = append(out, part)
|
out = append(out, part)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user