Skip to content

Commit

Permalink
fix write to bswap and use the correct size
Browse files Browse the repository at this point in the history
  • Loading branch information
KristofferC committed Jul 25, 2024
1 parent bf772fb commit 5bb15e1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/InlineStrings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -311,10 +311,10 @@ function Base.hash(x::T, h::UInt) where {T <: InlineString}
end

function Base.write(io::IO, x::T) where {T <: InlineString}
ref = Ref{T}(x)
ref = Ref{T}(_bswap(x))
return GC.@preserve ref begin
ptr = convert(Ptr{UInt8}, Base.unsafe_convert(Ptr{T}, ref))
Int(unsafe_write(io, ptr, reinterpret(UInt, sizeof(T))))::Int
Int(unsafe_write(io, ptr, reinterpret(UInt, sizeof(x))))::Int
end
end

Expand Down
7 changes: 7 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,13 @@ end
@test inlinestrings(["a", "b", ""]) == [String1("a"), String1("b"), String1("")]
@test String1("") == ""

@testset "write" begin
str = InlineString("abc")
iob = IOBuffer()
write(iob, str)
@test String(take!(iob)) == "abc"
end

# only test package extension on >= 1.9.0
if VERSION >= v"1.9.0" && Sys.WORD_SIZE == 64
include(joinpath(dirname(pathof(InlineStrings)), "../ext/tests.jl"))
Expand Down

0 comments on commit 5bb15e1

Please sign in to comment.