From 7b61d20853d7f89be9ac018dc9cc33adae10672f Mon Sep 17 00:00:00 2001 From: Jacob Quinn Date: Thu, 11 Jun 2026 14:54:31 -0600 Subject: [PATCH] fix(lazy): hash PtrString like strings Ensure PtrString equality with AbstractString is symmetric and hash-compatible so transient lazy keys behave correctly in hashed collections. Fixes #467 --- src/lazy.jl | 5 ++++- test/lazy.jl | 10 ++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/lazy.jl b/src/lazy.jl index 69f9092..089e5e3 100644 --- a/src/lazy.jl +++ b/src/lazy.jl @@ -445,9 +445,12 @@ function Base.convert(::Type{T}, x::PtrString) where {T <: Enum} end Base.:(==)(x::PtrString, y::AbstractString) = x.len == sizeof(y) && ccall(:memcmp, Cint, (Ptr{UInt8}, Ptr{UInt8}, Csize_t), x.ptr, pointer(y), x.len) == 0 +Base.:(==)(x::AbstractString, y::PtrString) = y == x Base.:(==)(x::PtrString, y::PtrString) = x.len == y.len && ccall(:memcmp, Cint, (Ptr{UInt8}, Ptr{UInt8}, Csize_t), x.ptr, y.ptr, x.len) == 0 Base.isequal(x::PtrString, y::AbstractString) = x == y +Base.isequal(x::AbstractString, y::PtrString) = y == x Base.isequal(x::PtrString, y::PtrString) = x == y +Base.hash(x::PtrString, h::UInt) = hash(unsafe_string(x.ptr, x.len), h) StructUtils.keyeq(x::PtrString, y::AbstractString) = x == y StructUtils.keyeq(x::PtrString, y::String) = x == y StructUtils.keyeq(x::PtrString, y::Symbol) = convert(Symbol, x) == y @@ -746,4 +749,4 @@ function Base.show(io::IO, x::LazyValue) else # bool/number Base.print(io, "JSON.LazyValue(", parse(x), ")") end -end \ No newline at end of file +end diff --git a/test/lazy.jl b/test/lazy.jl index ae415bf..7c31188 100644 --- a/test/lazy.jl +++ b/test/lazy.jl @@ -71,6 +71,16 @@ end x = JSON.lazy("{}"; allownan=true) @test_throws ArgumentError JSON.parsenumber(x) + str = JSON.lazy("\"alpha\"") + key, _ = JSON.parsestring(str) + @test key == "alpha" + @test "alpha" == key + @test isequal(key, "alpha") + @test isequal("alpha", key) + @test hash(key) == hash("alpha") + @test haskey(Dict{Any,Int}(key => 1), "alpha") + @test "alpha" in Set{Any}([key]) + # lazy indexing selection support # examples from https://support.smartbear.com/alertsite/docs/monitors/api/endpoint/jsonpath.html json = """