From 0ab036d34c5526e8c39da798bec2bc5d4a70a6b2 Mon Sep 17 00:00:00 2001
From: tjpcc <tjp@ctrl-c.club>
Date: Fri, 15 Sep 2023 17:00:08 -0600
Subject: allow slash-ending paths in pathTree

---
 internal/pathtree.go | 62 ++++++++++++++++++++++++++--------------------------
 1 file changed, 31 insertions(+), 31 deletions(-)

(limited to 'internal')

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)
 	}
 }
 
-- 
cgit v1.2.3