From d9c68d1ef475a8ede069da1f92e2aa8f42c3b46f Mon Sep 17 00:00:00 2001 From: Jameson Nash Date: Fri, 3 Oct 2025 18:48:05 +0000 Subject: [PATCH] Fix compatibility with Julia 1.13+ memhash removal MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Remove hash method definition when Base.memhash is not available. On Julia 1.13+, these AbstractString types will use the default AbstractString hash implementation which is now efficient and zero-copy based on codeunit/iterate. For Julia <1.13, continue using the memhash-based implementation for compatibility. Related to JuliaLang/julia#59697 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- src/StrFs.jl | 14 +++++++++----- test/runtests.jl | 9 +++++++++ 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/StrFs.jl b/src/StrFs.jl index 177402f..4adaa19 100644 --- a/src/StrFs.jl +++ b/src/StrFs.jl @@ -38,11 +38,13 @@ end show(io::IO, str::StrF) = show(io, String(str)) # this implementation is a modified copy from base/hashing2.jl -function hash(str::StrF, h::UInt) - h += Base.memhash_seed - # note: use pointer(s) here (see #6058). - ccall(Base.memhash, UInt, (Ptr{UInt8}, Csize_t, UInt32), - Base.cconvert(Ptr{UInt8}, str.bytes), sizeof(str), h % UInt32) + h +if isdefined(Base, :memhash) + function hash(str::StrF, h::UInt) + h += Base.memhash_seed + # note: use pointer(s) here (see #6058). + ccall(Base.memhash, UInt, (Ptr{UInt8}, Csize_t, UInt32), + Base.cconvert(Ptr{UInt8}, str.bytes), sizeof(str), h % UInt32) + h + end end promote_rule(::Type{String}, ::Type{StrF{S}}) where S = String @@ -51,6 +53,8 @@ promote_rule(::Type{StrF{A}}, ::Type{StrF{B}}) where {A,B} = StrF{max(A,B)} codeunit(::StrF{S}) where S = UInt8 +codeunit(str::StrF{S}, i::Int) where S = str.bytes[i] + function sizeof(str::StrF{S}) where S nul = findfirst(isequal(0x00), str.bytes) if nul ≡ nothing diff --git a/test/runtests.jl b/test/runtests.jl index 10b7a38..52e41b5 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -98,3 +98,12 @@ end @test Base.IteratorSize(S) ≡ Base.IteratorSize(String) @test Base.IteratorEltype(S) ≡ Base.IteratorEltype(S) end + +@testset "codeunit" begin + s = "hello" + strf = StrF{10}(s) + @test codeunit(strf) == UInt8 + for i in 1:sizeof(s) + @test codeunit(strf, i) == codeunit(s, i) + end +end