Skip to content

Commit

Permalink
Merge pull request #46 from JuliaString/ox/iv
Browse files Browse the repository at this point in the history
fix codeunit and isvalid so split works
  • Loading branch information
oxinabox authored Apr 12, 2021
2 parents f55c3ae + 65ca236 commit ce799ae
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
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

2 comments on commit ce799ae

@oxinabox
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/34135

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.3.6 -m "<description of version>" ce799ae54612a7b7edbcc934ad167673c09b2eb4
git push origin v0.3.6

Please sign in to comment.