Skip to content

Commit

Permalink
ensure that the input String is GC preserved in equality check (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
KristofferC committed Jul 23, 2024
1 parent 66586a1 commit bf772fb
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/InlineStrings.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
module InlineStrings

import Base: ==

export InlineString, InlineStringType, inlinestrings
export @inline_str

Expand Down Expand Up @@ -290,14 +288,16 @@ macro inline_str(ex)
end


(==)(x::T, y::T) where {T <: InlineString} = Base.eq_int(x, y)
function ==(x::String, y::T) where {T <: InlineString}
Base.:(==)(x::T, y::T) where {T <: InlineString} = Base.eq_int(x, y)
function Base.:(==)(x::String, y::T) where {T <: InlineString}
sizeof(x) == sizeof(y) || return false
ref = Ref{T}(_bswap(y))
return ccall(:memcmp, Cint, (Ptr{UInt8}, Ref{T}, Csize_t),
pointer(x), ref, sizeof(x)) == 0
GC.@preserve x begin
return ccall(:memcmp, Cint, (Ptr{UInt8}, Ref{T}, Csize_t),
pointer(x), ref, sizeof(x)) == 0
end
end
==(y::InlineString, x::String) = x == y
Base.:(==)(y::InlineString, x::String) = x == y

Base.cmp(a::T, b::T) where {T <: InlineString} =
Base.eq_int(a, b) ? 0 : Base.ult_int(a, b) ? -1 : 1
Expand Down

0 comments on commit bf772fb

Please sign in to comment.