Skip to content

Commit 8a14e7b

Browse files
authored
Merge pull request #6 from JuliaString/spj/makegeneric
Make API more generic
2 parents b42a93e + 056faec commit 8a14e7b

File tree

2 files changed

+24
-71
lines changed

2 files changed

+24
-71
lines changed

src/HTML_Entities.jl

Lines changed: 2 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -29,62 +29,8 @@ struct HTML_Table{T} <: AbstractEntityTable
2929
end
3030

3131
function __init__()
32-
global _tab =
32+
global default =
3333
HTML_Table(StrTables.load(joinpath(Pkg.dir("HTML_Entities"), "data", "html.dat"))...)
3434
nothing
3535
end
36-
37-
const _empty_str = ""
38-
const _empty_str_vec = Vector{String}()
39-
40-
function _get_str(ind)
41-
ind <= _tab.base32 && return string(Char(_tab.val16[ind]))
42-
ind <= _tab.base2c && return string(Char(_tab.val32[ind - _tab.base32] + 0x10000))
43-
val = _tab.val2c[ind - _tab.base2c]
44-
string(Char(val>>>16), Char(val&0xffff))
45-
end
46-
47-
function _get_strings(val::T, tab::Vector{T}, ind::Vector{UInt16}) where {T}
48-
rng = searchsorted(tab, val)
49-
isempty(rng) && return _empty_str_vec
50-
_tab.nam[ind[rng]]
51-
end
52-
53-
function lookupname(str::AbstractString)
54-
rng = searchsorted(_tab.nam, str)
55-
isempty(rng) ? _empty_str : _get_str(_tab.ind[rng.start])
56-
end
57-
58-
matchchar(ch::UInt32) =
59-
(ch <= 0x0ffff
60-
? _get_strings(ch%UInt16, _tab.val16, _tab.ind16)
61-
: (ch <= 0x1ffff ? _get_strings(ch%UInt16, _tab.val32, _tab.ind32) : _empty_str_vec))
62-
matchchar(ch::Char) = matchchar(UInt32(ch))
63-
64-
matches(str::AbstractString) = matches(convert(Vector{Char}, str))
65-
function matches(vec::Vector{Char})
66-
if length(vec) == 1
67-
matchchar(vec[1])
68-
elseif length(vec) == 2 && (vec[1] <= '\uffff' && vec[2] <= '\uffff')
69-
_get_strings(vec[1]%UInt32<<16 | vec[2]%UInt32, _tab.val2c, _tab.ind2c)
70-
else
71-
_empty_str_vec
72-
end
73-
end
74-
75-
longestmatches(str::AbstractString) = longestmatches(convert(Vector{Char}, str))
76-
function longestmatches(vec::Vector{Char})
77-
isempty(vec) && return _empty_str_vec
78-
if length(vec) >= 2 && (vec[1] <= '\uffff' && vec[2] <= '\uffff')
79-
res = _get_strings(vec[1]%UInt32<<16 | vec[2]%UInt32, _tab.val2c, _tab.ind2c)
80-
isempty(res) || return res
81-
# Fall through and check only the first character
82-
end
83-
matchchar(vec[1])
84-
end
85-
86-
completions(str::AbstractString) = completions(convert(String, str))
87-
completions(str::String) = StrTables.matchfirst(_tab.nam, str)
88-
89-
end # module
90-
36+
end # module HTML_Entities

test/runtests.jl

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,47 +7,54 @@ using HTML_Entities
77

88
HE = HTML_Entities
99

10+
he_matchchar(ch) = HE.matchchar(HE.default, ch)
11+
he_lookupname(nam) = HE.lookupname(HE.default, nam)
12+
he_longestmatches(str) = HE.longestmatches(HE.default, str)
13+
he_matches(str) = HE.matches(HE.default, str)
14+
he_completions(str) = HE.completions(HE.default, str)
15+
16+
1017
@testset "HTML_Entities" begin
1118
@testset "lookupname" begin
12-
@test HE.lookupname(SubString("My name is Spock", 12)) == ""
13-
@test HE.lookupname("foobar") == ""
14-
@test HE.lookupname("nle") == "\u2270"
15-
@test HE.lookupname("Pscr") == "\U1d4ab"
16-
@test HE.lookupname("lvnE") == "\u2268\ufe00"
19+
@test he_lookupname(SubString("My name is Spock", 12)) == ""
20+
@test he_lookupname("foobar") == ""
21+
@test he_lookupname("nle") == "\u2270"
22+
@test he_lookupname("Pscr") == "\U1d4ab"
23+
@test he_lookupname("lvnE") == "\u2268\ufe00"
1724
end
1825

1926
@testset "matches" begin
20-
@test isempty(HE.matches(""))
21-
@test isempty(HE.matches("\u201f"))
22-
@test isempty(HE.matches(SubString("This is \u201f", 9)))
27+
@test isempty(he_matches(""))
28+
@test isempty(he_matches("\u201f"))
29+
@test isempty(he_matches(SubString("This is \u201f", 9)))
2330
for (chrs, exp) in (("\u2270", ["nle", "nleq"]),
2431
("\U1d4ab", ["Pscr"]),
2532
("\U1d51e", ["afr"]),
2633
("\u2268\ufe00", ["lvertneqq", "lvnE"]))
27-
res = HE.matches(chrs)
34+
res = he_matches(chrs)
2835
@test length(res) >= length(exp)
2936
@test intersect(res, exp) == exp
3037
end
3138
end
3239

3340
@testset "longestmatches" begin
34-
@test isempty(HE.longestmatches("\u201f abcd"))
35-
@test isempty(HE.longestmatches(SubString("This is \U201f abcd", 9)))
41+
@test isempty(he_longestmatches("\u201f abcd"))
42+
@test isempty(he_longestmatches(SubString("This is \U201f abcd", 9)))
3643
for (chrs, exp) in (("\u2270 abcd", ["nle", "nleq"]),
3744
("\U1d4ab abcd", ["Pscr"]),
3845
("\u2268\ufe00 silly", ["lvertneqq", "lvnE"]))
39-
res = HE.longestmatches(chrs)
46+
res = he_longestmatches(chrs)
4047
@test length(res) >= length(exp)
4148
@test intersect(res, exp) == exp
4249
end
4350
end
4451

4552
@testset "completions" begin
46-
@test isempty(HE.completions("ScottPaulJones"))
47-
@test isempty(HE.completions(SubString("My name is Scott", 12)))
53+
@test isempty(he_completions("ScottPaulJones"))
54+
@test isempty(he_completions(SubString("My name is Scott", 12)))
4855
for (chrs, exp) in (("and", ["and", "andand", "andd", "andslope", "andv"]),
4956
("um", ["umacr", "uml"]))
50-
res = HE.completions(chrs)
57+
res = he_completions(chrs)
5158
@test length(res) >= length(exp)
5259
@test intersect(res, exp) == exp
5360
end

0 commit comments

Comments
 (0)