Skip to content

Commit 42a5832

Browse files
Fix TrieIterator to correctly handle non-ascii chararacters
Squashed version of commits in JuliaCollections#756 with relevant changes only. Backport to v0.18, meaning we're not including `find_prefixes`. Co-authored-by: Du Shiqiao <[email protected]> Co-authored-by: Reuben Gardos Reid <[email protected]>
1 parent 45f9985 commit 42a5832

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

src/trie.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,12 @@ end
111111
# since the root of the trie corresponds to a length 0 prefix of str.
112112
function Base.iterate(it::TrieIterator, (t, i) = (it.t, 0))
113113
if i == 0
114-
return it.t, (it.t, 1)
115-
elseif i == length(it.str) + 1 || !(it.str[i] in keys(t.children))
114+
return it.t, (it.t, firstindex(it.str))
115+
elseif i > lastindex(it.str) || !(it.str[i] in keys(t.children))
116116
return nothing
117117
else
118118
t = t.children[it.str[i]]
119-
return (t, (t, i + 1))
119+
return (t, (t, nextind(it.str, i)))
120120
end
121121
end
122122

test/test_trie.jl

+13
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,17 @@
4040
@test collect(partial_path(t, "ro")) == [t0, t1, t2]
4141
@test collect(partial_path(t, "roa")) == [t0, t1, t2]
4242
end
43+
44+
@testset "partial_path iterator non-ascii" begin
45+
t = Trie(["東京都"])
46+
t0 = t
47+
t1 = t0.children['']
48+
t2 = t1.children['']
49+
t3 = t2.children['']
50+
@test collect(partial_path(t, "西")) == [t0]
51+
@test collect(partial_path(t, "東京都")) == [t0, t1, t2, t3]
52+
@test collect(partial_path(t, "東京都渋谷区")) == [t0, t1, t2, t3]
53+
@test collect(partial_path(t, "東京")) == [t0, t1, t2]
54+
@test collect(partial_path(t, "東京スカイツリー")) == [t0, t1, t2]
55+
end
4356
end # @testset Trie

0 commit comments

Comments
 (0)