Skip to content
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 .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
with:
version: ${{ matrix.version }}
arch: ${{ matrix.arch }}
- uses: actions/cache@v1
- uses: actions/cache@v4
env:
cache-name: cache-artifacts
with:
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "WeakRefStrings"
uuid = "ea10d353-3f73-51f8-a26c-33c1cb351aa5"
authors = ["quinnj <quinn.jacobd@gmail.com>"]
version = "1.4.2"
version = "1.4.3"

[deps]
DataAPI = "9a962f9c-6df0-11e9-0e5d-c546b8b5ee8a"
Expand Down
13 changes: 10 additions & 3 deletions src/WeakRefStrings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,16 @@ function Base.cmp(a::WeakRefString{T}, b::WeakRefString{T}) where T
return c < 0 ? -1 : c > 0 ? +1 : cmp(al,bl)
end

function Base.hash(s::WeakRefString{T}, h::UInt) where {T}
h += Base.memhash_seed
ccall(Base.memhash, UInt, (Ptr{T}, Csize_t, UInt32), s.ptr, s.len, h % UInt32) + h
if isdefined(Base, :memhash) && isdefined(Base, :memhash_seed)
function Base.hash(s::WeakRefString{T}, h::UInt) where {T}
h += Base.memhash_seed
ccall(Base.memhash, UInt, (Ptr{T}, Csize_t, UInt32), s.ptr, s.len, h % UInt32) + h
end
elseif isdefined(Base, :hash_bytes) && isdefined(Base, :HASH_SECRET)
Base.hash(s::WeakRefString{UInt8}, h::UInt) = Base.hash_bytes(s.ptr, s.len, UInt64(h), Base.HASH_SECRET) % UInt
Base.hash(s::WeakRefString, h::UInt) = hash(String(s), h)
else
Base.hash(s::WeakRefString, h::UInt) = hash(String(s), h)
end

function Base.show(io::IO, x::WeakRefString{T}) where {T}
Expand Down
22 changes: 12 additions & 10 deletions src/poslenstrings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,18 @@ function Base.cmp(a::PosLenString, b::PosLenString)
return cmp(codeunits(a), codeunits(b))
end

function Base.hash(s::PosLenString, h::UInt)
h += Base.memhash_seed
if !escaped(s)
ccall(Base.memhash, UInt, (Ptr{UInt8}, Csize_t, UInt32), pointer(s), len(s), h % UInt32) + h
else
# TODO: this is expensive, even for rare escaped PosLenString
# this makes it about 4x slower than hash(::String)
# alternative is to maybe take what's needed from MurmurHash3.jl to operate by codeunit
x = copy(codeunits(s))
ccall(Base.memhash, UInt, (Ptr{UInt8}, Csize_t, UInt32), pointer(x), sizeof(x), h % UInt32) + h
if isdefined(Base, :memhash) && isdefined(Base, :memhash_seed)
function Base.hash(s::PosLenString, h::UInt)
h += Base.memhash_seed
if !escaped(s)
ccall(Base.memhash, UInt, (Ptr{UInt8}, Csize_t, UInt32), pointer(s), len(s), h % UInt32) + h
else
# TODO: this is expensive, even for rare escaped PosLenString
# this makes it about 4x slower than hash(::String)
# alternative is to maybe take what's needed from MurmurHash3.jl to operate by codeunit
x = copy(codeunits(s))
ccall(Base.memhash, UInt, (Ptr{UInt8}, Csize_t, UInt32), pointer(x), sizeof(x), h % UInt32) + h
end
end
end

Expand Down
15 changes: 15 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ include("poslenstrings.jl")
@test str[1] === 'h'
@test str[2] === 'e'
@test str[3] === 'y'
@test hash(str) isa UInt
@test hash(str, UInt(0x1234)) isa UInt
if !isdefined(Base, :memhash_seed)
@test hash(str, UInt(0x1234)) == hash("hey", UInt(0x1234))
end

io = IOBuffer()
show(io, str)
Expand All @@ -32,6 +37,11 @@ end
@test typeof(str) == WeakRefStrings.WeakRefString{UInt16}
@test String(str) == "hey"
@test str.len == 3
@test hash(str) isa UInt
@test hash(str, UInt(0x1234)) isa UInt
if !isdefined(Base, :memhash_seed)
@test hash(str, UInt(0x1234)) == hash("hey", UInt(0x1234))
end
end

@testset "WeakRefString{UInt32}" begin
Expand All @@ -42,6 +52,11 @@ end
@test typeof(str) == WeakRefStrings.WeakRefString{UInt32}
@test String(str) == "hey"
@test str.len == 4
@test hash(str) isa UInt
@test hash(str, UInt(0x1234)) isa UInt
if !isdefined(Base, :memhash_seed)
@test hash(str, UInt(0x1234)) == hash("hey", UInt(0x1234))
end
end

@testset "StringVector" begin
Expand Down
Loading