Replace Index*, SplitN with Cut

This commit is contained in:
OHZEKI Naoki 2022-11-26 13:52:51 +09:00
parent 8fe209a447
commit e1f453c27e
4 changed files with 11 additions and 13 deletions

View File

@ -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 {

View File

@ -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]
} }
} }
} }

View File

@ -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 +

View File

@ -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)