Skip to content

Commit

Permalink
Always use ncodeunits as size argument when converting to StdString
Browse files Browse the repository at this point in the history
  • Loading branch information
barche committed Oct 4, 2023
1 parent 326d9f8 commit ecd5279
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
7 changes: 4 additions & 3 deletions src/StdLib.jl
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,10 @@ Base.cmp(a::String, b::CppBasicString) = cmp(a,String(b))

# Make sure functions taking a C++ string as argument can also take a Julia string
CxxWrapCore.map_julia_arg_type(x::Type{<:StdString}) = AbstractString
StdLib.StdStringAllocated(x::String) = StdString(x,length(x))
Base.cconvert(::Type{CxxWrapCore.ConstCxxRef{StdString}}, x::String) = StdString(x,length(x))
Base.cconvert(::Type{StdLib.StdStringDereferenced}, x::String) = StdString(x,length(x))
StdString(x::String) = StdString(x,ncodeunits(x))
StdLib.StdStringAllocated(x::String) = StdString(x,ncodeunits(x))
Base.cconvert(::Type{CxxWrapCore.ConstCxxRef{StdString}}, x::String) = StdString(x,ncodeunits(x))
Base.cconvert(::Type{StdLib.StdStringDereferenced}, x::String) = StdString(x,ncodeunits(x))
Base.unsafe_convert(::Type{CxxWrapCore.ConstCxxRef{StdString}}, x::StdString) = ConstCxxRef(x)

function StdValArray(v::Vector{T}) where {T}
Expand Down
4 changes: 2 additions & 2 deletions test/stdlib.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ let s = StdString("foo")
end

let s = "\x01\x00\x02"
@test length(StdString(s)) == 1
@test length(StdString(s)) == 3
@test length(StdString(s, length(s))) == 3

@test String(StdString(s)) != s
@test String(StdString(s)) == s
@test String(StdString(s, length(s))) == s
end

Expand Down

0 comments on commit ecd5279

Please sign in to comment.