From e1f453c27eff49f88e81ed4ddce1c564235981d6 Mon Sep 17 00:00:00 2001 From: OHZEKI Naoki <0h23k1.n40k1@gmail.com> Date: Sat, 26 Nov 2022 13:52:51 +0900 Subject: [PATCH] Replace Index*, SplitN with Cut --- binding/form_mapping.go | 7 ++----- context.go | 6 +++--- tree.go | 7 ++++--- utils.go | 4 ++-- 4 files changed, 11 insertions(+), 13 deletions(-) diff --git a/binding/form_mapping.go b/binding/form_mapping.go index 540bbbb8..415c9162 100644 --- a/binding/form_mapping.go +++ b/binding/form_mapping.go @@ -369,11 +369,8 @@ func setTimeDuration(val string, value reflect.Value) error { } func head(str, sep string) (head string, tail string) { - idx := strings.Index(str, sep) - if idx < 0 { - return str, "" - } - return str[:idx], str[idx+len(sep):] + head, tail, _ = strings.Cut(str, sep) + return head, tail } func setFormMap(ptr any, form map[string][]string) error { diff --git a/context.go b/context.go index c41c71ec..d85a1baa 100644 --- a/context.go +++ b/context.go @@ -562,10 +562,10 @@ func (c *Context) get(m map[string][]string, key string) (map[string]string, boo dicts := make(map[string]string) exist := false for k, v := range m { - if i := strings.IndexByte(k, '['); i >= 1 && k[0:i] == key { - if j := strings.IndexByte(k[i+1:], ']'); j >= 1 { + if before, after, found := strings.Cut(k, "["); found && before == key { + if before, _, found := strings.Cut(after, "]"); found && before != "" { exist = true - dicts[k[i+1:][:j]] = v[0] + dicts[before] = v[0] } } } diff --git a/tree.go b/tree.go index 3f34b8ee..2937fe12 100644 --- a/tree.go +++ b/tree.go @@ -241,9 +241,10 @@ walk: // Wildcard conflict pathSeg := path 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 + "' in new path '" + fullPath + "' 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] == '/' { - pathSeg := strings.SplitN(n.children[0].path, "/", 2)[0] + pathSeg, _, _ := strings.Cut(n.children[0].path, "/") panic("catch-all wildcard '" + path + "' in new path '" + fullPath + "' conflicts with existing path segment '" + pathSeg + diff --git a/utils.go b/utils.go index 4021a2ab..3ad36d59 100644 --- a/utils.go +++ b/utils.go @@ -104,8 +104,8 @@ func parseAccept(acceptHeader string) []string { parts := strings.Split(acceptHeader, ",") out := make([]string, 0, len(parts)) for _, part := range parts { - if i := strings.IndexByte(part, ';'); i > 0 { - part = part[:i] + if before, _, found := strings.Cut(part, ";"); found { + part = before } if part = strings.TrimSpace(part); part != "" { out = append(out, part)