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