diff options
Diffstat (limited to 'internal/pathtree.go')
-rw-r--r-- | internal/pathtree.go | 62 |
1 files changed, 31 insertions, 31 deletions
diff --git a/internal/pathtree.go b/internal/pathtree.go index 25d000d..0ab6b81 100644 --- a/internal/pathtree.go +++ b/internal/pathtree.go @@ -197,39 +197,39 @@ func (st subtree[V]) Match(segments []string, m *match[V]) *match[V] { } func (st *subtree[V]) Add(pattern []string, value V) { - if len(pattern[0]) == 0 { - panic("invalid pattern") - } - - switch pattern[0][0] { - case '*': - if len(pattern) > 1 { - panic("invalid pattern: segments after *remainder") - } - if st.remainder != nil { - panic("pattern already exists") + if len(pattern[0]) > 0 { + switch pattern[0][0] { + case '*': + if len(pattern) > 1 { + panic("invalid pattern: segments after *remainder") + } + if st.remainder != nil { + panic("pattern already exists") + } + + st.remainder = &remainderNode[V]{ + param: pattern[0][1:], + value: value, + } + return + case ':': + child := st.wildcards.Find(pattern[0][1:]) + if child != nil { + child.Add(pattern[1:], value) + } else { + st.wildcards = append(st.wildcards, makeWildcard(pattern, value)) + sort.Sort(st.wildcards) + } + return } + } - st.remainder = &remainderNode[V]{ - param: pattern[0][1:], - value: value, - } - case ':': - child := st.wildcards.Find(pattern[0][1:]) - if child != nil { - child.Add(pattern[1:], value) - } else { - st.wildcards = append(st.wildcards, makeWildcard(pattern, value)) - sort.Sort(st.wildcards) - } - default: - child := st.segments.Find(pattern[0]) - if child != nil { - child.Add(pattern[1:], value) - } else { - st.segments = append(st.segments, makeSegment(pattern, value)) - sort.Sort(st.segments) - } + child := st.segments.Find(pattern[0]) + if child != nil { + child.Add(pattern[1:], value) + } else { + st.segments = append(st.segments, makeSegment(pattern, value)) + sort.Sort(st.segments) } } |