Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix codeunit and isvalid so split works #46

Merged
merged 2 commits into from
Apr 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name = "ShortStrings"
uuid = "63221d1c-8677-4ff0-9126-0ff0817b4975"
authors = ["Dai ZJ <[email protected]>", "ScottPJones <[email protected]>",
"Lyndon White <[email protected]>"]
version = "0.3.5"
version = "0.3.6"

[deps]
BitIntegers = "c3b6d118-76ef-56ca-8cc7-ebb389d030a1"
Expand Down
8 changes: 6 additions & 2 deletions src/base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ size_mask(s::ShortString{T}) where {T} = size_mask(T)
_swapped_str(s::ShortString) = ntoh(s.size_content & ~size_mask(s))

"""Internal function to pick up a byte at the given index in a ShortString"""
@inline _get_byte(s::ShortString, i::Int) = (s.size_content >>> (8*(sizeof(s) - i)))%UInt8
@inline function _get_byte(s::ShortString{T}, i::Int) where T
return (s.size_content >>> (8*(sizeof(T) - i)))%UInt8
end

"""
Internal function to pick up a UInt32 (i.e. to contain 1 Char) at the given index
Expand Down Expand Up @@ -153,7 +155,9 @@ Base.lastindex(s::ShortString) = sizeof(s)
Base.ncodeunits(s::ShortString) = sizeof(s)

# Checks top two bits of first byte of character to see if valid position
isvalid(s::String, i::Integer) = (0 < i <= sizeof(s)) && ((_get_byte(s, i) & 0xc0) != 0x80)
function Base.isvalid(s::ShortString, i::Integer)
return (0 < i <= sizeof(s)) && ((_get_byte(s, i) & 0xc0) !== 0x80)
end

@inline function Base.iterate(s::ShortString, i::Int=1)
0 < i <= ncodeunits(s) || return nothing
Expand Down
23 changes: 23 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,26 @@ end
@test_throws ErrorException ShortString("foobar", 3)
@test_throws ErrorException ss"foobar"b3
end


@testset "codeunit $T" for T in (ShortString3, ShortString7, ShortString255)
@test codeunit(T("abc"), 1) == 0x61
@test codeunit(T("abc"), 2) == 0x62
@test codeunit(T("abc"), 3) == 0x63
end

@testset "isvalid" begin
@test !isvalid(ShortString("a🍕c"), 0)
@test isvalid(ShortString("a🍕c"), 1)
@test isvalid(ShortString("a🍕c"), 2)
@test !isvalid(ShortString("a🍕c"), 3)
@test !isvalid(ShortString("a🍕c"), 4)
@test !isvalid(ShortString("a🍕c"), 5)
@test isvalid(ShortString("a🍕c"), 6)
@test !isvalid(ShortString("a🍕c"), 7)
end

@testset "split" begin
@test split(ShortString15("abc XYZ x")) == ["abc", "XYZ", "x"]
@test split(ShortString15("abc XYZ x")) isa Vector{SubString{ShortString15}}
end