Skip to content

Commit d258116

Browse files
committed
Further refactor, and switch to using LastIndex for best compatibility
1 parent 2ac36af commit d258116

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

tree.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func (n *Node) Add(key string, val interface{}) error {
7575
// Adds a leaf to a terminal node.
7676
// If the last wildcard contains an extension, add it to the 'extensions' map.
7777
func (n *Node) addLeaf(leaf *Leaf) error {
78-
extension := stripExtensionFromSegments(leaf.Wildcards)
78+
extension := stripExtensionFromLastSegment(leaf.Wildcards)
7979
if extension != "" {
8080
if n.extensions == nil {
8181
n.extensions = make(map[string]*Leaf)
@@ -154,9 +154,9 @@ func (n *Node) find(elements, exp []string) (leaf *Leaf, expansions []string) {
154154
// If this node has explicit extensions, check if the path matches one.
155155
if len(exp) > 0 && n.extensions != nil {
156156
lastExp := exp[len(exp)-1]
157-
extension, splitPosition := extensionForPath(lastExp)
157+
prefix, extension := extensionForPath(lastExp)
158158
if leaf := n.extensions[extension]; leaf != nil {
159-
exp[len(exp)-1] = lastExp[0:splitPosition]
159+
exp[len(exp)-1] = prefix
160160
return leaf, exp
161161
}
162162
}
@@ -194,12 +194,12 @@ func (n *Node) find(elements, exp []string) (leaf *Leaf, expansions []string) {
194194
return
195195
}
196196

197-
func extensionForPath(path string) (string, int) {
198-
dotPosition := strings.Index(path, ".")
197+
func extensionForPath(path string) (string, string) {
198+
dotPosition := strings.LastIndex(path, ".")
199199
if dotPosition != -1 {
200-
return path[dotPosition:], dotPosition
200+
return path[:dotPosition], path[dotPosition:]
201201
}
202-
return "", -1
202+
return "", ""
203203
}
204204

205205
func splitPath(key string) []string {
@@ -213,16 +213,16 @@ func splitPath(key string) []string {
213213
return elements
214214
}
215215

216-
// stripExtensionFromSegments determines if a string slice representing a path
216+
// stripExtensionFromLastSegment determines if a string slice representing a path
217217
// ends with a file extension, removes the extension from the input, and returns it.
218-
func stripExtensionFromSegments(segments []string) string {
218+
func stripExtensionFromLastSegment(segments []string) string {
219219
if len(segments) == 0 {
220220
return ""
221221
}
222222
lastSegment := segments[len(segments)-1]
223-
extension, splitPosition := extensionForPath(lastSegment)
223+
prefix, extension := extensionForPath(lastSegment)
224224
if extension != "" {
225-
segments[len(segments)-1] = lastSegment[0:splitPosition]
225+
segments[len(segments)-1] = prefix
226226
}
227227
return extension
228228
}

tree_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ func TestExtensions(t *testing.T) {
8181
found(t, n, "/a/b", []string{"a", "b"}, 3)
8282
found(t, n, "/a/b.json", []string{"a", "b"}, 1)
8383
found(t, n, "/a/b.xml", []string{"b"}, 2)
84+
found(t, n, "/a/b.c.xml", []string{"b.c"}, 2)
8485
found(t, n, "/other/b.xml", []string{"other", "b.xml"}, 3)
8586
}
8687

0 commit comments

Comments
 (0)