Skip to content
Open
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
14 changes: 9 additions & 5 deletions src/StrFs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
9 changes: 9 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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