Skip to content

Commit 28a1e24

Browse files
authored
Merge pull request #17 from oxinabox/ox/cmp
make cmp faster
2 parents 3d57f57 + 9c58435 commit 28a1e24

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

src/base.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ Base.collect(s::ShortString) = collect(String(s))
6363
String(s) == b
6464
end
6565

66+
function Base.cmp(a::ShortString{S}, b::ShortString{S}) where S
67+
return cmp(a.size_content, b.size_content)
68+
end
69+
6670
promote_rule(::Type{String}, ::Type{ShortString{S}}) where S = String
6771
promote_rule(::Type{ShortString{T}}, ::Type{ShortString{S}}) where {T,S} = ShortString{promote_rule(T,S)}
6872

test/runtests.jl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,22 @@ basic_test(ShortString{MyUInt2048}, 254)
5151
@test ss15"Short String!!!" === ShortString15("Short String!!!")
5252
@test ss7"ShrtStr" === ShortString7("ShrtStr")
5353
@test ss3"ss3" === ShortString3("ss3")
54+
55+
56+
@testset "cmp" begin
57+
@test cmp(ShortString3("abc"), ShortString3("abc")) == 0
58+
@test cmp(ShortString3("ab"), ShortString3("abc")) == -1
59+
@test cmp(ShortString3("abc"), ShortString3("ab")) == 1
60+
@test cmp(ShortString3("ab"), ShortString3("ac")) == -1
61+
@test cmp(ShortString3("ac"), ShortString3("ab")) == 1
62+
@test cmp(ShortString3("α"), ShortString3("a")) == 1
63+
@test cmp(ShortString3("b"), ShortString3("β")) == -1
64+
65+
@test cmp(ShortString3("abc"), "abc") == 0
66+
@test cmp(ShortString3("ab"), "abc") == -1
67+
@test cmp(ShortString3("abc"), "ab") == 1
68+
@test cmp(ShortString3("ab"), "ac") == -1
69+
@test cmp(ShortString3("ac"), "ab") == 1
70+
@test cmp(ShortString3("α"), "a") == 1
71+
@test cmp(ShortString3("b"), "β") == -1
72+
end

0 commit comments

Comments
 (0)