Skip to content

Commit c6a0701

Browse files
authored
Merge pull request #31 from luislavena/fix/non-root-lookup
Fix incorrect lookup in non-root nodes
2 parents f8df18b + f764b3a commit c6a0701

3 files changed

Lines changed: 18 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ so please check *Changed* and *Removed* notes before upgrading.
88

99
### Fixed
1010
- Correct lookup issue caused by partial shared key with glob [#23](https://github.com/luislavena/radix/issues/23)
11+
- Correct lookup caused by non-root key in suffix [#27](https://github.com/luislavena/radix/issues/27)
1112

1213
### Removed
1314
- Remove `Radix::Result#key` since exposes internal details about structure (breaking change)

spec/radix/tree_spec.cr

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,15 @@ module Radix
353353
result.found?.should be_true
354354
result.payload.should eq(:tags)
355355
end
356+
357+
it "do not find when lookup for non-root key" do
358+
tree = Tree(Symbol).new
359+
tree.add "/prefix/", :prefix
360+
tree.add "/prefix/foo", :foo
361+
362+
result = tree.find "/foo"
363+
result.found?.should be_false
364+
end
356365
end
357366

358367
context "unicode nodes with shared parent" do

src/radix/tree.cr

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,14 @@ module Radix
299299
end
300300
end
301301

302+
# determine if remaining part of key and path are still the same
303+
if (key_reader.has_next? && path_reader.has_next?) &&
304+
(key_reader.current_char != path_reader.current_char ||
305+
key_reader.peek_next_char != path_reader.peek_next_char)
306+
# path and key differ, skipping
307+
return
308+
end
309+
302310
# still path to walk, check for possible trailing slash or children
303311
# nodes
304312
if path_reader.has_next?

0 commit comments

Comments
 (0)